Added fog

This commit is contained in:
saschawillems 2016-06-25 20:40:22 +02:00
parent 8ff0ad57ba
commit cc5ade69f1
4 changed files with 19 additions and 1 deletions

View file

@ -10,6 +10,8 @@ layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec3 inEyePos;
layout (location = 5) in vec3 inWorldPos;
layout (location = 0) out vec4 outFragColor;
@ -40,11 +42,23 @@ vec3 sampleTerrainLayer()
return color;
}
float fog(float density)
{
const float LOG2 = -1.442695;
float dist = gl_FragCoord.z / gl_FragCoord.w * 0.1;
float d = density * dist;
return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
}
void main()
{
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 ambient = vec3(0.5);
vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
outFragColor = vec4((ambient + diffuse) * sampleTerrainLayer(), 1.0);
vec4 color = vec4((ambient + diffuse) * sampleTerrainLayer(), 1.0);
const vec4 fogColor = vec4(0.47, 0.5, 0.67, 0.0);
outFragColor = mix(color, fogColor, fog(0.25));
}

View file

@ -26,6 +26,8 @@ layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec3 outEyePos;
layout (location = 5) out vec3 outWorldPos;
void main()
{
@ -50,4 +52,6 @@ void main()
// Calculate vectors for lighting based on tessellated position
outViewVec = -pos.xyz;
outLightVec = normalize(ubo.lightPos.xyz + outViewVec);
outWorldPos = pos.xyz;
outEyePos = vec3(ubo.modelview * pos);
}