opengl: Replaced temp lighting with sun transmission lighting

This commit is contained in:
Michaël Lemaire 2014-11-21 11:40:47 +01:00
parent f7715b612e
commit 261eb5a674
6 changed files with 55 additions and 26 deletions

View file

@ -25,7 +25,7 @@ void OpenGLSkybox::initialize()
{ {
program = createShader("skybox"); program = createShader("skybox");
program->addVertexSource("skybox"); program->addVertexSource("skybox");
program->addFragmentSource("bruneton"); program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("skybox"); program->addFragmentSource("skybox");

View file

@ -52,7 +52,7 @@ void OpenGLTerrain::initialize()
// Prepare shader programs // Prepare shader programs
program = createShader("terrain"); program = createShader("terrain");
program->addVertexSource("terrain"); program->addVertexSource("terrain");
program->addFragmentSource("bruneton"); program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout"); program->addFragmentSource("fadeout");
program->addFragmentSource("terrain"); program->addFragmentSource("terrain");

View file

@ -24,7 +24,7 @@ void OpenGLWater::initialize()
{ {
program = createShader("water"); program = createShader("water");
program->addVertexSource("water"); program->addVertexSource("water");
program->addFragmentSource("bruneton"); program->addFragmentSource("atmosphere");
program->addFragmentSource("tonemapping"); program->addFragmentSource("tonemapping");
program->addFragmentSource("fadeout"); program->addFragmentSource("fadeout");
program->addFragmentSource("noise"); program->addFragmentSource("noise");

View file

@ -224,3 +224,53 @@ vec4 getSkyColor(vec3 location, vec3 direction)
result += sunTransmittance + vec4(inscattering, 0.0); result += sunTransmittance + vec4(inscattering, 0.0);
return result; 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;
}

View file

@ -4,7 +4,7 @@
<file>skybox.vert</file> <file>skybox.vert</file>
<file>water.frag</file> <file>water.frag</file>
<file>water.vert</file> <file>water.vert</file>
<file>bruneton.frag</file> <file>atmosphere.frag</file>
<file>tonemapping.frag</file> <file>tonemapping.frag</file>
<file>terrain.frag</file> <file>terrain.frag</file>
<file>terrain.vert</file> <file>terrain.vert</file>

View file

@ -1,32 +1,11 @@
uniform vec4 waterColor; uniform vec4 waterColor;
uniform float waterReflection; 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) void main(void)
{ {
vec3 normal = noiseNormal2d(unprojected.xz, 0.001); 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); vec3 reflected = reflect(unprojected - cameraLocation, normal);
reflected.y = max(reflected.y, 0.0); reflected.y = max(reflected.y, 0.0);