Sign in to follow this  

Problems with shadows in deferred renderer

This topic is 2571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I'm making a simple deferred (classic deferred) renderer in learning purpose. Most things work ok but I suddenly got stuck when trying to implement shadow mapping.


The idea is:
when rendering the lightvolume (e.g. a sphere for a point light) in order to calculate light contribution for that light I also read the shadow texture corresponding to that light. (that has been rendered just before the light pass).
I sample the shadow texture with a point sampled from the gbuffer that I transform with the shadow matrix.
Then I compare that sample points z value to the z-value of the sample position.

It should be simple enough but it just dont work for me so I have to ask here both if the general idea is right and if the actual code does what I intend.

The GBuffer:
This is a FBO with attachments for albedo, position etc. position is calculated in the attribute write pass like this:
vec3 vPosition = vec3(uModelViewMatrix * vec4(inPosition, 1.0));
vPosition is then directly written to the gbuffer in the frag shader.
so It is in eye (camera) space but without perspective transform.

The shadow map:
Mat4f shadowPerspective = Mat4f::Perspective(45.0f, 1.0, 0.1f, 500.0f);
the lightpos + lightdir is concatenated into lightViewMatrix
the shadow matrix is then:
Mat4f shadowMatrix = Mat4f::Multiply(shadowPerspective, lightViewMatrix);
By checking the rendered texture for the shadow I can tell that this works as expected. (I attached a color tex to see that the output looked like it should from the lights point of view)
By this I conclude that the shadowMatrix is correct.

In the light pass:
I first concat the inverse of the eye(camera) and the shadow matrix (I have verified that inverse works alright)
shadowMatrix = Mat4f::Multiply(inverseCameraMatrix, shadowMatrix);
this shadow matrix is then sent to the light shader (and called "uShadowMatrix" in the calculatins below)

The point light vertex shader:
I transform the light sphere to clip coord (camera space with perspective transform)
gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(inPosition, 1.0);

in the light frag shader:
First I extract the eye position from the gbuffer (this is eye space without perspective transform)
vec2 samplePoint = vec2(gl_FragCoord.x/uBufferSize.x, gl_FragCoord.y/uBufferSize.y);
vec3 position = texture2D(uPositionTex, samplePoint).rgb;
Now I want to transform this position into the shadow space
vec4 shadowspacePosition = uShadowMatrix * vec4(position, 1.0);
and remove the perspective transform from the shadow matrix
vec3 shadowcoord = shadowspacePosition.xyz / shadowspacePosition.w;
I then sample the shadow texture to get if it is in shadow or not:
vec3 shadowSample = texture2D(uShadowTex, shadowcoord.xy ).rgb;
if((shadowSample.z) < shadowcoord.z) shadow = 0.0;

But this doesn't work. Have been looking at my code for more then one day now but I just can't seems to get it right at this time. Is there something very obviously wrong with my thinking here?
I would very much appreciate your help!

Best regards
/Curler








Share this post


Link to post
Share on other sites

This topic is 2571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this