mobeen

01-28-2011, 04:44 AM

Hi all,

I have read the fabien tutorial on basic shadow mapping here http://www.fabiensanglard.net/shadowmapping/index.php but my shadow mapping is wrong. Check the attached image.

http://www3.ntu.edu.sg/home2007/mova0002/test/problem.png

I am calc. the lightMatrix using glm as follows.

lightOffsetMatrix=glm::translate(glm::mat4(1.0f), glm::vec3(0.5,0.5,0.5));

lightOffsetMatrix=glm::scale(lightOffsetMatrix, glm::vec3(0.5,0.5,0.5));

lightMV = glmLookAt(glm::vec3(light_position[0], light_position[1], light_position[2]),glm::vec3(0.0f), glm::vec3(0.0,1.0,0.0));

lightMatrix = (lightOffsetMatrix* (P*lightMV));

I define the glmLookAt function like this

glm::mat4 glmLookAt(glm::vec3 eye, glm::vec3 tgt, glm::vec3 up) {

glm::vec3 F = tgt - eye;

F = glm::normalize(F);

glm::vec3 newUp = glm::normalize(up);

glm::vec3 s = glm::cross(F,newUp);

up = glm::cross(s,F);

glm::mat4 matrix=glm::mat4(1.0f);

matrix[0]=glm::vec4(s.x, up.x,-F.x,0);

matrix[1]=glm::vec4(s.y, up.y,-F.y,0);

matrix[2]=glm::vec4(s.z, up.z,-F.z,0);

return glm::translate(matrix, -eye);

}

My vertex shader is this,

#version 330

in vec2 vUV;

in vec3 vVertex;

in vec3 vNormal;

smooth out vec2 vTexCoord;

smooth out vec4 vShadowCoord;

smooth out vec4 color;

uniform mat3 N;

uniform mat4 MV;

uniform mat4 MVP;

uniform vec4 lightPos;

uniform vec4 mat_ambient;

uniform vec4 mat_diffuse;

uniform vec4 mat_specular;

uniform float mat_shininess;

uniform vec4 light_ambient;

uniform vec4 light_diffuse;

uniform vec4 light_specular;

uniform mat4 lightMatrix;

void main()

{

vTexCoord = vUV;

vec3 Nr = normalize(N*vNormal); //transform the normal vector by the normal matrix (inverse transpose of the modelview matrix)

vec4 esPos = MV*vec4(vVertex,1);

vec3 ecPosition3 = (vec3 (esPos.xyz)) / esPos.w;

vec3 V = vec3 (0.0, 0.0, 1.0);

// Compute vector from surface to light position

vec3 L = lightPos.xyz - ecPosition3;

// Normalize the vector from surface to light position

L = normalize(L);

vec3 H = normalize( V + L);

vec4 A = mat_ambient*light_ambient;

float diffuse = max(dot(Nr,L),0.0);

float pf=0;

if (diffuse == 0.0)

{

pf = 0.0;

}

else

{

pf = max( pow(dot(Nr,H), mat_shininess), 0.);

}

vec4 S = light_specular*mat_specular* pf;

vec4 D = diffuse*mat_diffuse*light_diffuse;

color = A + D + S;

vShadowCoord = (lightMatrix*vec4(vVertex,1)) ;

gl_Position = MVP*vec4(vVertex,1);

}

and the fragment shader is this,

#version 330

smooth in vec2 vTexCoord;

smooth in vec4 color;

smooth in vec4 vShadowCoord;

out vec4 vFragColor;

uniform sampler2D textureMap;

uniform bool has_texture;

uniform sampler2DShadow shadowMap;

void main(void)

{

if(has_texture)

vFragColor = texture(textureMap, vTexCoord)*color;

else

vFragColor = color;

float shadow = texture (shadowMap, vShadowCoord.xyz);

vFragColor *= shadow;

}

Am i missing anything obvious.

I have read the fabien tutorial on basic shadow mapping here http://www.fabiensanglard.net/shadowmapping/index.php but my shadow mapping is wrong. Check the attached image.

http://www3.ntu.edu.sg/home2007/mova0002/test/problem.png

I am calc. the lightMatrix using glm as follows.

lightOffsetMatrix=glm::translate(glm::mat4(1.0f), glm::vec3(0.5,0.5,0.5));

lightOffsetMatrix=glm::scale(lightOffsetMatrix, glm::vec3(0.5,0.5,0.5));

lightMV = glmLookAt(glm::vec3(light_position[0], light_position[1], light_position[2]),glm::vec3(0.0f), glm::vec3(0.0,1.0,0.0));

lightMatrix = (lightOffsetMatrix* (P*lightMV));

I define the glmLookAt function like this

glm::mat4 glmLookAt(glm::vec3 eye, glm::vec3 tgt, glm::vec3 up) {

glm::vec3 F = tgt - eye;

F = glm::normalize(F);

glm::vec3 newUp = glm::normalize(up);

glm::vec3 s = glm::cross(F,newUp);

up = glm::cross(s,F);

glm::mat4 matrix=glm::mat4(1.0f);

matrix[0]=glm::vec4(s.x, up.x,-F.x,0);

matrix[1]=glm::vec4(s.y, up.y,-F.y,0);

matrix[2]=glm::vec4(s.z, up.z,-F.z,0);

return glm::translate(matrix, -eye);

}

My vertex shader is this,

#version 330

in vec2 vUV;

in vec3 vVertex;

in vec3 vNormal;

smooth out vec2 vTexCoord;

smooth out vec4 vShadowCoord;

smooth out vec4 color;

uniform mat3 N;

uniform mat4 MV;

uniform mat4 MVP;

uniform vec4 lightPos;

uniform vec4 mat_ambient;

uniform vec4 mat_diffuse;

uniform vec4 mat_specular;

uniform float mat_shininess;

uniform vec4 light_ambient;

uniform vec4 light_diffuse;

uniform vec4 light_specular;

uniform mat4 lightMatrix;

void main()

{

vTexCoord = vUV;

vec3 Nr = normalize(N*vNormal); //transform the normal vector by the normal matrix (inverse transpose of the modelview matrix)

vec4 esPos = MV*vec4(vVertex,1);

vec3 ecPosition3 = (vec3 (esPos.xyz)) / esPos.w;

vec3 V = vec3 (0.0, 0.0, 1.0);

// Compute vector from surface to light position

vec3 L = lightPos.xyz - ecPosition3;

// Normalize the vector from surface to light position

L = normalize(L);

vec3 H = normalize( V + L);

vec4 A = mat_ambient*light_ambient;

float diffuse = max(dot(Nr,L),0.0);

float pf=0;

if (diffuse == 0.0)

{

pf = 0.0;

}

else

{

pf = max( pow(dot(Nr,H), mat_shininess), 0.);

}

vec4 S = light_specular*mat_specular* pf;

vec4 D = diffuse*mat_diffuse*light_diffuse;

color = A + D + S;

vShadowCoord = (lightMatrix*vec4(vVertex,1)) ;

gl_Position = MVP*vec4(vVertex,1);

}

and the fragment shader is this,

#version 330

smooth in vec2 vTexCoord;

smooth in vec4 color;

smooth in vec4 vShadowCoord;

out vec4 vFragColor;

uniform sampler2D textureMap;

uniform bool has_texture;

uniform sampler2DShadow shadowMap;

void main(void)

{

if(has_texture)

vFragColor = texture(textureMap, vTexCoord)*color;

else

vFragColor = color;

float shadow = texture (shadowMap, vShadowCoord.xyz);

vFragColor *= shadow;

}

Am i missing anything obvious.