/******************************************************************************* * * Simple HLSL FX file * *******************************************************************************/ // un-tweakables // structures and shaders ///////////////////// float4x4 wvIT : WorldViewInverseTranspose; float4x4 wvp : WorldViewProjection; float4x4 wv : WorldView; void mainVS( float4 vertex : POSITION0, float3 normal : NORMAL0, float4 tex: TEXCOORD0, float3 tangent : TEXCOORD1, float3 binormal : TEXCOORD2, out float4 position : POSITION0, out float3 eyeNormal : TEXCOORD0, out float3 eyeView : TEXCOORD1, out float2 texCoord: TEXCOORD2, out float3 eyeTangent : TEXCOORD3, out float3 eyeBinormal : TEXCOORD4) { // Must write gl_Position for rasterization to be defined.... // position = mul( vertex, wvp); // Transform to shading space (we are going to shade in eyespace) // // eyeNormal = mul( normal, float3x3(wvIT)); eyeTangent = mul( tangent, float3x3(wvIT)); eyeBinormal = mul( binormal, float3x3(wvIT)); eyeView = -mul( vertex, wv).xyz; texCoord = tex.xy; } float3 lightDir = float3( 0.57735, 0.57735, 0.57735); float3 lightColor = float3( 1.0, 1.0, 1.0); float3 diffuseMaterial = float3( 0.0, 0.0, 0.8); float3 ambientLight = float3( 0.2, 0.2, 0.2); sampler2D bumpMap; float shininess = 16.0; float4 mainPS( float3 eyeNormal : TEXCOORD0, float3 eyeView : TEXCOORD1, float2 texCoord: TEXCOORD2, float3 eyeTangent : TEXCOORD3, float3 eyeBinormal : TEXCOORD4) : COLOR0 { // // Since the EyeNormal is getting interpolated, we // have to first restore it by normalizing it. // float3 norm = normalize( eyeNormal ); float3 tang = normalize( eyeTangent ); float3 binorm = normalize( eyeBinormal ); float3 view = normalize(eyeView); float3 bumpNorm = tex2D( bumpMap, texCoord).xyz * 2.0 - 1.0; bumpNorm = norm*bumpNorm.z + tang*bumpNorm.x + binorm*bumpNorm.y; float3 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; float3 color = lightColor*nDotL*diffuseMaterial + diffuseMaterial*ambientLight + rDotL*lightColor; return float4( color, 1.0); } sampler2D diffuseTexture; float4 mainTexPS(float3 eyeNormal : TEXCOORD0, float3 eyeView : TEXCOORD1, float2 texCoord: TEXCOORD2, float3 eyeTangent : TEXCOORD3, float3 eyeBinormal : TEXCOORD4) : COLOR0 { // // Since the EyeNormal is getting interpolated, we // have to first restore it by normalizing it. // float3 norm = normalize( eyeNormal ); float3 view = normalize(eyeView); float3 tang = normalize( eyeTangent ); float3 binorm = normalize( eyeBinormal ); float3 bumpNorm = tex2D( bumpMap, texCoord).xyz * 2.0 - 1.0; bumpNorm = norm*bumpNorm.z + tang*bumpNorm.x + binorm*bumpNorm.y; float3 refl = normalize( -reflect( view, bumpNorm)); float3 diffuse = tex2D( 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; float3 color = lightColor*nDotL*diffuse + diffuse*ambientLight + rDotL*lightColor; return float4( 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 mainVS(); ZEnable = true; ZWriteEnable = true; ZFunc = LEqual; CullMode = Back; PixelShader = compile ps_2_0 mainTexPS(); } }