🎉 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!

Projection Offset Problem

Started by
13 comments, last by ReaperSMS 5 years, 1 month ago

I ham having problems understanding a projection and have attached an image to illustrate.  The point can be found by:      Zfar / (Zfar-Znear)  and subtracting (Zfar * Znear) / (Zfar-Znear)  

The subtracted fraction is what I don't understand.  It is an offset of the transformed point and is a discrepancy.  Apparently it is pretty easily understood, but I don't understand it.  Could somebody please explain?  I  have a feeling that I still won't understand it, so easy does it, please!  :)

Thank you,

Josheir

 

mathproblem.jpg

Advertisement

The intended result is to transform the coordinate such that the range [Near,Far] maps to [0,1], but after the perspective divide.

Ignoring the divide to start with, we start by translating by -Near, so that Near maps to 0.

Zout = Zin - Near

Now, in the given case, Z values at the near plane become 0, Z values at the far plane become 9. We rescale by 1/(Far-Near) to bring that to the range [0,1]

Zout = (Zin - Near) / (Far - Near)

To make this easier to calculate with a matrix, we want it in the form A * z + D, so we distribute and rearrange things

Zout = Zin * 1/(Far - Near) - Near / (Far - Near)

If it is an orthographic projection, we're done. If it is a perspective projection, we must take into account the divide by Zin that will happen.

Zclip = Zin * 1/(Far - Near) - Near / (Far - Near)

Zout = Zclip / Zin

For Zin = Near, Zclip is 0.0, and nothing would change, but for Zin = Far, we would get a result of:

Zclip = Zfar * 1/(Far - Near) - Near / (Far - Near) = 10 / 9 - 1 / 9 = 9 / 9 = 1

Zout = Zclip / Zin = Zclip / Far = 1 / 10

To get a Zout of 1, we have to scale things by Far, which will give the correct result of Zin = Near -> 0.0, Zin = Far -> 1.0. Distributing it across:

Zclip = Zin * Far / (Far - Near) - (Near * Far) / (Far - Near)

Zout = Zclip / Zin

I'm not sure my answer was 100% correct. <removed>

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

I'm just wondering in plain English what the subtraction of (Zfar * Znear) / (Zfar-Znear)   is about.  I think it can be explained simply!

Thanks,

Josheir

Its the top times Znear if that helps.

Josheir

7 hours ago, Josheir said:

I'm just wondering in plain English what the subtraction of (Zfar * Znear) / (Zfar-Znear)   is about.  I think it can be explained simply!

It adjusts the frustum so that the near plane sits on the z-axis at 0.

Be aware that the video isn't showing how to create an OpenGL projection matrix. It is close to a DirectX projection matrix, it might even be a proper DirectX projection matrix.

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

Okay, maybe I'm not making sense.  I'll explain the first half to demonstrate:

If a far plane is at ten and a near plane is at 1 than to work out where the point is we need to first scale it to a normalized system.  Zfar - Znear equals to nine so we divide our point by nine which gives us a point between zero and one.  But, we need to scale it back up again to ten, so this gives us :

zfar  /  (zfar-znear) 

Simple, plain English, a great skill to have with mathematical expertise; communication so others understand it,

Could you help me please?

Josheir

 

9 hours ago, fleabay said:

It adjusts the frustum so that the near plane sits on the z-axis at 0.

Be aware that the video isn't showing how to create an OpenGL projection matrix. It is close to a DirectX projection matrix, it might even be a proper DirectX projection matrix.

No, it's from a video that creates a 3d engine from scratch :  

Quite good,  I'm not understanding how a subtraction is doing anything.

Josheir 

P.S.  Description starts at : 21:00.

I'm working on ReaperSMS's solution too.

 

Taking the example case of Far = 10, Near = 1, just dividing by Far-Near would put points at the far plane at 10/9, and points at the near plane at 1/9. Subtracting Near / (Far-Near) changes that so that points on the far plane become 1, and points on the near plane become 0.

The scale by Far is to counteract the perspective divide

This topic is closed to new replies.

Advertisement