Skip to main content
GameDev.net gamedev.net
🔒 Locked

Projection Offset Problem

Started by Josheir May 9, 2019 at 6:08 PM 13 replies 6.3k views
Original Post
Josheir
Josheir

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

ReaperSMS
ReaperSMS

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

fleabay
fleabay

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

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

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

Josheir
Josheir

Its the top times Znear if that helps.

Josheir

fleabay
fleabay
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.
Josheir
Josheir

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


Josheir
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.

Josheir
Josheir

I'm working on ReaperSMS's solution too.


ReaperSMS
ReaperSMS

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

fleabay
fleabay
1 hour ago, Josheir said:

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

That doens't change the fact that it is very similar, if not exact, to a DirectX projection matrix.

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


1 minute ago, fleabay said:

That doens't change the fact that it is very similar, if not exact, to a DirectX projection matrix.

I misunderstood the point. Something else I may research than.

Josheir
Josheir

I understand now, be happy! :)

On ‎5‎/‎9‎/‎2019 at 4:37 PM, ReaperSMS said:

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.

Why's that, why does orthographic projection have no division and perspective projection does have division?

Josheir

ReaperSMS
ReaperSMS

Because that is what makes a perspective projection have perspective. The division is what makes things shrink as they move further from the camera.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.