/******************************************************************************* * * Simple GLSL FX file * *******************************************************************************/ #pragma language GLSL // un-tweakables // structures and shaders ///////////////////// varying vec3 eyeNormal; varying vec4 eyePos; varying vec3 eyeView; varying vec2 texCoord; varying vec3 eyeTangent; varying vec3 eyeBinormal; 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; eyeTangent = gl_NormalMatrix * gl_MultiTexCoord1.stp; eyeBinormal = gl_NormalMatrix * gl_MultiTexCoord2.stp; eyeView = -vec3(gl_ModelViewMatrix * gl_Vertex); texCoord = gl_MultiTexCoord0.st; } 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; eyeTangent = gl_NormalMatrix * gl_MultiTexCoord1.stp; eyeBinormal = gl_NormalMatrix * gl_MultiTexCoord2.stp; 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 sampler2D bumpMap; 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 tang = normalize( eyeTangent ); vec3 binorm = normalize( eyeBinormal ); vec3 view = normalize(eyeView); vec3 bumpNorm = texture2D( bumpMap, texCoord).xyz * 2.0 - 1.0; bumpNorm = norm*bumpNorm.z + tang*bumpNorm.x + binorm*bumpNorm.y; vec3 refl = normalize( -reflect( view, bumpNorm)); float nDotL = clamp( dot( bumpNorm, 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 tang = normalize( eyeTangent ); vec3 binorm = normalize( eyeBinormal ); vec3 bumpNorm = texture2D( bumpMap, texCoord).xyz * 2.0 - 1.0; bumpNorm = norm*bumpNorm.z + tang*bumpNorm.x + binorm*bumpNorm.y; vec3 refl = normalize( -reflect( view, bumpNorm)); vec3 diffuse = texture2D( diffuseTexture, texCoord).rgb; float nDotL = clamp( dot( bumpNorm, 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 phongBump { pass p0 { VertexShader = compile vs_2_0 mainVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainPS(); } } technique texturedPhongBump { pass p0 { VertexShader = compile vs_2_0 mainTexVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainTexPS(); } }