I got a problem with my shadow map projection.
The problem is that i don’t know how to project my shadowMap down to my plane. I got my shadowMap correct made.
What i don’t understand i this code here:
shadowCrd.x = 0.5 * (projPos.z + projPos.x);
shadowCrd.y = 0.5 * (projPos.z - projPos.y);
shadowCrd.z = 0.0;
shadowCrd.w = -projPos.z;
it the coordinates of the shadowMap.
This example is taken from RenderMonkey DX9, and i have tried to transelate it into GLSL. My code is a bit different from the example.
Vertex Shader:
uniform vec4 lightPos;
uniform vec4 viewPos;
uniform float test;
varying vec3 vNormal;
varying vec3 lightVec;
varying vec3 viewVec;
varying vec4 shadowCrd;
varying vec4 vertex;
void main(void)
{
vec4 vertex = gl_Vertex;
vec4 objectPos = gl_Vertex;
objectPos.xyz *= 1.0;
objectPos.y -= 0.0; //40.0;
vNormal = gl_Normal;
// WORLD SPACE
gl_Position = gl_ModelViewProjectionMatrix * objectPos;
lightVec = lightPos.xyz - objectPos.xyz;
viewVec = viewPos.xyz - objectPos.xyz;
// Creating wiew vectors and view matrix for the light
vec3 n = vec3(normalize(lightPos.xyz));//n mot punkt
vec3 u = normalize(cross(vec3(0.0, 1.0, 0.0),n)); // u til siden
vec3 v = normalize(cross(n,u));//v opp
// Transform into light eye space
vec4 Pos = gl_Vertex;
Pos.xyz -= vec3(lightPos.x, lightPos.y, lightPos.z);
vec4 pos;
pos.x = dot(u, Pos.xyz);
pos.y = dot(v, Pos.xyz);
pos.z = dot(n, Pos.xyz);
pos.w = 1.0;
// Project it.
vec4 projPos = gl_ProjectionMatrix * pos;
// Use projective texturing to map the position of each fragment
// to its corresponding texel in the shadow map.
projPos.z += test; //0.0; // Øker størrelsen på skyggen.
shadowCrd.x = 0.5 * (projPos.z + projPos.x);
shadowCrd.y = 0.5 * (projPos.z - projPos.y);
shadowCrd.z = 0.0;
shadowCrd.w = -projPos.z;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment shader:
uniform sampler2D ShadowMap;
uniform sampler2D texFloor;
uniform float shadowBias;
uniform float backProjectionCut;
uniform vec4 platformColor;
uniform vec4 lightPos;
varying vec3 vNormal;
varying vec3 lightVec;
varying vec3 viewVec;
varying vec4 shadowCrd;
varying vec4 vertex;
void main(void)
{
// Normal object
vec3 Normal = normalize(vNormal);
// Radial distance
float depth = length(lightVec);
// Normalizes light vector
vec3 newlightVec = lightVec/depth;
// Standard lighting
float diffuse = clamp(dot(normalize(lightVec), Normal), 0.0, 1.0);
diffuse = diffuse * 2.0 - 1.0;
float specular = pow(clamp(dot(reflect(-normalize(viewVec), Normal), normalize(lightVec)), 0.0, 1.0), 32.0);
// The depth of the fragment closest to the light
vec4 shadowMap = texture2DProj(ShadowMap, shadowCrd);
// If the depth is larger than the stored depth, this fragment
// is not the closest to the light, that is we are in shadow.
// Otherwise, we're lit. Add a bias to avoid precision issues.
float shadow = 0.0;
if( 1.0/length(lightPos.xyz - vertex.xyz) >= shadowMap.x )
shadow = 1.0;
// TESTING!!!
vec4 colFloor = texture2D(texFloor, gl_TexCoord[2].st);
gl_FragColor = colFloor + (specular + diffuse * colFloor) * 0.5 * shadow;
}