PDA

View Full Version : Problem with point light shadows (cubemap shadows)



Ryno Engine
08-20-2015, 01:00 AM
Hi everyone, I'm implementing shadows for point lights, by rendering the shadows inside a cubemap, and then sampling it in the light pass.

I managed to write inside the cubemap,but i'm having troubles in using the retrieved data during the lighting pass, even though, i think, the coordinate system is correctly calculated.
I'll post the shaders and explain them step by step:

SHADOW.FS

#version 430

in vec3 world_pos; //world position of the fragment
uniform vec3 light_world_pos; //world position of the light

layout(location = 0) out float color;

void main() {


color = length(world_pos - light_world_pos);
}


Here i just render in all six directions, and save the distance from the light (in world coordinates) inside the cubemap.

LIGHT.FS

float bias = 0.5;

vec4 position_world_space = inverse_VP_matrix * position_screen_space;
vec3 world_position = position_world_space.xyz / position_world_space.w;
vec3 world_light_position = point_light.position_and_attenuation.xyz;
vec3 light_direction = world_position - world_light_position;

float sampled_depth = texture(shadow_cube, light_direction).x;
float current_depth = length(light_direction ) - bias;


if (sampled_depth < current_depth ) visibility = 0;

Here i have a fragment position in screen space (because i'm using deferred rendering and i'm sampling the position from a buffer). I proceed to convert it to screen space with the inverse VP matrix.
The light position is a uniform in world space.
So now that both of them are in world position, I subtract them, and use the vector to sample the cubemap, and if the sampled value is shorter than the current distance, let there be shadows.
But somehow the shadows are wrong...can you see the error?

Ryno Engine
08-20-2015, 03:36 AM
Here are the target and up vector i use for the faces


{ GL_TEXTURE_CUBE_MAP_POSITIVE_X, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f,1.0f, 0.0f) },
{ GL_TEXTURE_CUBE_MAP_NEGATIVE_X, glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f) },
{ GL_TEXTURE_CUBE_MAP_POSITIVE_Y, glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f) },
{ GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f) },
{ GL_TEXTURE_CUBE_MAP_POSITIVE_Z, glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f) },
{ GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f) }

And i should also add that the light_position i use as uniform has the Z axis inverted, because it is specified in a system coordinate with the positive Z pointing towards