From 34d83859cd9aff669da3d0d64b918e3ac4efe3ae Mon Sep 17 00:00:00 2001 From: vincentlj Date: Mon, 23 Dec 2013 17:00:34 +0000 Subject: [PATCH] Add support for specular light Note that specular light is applied unconditionnaly, thus every object will shine. TODO : Specular map should be defined using texture or a material flag. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@14756 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- data/shaders/lightblend.frag | 11 +++-------- data/shaders/pointlight.frag | 11 +++++------ data/shaders/sunlight.frag | 15 +++++++-------- src/graphics/render.cpp | 2 +- 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/data/shaders/lightblend.frag b/data/shaders/lightblend.frag index 101f2e9e0..f44c94098 100644 --- a/data/shaders/lightblend.frag +++ b/data/shaders/lightblend.frag @@ -7,14 +7,9 @@ void main() { vec2 texc = gl_TexCoord[0].xy; - vec4 col = texture2D(diffuse_and_spec, texc); + vec3 diffuse = texture2D(diffuse_and_spec, texc).xyz; + vec3 specular = texture2D(diffuse_and_spec, texc).www; float ao = texture2D(ambient_occlusion, texc).x; - col.xyz += ao * ambient; - float spec = col.a - 0.05; - //spec *= specular.a; - col.xyz += spec * col.xyz; - col.a = 1.0; - - gl_FragColor = col; + gl_FragColor = vec4(diffuse + specular + ao * ambient, 1.0); } diff --git a/data/shaders/pointlight.frag b/data/shaders/pointlight.frag index 91ccf6b5e..e017fe03b 100644 --- a/data/shaders/pointlight.frag +++ b/data/shaders/pointlight.frag @@ -24,13 +24,12 @@ void main() { // Light Direction vec3 L = normalize(xpos.xyz - center); - vec3 eyedir = normalize(xpos.xyz); - vec3 H = normalize(-L + eyedir); float NdotL = max(0.0, dot(norm, -L)) * att; - float NdotH = max(0.0, dot(norm, H)); - NdotH = pow(NdotH, spec); - NdotH += 0.05; // offset so that the alpha test doesn't kill us + // Reflected light dir + vec3 R = reflect(-L, norm); + float RdotE = max(0.0, dot(R, normalize(xpos))); + float Specular = pow(RdotE, spec); - gl_FragColor = NdotL * vec4(NdotL * col, NdotH); + gl_FragColor = vec4(NdotL * col, Specular + 0.001); // Irrlicht force alpha test, can't be 0 } diff --git a/data/shaders/sunlight.frag b/data/shaders/sunlight.frag index fb0009934..0691a5194 100644 --- a/data/shaders/sunlight.frag +++ b/data/shaders/sunlight.frag @@ -12,6 +12,9 @@ void main() { vec2 texc = gl_FragCoord.xy / screen; float z = texture2D(ntex, texc).a; + vec4 xpos = 2.0 * vec4(texc, z, 1.0) - 1.0; + xpos = invproj * xpos; + xpos.xyz /= xpos.w; if (z < 0.03) { @@ -28,18 +31,14 @@ void main() { vec3 L = center; float NdotL = max(0.0, dot(norm, L)); + vec3 R = reflect(L, norm); + float RdotE = max(0.0, dot(R, normalize(xpos))); + float Specular = pow(RdotE, 200); vec3 outcol = NdotL * col; if (hasclouds == 1) { - vec3 tmp = vec3(texc, z); - tmp = tmp * 2.0 - 1.0; - - vec4 xpos = vec4(tmp, 1.0); - xpos = invproj * xpos; - xpos.xyz /= xpos.w; - vec2 cloudcoord = (xpos.xz * 0.00833333) + wind; float cloud = texture2D(cloudtex, cloudcoord).x; //float cloud = step(0.5, cloudcoord.x) * step(0.5, cloudcoord.y); @@ -47,6 +46,6 @@ void main() { outcol *= cloud; } - gl_FragData[0] = vec4(outcol, 0.05); + gl_FragData[0] = vec4(NdotL * col, Specular+ 0.001); // Irrlicht force alpha test, can't be 0 gl_FragData[1] = vec4(1.0); } diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 8dedd1050..613b61fe2 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -673,7 +673,7 @@ void IrrDriver::renderLights(const core::aabbox3df& cambox, if (!m_lightviz) { m_video_driver->setRenderTarget(m_rtts->getRTT(RTT_TMP1), true, false, - video::SColor(1, 0, 0, 0)); + video::SColor(0, 0, 0, 0)); } else { m_video_driver->setRenderTarget(m_rtts->getRTT(RTT_COLOR), false, false);