Adam Gorski
Adam Gorski

Reputation: 69

Interpolating Z depth in a interpolated perspective orthographic viewing frustum

To interpolate between perspective and orthographic projection I'm using the following formula (which works great because the clipping planes align perfectly):

 X' = rw + X / ((Z * fwi - ox) * (1f - a) + ox);
 Y' = rh + Y / ((Z * fhi - oy) * (1f - a) + oy);

Where:
rw or rh is (renderWidth or Height - 1) / 2f,
ox is vSize / rw, oy is hSize / rh,
fwi is tan(vFOV/2) / rw, fhi is tan(hFOV/2) / rh,
a is lerp coefficient; 0 = perspective, 1 = orthographic, 0.5 = in between.

Now to correctly calculate the Z depth in for each fragment, I'm calculating z = 1/z before interpolating it in screen space via a ScanLine function, and finally calculating the true depth at the end via 1/z.

However orthographic projection does not require this depth correction, so I was wondering whats the correct way to determine the true Z, when the projection is being blended. The current XYZ to XY formula can be summarized via:

formula

Currently I'm interpolating UV coords via the Depth and it is clearly incorrect when the projection is in between perspective and orthographic. When it reaches 1, it bypasses the 1/z depth correction.

image
Please note that this is a software renderer, so performance is quite important. Ideally I would like to be doing the same thing OpenGL is doing in these types of situations.

Upvotes: 3

Views: 724

Answers (1)

Adam Gorski
Adam Gorski

Reputation: 69

Although the method I used to interpolate probably won't be used by anyone, the math behind it can be.

My clipping plane formula can be described as:

Formula

Where o = Base Size, a = Base FOV, l = 0 to 1
It is based on the y axis to allow FOV = 0 clipping.

When setting the FOV really small and setting the Z very far back, a orthographic projection can be faked thanks to the tiny 1/z tangent. However I couldn't use a dolly zoom formula as I had already implemented a custom XYZ to XY system.

So all I had to do was offset the Z value by some number.
Looking at dolly zoom clipping plane graphs, it became obvious that the Z offset had to be the Y intercept.

Graph
With dolly zoom the vertex Z is offset rather than the actual clipping slope but my XYZ to XY method required to offset the clipping slopes.

Reversing the formula to solve for 0 gives (*-1 for y intercept):

Formula

And then finally the Z correction can be calculated:

Formula

And with the Z offset added the depth is being now correctly interpolated:

Demo

Upvotes: 1

Related Questions