Jump to content
  • Advertisement
Sign in to follow this  
Josheir

Projection Offset Problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Posted (edited)

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

Edited by ReaperSMS

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites
Posted (edited)
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.

Edited by Josheir

Share this post


Link to post
Share on other sites
Posted (edited)

I'm working on ReaperSMS's solution too.

 

Edited by Josheir

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!