🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Reverse projection

Started by
6 comments, last by binder2 3 years, 11 months ago

I've recently been playing around with the Reverse Projection trick that I have read about on these forums. I'm a little confused though.

I plotted a graph of a normal projection matrix over a range of values from the near/far planes, and as expected, I got a graph that wasn't at all linear. Only values very close to the near plane had a range of say[0, 0.99], but 98% of the values were 0.99 or higher.

Having read about the Reverse Projection trick, and how it's supposed to give more of a linear distribution, I set up the same example with a Reverse Projection matrix, expecting to see more of a linear graph. So for example values half way between Near and Far to have a depth of around 0.5? .. but the graph looked exactly the same as without the Reverse Projection trick, just that it's upside down? so I don't understand how the Reverse Projection trick is actually working? how is it giving a better distribution if the graph is not more linear?

Or have I done something wrong, and the graph should look more linear?

Advertisement

I think what you eventually miss is the fact that floating precision is ‘better’ at smaller values.

So if you have an equal distribution of points, you likely want to distribute it to floating point accuracy in a way so at the near plane precision is ‘worse’ but still good enough to represent depth order, and precision is ‘better’ at the far plane so depth sorting still works there as well.
And this then leads to the ‘upside down’ distribution of projected depths you got which may be desired.
But not sure if i miss your point.

I may have missunderstood things, but let me try to explain what I was expecting.

With an orthographic projection, if you plot the depth buffer value against the (world space) distance from the near plane, you get a linear curve. As the distribution is linear, zfighting is just as likely no matter how far you are from the near plane.

With a perspective projection, if you plot the depth buffer value against the (world space) distance from the near plane, you get an exponential curve. As the distribution is not linear, z fighting is less likely close to the near plane, but more likley further away from the near plane.

Given that the Reverse Projection is supposed to give a more linear distribution, I was expecting it to plot a graph more like an an orthographic projection?

But it sounds like it is more to do with working better with the floating point format. So whilst you'll still get the exponential curve, the floating point error issues will be reduced?

binder2 said:
you get a linear curve. As the distribution is linear, zfighting is just as likely no matter how far you are from the near plane.

The distribution is linear in depth, but your entire assumption would hold only if GPUs would use integers to handle depth.
I'm not sure, but i think GCN always uses 32bit floats for depth. (even if you get something different if you use it as a resource to sample from, for example.)
So assuming all current GPUs use floating point under the hood, what matters is the distribution to floating point accuracy, and this accuracy is exponential - high at 0 and low at 1.

So when people say

binder2 said:
Reverse Projection is supposed to give a more linear distribution

… they most likely relate this to the technical details of floating point arithmetic, and not the numbers itself.

binder2 said:
But it sounds like it is more to do with working better with the floating point format. So whilst you'll still get the exponential curve, the floating point error issues will be reduced?

Yes. Unfortunately i can't answer in detail because… til yet i was too lazy trying to improve my projections, tbh : )

binder2 said:
I've recently been playing around with the Reverse Projection trick that I have read about on these forums.

Can you post links? I'm interested in this but have no idea what reverse projection is. (I don't think)

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

fleabay said:
Can you post links?

glm has a number of perspective matrix setups:

https://github.com/g-truc/glm/blob/c8440b09e24a239f774521db57ac3663ef497510/glm/ext/matrix_clip_space.inl

I just switched from using glm::perspective to glm::infinitePerspective, and much less Z fighting : )

Reverse is a bit more work: https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/

@JoeJ cheers

I followed the same OpenGL tutorial as Joe linked : https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/

This is also helpful : https://developer.nvidia.com/content/depth-precision-visualized

This topic is closed to new replies.

Advertisement