From 65b32757f3b900da240f5f9bd4468dcd89293755 Mon Sep 17 00:00:00 2001 From: vincentlj Date: Tue, 24 Dec 2013 20:51:53 +0000 Subject: [PATCH] Separate Diffuse and Specular components This allows to bypass alpha test, and have colored specular lights. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@14777 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- data/shaders/lightblend.frag | 10 +++++----- data/shaders/pointlight.frag | 5 +++-- data/shaders/sunlight.frag | 5 +++-- src/graphics/callbacks.cpp | 4 +++- src/graphics/render.cpp | 13 +++++++++---- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/data/shaders/lightblend.frag b/data/shaders/lightblend.frag index 4e72ef987..be44256b2 100644 --- a/data/shaders/lightblend.frag +++ b/data/shaders/lightblend.frag @@ -1,15 +1,15 @@ -uniform sampler2D diffuse_and_spec; +uniform sampler2D diffuse; +uniform sampler2D specular; uniform sampler2D ambient_occlusion; uniform vec3 ambient; -//uniform sampler2D spectex; void main() { vec2 texc = gl_TexCoord[0].xy; - vec3 diffuse = texture2D(diffuse_and_spec, texc).xyz; - vec3 specular = texture2D(diffuse_and_spec, texc).www; + vec3 diffuse = texture2D(diffuse, texc).xyz; + vec3 spec = texture2D(specular, texc).xyz; float ao = texture2D(ambient_occlusion, texc).x; - gl_FragColor = vec4(diffuse + ao * ambient, 1.0); + gl_FragColor = vec4(diffuse + spec + ao * ambient, 1.0); } diff --git a/data/shaders/pointlight.frag b/data/shaders/pointlight.frag index c1e860014..30a933b8f 100644 --- a/data/shaders/pointlight.frag +++ b/data/shaders/pointlight.frag @@ -25,11 +25,12 @@ void main() { // Light Direction vec3 L = normalize(xpos.xyz - center); - float NdotL = max(0.0, dot(norm, -L)) * att; + float NdotL = max(0.0, dot(norm, -L)); // Reflected light dir vec3 R = reflect(-L, norm); float RdotE = max(0.0, dot(R, normalize(xpos.xyz))); float Specular = pow(RdotE, spec); - gl_FragColor = vec4(NdotL * col, Specular + 0.001); // Irrlicht force alpha test, can't be 0 + gl_FragData[0] = vec4(NdotL * col * att, 1.); + gl_FragData[1] = vec4(Specular * col, 1.); } diff --git a/data/shaders/sunlight.frag b/data/shaders/sunlight.frag index 333897c1a..a96812ce6 100644 --- a/data/shaders/sunlight.frag +++ b/data/shaders/sunlight.frag @@ -46,6 +46,7 @@ void main() { outcol *= cloud; } - gl_FragData[0] = vec4(NdotL * col, Specular+ 0.001); // Irrlicht force alpha test, can't be 0 - gl_FragData[1] = vec4(1.0); + gl_FragData[0] = vec4(NdotL * col, 1.); + gl_FragData[1] = vec4(Specular * col, 1.); + gl_FragData[2] = vec4(1.0); } diff --git a/src/graphics/callbacks.cpp b/src/graphics/callbacks.cpp index 453e4ac6e..283dddaa7 100644 --- a/src/graphics/callbacks.cpp +++ b/src/graphics/callbacks.cpp @@ -343,8 +343,10 @@ void LightBlendProvider::OnSetConstants(IMaterialRendererServices *srv, int) srv->setVertexShaderConstant("ambient", ambient, 3); int tex = 0; - srv->setVertexShaderConstant("diffuse_and_spec", &tex, 1); + srv->setVertexShaderConstant("diffuse", &tex, 1); tex = 1; + srv->setVertexShaderConstant("specular", &tex, 1); + tex = 2; srv->setVertexShaderConstant("ambient_occlusion", &tex, 1); } diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 2f2f8a47b..f6962a206 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -670,7 +670,13 @@ void IrrDriver::renderLights(const core::aabbox3df& cambox, video::SOverrideMaterial &overridemat, int cam) { - m_video_driver->setRenderTarget(m_rtts->getRTT(RTT_TMP1), true, false, + core::array rtts; + // Diffuse + rtts.push_back(m_rtts->getRTT(RTT_TMP1)); + // Specular + rtts.push_back(m_rtts->getRTT(RTT_TMP2)); + + m_video_driver->setRenderTarget(rtts, true, false, video::SColor(0, 0, 0, 0)); const vector3df camcenter = cambox.getCenter(); @@ -781,9 +787,8 @@ void IrrDriver::renderLights(const core::aabbox3df& cambox, lightmat.ZBuffer = video::ECFN_ALWAYS; lightmat.setFlag(video::EMF_BILINEAR_FILTER, false); lightmat.setTexture(0, m_rtts->getRTT(RTT_TMP1)); - - // Apply ambient occlusion - lightmat.setTexture(1, m_rtts->getRTT(RTT_SSAO)); + lightmat.setTexture(1, m_rtts->getRTT(RTT_TMP2)); + lightmat.setTexture(2, m_rtts->getRTT(RTT_SSAO)); lightmat.MaterialType = m_shaders->getShader(ES_LIGHTBLEND); if (!m_lightviz)