I have already integrated an algorithm to find all possible shadow casters for a directional light, in and around the camera frustum. I then calculate the area that creates the tightest fit around the casters and create the orthogonal projection matrix from that.

This works, as long as the light is pointing straight up or down. If it's pointing in any other direction, the shadows are stretched, and the objects end up being rendered outside of the cascade texture maps. This video should make clear what I mean:

http://youtu.be/ihtMfS2V3jk

At the top you can see the cascades. The only shadow caster in this scene is the tree. Ignore the self-shadowing, that's a problem for another time.

At around 10 seconds in I start changing the direction of the light. You can immediately tell by the cascade maps, that the object is being cut off.

My question is: How to I adjust my projection matrix to fit the entire shadows, if the light is pointing at an angle?
I'm assuming I need to transform either the orthographic matrix, or the points it is created from, with the view matrix, I'm just unsure how.

Dark Photon

10-09-2014, 05:54 AM

The light source direction determines the MODELING matrix for your light source. On top of that you need to determine what your ortho light frustum is going to be (i.e. left/right/bottom/top/near/far clip planes) -- i.e. what your projection transformation is. There are a number of schools of thought on the "best" way to do this. Typically the best method involves one in which you end up with as little "shadow texel crawling" as possible, which either means 1) you need to have a more shadow resolution than you need in all cases (difficult to guarentee), or 2) you work to ensure that the coverage of shadow texels in your scene appears not to change, at least for shadows cast by static objects.

It looks like from your example that you may be trying to tight-fit your light/shadow frustum to the casters which may cast into your scene. You can definitely do this, but consider what happens to shadow quality when a new shadow caster moves into/out of your light frustum. Is this a problem for you? If not, then it looks like you just need to work on ensuring that your light-space LRBTNF clip planes (i.e. your light-space "box") encompass the entire bounds of your casters in light space.

Also consider what happens when casting shadows when you depth-test against a point which is out of the light frustum (i.e. outside the rectangular parallelipiped of space over which the casters are sampled into the depth map). For receivers which lie beyond the bounds of the shadow frustum far plane, you may find that you want clamp_to_edge behavior.

There's a newer book, ShaderX7. Do you know if I can assume it still has the articles from the previous books, or does it have to be ShaderX6?

Dark Photon

10-15-2014, 06:22 AM

No it shouldn't. ShaderX*/GPUPro* series has different articles/chapters related to graphics in each book.

Silverlan

11-20-2014, 11:20 AM

I've bought the book and tried to implement their algorithm, but I'm still having a bit of a struggle.

The article says you have to find the minimum enclosing circle/sphere(MEC) of the cascade frustum, and generate the view and projection matrix from the center of the MEC and the light direction:

[...]Now we can generate the initial look-at shadow map matrix - MShadowLookAt - and (orthogonal) projection matrix - MShadowOrtho - for each cascade. The look-at matrix uses the original light vector as the look-at direction and the center of the MEC as the target point. We keep the third ("up") vector constant; for example, one may use the vector exported by the content creation pipeline. The orthogonal projection matrix uses the diameter of the MEC as its volume width and height as illustrated in Figure 4.1.4. The minimum and maximum depths of the projection volume depend on the scene data, so both values can be computed per frame using the objects' bounding volumes in the shadow matrix frustum.[...]

My code for generating the view-projection matrix now looks like this:

If not, then it looks like you just need to work on ensuring that your light-space LRBTNF clip planes (i.e. your light-space "box") encompass the entire bounds of your casters in light space.
How would I actually do that? I thought I could just transform the min/max bounds around my shadow casters by the view matrix of the cascade, but I suppose it's not that simple?