/******************************************************************************* * * Simple GLSL FX file * *******************************************************************************/ #pragma language GLSL // un-tweakables // structures and shaders ///////////////////// varying vec3 eyeNormal; varying vec4 eyePos; varying vec3 eyeView; varying vec2 texCoord; void mainVS() { // Must write gl_Position for rasterization to be defined.... // gl_Position = ftransform(); // Transform to shading space (we are going to shade in eyespace) // // eyeNormal = gl_NormalMatrix * gl_Normal; eyeView = -vec3(gl_ModelViewMatrix * gl_Vertex); } void mainTexVS() { // Must write gl_Position for rasterization to be defined.... // gl_Position = ftransform(); // Transform to shading space (we are going to shade in eyespace) // // eyeNormal = gl_NormalMatrix * gl_Normal; eyeView = -vec3(gl_ModelViewMatrix * gl_Vertex); texCoord = gl_MultiTexCoord0.st; } uniform vec3 lightDir = vec3( 0.57735, 0.57735, 0.57735); uniform vec3 lightColor = vec3( 1.0, 1.0, 1.0); uniform vec3 diffuseMaterial = vec3( 0.0, 0.0, 0.8); uniform vec3 ambientLight = vec3( 0.2, 0.2, 0.2); uniform float shininess = 16.0; void mainPS() { // // Since the EyeNormal is getting interpolated, we // have to first restore it by normalizing it. // vec3 norm = normalize( eyeNormal ); vec3 view = normalize(eyeView); vec3 refl = normalize( -reflect(view,norm)); float nDotL = clamp( dot( norm, lightDir), 0.0, 1.0); float rDotL = clamp( dot( refl, lightDir), 0.0, 1.0); rDotL = (nDotL > 0.0) ? pow( rDotL, shininess) : 0.0; vec3 color = lightColor*nDotL*diffuseMaterial + diffuseMaterial*ambientLight + rDotL*lightColor; gl_FragColor = vec4( color, 1.0); } uniform sampler2D diffuseTexture; void mainTexPS() { // // Since the EyeNormal is getting interpolated, we // have to first restore it by normalizing it. // vec3 norm = normalize( eyeNormal ); vec3 view = normalize(eyeView); vec3 refl = normalize( -reflect(view,norm)); vec3 diffuse = texture2D( diffuseTexture, texCoord).rgb; float nDotL = clamp( dot( norm, lightDir), 0.0, 1.0); float rDotL = clamp( dot( refl, lightDir), 0.0, 1.0); rDotL = (nDotL > 0.0) ? pow( rDotL, shininess) : 0.0; vec3 color = lightColor*nDotL*diffuse + diffuse*ambientLight + rDotL*lightColor; gl_FragColor = vec4( color, 1.0); } //////// techniques //////////////////////////// technique phong { pass p0 { VertexShader = compile vs_2_0 mainVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainPS(); } } technique texturedPhong { pass p0 { VertexShader = compile vs_2_0 mainTexVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainTexPS(); } }