From 56fc0f9076fd426326aa07928c2692aed55ff53d Mon Sep 17 00:00:00 2001 From: timoore <timoore> Date: Tue, 17 Nov 2009 21:57:07 +0000 Subject: [PATCH] clamp lighting values and support transparency material animation The transparency animation support is a hack, but should get the job done for a bit. Author: Tim Moore <timoore@redhat.com> --- Shaders/default.frag | 14 ++++++++++---- Shaders/model-default.frag | 13 +++++++++---- Shaders/model-default.vert | 11 ++++++++--- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Shaders/default.frag b/Shaders/default.frag index 68c363dd2..f6bf8abd4 100644 --- a/Shaders/default.frag +++ b/Shaders/default.frag @@ -13,7 +13,8 @@ void main() vec4 color = ambient + gl_FrontMaterial.emission; vec4 texel; vec4 fragColor; - + vec4 specular = vec4(0.0); + n = normalize(normal); NdotL = max(dot(n, lightDir), 0.0); if (NdotL > 0.0) { @@ -21,12 +22,17 @@ void main() halfV = normalize(halfVector); NdotHV = max(dot(n, halfV), 0.0); if (gl_FrontMaterial.shininess > 0.0) - color += gl_FrontMaterial.specular * gl_LightSource[0].specular - * pow(NdotHV, gl_FrontMaterial.shininess); + specular.rgb = (gl_FrontMaterial.specular.rgb + * gl_LightSource[0].specular.rgb + * pow(NdotHV, gl_FrontMaterial.shininess)); } color.a = alpha; + // This shouldn't be necessary, but our lighting becomes very + // saturated. Clamping the color before modulating by the texture + // is closer to what the OpenGL fixed function pipeline does. + color = clamp(color, 0.0, 1.0); texel = texture2D(texture, gl_TexCoord[0].st); - fragColor = color * texel; + fragColor = color * texel + specular; fogFactor = exp(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord); gl_FragColor = mix(gl_Fog.color, fragColor, fogFactor); } diff --git a/Shaders/model-default.frag b/Shaders/model-default.frag index ac13ec7a8..25bbe685b 100644 --- a/Shaders/model-default.frag +++ b/Shaders/model-default.frag @@ -13,7 +13,7 @@ void main() vec4 color = constantColor; vec4 texel; vec4 fragColor; - + vec4 specular = vec4(0.0); n = normalize(normal); NdotL = max(dot(n, lightDir), 0.0); if (NdotL > 0.0) { @@ -21,12 +21,17 @@ void main() halfV = normalize(halfVector); NdotHV = max(dot(n, halfV), 0.0); if (gl_FrontMaterial.shininess > 0.0) - color += gl_FrontMaterial.specular * gl_LightSource[0].specular - * pow(NdotHV, gl_FrontMaterial.shininess); + specular.rgb = (gl_FrontMaterial.specular.rgb + * gl_LightSource[0].specular.rgb + * pow(NdotHV, gl_FrontMaterial.shininess)); } color.a = alpha; + // This shouldn't be necessary, but our lighting becomes very + // saturated. Clamping the color before modulating by the texture + // is closer to what the OpenGL fixed function pipeline does. + color = clamp(color, 0.0, 1.0); texel = texture2D(texture, gl_TexCoord[0].st); - fragColor = color * texel; + fragColor = color * texel + specular; fogFactor = exp(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord); gl_FragColor = mix(gl_Fog.color, fragColor, fogFactor); } diff --git a/Shaders/model-default.vert b/Shaders/model-default.vert index f7ba60392..50ba771fc 100644 --- a/Shaders/model-default.vert +++ b/Shaders/model-default.vert @@ -5,8 +5,8 @@ // The only light used is gl_LightSource[0], which is assumed to be // directional. // -// Diffuse and ambient colors come from the gl_Color. This is -// equivalent to osg::Material::AMBIENT_AND_DIFFUSE. +// Diffuse colors come from the gl_Color, ambient from the material. This is +// equivalent to osg::Material::DIFFUSE. varying vec4 diffuse, constantColor; varying vec3 normal, lightDir, halfVector; @@ -22,7 +22,12 @@ void main() lightDir = normalize(vec3(gl_LightSource[0].position)); halfVector = normalize(gl_LightSource[0].halfVector.xyz); diffuse = gl_Color * gl_LightSource[0].diffuse; - alpha = gl_Color.a; + // Super hack: if diffuse material alpha is less than 1, assume a + // transparency animation is at work + if (gl_FrontMaterial.diffuse.a < 1.0) + alpha = gl_FrontMaterial.diffuse.a; + else + alpha = gl_Color.a; constantColor = gl_FrontLightModelProduct.sceneColor + gl_FrontMaterial.ambient * gl_LightSource[0].ambient; fogCoord = abs(ecPosition3.z);