Computing position for directional light for shadow mapping

Started by
2 comments, last by ApochPiQ 17 years, 7 months ago
Ok I am trying to compute the position for a directional light that I will then use to perform shadow mapping. However, I need to generate this position so that it is close to my view frustum, so that the shadows have good resolution. Here are the steps of what I am doing: [1] Get the view frustum corners in world space I get the 8 corners of the view frustum in world space. My frustum has a near Z value of 3 and a farZ value of 10000. I use a field of view of 60.0 degrees. I get the correct values, but as expected the view frustum covers a huge area. [2] Calculate the AABB of the 8 frustum corners I now calculate the Axis Aligned Bounding Box (AABB) of the frustum as well as the center coordinate of the frustum both of these are in world space. This AABB is rather large. [3] Calculate intersection of view frustum AABB and light direction From the center of the frustum I calculate the intersection point in the opposite direction of the light to the AABB of the frustum calculated earlier. This will be the "temporary" position of the light. The problem is since the frustum is so large and the AABB as a result is large, the light is far away and I am not getting a light position that is close to the shadow casters. Hence the shadows are very blurry. [4] Calculate the light view matrix I now have a position for the light and the look at direction is the center of the frustum. So I use this plus an up vector to generate the view matrix for the light source. [5] Transform the 8 view frustum corners into light view space. I now transform the 8 view frustum corners into the light view using the view matrix that was calculated earlier and generate an AABB for these transformed points that is relative to the view matrix of the light. [6] Generate the orthographic projection matrix for the light I generate the orthographic projection matrix using D3DXMatrixOrthoOffCenterLH in Direct3D. D3DXMATRIX * D3DXMatrixOrthoOffCenterLH( D3DXMATRIX * pOut, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf ); for l and r I use the min.x and max.x of the AABB generated in step 5 for b and t I use the min.y and max.y of the ABBB generated in step 5 for zn I use 0 but am wondering if I should use min.z for the AABB generated in step 5 for zf I use max.z - min.z of the AABB from step 5 but I am wondering if I should just use max.z. So my question is am I generating the projection matrix properly or picking the right parameters? My shadows still look very blurry and I feel that it is because the position of the light is very far away. Is there any way to fix this. I think the problem is that I have a very large frustum but don't really know how to get around that. Also maybe there is something I am missing in my approach. I would be greatful for any help anyone can give me or any thoughts and opinions. Thanks.
Advertisement
A directional light source is one that generates light that all arrives in parallel rays; for any given incoming ray of light, the direction (in world space) is identical to the direction of any other ray. Directional lights technically have no position, but you can also think of them as infinitely large planes infinitely far away from the scene.

What type of light source are you wanting to achieve? Are you truly after directional lighting, or are you looking for a point light/area light/emissive geometry/etc.? For a truly directional light, your shadow projection will be done using an orthographic projection matrix (see Google for examples/details). For points lights, you simply do a perspective projection using the light source position as the view point; this can also work for directed lights which emit from a single infinitely small point, but are constrained to certain directions, e.g. conical spotlights. Other lightsources are nontrivial, particularly emissive geometry, but can be approximated fairly well with a few techniques.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Well I am trying to have a single light in the world to render shadows from. At first I had a point light in the center of the world. But for objects that were far away from the light, the shadows were very blurry even though the camera was close to these objects. So now I am attempting to use a directional light with view and projection modifications to alleviate this problem
OK. In that case you need an orthographic projection matrix as I noted. Generally you can obtain a workable projection matrix from the usual library functions; all you need to do is ensure that the effective viewport size of the projection is large enough to cover the entire visible scene.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement