1
$\begingroup$

As far as my understanding goes, in projection matrices $x_\text{eye}$ and $y_\text{eye}$ are mapped linearly to NDC by first using proportions to find $x_n = \frac{n\cdot x_e}{-z_e}$ and $y_n = \frac{n\cdot y_e}{-z_e}$ and then remapping linearly $[l,r]\rightarrow [-1,1]$ horizontally and $[b,t]\rightarrow [-1,1]$ vertically, why isn't the same done for $z_\text{eye}$?

Now, I understand how the kind of mapping that is usually done turns out to be useful by providing better floating point precision around the near plane to improve z-fighting; what I don't understand if that is just a convenient consequence of how the third row of the projection matrix is derived or the other way around.

I would like a more thorough explanation of the why that is not just "that is how it is usually done" that I've seen online when researching the topic aka the "put A and B in $\begin{bmatrix}\frac{2n}{r-l}&0&\frac{r+l}{r-l}&0\\0&\frac{2n}{t-b}&\frac{t+b}{t-b}&0\\0&0&A&B\\0&0&-1&0\end{bmatrix}\cdot\begin{bmatrix}x_e\\y_e\\z_e\\w_e\end{bmatrix}$ and solve a linear system", I wish to understand the reasoning and the "methodology" behind this and if there are alternative mappings out there and what are their strengths and shortcomings

n.b. I'd actually try and test a linear mapping for z but I still haven't written the proper C code for that as I wanted to first have a basic grasp of the concept before starting

$\endgroup$

1 Answer 1

1
$\begingroup$

I think I have figured it out and I'm writing it out for myself and others that are struggling with this topic:

It all boils down to the fact that when projecting a generic vector $\vec{v}_e = (x_e,y_e,z_e,w_e=1)$ to the near plane to a point $\vec{v}_p$, both $x_p = \frac{n\cdot x_e}{-z_e}$ and $y_p = \frac{n\cdot y_e}{-z_e}$ are divided by $-z_e$ aka the so called perspective division that happens when going from clip space to NDC:

$$\vec{v}_\text{NDC} = \left(\begin{array}{c}x_\text{NDC}\\y_\text{NDC}\\z_\text{NDC}\end{array}\right) = \left(\begin{array}{c}\frac{x_c}{w_c}\\\frac{y_c}{w_c}\\\frac{z_c}{w_c}\end{array}\right)$$

Perspective division is handled by the last row of the matrix that sets $w_c = -z_e$ (w component of the same point in clip space coordinates).

Now, since perspective division is a mandatory step, when attempting to map $z_e$ from $[-n,-f]$ to $[-1,1]$ using a linear relationship like $z_c = A \cdot z_e + B$, the division by $-z_e$ turns that relationship into $z_\text{NDC} = \frac{z_c}{-z_e} = -A -\frac{B}{z_e}$ which will actually remove much needed z depth information if trying to map $z_e$ like it was done with $x_e$ and $y_e$; now we just need to find A and B with a system of linear equations with $(z_e, z_\text{NDC}) = (-n,-1)$ and $= (-f,1)$:

$$ \left\{\begin{array}{c}-A-\frac{B}{-n} = -1\\-A-\frac{B}{-f} = 1\end{array}\right. $$

Solving for A and B actually gives $A=-\frac{(n+f)}{n-f}$ and $B=\frac{2nf}{n-f}$ as coefficients of $z_e$ and $w_e=1$ to put in place of A and B in the matrix in my original question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.