workaround for gl_FrontFacing bug
gl_FrontFacing isn't well supported on some Macintosh / ATI combinations.
This commit is contained in:
parent
acac689d59
commit
c964caa845
4 changed files with 72 additions and 6 deletions
|
@ -5,6 +5,7 @@
|
||||||
<texture n ="0">
|
<texture n ="0">
|
||||||
<type>white</type>
|
<type>white</type>
|
||||||
</texture>
|
</texture>
|
||||||
|
<vertex-program-two-side type="bool">false</vertex-program-two-side>
|
||||||
</parameters>
|
</parameters>
|
||||||
<technique n="10">
|
<technique n="10">
|
||||||
<predicate>
|
<predicate>
|
||||||
|
@ -60,14 +61,23 @@
|
||||||
</internal-format>
|
</internal-format>
|
||||||
-->
|
-->
|
||||||
</texture-unit>
|
</texture-unit>
|
||||||
|
<vertex-program-two-side>
|
||||||
|
<use>vertex-program-two-side</use>
|
||||||
|
</vertex-program-two-side>
|
||||||
<program>
|
<program>
|
||||||
<vertex-shader>Shaders/model-default.vert</vertex-shader>
|
<vertex-shader>Shaders/model-default.vert</vertex-shader>
|
||||||
<fragment-shader>Shaders/default.frag</fragment-shader>
|
<fragment-shader>Shaders/model-default.frag</fragment-shader>
|
||||||
</program>
|
</program>
|
||||||
<uniform>
|
<uniform>
|
||||||
<name>texture</name>
|
<name>texture</name>
|
||||||
<type>sampler-2d</type>
|
<type>sampler-2d</type>
|
||||||
<value type="int">0</value></uniform>
|
<value type="int">0</value>
|
||||||
|
</uniform>
|
||||||
|
<uniform>
|
||||||
|
<name>twoSideHack</name>
|
||||||
|
<type>bool</type>
|
||||||
|
<value><use>vertex-program-two-side</use></value>
|
||||||
|
</uniform>
|
||||||
</pass>
|
</pass>
|
||||||
</technique>
|
</technique>
|
||||||
<technique n="11">
|
<technique n="11">
|
||||||
|
@ -106,6 +116,10 @@
|
||||||
<mode>modulate</mode>
|
<mode>modulate</mode>
|
||||||
</environment>
|
</environment>
|
||||||
</texture-unit>
|
</texture-unit>
|
||||||
|
<!-- A two-sided lighting model is set by default near the root
|
||||||
|
of the scene graph. Perhaps that ought to be set in this
|
||||||
|
effect?
|
||||||
|
-->
|
||||||
</pass>
|
</pass>
|
||||||
</technique>
|
</technique>
|
||||||
</PropertyList>
|
</PropertyList>
|
||||||
|
|
46
Shaders/model-default.frag
Normal file
46
Shaders/model-default.frag
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
// -*-C++-*-
|
||||||
|
|
||||||
|
varying vec4 diffuse, constantColor;
|
||||||
|
varying vec3 normal, lightDir, halfVector;
|
||||||
|
varying float fogCoord, alpha;
|
||||||
|
|
||||||
|
uniform bool twoSideHack;
|
||||||
|
|
||||||
|
uniform sampler2D texture;
|
||||||
|
|
||||||
|
float luminance(vec3 color)
|
||||||
|
{
|
||||||
|
return dot(vec3(0.212671, 0.715160, 0.072169), color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec3 n, halfV;
|
||||||
|
float NdotL, NdotHV, fogFactor;
|
||||||
|
vec4 color = constantColor;
|
||||||
|
vec4 texel;
|
||||||
|
vec4 fragColor;
|
||||||
|
vec4 specular = vec4(0.0);
|
||||||
|
n = normalize(normal);
|
||||||
|
if (twoSideHack && gl_Color.a == 0.0)
|
||||||
|
n = -n;
|
||||||
|
NdotL = max(dot(n, lightDir), 0.0);
|
||||||
|
if (NdotL > 0.0) {
|
||||||
|
color += diffuse * NdotL;
|
||||||
|
halfV = normalize(halfVector);
|
||||||
|
NdotHV = max(dot(n, halfV), 0.0);
|
||||||
|
if (gl_FrontMaterial.shininess > 0.0)
|
||||||
|
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 + specular;
|
||||||
|
fogFactor = exp(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord);
|
||||||
|
gl_FragColor = mix(gl_Fog.color, fragColor, fogFactor);
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ varying vec4 diffuse, constantColor;
|
||||||
varying vec3 normal, lightDir, halfVector;
|
varying vec3 normal, lightDir, halfVector;
|
||||||
varying float alpha, fogCoord;
|
varying float alpha, fogCoord;
|
||||||
|
|
||||||
|
uniform bool twoSideHack;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
|
vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
|
||||||
|
@ -28,6 +30,12 @@ void main()
|
||||||
alpha = gl_FrontMaterial.diffuse.a;
|
alpha = gl_FrontMaterial.diffuse.a;
|
||||||
else
|
else
|
||||||
alpha = gl_Color.a;
|
alpha = gl_Color.a;
|
||||||
|
// Another hack for supporting two-sided lighting without using
|
||||||
|
// gl_FrontFacing in the fragment shader.
|
||||||
|
if (twoSideHack) {
|
||||||
|
gl_FrontColor = vec4(0.0, 0.0, 0.0, 1.0);
|
||||||
|
gl_BackColor = vec4(0.0, 0.0, 0.0, 0.0);
|
||||||
|
}
|
||||||
constantColor = gl_FrontLightModelProduct.sceneColor
|
constantColor = gl_FrontLightModelProduct.sceneColor
|
||||||
+ gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
|
+ gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
|
||||||
fogCoord = abs(ecPosition3.z);
|
fogCoord = abs(ecPosition3.z);
|
||||||
|
|
|
@ -22,8 +22,6 @@ void main()
|
||||||
vec4 specular = vec4(0.0);
|
vec4 specular = vec4(0.0);
|
||||||
|
|
||||||
n = normalize(normal);
|
n = normalize(normal);
|
||||||
if (!gl_FrontFacing)
|
|
||||||
n = -n;
|
|
||||||
NdotL = max(dot(n, lightDir), 0.0);
|
NdotL = max(dot(n, lightDir), 0.0);
|
||||||
if (NdotL > 0.0) {
|
if (NdotL > 0.0) {
|
||||||
color += diffuse * NdotL;
|
color += diffuse * NdotL;
|
||||||
|
|
Loading…
Reference in a new issue