struct VertexOut{
float4 oPosition : POSITION;
float4 texCoordProj : TEXCOORD0;
float4 diffuseLighting : TEXCOORD1;
};
VertexOut ShadowMapVert(float4 position : POSITION,
float3 normal : NORMAL,
uniform float4x4 modelViewProj,
uniform float3 lightPosition,
uniform float4x4 textureMatrix)
{
VertexOut Oute;
Oute.oPosition = mul(modelViewProj, position);
float3 lightDirection = normalize(lightPosition - position.xyz);
Oute.diffuseLighting = dot(lightDirection, normal);
// Compute texture coordinates for
// querying the shadow map
Oute.texCoordProj = mul(textureMatrix, position);
return Oute;
}
struct FragmentOut
{
float4 color : COLOR;
};
FragmentOut ShadowMapFrag(float4 col : COLOR IN,
float4 texCoordProj : TEXCOORD0,
float4 diffuseLighting : TEXCOORD1,
uniform sampler2D ShadowMap)
{
FragmentOut Out;
// Fetch color from the projective texture
float4 projColor = tex2Dproj(ShadowMap, texCoordProj);
Out.color = projColor * diffuseLighting;
return Out;
}
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &shadowMapTexture);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16_ARB, shadowMapSize, shadowMapSize, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL );
glTexParameteri(GL_TEXTURE_2D,GL_DEPTH_TEXTURE_MODE,GL_INTENSITY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
...
RenderWorld(update);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize);
...
