float4x4 WorldViewProj; float4x4 WorldView; float4x4 WorldViewIT; float3 mCameraPos; // model-space Camera position float3 mLightPos; // model-space Light position float focalDist; float focalRange; //------------------------------------------------------------------------------------ // macro definition for filtered samplers //------------------------------------------------------------------------------------ #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \ sampler2D g_samplerMap = sampler_state { \ Texture = ; \ MinFilter = Linear; \ MagFilter = Linear; \ MipFilter = Linear; \ AddressU = WRAP; \ AddressV = WRAP; \ }; texture ColorMap; SAMPLER_LINEAR(ColorMapSampler, ColorMap); texture BumpMap; SAMPLER_LINEAR(BumpMapSampler,BumpMap); // Illumination functions #include "shaders/illum.hlsl" struct VS_INPUT {float4 Position : POSITION; float3 Normal : NORMAL; float3 Binormal : BINORMAL; float3 Tangent : TANGENT; float4 TexCoord0 : TEXCOORD0; }; struct VS_OUTPUT { float4 hPosition : POSITION; // point in normalized device space before homogeneous division float2 TexCoord : TEXCOORD0; // texture coordinates float3 tView : TEXCOORD1; // tangent space view vector float3 tLight : TEXCOORD2; // tangent space light vector float Depth : TEXCOORD3; }; struct PS_OUTPUT { float4 Color : COLOR0; float4 Depth : COLOR1; }; //------------------------------------------------------------------------------------ // // Base vertex shader: vertex shader for all methods: calculates tangent-space // tLight, tView, hPosition vectors // //------------------------------------------------------------------------------------ VS_OUTPUT BumpVS(VS_INPUT IN) { VS_OUTPUT output; // object-space tangent matrix float3x3 Tan = float3x3(normalize(IN.Tangent), normalize(IN.Binormal), IN.Normal); // position in view-space float3 P = mul(IN.Position, WorldView); // model-space view vector float3 mView = mCameraPos - IN.Position; // model-space light vector float3 mLight = mLightPos - IN.Position; // tangent-space view vector output.tView = mul(Tan, mView); // tangent-space light vector output.tLight = mul(Tan, mLight); // vertex position before homogenious division output.hPosition = mul(IN.Position, WorldViewProj); // tex coordinates passed to pixel shader output.TexCoord = IN.TexCoord0; output.Depth = output.hPosition.z; return output; } //------------------------------------------------------------------------------------ // // BumpPS: Bump Mapping pixel shader // //------------------------------------------------------------------------------------ PS_OUTPUT BumpPS(VS_OUTPUT IN) { PS_OUTPUT output; // needs normalization because of linear interpolation float3 View = normalize( IN.tView ); // needs normalization because of linear interpolation float3 Light = normalize( IN.tLight ); // get tangent-space normal from normal map float3 Normal = tex2D(BumpMapSampler, IN.TexCoord).rgb; // illumination calculation output.Color = Illumination(Light, Normal, View, IN.TexCoord, Attenuation(IN.tLight)); float blur = saturate(abs(IN.Depth - focalDist) * focalRange); output.Depth = float4(IN.Depth, blur, 0, 0); return output; } //------------------------------------------------------------------------------------ // Technique definitions //------------------------------------------------------------------------------------ technique BumpMapping { pass p0 { VertexShader = compile vs_3_0 BumpVS(); PixelShader = compile ps_3_0 BumpPS(); } }