Hi all.
Attached are two patches that should fix bug reports
http://code.google.com/p/flightgear-bugs/issues/detail?id=246 and
http://code.google.com/p/flightgear-bugs/issues/detail?id=252 . The
patches should be against latest git, they are done with command "git
diff".
The first one just normalizes the length of the normal after flipping it
in case of double sided polygons. I guess there is some interpolation
issue with older nvidia cards leading to the light saturation issue, but
at least on my computer this seemed to fix it.
The second one removes unused varyings (vec4 -> vec3 etc) from reflect,
bumpspec and reflect-bump-spec shaders and should allow running those
shaders on older hardware. Authors of models using those shaders should
probably check that the visual appearance is still the same, I did a
quick check with some aircrafts and they looked fine to me.
- Lauri Peltonen (Zan)
diff --git a/Shaders/default.frag b/Shaders/default.frag
index 3b01a66..52afbaf 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 5ed50a6..2c94b92 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);
diff --git a/Shaders/bumpspec.frag b/Shaders/bumpspec.frag
index b708746..16f8d16 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 4ed1b8a..d7b7a89 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 859d21e..70a6c1a 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 d2b7e9f..6eb1a68 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 1f7fb69..d1424b4 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 ed76cee..4c89281 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
+}
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel