diff --git a/Shader/phong.fsh b/Shader/phong.fsh index b76a2db..191e24b 100644 --- a/Shader/phong.fsh +++ b/Shader/phong.fsh @@ -20,6 +20,8 @@ uniform float directionalIntensity; uniform vec3 lightSources[128]; uniform vec3 lightColors[128]; uniform float lightIntensities[128]; +uniform float fogStart; +uniform vec3 fogColor; void main() { @@ -48,7 +50,11 @@ void main() *specularFactor*intensity*lightColors[i]; } } + vec3 finalColor = specularColor + diffuseColor + ambientColor; + float distanceCamera = distance(camera, vec3(fragPosition)); + float fogFactor = clamp((1.0 - exp(-distanceCamera+fogStart)), 0.0, 1.0); + finalColor = mix(finalColor, fogColor, fogFactor); vec4 texture = texture(uTexture, vTexCoord).rgba; oColor = vec4(finalColor, 1.0f)*texture; diff --git a/graphics.cc b/graphics.cc index 8f7c05e..6e127fc 100644 --- a/graphics.cc +++ b/graphics.cc @@ -139,9 +139,13 @@ void Graphics::render(Level* level, ACGL::OpenGL::SharedShaderProgram shader) level->getDirectionalLight()->getIntensity()); } + // set fog Parameters + shader->setUniform("fogStart", farPlane-50.0f); + shader->setUniform("fogColor", level->getFogColor()); + // set Material Parameters shader->setUniform("ambientColor", level->getAmbientLight()); - shader->setUniform("camera", glm::vec3(0.0f, 0.0f, 0.0f)); + shader->setUniform("camera", level->getCameraPosition()); // render the level(currently only a bunny): level->render(); diff --git a/level.cc b/level.cc index 10bf0fe..3e3fd49 100644 --- a/level.cc +++ b/level.cc @@ -54,6 +54,7 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { //set lighting parameters ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); + fogColor = glm::vec3(0.10f, 0.14f, 0.14f); directionalLight = Light(glm::vec3(-0.5f, 0.0f, -0.5f), glm::vec3(1.0f, 1.0f, 0.0f), 0.4f); Light light = Light(glm::vec3(-3.0f, 7.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 5.0f); lights.push_back(light); @@ -134,6 +135,10 @@ Light* Level::getDirectionalLight() { return &directionalLight; } +glm::vec3 Level::getFogColor() { + return fogColor; +} + glm::vec3 Level::getCameraPosition() { return cameraCenter->getPosition() + camera.getVector(); } diff --git a/level.hh b/level.hh index 4efd74b..9cb091b 100644 --- a/level.hh +++ b/level.hh @@ -24,11 +24,13 @@ class Level { Object* getCameraCenter(); Camera* getCamera(); glm::vec3 getCameraPosition(); + glm::vec3 getFogColor(); private: std::string filePath; std::vector objects; std::vector lights; glm::vec3 ambientLight; + glm::vec3 fogColor; Light directionalLight; Object* cameraCenter; Physics physics;