Perspective projection depth?

I implemented CSM, and relied on the linear projection of the orthographic matrix. I am trying to implement cube pointlights now (using a 4:2 2D texture) and I have realized perspective projection uses a non-linear depth.

Should I use a fragment shader to tweak the depth, or should I tweak the shadow z coordinate when objects are rendered? What is the equation to convert a linear depth from 0.0 to 1.0 into the depth perspective projection uses?

Really I just need to convert the linear depth value into a non-linear one like the projection matrix uses.

Let’s say the fragment is located 7.5 units away from the camera.

The light range / far plane is 10.0

The linear depth value is then 0.75.

The near plane is 0.1.

Now let’s plug those values into this equation:

( zNear * zFar / (zNear-zFar)) / (z-zFar/(zFar-zNear));

( 0.1 * 10.0 / (0.1-10.0)) / (7.5-10.0/(10.0-0.1))

=( 0.1 * 10.0 / (-9.9)) / (7.5 - 10.0/(9.9)) )

=( 0.1 * -1.010101 ) / ( 7.5 - 1.01010101 )

= -0.1010101 / 6.489898

= 0.015564

Does this seem right?

What is the relation with linear depth and CSM?
Yes, orthographics projection gives linear depth.
It’s just a matter at looking at how a vertex gets transformed into window space.

Not sure what you are calculating. It looks like you want to transform a vertex to clip space.

CSM uses orthographic projection, which is why I did not have a problem with this until point lights.

The far light range is 10.

The near light range is 0.1.

The fragment distance is 7.5.

The shadowmap I rendered uses perspective projection, because it is a point light.

I want to take the values above and calculate a non-linear depth value that can be used with shadow2D() in this depth map. I do not want to use a matrix and shadow2DProj() because there are too many places it could potentially go wrong.

I have the shadowmap x and y coords calculated correctly. I can draw the shadowmap texture on the scene and it lines up with geometry. I just need the shadow z coord.

Well, I figured it out. My equation was correct, but for some reason I was normalizing the fragment position! :smiley:

I wrote down some math on this subject a while ago:
http://www.humus.ca/temp/Linearize%20depth.txt

Thanks Humus, I used one of those equations in the SSAO shader I am working on.

I really like your demos, by the way.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.