0
votes

I'm working on implementing directional light and an associated shadowmap.

I'm successfully rendering the shadowmap texture using an orthographic projection. But this is where I'm stuck; in the final render pass, how do I work out the correct texture coordinate from my shadow map to sample? And then, how do I determine whether this object is behind that depth?

What I've tried to do so far:

  1. Pass the lights View+Projection matrix to the vertex shader for my object
  2. Multiply the objects vertices in the Vertex Shader by this matrix to pass to the fragment shader
  3. This gives my fragment shader the location of the vertex when it was drawn to the shadowmap

So, with this I can sample the shadow map?

And this gives me the depth of whatever was drawn there.

But it doesn't tell me whether I'm behind or in front of this depth.

1

1 Answers

1
votes

After executing your second step, you get a 4-vector (x,y,z,w). Lets assume you performed perspective division on it, and w = 1. Then you have x,y,z coordinates of a vertex in light source's space, that you pass to your fragment shader.

x and y you use to sample the shadow map in the next step, but what about the z coordinate? Well, it is the would-be depth value of the fragment from the light source's PoV. It is this value you compare your shadow map sample to. If it is bigger than shadowmaps value(+bias), than your fragment is in the shadow. Otherwise it isn't.