diff --git a/src/render/opengl/OpenGLSkybox.cpp b/src/render/opengl/OpenGLSkybox.cpp
index bdf3139..6b6e879 100644
--- a/src/render/opengl/OpenGLSkybox.cpp
+++ b/src/render/opengl/OpenGLSkybox.cpp
@@ -25,7 +25,7 @@ void OpenGLSkybox::initialize()
{
program = createShader("skybox");
program->addVertexSource("skybox");
- program->addFragmentSource("bruneton");
+ program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping");
program->addFragmentSource("skybox");
diff --git a/src/render/opengl/OpenGLTerrain.cpp b/src/render/opengl/OpenGLTerrain.cpp
index b5ecfa2..bfe7a44 100644
--- a/src/render/opengl/OpenGLTerrain.cpp
+++ b/src/render/opengl/OpenGLTerrain.cpp
@@ -52,7 +52,7 @@ void OpenGLTerrain::initialize()
// Prepare shader programs
program = createShader("terrain");
program->addVertexSource("terrain");
- program->addFragmentSource("bruneton");
+ program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout");
program->addFragmentSource("terrain");
diff --git a/src/render/opengl/OpenGLWater.cpp b/src/render/opengl/OpenGLWater.cpp
index fa57d2c..88f6a00 100644
--- a/src/render/opengl/OpenGLWater.cpp
+++ b/src/render/opengl/OpenGLWater.cpp
@@ -24,7 +24,7 @@ void OpenGLWater::initialize()
{
program = createShader("water");
program->addVertexSource("water");
- program->addFragmentSource("bruneton");
+ program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout");
program->addFragmentSource("noise");
diff --git a/src/render/opengl/shaders/bruneton.frag b/src/render/opengl/shaders/atmosphere.frag
similarity index 84%
rename from src/render/opengl/shaders/bruneton.frag
rename to src/render/opengl/shaders/atmosphere.frag
index c9096d9..f486dda 100644
--- a/src/render/opengl/shaders/bruneton.frag
+++ b/src/render/opengl/shaders/atmosphere.frag
@@ -224,3 +224,53 @@ vec4 getSkyColor(vec3 location, vec3 direction)
result += sunTransmittance + vec4(inscattering, 0.0);
return result;
}
+
+vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess)
+{
+ float material_hardness = 0.3;
+ float material_reflection = 1.0;
+
+ float r0 = Rg + location.y * WORLD_SCALING;
+ vec3 sun_position = sunDirection * SUN_DISTANCE;
+ float muS = dot(vec3(0.0, 1.0, 0.0), normalize(sun_position - vec3(0.0, r0, 0.0)));
+
+ vec4 light_color = _transmittanceWithShadow(r0, muS);
+
+ vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
+
+ /* diffused light */
+ float diffuse = dot(sunDirection, normal);
+ float sign = (diffuse < 0.0) ? -1.0 : 1.0;
+ if (material_hardness <= 0.5)
+ {
+ float hardness = material_hardness * 2.0;
+ diffuse = (1.0 - hardness) * (diffuse * diffuse) * sign + hardness * diffuse;
+ }
+ else if (diffuse != 0.0)
+ {
+ float hardness = (material_hardness - 0.5) * 2.0;
+ diffuse = (1.0 - hardness) * diffuse + hardness * sign * sqrt(abs(diffuse));
+ }
+ if (diffuse > 0.0)
+ {
+ result += diffuse * color * light_color;
+ }
+
+ /* specular reflection */
+ if (shininess > 0.0 && material_reflection > 0.0)
+ {
+ vec3 view = normalize(location - cameraLocation);
+ vec3 reflect = sunDirection - normal * 2.0 * dot(sunDirection, normal);
+ float specular = dot(reflect, view);
+ if (specular > 0.0)
+ {
+ specular = pow(specular, shininess) * material_reflection;
+ if (specular > 0.0)
+ {
+ result += specular * light_color;
+ }
+ }
+ }
+
+ return result;
+}
diff --git a/src/render/opengl/shaders/resources.qrc b/src/render/opengl/shaders/resources.qrc
index 72a4bb6..ed4c445 100644
--- a/src/render/opengl/shaders/resources.qrc
+++ b/src/render/opengl/shaders/resources.qrc
@@ -4,7 +4,7 @@
skybox.vert
water.frag
water.vert
- bruneton.frag
+ atmosphere.frag
tonemapping.frag
terrain.frag
terrain.vert
diff --git a/src/render/opengl/shaders/water.frag b/src/render/opengl/shaders/water.frag
index 1f5a9d7..d6b5279 100644
--- a/src/render/opengl/shaders/water.frag
+++ b/src/render/opengl/shaders/water.frag
@@ -1,32 +1,11 @@
uniform vec4 waterColor;
uniform float waterReflection;
-vec4 applyLighting(vec3 location, vec3 normal, vec4 color, float shininess)
-{
- // TEMP phong lighting implementation for testing
- vec3 N = normalize(normal);
- vec3 L = sunDirection;
- vec3 E = normalize(cameraLocation - location);
- vec3 R = normalize(-reflect(L, N));
-
- //calculate Ambient Term:
- vec4 Iamb = vec4(0.1, 0.1, 0.1, 1.0);
-
- //calculate Diffuse Term:
- vec4 Idiff = vec4(3.0, 3.0, 3.0, 1.0) * color * max(dot(N, L), 0.0);
-
- // calculate Specular Term:
- vec4 Ispec = vec4(3.0, 3.0, 3.0, 1.0) * pow(max(dot(R,E),0.0),0.3*shininess);
-
- // write Total Color:
- return Iamb + Idiff + Ispec;
-}
-
void main(void)
{
vec3 normal = noiseNormal2d(unprojected.xz, 0.001);
- gl_FragColor = applyLighting(unprojected, normal, waterColor, 100.0);
+ gl_FragColor = applyLighting(unprojected, normal, waterColor, 16.0);
vec3 reflected = reflect(unprojected - cameraLocation, normal);
reflected.y = max(reflected.y, 0.0);