Jump to content
  • Advertisement
Sign in to follow this  
mokaschitta

OpenGL Fitting Camera frustum into Light Frustum for simple directional shadow mapping

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

Hi,

I am currently implementing some simple shadow mapping for a dynamic scene (basically just the camera is moving) for the first time ever, and I want the light frustum to be just big enough to fit in the cameras frustum (maybe slightly bigger to contain all occluders, nothing fancy like CSM, just one simple depth map). The cameras projection matrix is just simple orthographic projection, slightly rotated to get a fake isometric look.

I use openGL, and I compute the cameras box frustum(since its orthogonal) in world space. Now, I guess that I would setup my light matrix looking in the lights direction at the camera frustums center, and setup an orthogonal projection for it beeing just big enough. Now my questions are the following:

1. How do I choose the position of lights camera to render the depth map. Should I just go from the center of the cameras frustum in the lights direction for a bit?

2. How do I fit the cameras frustum in the lights frustum? As I said I have the AABB of the cameras frustum in world space, do I somehow need to bring it to the lights camera space, or is it good enough in world space? If it's good enough in world space, would I simply do something similar to this:


//Pseudocode
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-camAABB.getWidth()*0.5, camAABB.getWidth()*0.5, -camAABB.getHeight()*0.5, camAABB.getHeight()*0.5, ..., ...);
//for near and far I would propably have to find the closest side of the cameras frustum and use it as near, and the furthest as far?

//not really sure about this
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camAABB.getCenter().x-lightDir.x*someValue, camAABB.getCenter().y-lightDir.y*someValue, camAABB.getCenter().z-lightDir.z*someValue,
camAABB.getCenter().x, camAABB.getCenter().y, camAABB.getCenter().x, 0, 1, 0);


As you can see I am kind of sure about what to do(at least to a certain degree) but I don't really know how to get there. I would be glad if anybody could help me out! Thanks a ton!

Share this post


Link to post
Share on other sites
Advertisement
Okay, I am pretty sure that I have to get the 8 corners of my camera frustum to lights view space. How would I do that though if I don't really have any position for my light yet(only the direction)?

[Edited by - mokaschitta on August 24, 2010 1:28:26 AM]

Share this post


Link to post
Share on other sites
Okay, here is what I do now, which does not seem to work (at least partly)

I simply choose a point to look from by substracting the light direction with a certain amount from my cameras frustum center in world space and look at it from there.

next I try to convert the camera frustum corners to the lights view space by multiplying them with the inverse of the lights modelview matrix, but I think thats wrong, at least the min/max results I get from it seem to be rather weird, do I maybe have to transform the frustum corners from their local space to lights view space instead?

If I had the correct min/max values of the cameras frustum in light view space I guess I would do something like this to setup the proper projection:


glOrtho(minX, maxX, minY, maxY, minZ, maxZ);

Share this post


Link to post
Share on other sites
You should checkout PSSM (OpenGL implementation): http://appsrv.cse.cuhk.edu.hk/~fzhang/pssm_vrcia/

I personally did the DirectX implementation (I am not terribly familiar with Open GL). I'm aware that you are only interested in 1 shadow map however, I believe you will still need to do the same core work such as build a tight fitting bounding box to contain the scene.

1. As you pointed out, your matrix is ortho, so only the direction should play a factor. The position of the sunlight (eye) is basially zero. Your target can basically be the forward vector of your sunlight matrix.

2. Basically you would need to build an AABB that contains everything *both* camera & light both see since the sunlight sees elements the camera cannot see however they cast shadow into your scene. This is the only part where you compute an actual sun world position only to build a frustum for it. You can use both frustums (light + camera) to figure out all the elements they touch. It may suffice to simply back the sunlight behind the camera a few units to grab all the elements that can possibly cast shadow into the viewable area. There is probably some really intelligent algorithm out there. I personally just picked a nice dist for my sun since my scene is fairly simple atm.

Code: For building the ortho matrix for my Sun, I used the DirectX function D3DXMatrixOrthoOffCenterLH which takes the projection matrix and dimensions of my viewbox as arguments

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!