From 3c8105adf10ebfd994b6a66508303ee95ee959ec Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Wed, 16 Feb 2011 09:48:58 +0100 Subject: [PATCH 1/2] Fix issue #252 http://code.google.com/p/flightgear-bugs/issues/detail?id=252 by Lauri Peltonen --- Shaders/bumpspec.frag | 7 +++---- Shaders/bumpspec.vert | 11 ++++++----- Shaders/reflect-bump-spec.frag | 16 +++++++++------- Shaders/reflect-bump-spec.vert | 34 ++++++++++++++-------------------- Shaders/reflect.frag | 16 ++++++++-------- Shaders/reflect.vert | 21 ++++++++------------- 6 files changed, 48 insertions(+), 57 deletions(-) diff --git a/Shaders/bumpspec.frag b/Shaders/bumpspec.frag index b708746eb..16f8d161c 100644 --- a/Shaders/bumpspec.frag +++ b/Shaders/bumpspec.frag @@ -2,11 +2,11 @@ // Licence: GPL v2 // Author: Frederic Bouvier -varying vec4 ecPosition; +varying float fogCoord; + varying vec3 VNormal; varying vec3 VTangent; varying vec3 VBinormal; -varying vec4 constantColor; uniform sampler2D tex_color; uniform sampler2D tex_normal; @@ -29,7 +29,7 @@ void main (void) vec4 Diffuse = gl_LightSource[0].diffuse * nDotVP; vec4 Specular = gl_LightSource[0].specular * pf; - vec4 color = constantColor + Diffuse * gl_FrontMaterial.diffuse; + vec4 color = gl_Color + Diffuse * gl_FrontMaterial.diffuse; color *= texture2D(tex_color, gl_TexCoord[0].xy); color += Specular * gl_FrontMaterial.specular * ns.a; @@ -37,7 +37,6 @@ void main (void) float fogFactor; - float fogCoord = ecPosition.z; const float LOG2 = 1.442695; fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); fogFactor = clamp(fogFactor, 0.0, 1.0); diff --git a/Shaders/bumpspec.vert b/Shaders/bumpspec.vert index 4ed1b8a59..d7b7a8974 100644 --- a/Shaders/bumpspec.vert +++ b/Shaders/bumpspec.vert @@ -2,23 +2,24 @@ // Licence: GPL v2 // Author: Frederic Bouvier -varying vec4 ecPosition; +varying float fogCoord; varying vec3 VNormal; varying vec3 VTangent; varying vec3 VBinormal; -varying vec4 constantColor; attribute vec3 tangent; attribute vec3 binormal; void main (void) { - ecPosition = gl_ModelViewMatrix * gl_Vertex; + vec4 pos = gl_ModelViewMatrix * gl_Vertex; + fogCoord = pos.z / pos.w; + VNormal = normalize(gl_NormalMatrix * gl_Normal); VTangent = normalize(gl_NormalMatrix * tangent); VBinormal = normalize(gl_NormalMatrix * binormal); - constantColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient; - gl_FrontColor = constantColor; + + gl_FrontColor = gl_FrontLightModelProduct.sceneColor + gl_LightSource[0].ambient * gl_FrontMaterial.ambient; gl_TexCoord[0] = gl_MultiTexCoord0; gl_Position = ftransform(); } diff --git a/Shaders/reflect-bump-spec.frag b/Shaders/reflect-bump-spec.frag index 859d21e40..70a6c1a60 100644 --- a/Shaders/reflect-bump-spec.frag +++ b/Shaders/reflect-bump-spec.frag @@ -4,19 +4,17 @@ #version 120 -varying vec4 rawpos; -varying vec4 ecPosition; +varying vec3 rawpos; varying vec3 VNormal; varying vec3 VTangent; varying vec3 VBinormal; varying vec3 Normal; -varying vec4 constantColor; varying vec3 vViewVec; varying vec3 reflVec; varying vec4 Diffuse; -varying vec3 lightDir, halfVector; -varying float alpha, fogCoord; +varying float alpha; +varying float fogCoord; uniform samplerCube Environment; uniform sampler2D Rainbow; @@ -37,7 +35,12 @@ void main (void) { vec3 halfV; float NdotL, NdotHV; - vec4 color = constantColor; + + vec3 lightDir = gl_LightSource[0].position.xyz; + vec3 halfVector = gl_LightSource[0].halfVector.xyz; + + + vec4 color = gl_Color; vec4 specular = vec4(0.0); vec4 ns = texture2D(NormalTex, gl_TexCoord[0].st); vec3 n = ns.rgb * 2.0 - 1.0; @@ -61,7 +64,6 @@ void main (void) vec4 texelcolor = color * texel + specular; // calculate the fog factor - float fogCoord = ecPosition.z; const float LOG2 = 1.442695; float fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); fogFactor = clamp(fogFactor, 0.0, 1.0); diff --git a/Shaders/reflect-bump-spec.vert b/Shaders/reflect-bump-spec.vert index d2b7e9ff2..6eb1a68db 100644 --- a/Shaders/reflect-bump-spec.vert +++ b/Shaders/reflect-bump-spec.vert @@ -2,19 +2,17 @@ // Licence: GPL v2 // Author: Vivian Meazza. -varying vec4 rawpos; -varying vec4 ecPosition; +varying vec3 rawpos; +varying float fogCoord; varying vec3 VNormal; varying vec3 VTangent; varying vec3 VBinormal; varying vec3 Normal; -varying vec4 constantColor; varying vec3 vViewVec; varying vec3 reflVec; varying vec4 Diffuse; -varying vec3 normal, lightDir, halfVector; -varying float alpha, fogCoord; +varying float alpha; uniform mat4 osg_ViewMatrixInverse; @@ -23,23 +21,23 @@ attribute vec3 binormal; void main(void) { - rawpos = gl_Vertex; - ecPosition = gl_ModelViewMatrix * gl_Vertex; - vec3 ecPosition3 = vec3(gl_ModelViewMatrix * gl_Vertex) / ecPosition.w; + rawpos = gl_Vertex.xyz / gl_Vertex.w; + vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex; + ecPosition.xyz = ecPosition.xyz / ecPosition.w; + fogCoord = ecPosition.z; - vec3 t = normalize(cross(gl_Normal, vec3(1.0,0.0,0.0))); - vec3 b = normalize(cross(gl_Normal,t)); vec3 n = normalize(gl_Normal); + vec3 t = cross(gl_Normal, vec3(1.0,0.0,0.0)); + vec3 b = cross(n,t); VNormal = normalize(gl_NormalMatrix * gl_Normal); - VTangent = normalize(gl_NormalMatrix * tangent); - VBinormal = normalize(gl_NormalMatrix * binormal); + VTangent = normalize(gl_NormalMatrix * tangent); + VBinormal = normalize(gl_NormalMatrix * binormal); Normal = normalize(gl_Normal); - lightDir = normalize(vec3(gl_LightSource[0].position)); - halfVector = normalize(gl_LightSource[0].halfVector.xyz); Diffuse = gl_Color * gl_LightSource[0].diffuse; //Diffuse= gl_Color.rgb * max(0.0, dot(normalize(VNormal), gl_LightSource[0].position.xyz)); + // Super hack: if diffuse material alpha is less than 1, assume a // transparency animation is at work if (gl_FrontMaterial.diffuse.a < 1.0) @@ -47,8 +45,6 @@ void main(void) else alpha = gl_Color.a; - fogCoord = abs(ecPosition3.z); - // Vertex in eye coordinates vec3 vertVec = ecPosition.xyz; @@ -60,10 +56,8 @@ void main(void) vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0); reflVec = normalize(gl_ModelViewMatrixInverse * reflect_eye).xyz; - gl_FrontColor = gl_Color; - constantColor = gl_FrontMaterial.emission - + gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient); + gl_FrontColor = gl_FrontMaterial.emission + gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient); gl_Position = ftransform(); gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -} \ No newline at end of file +} diff --git a/Shaders/reflect.frag b/Shaders/reflect.frag index 1f7fb6924..d1424b478 100644 --- a/Shaders/reflect.frag +++ b/Shaders/reflect.frag @@ -4,17 +4,15 @@ #version 120 -varying vec4 rawpos; -varying vec4 ecPosition; +varying vec3 rawpos; varying vec3 VNormal; -varying vec3 Normal; varying vec4 constantColor; varying vec3 vViewVec; varying vec3 reflVec; varying vec4 Diffuse; -varying vec3 lightDir, halfVector; -varying float alpha, fogCoord; +varying float alpha; +varying float fogCoord; uniform samplerCube Environment; uniform sampler2D Rainbow; @@ -36,8 +34,11 @@ void main (void) float NdotL, NdotHV; vec4 color = constantColor; vec4 specular = vec4(0.0); - n = VNormal; - NdotL = max(0.0, dot(n, lightDir)); + n = normalize(VNormal); + vec3 lightDir = gl_LightSource[0].position.xyz; + vec3 halfVector = gl_LightSource[0].halfVector.xyz; + + NdotL = dot(n, lightDir); // calculate the specular light if (NdotL > 0.0) { @@ -56,7 +57,6 @@ void main (void) vec4 texelcolor = color * texel + specular; // calculate the fog factor - float fogCoord = ecPosition.z; const float LOG2 = 1.442695; float fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); fogFactor = clamp(fogFactor, 0.0, 1.0); diff --git a/Shaders/reflect.vert b/Shaders/reflect.vert index ed76cee0f..4c8928120 100644 --- a/Shaders/reflect.vert +++ b/Shaders/reflect.vert @@ -2,17 +2,15 @@ // Licence: GPL v2 // Author: Vivian Meazza. -varying vec4 rawpos; -varying vec4 ecPosition; +varying vec3 rawpos; varying vec3 VNormal; -varying vec3 Normal; varying vec4 constantColor; varying vec3 vViewVec; varying vec3 reflVec; varying vec4 Diffuse; -varying vec3 normal, lightDir, halfVector; -varying float alpha, fogCoord; +varying float alpha; +varying float fogCoord; uniform mat4 osg_ViewMatrixInverse; @@ -20,19 +18,16 @@ uniform mat4 osg_ViewMatrixInverse; void main(void) { - rawpos = gl_Vertex; - ecPosition = gl_ModelViewMatrix * gl_Vertex; - vec3 ecPosition3 = vec3(gl_ModelViewMatrix * gl_Vertex) / ecPosition.w; + rawpos = gl_Vertex.xyz / gl_Vertex.w; + vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex; + ecPosition.xyz = ecPosition.xyz / ecPosition.w; vec3 t = normalize(cross(gl_Normal, vec3(1.0,0.0,0.0))); vec3 b = normalize(cross(gl_Normal,t)); vec3 n = normalize(gl_Normal); VNormal = normalize(gl_NormalMatrix * gl_Normal); - Normal = normalize(gl_Normal); - lightDir = normalize(vec3(gl_LightSource[0].position)); - halfVector = normalize(gl_LightSource[0].halfVector.xyz); Diffuse = gl_Color * gl_LightSource[0].diffuse; //Diffuse= gl_Color.rgb * max(0.0, dot(normalize(VNormal), gl_LightSource[0].position.xyz)); // Super hack: if diffuse material alpha is less than 1, assume a @@ -42,7 +37,7 @@ void main(void) else alpha = gl_Color.a; - fogCoord = abs(ecPosition3.z); + fogCoord = abs(ecPosition.z); // Vertex in eye coordinates vec3 vertVec = ecPosition.xyz; @@ -61,4 +56,4 @@ void main(void) gl_Position = ftransform(); gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; -} \ No newline at end of file +} From 0fa470269f1f59fa6be4df8b0e6f1c34d06fb2b1 Mon Sep 17 00:00:00 2001 From: Frederic Bouvier Date: Thu, 17 Feb 2011 23:19:03 +0100 Subject: [PATCH 2/2] Fix issue #246 http://code.google.com/p/flightgear-bugs/issues/detail?id=246 by Lauri Peltonen --- Shaders/default.frag | 12 ++++++------ Shaders/model-default.frag | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Shaders/default.frag b/Shaders/default.frag index 3b01a6676..52afbaf2d 100644 --- a/Shaders/default.frag +++ b/Shaders/default.frag @@ -14,7 +14,7 @@ float luminance(vec3 color) void main() { - vec3 n, halfV; + vec3 n; float NdotL, NdotHV, fogFactor; vec4 color = gl_Color; vec3 lightDir = gl_LightSource[0].position.xyz; @@ -22,16 +22,16 @@ void main() vec4 texel; vec4 fragColor; vec4 specular = vec4(0.0); - n = normalize(normal); + // If gl_Color.a == 0, this is a back-facing polygon and the // normal should be reversed. + n = (2.0 * gl_Color.a - 1.0) * normal; + n = normalize(n); - n = (2.0 * gl_Color.a - 1.0) * n; - NdotL = max(dot(n, lightDir), 0.0); + NdotL = dot(n, lightDir); if (NdotL > 0.0) { color += diffuse_term * NdotL; - halfV = halfVector; - NdotHV = max(dot(n, halfV), 0.0); + NdotHV = max(dot(n, halfVector), 0.0); if (gl_FrontMaterial.shininess > 0.0) specular.rgb = (gl_FrontMaterial.specular.rgb * gl_LightSource[0].specular.rgb diff --git a/Shaders/model-default.frag b/Shaders/model-default.frag index 5ed50a642..2c94b92d0 100644 --- a/Shaders/model-default.frag +++ b/Shaders/model-default.frag @@ -22,11 +22,11 @@ void main() vec4 texel; vec4 fragColor; vec4 specular = vec4(0.0); - n = normalize(normal); // If gl_Color.a == 0, this is a back-facing polygon and the // normal should be reversed. - n = (2.0 * gl_Color.a - 1.0) * n; - NdotL = max(dot(n, lightDir), 0.0); + n = (2.0 * gl_Color.a - 1.0) * normal; + n = normalize(n); + NdotL = dot(n, lightDir); if (NdotL > 0.0) { color += diffuse_term * NdotL; halfV = normalize(halfVector);