1
0
Fork 0
fgdata/Shaders/default.frag
Tim Moore d27174028e shader optimizations
Use two-pass rendering for some shaders to reduce the cost of expensive shaders.

Implement some material animations using a shader uniform.

Eliminate use of gl_FrontFacing. It's too buggy on ATI/ Macintosh.
2010-08-14 00:13:16 +02:00

49 lines
1.6 KiB
C++

// -*-C++-*-
// Ambient term comes in gl_Color.rgb.
varying vec4 diffuse_term;
varying vec3 normal;
varying float fogCoord;
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 = gl_Color;
vec3 lightDir = gl_LightSource[0].position.xyz;
vec3 halfVector = gl_LightSource[0].halfVector.xyz;
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);
if (NdotL > 0.0) {
color += diffuse_term * NdotL;
halfV = 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 = diffuse_term.a;
// 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);
}