/******************************************************************************* * * Simple GLSL FX file * *******************************************************************************/ #pragma language GLSL // un-tweakables // structures and shaders ///////////////////// varying vec3 eyeNormal; varying vec4 eyePos; varying vec3 eyeView; varying vec3 tangent; varying vec3 binormal; 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); tangent = normalize(gl_NormalMatrix * gl_MultiTexCoord1.stp); binormal = gl_NormalMatrix * gl_MultiTexCoord2.stp; } uniform vec3 lightDir = vec3( 0.57735, 0.57735, 0.57735); uniform vec3 lightColor = vec3( 1.0, 1.0, 1.0); uniform float xRoughness = 0.5; uniform float yRoughness = 0.15; 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 hvec = normalize(view+lightDir); vec3 tan = normalize(tangent) / xRoughness; vec3 binorm = normalize(cross(norm,tan)) / yRoughness; float cos_theta_r = clamp( dot( norm, view), 0.0, 1.0); float cos_theta_i = clamp( dot( norm, lightDir), 0.0, 1.0); float xDotH = dot( tan, hvec); float yDotH = dot( binorm, hvec); float rho = exp( -2.0*(xDotH*xDotH + yDotH*yDotH) / (1.0 + dot(hvec, norm))); rho = rho * inversesqrt( cos_theta_i * cos_theta_r); float illum = ( cos_theta_i > 0.0) ? cos_theta_i * rho : 0.0; illum /= 4.0*xRoughness*yRoughness; gl_FragColor = vec4( illum*lightColor, 1.0); } //////// techniques //////////////////////////// technique aniso { pass p0 { VertexShader = compile vs_2_0 mainVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainPS(); } }