Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / 3D Theory & Graphics / Depth in perspective shadowmap Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

March 09, 2005, 08:53 AM


Currently I'm working on shadows and I have one problem with perspective shadowmaps. I'm rendering my scene from light position to special depth texture. I would like to have linear distance from light to all objects (pixels). In vertex shader I'm transforming my vertex position to world space and then I'm sending .z and .w components to pixel shader. In pixel shader I'm computing distance by dividing .z/.w. And here is my problem. For orthogonal projection .w is always 1, so my distance is linear. But not for perspective projection. I was trying to compute distance in that way: dist=(.z/.w)*(.z/.w), but it's still incorrect. And here is my question - what I have to do to calculate linear distance in perspective projection?

Thanks, regards
Tomasz Przechodzki


March 09, 2005, 03:31 PM

You apply the same transformation to the vertex coordinates that you used when rendering the perspective shadow map. That is, you multiply vertex locations by the same modelview-projection matrix from the shadow map rendering pass, and then you can do the homogenous divide in the pixel shader.

Remember, you don't actually need a linear distance. You only need the distances in the shadow map to be the same coordinate system as the ones output by the vertex shader, so you can do the depth comparison.


March 10, 2005, 04:23 AM

Yes, that's true - I don't need a linear distance. But when I'm doing my depth comparison then it only works fine for objects which are quite close to near clip plane of my light's frustum, because my depth buffer is loosing precision very fast (I'm using D3DFMT_R32F texture). For example: in my application I'm casting shadow from an object with distance about 50 from the light and to create correct shadow I have to move near clip plane to about 20. So it works fine, but only for objects which are not far than about 30 from near clip plane.


March 10, 2005, 01:18 PM

There are a couple tricks you can try to rectify the precision problem. One is to use a small negative polygon offset when rendering the depth map, so that surfaces do not incorrectly self-shadow. The other trick, which is probably more robust, is to render only back faces to the depth map. Maybe this will help resolve your precision problems.

This thread contains 4 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.