Matrix for shadow mapping

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Brainsaw
Posts: 1252
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Matrix for shadow mapping

Post by Brainsaw »

Hello,

I want to improve my own shader (see "Code Snippets" forum), but due to my lack of mathematical skills I don't make any progress at the moment.

I want to create an orthogonal matrix for my camera which starts at a given point (at the "near" value of the active camera) and ends at another specific point ("far" value of the camera), but I have no idea how I can calculate that.

My idea is to get the distance between those points from the perspective of the shadow camera, i.e. the width and height will vary. Does anyone know how to proceed there?

Thanks in advance
Image
Dustbin::Games on the web: https://www.dustbin-online.de/
CuteAlien
Admin
Posts: 10042
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Matrix for shadow mapping

Post by CuteAlien »

I'm probably missing something. But Irrlicht matrix has the function buildProjectionMatrixOrthoLH which allows you to set the near and far plane and width/height. The world-view transformation is the same as for other cameras.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Brainsaw
Posts: 1252
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Re: Matrix for shadow mapping

Post by Brainsaw »

What I am struggling with is to limit my shadow maps so that they start exactly at the near values of the player's camera's view frustum and don't go too far. In my test application I have added a scene node which shows the view frustum (from the light's perspective), and once I pitch the camera up or down the shadow covers more than necessary, i.e. it doesn't start with the active camera but behind it.

I tried a little this afternoon but didn't get anywhere, at the moment I think my shadows are ok the way they are
Image
Dustbin::Games on the web: https://www.dustbin-online.de/
CuteAlien
Admin
Posts: 10042
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Matrix for shadow mapping

Post by CuteAlien »

Ah yes - covering exactly how much you need is a bit tricky. But having a bit more won't hurt much.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
CuteAlien
Admin
Posts: 10042
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Matrix for shadow mapping

Post by CuteAlien »

Sorry, was a bit too tired yesterday to think about it. Also I'm using shadow maps, but haven't coded them myself, so take everything I say with a grain of salt.

In theory near/far wouldn't matter as light is infinite far away and you just want all shadows. In reality we have to struggle with the depth-buffer resolution and want it therefore as small as possible to avoid ugly blocks when looking it up later.

The far plane should be limited by the frustum corners of the render camera. I guess just check all 8 corners and take the furthest to find the far-value. And probably don't take distance as you want to check furthest plane cutting it - so it 's probably (normalized view vector of your shadowmap camera).dotProduct(cornerpoint).

The near plane depends a bit on your shadow camera position. In theory you'd do the same as for the far-plane, but for every vertex in your scene. Yeah, that's too slow. So... vs bounding-box of scene. Which can move the near-plane a good deal too close to your camera, but that's likely hard to avoid. You could also do checks vs the bounding-box of each object which may give better results. Also ensure your shadow camera is definitely outside the scene bounding-box itself.

For the width/height I guess you can do checks against the frustum corners again. But in this case you don't need the shadow-camera view vector but the right (or left) and up (or down) vectors. Don't trust the camera up-vector for this as it's not necessarily perpendicular to the view vector. But you can use it to calculate the right-vector (view.crossProduct(up)... or maybe it was up.crossProduct(view)... you either get left or right, I never remember which way). And then you can get the the real perpendicular up vector from doing the cross product with your view and the right vector. Then dot product those against your frustum corners and get furthest again. And too lazy too look up right now what inputs buildProjectionMatrixOrthoLH takes, either you can use those distance or you have to multiply by 2 as it takes in full width/height.

Well, I just made this all up while writing assuming this is approximately what you should take. But no guarantees it's correct. So just take it as inspiration.

Also your solution mentioned in other thread reminds me a bit about cascading shadow maps. So you may want to search around the web a bit about how those are implemented.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply