I have a 1920x1200 shadowmap which just has the depth values of my scene drawn from the point of view of the light.

I also have a 1920x1200 gbuffer that draws my scene to a fullscreen quad.

How can I sample the shadowmap from the gbuffer lighting phase? What kind of maths needs to be performed? I have access to view space normal and view space position.

  • 2
    \$\begingroup\$ Why are you rendering a 1920x1200 shadow map? Your shadow map isn't in screen space; it's in light space. \$\endgroup\$ – Nicol Bolas Apr 13 '12 at 1:46
  • \$\begingroup\$ Dont know, its just for schoolwork I don't really care about performance. \$\endgroup\$ – Dave Apr 13 '12 at 21:36
  • \$\begingroup\$ It's not a question of performance so much as quality. \$\endgroup\$ – Nicol Bolas Apr 13 '12 at 21:52
  • \$\begingroup\$ What should I set it to for better quality? I assumed that the larger the better. \$\endgroup\$ – Dave Apr 13 '12 at 22:39
  • \$\begingroup\$ My point is that the shadow map's size doesn't need to be the same size as the screen. You can make it bigger, smaller, whatever you feel works best. \$\endgroup\$ – Nicol Bolas Apr 13 '12 at 22:41

Once you have the position of the shaded point, it's just the same as applying a shadow map in regular forward lighting. You need to convert the shaded point to shadow map screen space, which means getting its world position and then transforming it through the same view and projection matrices used to render the shadow map. You can bake all down into one 4x4 "view space to shadow" matrix that you pass in to your lighting shader.

Then once you have the point in shadow map space, do a texture lookup with comparison mode enabled, as usual for shadow maps.

  • \$\begingroup\$ Your the best! Helping newbies like that, You should be rewarded for your efforts. I managed to get it working, no matter what text I read it was unclear until I read your simple answer here. \$\endgroup\$ – Dave Apr 13 '12 at 21:35
  • \$\begingroup\$ Question: In the forward rendering setup, it was enough to do this transformation in the vertex shader and hand the pixel shader a set of interpolated texture lookup coordinates. It looks to me though like I have to do this transformation per-pixel in deferred rendering because the coordinates that I need are inside a texture that I can only access in the fragment shader. Is there a way around that? \$\endgroup\$ – TravisG Aug 16 '13 at 15:27
  • \$\begingroup\$ @TravisG Sort of. You can do most of the transformation in the vertex shader, and all the fragment shader has to do is convert the depth buffer to linear and do a mad to get the final position. Matt Pettineo has a blog post on the subject: Position From Depth. \$\endgroup\$ – Nathan Reed Aug 16 '13 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.