View Full Version : Fit orthogonal projection matrix around shadow casters (CSM)

Silverlan

10-08-2014, 10:17 PM

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.

I'd recommend reading Michael Valient's article in ShaderX6 (Stable Rendering of Cascaded Shadow Maps). This talks about a method to fit your light frustum around bspheres you fit around the view frustum splits to minimize shadow edge crawling/flickering artifacts.

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.

Silverlan

10-14-2014, 11:15 PM

I'd recommend reading Michael Valient's article in ShaderX6 (Stable Rendering of Cascaded Shadow Maps). This talks about a method to fit your light frustum around bspheres you fit around the view frustum splits to minimize shadow edge crawling/flickering artifacts.

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:

std::vector<glm::vec3> trapezoid(4);

for(unsigned int i=0;i<numCascades;i++)

{

Frustum &frustum = m_frustums[i]; // The cascade frustum

glm::vec3 &ftr = frustum.points[FRUSTUM_POINT::FAR_TOP_RIGHT];

glm::vec3 &fbl = frustum.points[FRUSTUM_POINT::FAR_BOTTOM_LEFT];

glm::vec3 &nbl = frustum.points[FRUSTUM_POINT::NEAR_BOTTOM_LEFT];

glm::vec3 &ntr = frustum.points[FRUSTUM_POINT::NEAR_TOP_RIGHT];

trapezoid[0] = ftr;

trapezoid[1] = fbl;

trapezoid[2] = nbl;

trapezoid[3] = ntr;

glm::vec3 center;

float radius;

Seb::Calculate(trapezoid,center,radius); // Generates the MEC from the frustum's trapezoid

glm::mat4 matView = Matrix::LookAt(center -dir,center,glm::vec3(0,1,0)); // 'dir' is the light direction; 'center' the center of the MEC (Parameters are the same as for glm::lookAt)

float diameter = radius *2.f;

m_frustums[i].projection = glm::ortho(0.f,diameter,diameter,0.f,0.f,1000.f); // width and height = diameter of the MEC, near and far plane are arbitrary values for now

m_frustums[i].viewProjection = m_frustums[i].projection *matView;

}

[...]

It doesn't work, but I don't know what's wrong. (Shadow casters aren't rendered to the shadow maps properly.)

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?

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.