<p>I think I have figured it out and I'm writing it out for myself and others that are struggling with this topic:</p>
<p>It all boils down to the fact that when projecting a generic vector <span class="math-container">$\vec{v}_e = (x_e,y_e,z_e,w_e=1)$</span> to the near plane to a point <span class="math-container">$\vec{v}_p$</span>, both <span class="math-container">$x_p = \frac{n\cdot x_e}{-z_e}$</span> and <span class="math-container">$y_p = \frac{n\cdot y_e}{-z_e}$</span> are divided by <span class="math-container">$-z_e$</span> aka the so called <em>perspective division</em> that happens when going from clip space to NDC:</p>
<p><span class="math-container">$$\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)$$</span></p>
<p>Perspective division is handled by the last row of the matrix that sets <span class="math-container">$w_c = -z_e$</span> (w component of the same point in clip space coordinates).</p>
<p>Now, since perspective division is a mandatory step, when attempting to map <span class="math-container">$z_e$</span> from <span class="math-container">$[-n,-f]$</span> to <span class="math-container">$[-1,1]$</span> using a linear relationship like <span class="math-container">$z_c = A \cdot z_e + B$</span>, the division by <span class="math-container">$-z_e$</span> turns that relationship into <span class="math-container">$z_\text{NDC} = \frac{z_c}{-z_e} = -A -\frac{B}{z_e}$</span> which will actually remove much needed z depth information if trying to map <span class="math-container">$z_e$</span> like it was done with <span class="math-container">$x_e$</span> and <span class="math-container">$y_e$</span>; now we just need to find A and B with a system of linear equations with <span class="math-container">$(z_e, z_\text{NDC}) = (-n,-1)$</span> and <span class="math-container">$= (-f,1)$</span>:</p>
<p><span class="math-container">$$
\left\{\begin{array}{c}-A-\frac{B}{-n} = -1\\-A-\frac{B}{-f} = 1\end{array}\right.
$$</span></p>
<p>Solving for A and B actually gives <span class="math-container">$A=-\frac{(n+f)}{n-f}$</span> and <span class="math-container">$B=\frac{2nf}{n-f}$</span> as coefficients of <span class="math-container">$z_e$</span> and <span class="math-container">$w_e=1$</span> to put in place of A and B in the matrix in my original question.</p>

CC BY-SA 4.0
2024-05-05T18:30:35.233Z
2024-05-05T18:30:35.233Z
blvck_mvgic_dot_exe
<p>As far as my understanding goes, in projection matrices <span class="math-container">$x_\text{eye}$</span> and <span class="math-container">$y_\text{eye}$</span> are mapped linearly to NDC by first using proportions to find <span class="math-container">$x_n = \frac{n\cdot x_e}{-z_e}$</span> and <span class="math-container">$y_n = \frac{n\cdot y_e}{-z_e}$</span> and then remapping linearly <span class="math-container">$[l,r]\rightarrow [-1,1]$</span> horizontally and <span class="math-container">$[b,t]\rightarrow [-1,1]$</span> vertically, why isn't the same done for <span class="math-container">$z_\text{eye}$</span>?</p>
<p>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.</p>
<p>I would like a more thorough explanation of the <em>why</em> that is <em>not</em> just &quot;that is how it is usually done&quot; that I've seen online when researching the topic aka the &quot;put A and B in <span class="math-container">$\begin{bmatrix}\frac{2n}{r-l}&amp;0&amp;\frac{r+l}{r-l}&amp;0\\0&amp;\frac{2n}{t-b}&amp;\frac{t+b}{t-b}&amp;0\\0&amp;0&amp;A&amp;B\\0&amp;0&amp;-1&amp;0\end{bmatrix}\cdot\begin{bmatrix}x_e\\y_e\\z_e\\w_e\end{bmatrix}$</span> and solve a linear system&quot;, I wish to understand the reasoning and the &quot;methodology&quot; behind this and if there are alternative mappings out there and what are their strengths and shortcomings</p>
<p>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</p>

<opengl><projections><perspective>
In perspective projection matrices, why isn't $z_\text{eye}$ mapped linearly to NDC?
blvck_mvgic_dot_exe
# In perspective projection matrices, why isn't $z_\text{eye}$ mapped linearly to NDC?

(Asked by: username_0 on 2024-05-05)

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

**Tags:** opengl, projections, perspective


---

## 1 answers

### Answer by username_0 on 2024-05-05. Score: 1

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.


---

