Jump to content
  • Advertisement
Sign in to follow this  
KaiserJohan

OpenGL Directional light shadow mapping

This topic is 1322 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

I am doing cascaded shadowmapping and having some issues.

 

I have a scene like this (yellow light is from a point light; theres also a chair just outside the view to the left):

 

[attachment=26364:1.png]

 

If do not use a lights view matrix at all, i.e I only use an orthographic projection matrix when rendering shadow maps, it looks OK. The cascade order is top left, top right, bottom left, bottom right.

 

[attachment=26365:asd.png]

 

Now if I use an orthographic projection matrix and use a rotation matrix (as in the code below) based on the lights direction, it instead looks like this:

 

[attachment=26366:asd2.png]

 

Which is not correct, for example, the boxes are completely missing, even though they are encompassed in the orthographic projection

 

The resulting shadows then look like this:

 

[attachment=26369:asd3.png]

 

Some parts of shadow are correct, but major artifacts.

 

Here's how I build the matrix:

Mat4 CreateDirLightVPMatrix(const CameraFrustrum& cameraFrustrum, const Vec3& lightDir /* == Vec3(-1.0f, -1.0f, -1.0f) in this example */)
{
	// "cameraFrustrum" contains the 8 corners of the cameras frustrum in world space
	float maxZ = cameraFrustrum[0].z, minZ = cameraFrustrum[0].z;
	float maxX = cameraFrustrum[0].x, minX = cameraFrustrum[0].x;
	float maxY = cameraFrustrum[0].y, minY = cameraFrustrum[0].y;
	for (uint32_t i = 1; i < 8; i++)
	{
		if (cameraFrustrum[i].z > maxZ) maxZ = cameraFrustrum[i].z;
		if (cameraFrustrum[i].z < minZ) minZ = cameraFrustrum[i].z;
		if (cameraFrustrum[i].x > maxX) maxX = cameraFrustrum[i].x;
		if (cameraFrustrum[i].x < minX) minX = cameraFrustrum[i].x;
		if (cameraFrustrum[i].y > maxY) maxY = cameraFrustrum[i].y;
		if (cameraFrustrum[i].y < minY) minY = cameraFrustrum[i].y;
	}

	Vec3 right = glm::normalize(glm::cross(glm::normalize(lightDir), Vec3(0.0f, 1.0f, 0.0f)));
	Vec3 up = glm::normalize(glm::cross(glm::normalize(lightDir), right));

	Mat4 lightViewMatrix = Mat4(Vec4(right, 0.0f),
								Vec4(-up, 0.0f),		// why do I need to negate this btw?
								Vec4(lightDir, 0.0f),
								Vec4(0.0f, 0.0f, 0.0f, 1.0f));

	return OrthographicMatrix(minX, maxX, maxY, minY, maxZ, minZ) * lightViewMatrix;
}

It was my understanding (based on topics like https://www.opengl.org/discussion_boards/showthread.php/155674-Shadow-maps-for-infinite-light-sources), that all I needed to do shadow mapping for directional light was an orthographic projection matrix and then a rotation matrix (with no translation component, since the dir light has no position, and as I've understod it, translation dosnt matter since it is orthographic meaning no perspective anyway).

 

Then what is causing the errors in the 3rd image? Is there something I am missing?

Edited by KaiserJohan

Share this post


Link to post
Share on other sites
Advertisement

Which is not correct, for example, the boxes are completely missing, even though they are encompassed in the orthographic projection

So how are you culling them?


L. Spiro

Share this post


Link to post
Share on other sites

If you want your camera to move past it's original position, then you'll need a light matrix to do a transform. The directional light has no position, but your orthographic projection still needs to know where it's pointing at and what it's bounds are.

Share this post


Link to post
Share on other sites

 

Which is not correct, for example, the boxes are completely missing, even though they are encompassed in the orthographic projection

So how are you culling them?


L. Spiro

 

 

Not sure I follow; they are in the bounds of the orthographic matrix (as seen in the 2nd image) so why are they not visible after I apply the rotation matrix?

 

If you want your camera to move past it's original position, then you'll need a light matrix to do a transform. 

 

What has this got to do with the (scene) camera? Or do you mean the camera as in the lights view? If so, my understanding of orthographic projection was that I didn't need to translate the camera at all

 

 

 

The directional light has no position, but your orthographic projection still needs to know where it's pointing at and what it's bounds are.

 

"Where its pointing at" - defined by the rotation matrix? "What its bounds are" - defined by the orthographic projection matrix, built from the max/min of the camera frustrum corners? Is there anything I'm missing?

Share this post


Link to post
Share on other sites

so why are they not visible after I apply the rotation matrix?

I guess that was my point.
Why isn’t it visible? How are you culling it? How are you drawing it?
Are you sure it is in view? Because if so, and you actually sent valid commands to draw it, it would be there with the rest of the objects.


L. Spiro

Share this post


Link to post
Share on other sites

 

so why are they not visible after I apply the rotation matrix?

I guess that was my point.
Why isn’t it visible? How are you culling it? How are you drawing it?
Are you sure it is in view? Because if so, and you actually sent valid commands to draw it, it would be there with the rest of the objects.


L. Spiro

 

 

I'm positive the problem is related to the projection and/or lights view matrix. Is there anything I'm missing when building the lights projection/view matrix? It is true the view matrix needs no translation at all and just the rotation as in my code above?

Edited by KaiserJohan

Share this post


Link to post
Share on other sites


It is true the view matrix needs no translation at all and just the rotation as in my code above?

 

False. The projection determines the size and shape of a frustum "box." That box has no direct relationship to world coordinates. The view matrix determines the position and orientation of objects that will (eventually) be included/excluded from the frustum volume. Thinking in sort a backwards sense, the view matrix "positions" the frustum volume in the world, and objects which are not in the volume don't get rendered.

Share this post


Link to post
Share on other sites

 


It is true the view matrix needs no translation at all and just the rotation as in my code above?

 

False. The projection determines the size and shape of a frustum "box." That box has no direct relationship to world coordinates. The view matrix determines the position and orientation of objects that will (eventually) be included/excluded from the frustum volume. Thinking in sort a backwards sense, the view matrix "positions" the frustum volume in the world, and objects which are not in the volume don't get rendered.

 

 

That makes sense!

So, if I already have the world position of the box, how do I build a view matrix that dosn't change the position, only the orientation of the objects?

I.e, I have the world space corners of the camera frustrum, that will build the ortho box. I want the objects in it, but viewed from a certain direction, how do I build such a matrix? 

Share this post


Link to post
Share on other sites

MJP has a good sample on how to do that here: https://mynameismjp.wordpress.com/2009/02/17/deferred-cascaded-shadow-maps/.

Essentially you make an AABB around the game camera, find the center of the box, project a point from that position in the opposite direction of your light's direction scaled by some factor. The scale factor determines how far your light pos will be from the camera, the closer you are the better resolution you'll get in your shadow map, but you may cull valid occluders since they are behind the camera or such. There is a lot of fiddling with that value to get it right, in my experience.

Share this post


Link to post
Share on other sites

First (minor) thing is you should normalize your lightDir as well. And your lightView matrix is actually a lookAt matrix.

 

I've implemented a 1-cascade shadow map, the solution is very similar. This is based on an nvidia paper and other resources which can be found in the internet. smile.png

 

So the first step, I calculate a view matrix like you do. This is actually the same as your code (just normalize your direction! smile.png)

view.lookAt(Vector3::Zero, lightDir, Vector3::Up);

After I calculate the bounds of the view frustum points "looked from the direction of the light"

Vector3 min = Vector3::Max;
Vector3 max = Vector3::Min;

for (int i = 0; i < BoundingFrustum::NumCorners; i++)
{
    transformed = Vector3::transformCoord(corners[i], view);

    min = Vector3::getMin(transformed, min);
    max = Vector3::getMax(transformed, max);
}

Then my projection matrix is a simple ortho:

proj.orthographicOffCenter(-1, 1, -1, 1, min.z, max.z);

But I'm using another matrix called cropMat which will "position and clip".

const float32 scaleX = 2.0f / (max.x - min.x);
const float32 scaleY = 2.0f / (max.y - min.y);
const float32 offsetX = -0.5f * (min.x + max.x) * scaleX;
const float32 offsetY = -0.5f * (min.y + max.y) * scaleY;

cropMat.m00 = scaleX;
cropMat.m11 = scaleY;
cropMat.m22 = 1.0f;
cropMat.m30 = offsetX;
cropMat.m31 = offsetY;
cropMat.m33 = 1.0f;

The final viewProj matrix is calculated by multiplying the view, the projection and the crop matrix. NOTE that I'm using the DX-based matrices, care with the matrix row/column order. smile.png

 

As a side note:

You can use a sphere instead of a box. With a sphere you loose some precision but it also disables the flickering when the camera rotates:

Vector3 center;
for (int i = 0; i < BoundingFrustum::NumCorners; i++)
    center += corners[i];
center /= BoundingFrustum::NumCorners;
center = Vector3::transformCoord(center, view);

const float32 radius = Vector3::distance(corners[BoundingFrustum::FLB], corners[BoundingFrustum::NRT]);

min = center - Vector3(radius);
max = center + Vector3(radius);

You can also apply a rounding matrix which disables flickering when camera moves, something like this:

// round to textel
Vector3 origin = Vector3::transformCoord(Vector3::Zero, viewProj);
origin *= halfSize;

Vector3 rounding;
rounding.x = Math::round(origin.x) - origin.x;
rounding.y = Math::round(origin.y) - origin.y;
rounding /= halfSize;

roundMat.translate(rounding);
viewProj *= roundMat;

 

Edit:
This is an old code, can contains bugs. :)

Edited by csisy

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!