// Hardware ShadowMap // tex2Dproj with NO linear filtering // SHADOWMAP generation //----------------------------------------------------------------------------- // Vertex Shader: RenderShadowMap_00_VS // Desc: Process vertex for the shadow map //----------------------------------------------------------------------------- void RenderShadowMap_00_VS( in float4 Position : POSITION, // input position in modeling space out float4 ldPosition : POSITION ) // output position in light's device space { ldPosition = mul( Position, WorldLightProj ); } //----------------------------------------------------------------------------- // Pixel Shader: RenderShadowMap_00_PS // Desc: Process pixel for the shadow map //----------------------------------------------------------------------------- float4 RenderShadowMap_00_PS(void) : COLOR { return float4(0, 0, 0, 1); // just return any color, only the z is used } // SCENE RENDERING //----------------------------------------------------------------------------- // Vertex Shader: RenderSceneWithTechnique_00_VS // Desc: Process vertex for scene //----------------------------------------------------------------------------- VS_OUTPUT RenderSceneWithTechnique_00_VS( VS_INPUT IN ) { VS_OUTPUT OUT = (VS_OUTPUT)0; OUT.Color = IN.Color; OUT.Tex = IN.Tex; // transform model-space vertex position to light's normalized device space: OUT.ldPosition = mul(mul(IN.Position, WorldLightProj), TexScaleBias); // transform model-space vertex position to normalized screen space: OUT.hPosition = mul(IN.Position, WorldViewProj); return OUT; } //----------------------------------------------------------------------------- // Pixel Shader: RenderSceneWithTechnique_00_PS // Desc: Hardware shadow map with NO linear filtering and 1 sample //----------------------------------------------------------------------------- float4 RenderSceneWithTechnique_00_PS( VS_OUTPUT IN ) : COLOR { float visibility = tex2Dproj( g_ShadowMapZSampler_Point, IN.ldPosition ).x; float4 retColor = float4(0,0,0,1); if( g_bRenderBaseTexture ){ retColor.xyz = tex2D( g_samScene, IN.Tex ).xyz * g_vMaterial; } else{ visibility = ( visibility - 1 ) * g_fIntensity * g_fRealSamples; retColor = float4( visibility, visibility, visibility, 1 ); } return retColor; } // TECHNIQUE //----------------------------------------------------------------------------- // Techniques: RenderShadowMap // Desc: Render the shadow map //----------------------------------------------------------------------------- technique RenderShadowMap_0 { pass p0 { VertexShader = compile vs_2_0 RenderShadowMap_00_VS(); PixelShader = compile ps_2_0 RenderShadowMap_00_PS(); ZEnable = True; Lighting = False; ColorWriteEnable = 0; // no need to render to color, we only need z } } technique RenderSceneWithTechnique_0 { pass p0 { VertexShader = compile vs_2_0 RenderSceneWithTechnique_00_VS(); PixelShader = compile ps_2_0 RenderSceneWithTechnique_00_PS(); } }