\$10

### Image of the Day Submit

IOTD | Top Screenshots

## Shadow Mapping: Understanding Orthographic Projection

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Lambdacerro  Members

Posted 21 May 2013 - 04:38 PM

Hello,

Im currently in the process of implementing shadow mapping on my engine, im using a deferred renderer for now and works just fine, my main issue comes to implementing directional lights.

I've managed to setup a ortographic matrix and multiply it with a view matrix to obtain a camera who can render the scene and draw proper shadows, the problem arises in the fact that not the entire geometry is covered in the camera range.

The solution i've read is to create the camera based on the user-controlled camera frustum, however, i do not quite understand how the ortho projection works, mainly what each parameter means, right now im using a function who takes 4 parameters, this is the code (im using the irrlicht math library for my engine as i was used to it already).

	template <class T>
inline CMatrix4<T>& CMatrix4<T>::buildProjectionMatrixOrthoLH(
float widthOfViewVolume, float heightOfViewVolume, float zNear, float zFar)
{
AX_ASSERT_IF(widthOfViewVolume==0.f); //divide by zero
AX_ASSERT_IF(heightOfViewVolume==0.f); //divide by zero
AX_ASSERT_IF(zNear==zFar); //divide by zero
M[0] = (T)(2/widthOfViewVolume);
M[1] = 0;
M[2] = 0;
M[3] = 0;

M[4] = 0;
M[5] = (T)(2/heightOfViewVolume);
M[6] = 0;
M[7] = 0;

M[8] = 0;
M[9] = 0;
M[10] = (T)(1/(zFar-zNear));
M[11] = 0;

M[12] = 0;
M[13] = 0;
M[14] = (T)(zNear/(zNear-zFar));
M[15] = 1;

#if defined ( USE_MATRIX_TEST )
definitelyIdentityMatrix=false;
#endif
return *this;
}


I dont know what exactly each parameter means when it comes to projection, i'm also not quite sure if i need a view matrix to create the light camera, maybe someone can help me with this.

Thanks!

### #2TiagoCosta  Members

Posted 22 May 2013 - 02:06 PM

Take a point whose view space coordinates are (x, y, z).

If you create an orthographic projection matrix with the parameters buildProjectionMatrixOrthoLH(200, 100, 0, 50), the point will only be visible if its coordinates in view space are within the following ranges:
-100 <= x <= 100
-50 <= y <= 50
0 <= z <= 50

(I'm not sure if the comparisons should be < or <= but its isn't important).

Basically, the parameters are used to build a box centered in front of the camera, it the vertices (in view position) are only visible if they're inside the box.

And yes you need a light view matrix.

If some geometry is not casting shadows increase the width, height and zfar of the projection matrix.

Since directional lights don't have a position. You should create the light view matrix (used to created the shadow map) every frame based on the camera position:

//Calculate a position used in the view matrix for this frame
lightPosition = cameraPosition - (lightDirection*backupDist)
lightViewMatrix = createViewMatrix(lightPosition, lightDirection, ...)


This way geometry around the camera will always cast shadows.

EDIT:

There's an error in your math:
M[14] should be:
M[14] = (T)(-zNear/(zFar-zNear));

Edited by TiagoCosta, 22 May 2013 - 02:17 PM.

### #3Lambdacerro  Members

Posted 22 May 2013 - 04:11 PM

Take a point whose view space coordinates are (x, y, z).

If you create an orthographic projection matrix with the parameters buildProjectionMatrixOrthoLH(200, 100, 0, 50), the point will only be visible if its coordinates in view space are within the following ranges:
-100 <= x <= 100
-50 <= y <= 50
0 <= z <= 50

(I'm not sure if the comparisons should be < or <= but its isn't important).

Basically, the parameters are used to build a box centered in front of the camera, it the vertices (in view position) are only visible if they're inside the box.

And yes you need a light view matrix.

If some geometry is not casting shadows increase the width, height and zfar of the projection matrix.

Since directional lights don't have a position. You should create the light view matrix (used to created the shadow map) every frame based on the camera position:

//Calculate a position used in the view matrix for this frame
lightPosition = cameraPosition - (lightDirection*backupDist)
lightViewMatrix = createViewMatrix(lightPosition, lightDirection, ...)


This way geometry around the camera will always cast shadows.

EDIT:

There's an error in your math:
M[14] should be:
M[14] = (T)(-zNear/(zFar-zNear));

Thank you for the answer, this helped me to clear out the projection part, i guess i would have to construct the ortho projection every frame based on the user camera frustum so the ortho camera always "gets" the area visible by the player.

I still dont know how the view matrix on this case works, do i need to set te position of the camera in the "center" of the frustum bbox? what about the LookAt vector? Honestly i never got the idea of what exactly does the view matrix and how the LookAt vector works (its an absolute position? can it be a normalized direction vector?)

About the bug, well i didnt wrote the library as it was taken from the Irrlicht engine, i will report the bug to them

Thanks!

Edited by Lambdacerro, 22 May 2013 - 04:14 PM.

### #4Burnt_Fyr  Members

Posted 30 May 2013 - 03:06 PM

The lookat is exactly as it says...the position that a light/camera/object is looking at. It is used to create the forward vector of the objectss orthonormal basis. this, combined with an up vector, can give a compete rotation matrix that defines how the object is oriented. Coupled with the objects position position, you can give a transform that equates to that objects world matrix. The ortho projection should not change from frame to frame, only when the parameters of that projection change. This is the whole point of separate view and projection transfoms. The light "view matrix" is used to get what the light "sees" and the projection describes how it is to be projected onto the shadow map.

### #5Lambdacerro  Members

Posted 04 June 2013 - 05:13 PM

The lookat is exactly as it says...the position that a light/camera/object is looking at. It is used to create the forward vector of the objectss orthonormal basis. this, combined with an up vector, can give a compete rotation matrix that defines how the object is oriented. Coupled with the objects position position, you can give a transform that equates to that objects world matrix. The ortho projection should not change from frame to frame, only when the parameters of that projection change. This is the whole point of separate view and projection transfoms. The light "view matrix" is used to get what the light "sees" and the projection describes how it is to be projected onto the shadow map.

Yeah after some trial and error got it working, basically i provide the player position as the camera position and the player position + light direction as the lookat vector so the shadow maps follows the player, i have a bug related with the moment each thing is update making the shadow jitter a bit but is something i have to investigate.

Thanks anyway

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.