bwdun123

04-11-2017, 02:40 AM

Hello!

I've implemented basic shadow mapping for directional light, but the result is a bit different than what i imagined.

Here is 2361 image of the scene from light's position but i moved a bit to the left so we could see the shadow. I expected the shadow to be the same width and height as the cube that "casts" it on the ground. Is it normal for a shadow to be smaller ?

Here are the matrices i am using:

glm::vec3 worldSpace_lightPos = glm::vec3( 4.0f, 7.0f, 4.0f );

glm::vec3 worldSpace_planePos = glm::vec3( 8.0f, 0.0f, 5.0f ); // Plane mesh's position

glm::vec3 worldSpace_cubePos = glm::vec3( 5.0f, 1.5f, 5.0f ); // Cube mesh's position

glm::vec3 worldSpace_invLightDir = worldSpace_lightPos - ( worldSpace_cubePos + glm::vec3(0.0f, +0.5f, 0.0f) ); // from cube towards light's pos

glm::vec3 lightSpace_cameraDir = worldSpace_cubePos + glm::vec3(0.0f, +0.5f, 0.0f); // center of the cube

glm::mat4 V = glm::lookAt( glm::vec3(-8.0f, 7.0f, 0.0f), glm::vec3(0,0,0), glm::vec3(0,1,0) );

glm::mat4 P = glm::perspective(45.0f, 4.0f/3.0f, 0.01f, 100.0f);

glm::mat4 depthV = glm::lookAt(worldSpace_lightPos, lightSpace_cameraDir, glm::vec3(0,1,0));

glm::mat4 depthP = glm::ortho<float>(-10,10,-20,20,-20,20);

And texture params:

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);

And my 2nd pass fragment shader:

#version 330 core

in vec3 eyeSpace_coordinates;

in vec3 eyeSpace_normals;

in vec4 lightSpace_coordinates;

uniform sampler2D ShadowMap;

uniform vec3 lightDir;

uniform mat4 view;

uniform mat4 projection;

float calculateShadow()

{

float bias = 0.0005f;

float isShadowed = 1.0f;

vec3 nuevo =

// lightSpace_coordinates.xyz / lightSpace_coordinates.z;

lightSpace_coordinates.xyz;

// nuevo = nuevo * 0.5 + 0.5;

float closestDepth = texture(ShadowMap, nuevo.xy).r;

float currentDepth = nuevo.z - bias;

if( currentDepth > closestDepth )

isShadowed = 0.3f;

return isShadowed;

}

vec3 CalcPointLight(vec3 normal, vec3 fragPos)

{

vec3 eyeSpace_lightDir = ( view * vec4( lightDir, 0.0f ) ).xyz;

vec3 l = normalize(eyeSpace_lightDir);

float cosTheta = max( dot(normal, l), 0.0 );

vec3 diffuse = vec3( 0.8, 0.8, 0.8 ) * cosTheta * vec3(1.0f, 0.0f, 0.0f);

return diffuse;

}

void main()

{

vec4 ls_Pos = lightSpace_coordinates/ lightSpace_coordinates.w;

vec3 n = normalize(eyeSpace_normals);

float shadow = calculateShadow();

vec3 light = CalcPointLight(n, eyeSpace_coordinates);

gl_FragColor = vec4( vec3(shadow), 1.0f ) * vec4(light, 1.0f);

}

Sorry for the bad image, but i'm not allowed to post URL's... hopefully you get the idea :)

I've implemented basic shadow mapping for directional light, but the result is a bit different than what i imagined.

Here is 2361 image of the scene from light's position but i moved a bit to the left so we could see the shadow. I expected the shadow to be the same width and height as the cube that "casts" it on the ground. Is it normal for a shadow to be smaller ?

Here are the matrices i am using:

glm::vec3 worldSpace_lightPos = glm::vec3( 4.0f, 7.0f, 4.0f );

glm::vec3 worldSpace_planePos = glm::vec3( 8.0f, 0.0f, 5.0f ); // Plane mesh's position

glm::vec3 worldSpace_cubePos = glm::vec3( 5.0f, 1.5f, 5.0f ); // Cube mesh's position

glm::vec3 worldSpace_invLightDir = worldSpace_lightPos - ( worldSpace_cubePos + glm::vec3(0.0f, +0.5f, 0.0f) ); // from cube towards light's pos

glm::vec3 lightSpace_cameraDir = worldSpace_cubePos + glm::vec3(0.0f, +0.5f, 0.0f); // center of the cube

glm::mat4 V = glm::lookAt( glm::vec3(-8.0f, 7.0f, 0.0f), glm::vec3(0,0,0), glm::vec3(0,1,0) );

glm::mat4 P = glm::perspective(45.0f, 4.0f/3.0f, 0.01f, 100.0f);

glm::mat4 depthV = glm::lookAt(worldSpace_lightPos, lightSpace_cameraDir, glm::vec3(0,1,0));

glm::mat4 depthP = glm::ortho<float>(-10,10,-20,20,-20,20);

And texture params:

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);

And my 2nd pass fragment shader:

#version 330 core

in vec3 eyeSpace_coordinates;

in vec3 eyeSpace_normals;

in vec4 lightSpace_coordinates;

uniform sampler2D ShadowMap;

uniform vec3 lightDir;

uniform mat4 view;

uniform mat4 projection;

float calculateShadow()

{

float bias = 0.0005f;

float isShadowed = 1.0f;

vec3 nuevo =

// lightSpace_coordinates.xyz / lightSpace_coordinates.z;

lightSpace_coordinates.xyz;

// nuevo = nuevo * 0.5 + 0.5;

float closestDepth = texture(ShadowMap, nuevo.xy).r;

float currentDepth = nuevo.z - bias;

if( currentDepth > closestDepth )

isShadowed = 0.3f;

return isShadowed;

}

vec3 CalcPointLight(vec3 normal, vec3 fragPos)

{

vec3 eyeSpace_lightDir = ( view * vec4( lightDir, 0.0f ) ).xyz;

vec3 l = normalize(eyeSpace_lightDir);

float cosTheta = max( dot(normal, l), 0.0 );

vec3 diffuse = vec3( 0.8, 0.8, 0.8 ) * cosTheta * vec3(1.0f, 0.0f, 0.0f);

return diffuse;

}

void main()

{

vec4 ls_Pos = lightSpace_coordinates/ lightSpace_coordinates.w;

vec3 n = normalize(eyeSpace_normals);

float shadow = calculateShadow();

vec3 light = CalcPointLight(n, eyeSpace_coordinates);

gl_FragColor = vec4( vec3(shadow), 1.0f ) * vec4(light, 1.0f);

}

Sorry for the bad image, but i'm not allowed to post URL's... hopefully you get the idea :)