diff --git a/Effects/model-default.eff b/Effects/model-default.eff index 0fc689570..3cafe42f2 100644 --- a/Effects/model-default.eff +++ b/Effects/model-default.eff @@ -50,8 +50,8 @@ --> - Shaders/default.vert - Shaders/default.frag + Shaders/model-default.vert + Shaders/model-default.frag texture diff --git a/Shaders/model-default.frag b/Shaders/model-default.frag new file mode 100644 index 000000000..69b18221b --- /dev/null +++ b/Shaders/model-default.frag @@ -0,0 +1,31 @@ +// -*-C++-*- + +varying vec4 diffuse, constantColor; +varying vec3 normal, lightDir, halfVector; +varying float fogCoord; + +uniform sampler2D texture; + +void main() +{ + vec3 n, halfV; + float NdotL, NdotHV, fogFactor; + vec4 color = constantColor; + vec4 texel; + vec4 fragColor; + + n = normalize(normal); + 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) + color += gl_FrontMaterial.specular * gl_LightSource[0].specular + * pow(NdotHV, gl_FrontMaterial.shininess); + } + texel = texture2D(texture, gl_TexCoord[0].st); + fragColor = color * texel; + 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 new file mode 100644 index 000000000..014464bb7 --- /dev/null +++ b/Shaders/model-default.vert @@ -0,0 +1,27 @@ +// -*-C++-*- + +// Shader that uses OpenGL state values to do per-pixel lighting +// +// 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. + +varying vec4 diffuse, constantColor; +varying vec3 normal, lightDir, halfVector; +varying float fogCoord; + +void main() +{ + vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex); + gl_Position = ftransform(); + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; + normal = gl_NormalMatrix * gl_Normal; + lightDir = normalize(vec3(gl_LightSource[0].position)); + halfVector = normalize(gl_LightSource[0].halfVector.xyz); + diffuse = gl_Color * gl_LightSource[0].diffuse; + constantColor = gl_FrontLightModelProduct.sceneColor + + gl_FrontMaterial.ambient * gl_LightSource[0].ambient; + fogCoord = abs(ecPosition.z); +}