<p>Note: I'll be using column vector notation (eg. <span class="math-container">$Ax=y$</span>), thus if you are using row notation, transpose the matrix and multiply from the left instead of the right. (eg. <span class="math-container">$y=x^TA^T$</span>)</p>

<p>A perspective transformation matrix transforms a view frustum into a rectangular view volume. Note that the resulting view volume might <strong>not</strong> be a cube and might <strong>not</strong> be centered at 0.</p>

<p>One <em>example</em> of a perspective transformation matrix. There are <strong>many more</strong>, and yours is included.</p>

<p>Let <span class="math-container">$l=left,\\ r = right,\\ b = bottom,\\ t = top,\\ n = near,\\ f = far$</span></p>

<p><span class="math-container">$M_{persp}=\begin{bmatrix}
1&amp; &amp; &amp; \\
 &amp;1&amp; &amp; \\
 &amp; &amp;\frac{n+f}{n}&amp;-f\\
 &amp; &amp;\frac{1}{n}&amp; 
\end{bmatrix}$</span></p>

<p>Since the resulting view volume is not the canonical view volume, we need to apply another matrix.</p>

<p>A orthographic transformation matrix transforms a rectangular view volume into a cubic view volume with two corners at (-1, -1, -1) and (1, 1, 1), which happens to be the canonical view volume.</p>

<p>Intuitively it will need one translation and one scaling.</p>

<p><span class="math-container">$\text{T}_{ortho} = S\left(\frac{2}{r-l},\frac{2}{t-b},\frac{2}{n-f}\right)T\left(\frac{-(l+r)}{2},\frac{-(b+t)}{2}\frac{-(n+f)}{2}\right)$</span></p>

<p><span class="math-container">$M_{ortho} = 
\begin{bmatrix}
    \frac{2}{r-l}&amp; &amp; &amp; \\
    &amp; \frac{2}{t-b} &amp; &amp; \\
    &amp; &amp; \frac{2}{n-f} &amp; \\
    &amp; &amp; &amp; 1
\end{bmatrix}
\begin{bmatrix}
    1 &amp; &amp; &amp; \frac{-(l+r)}{2} \\
    &amp; 1 &amp; &amp; \frac{-(b+t)}{2} \\
    &amp; &amp; 1 &amp; \frac{-(n+f)}{2} \\
    &amp; &amp; &amp; 1
\end{bmatrix}
$</span></p>

<p><span class="math-container">$M_{ortho} = 
\begin{bmatrix}
    \frac{2}{r-l}&amp; &amp; &amp; -\frac{l+r}{r-l} \\
    &amp; \frac{2}{t-b} &amp; &amp; -\frac{t+b}{t-b} \\
    &amp; &amp; \frac{2}{n-f} &amp; -\frac{n+f}{n-f}\\
    &amp; &amp; &amp; 1
\end{bmatrix}
$</span></p>

<p>Now you can combine them together to form a <strong>perspective projection</strong> matrix.</p>

<p><span class="math-container">$M_{proj} = M_{ortho}M_{persp}$</span></p>

<p><span class="math-container">$M_{proj} = 
\begin{bmatrix}
    \frac{2}{r-l}&amp; &amp; &amp; -\frac{l+r}{r-l} \\
    &amp; \frac{2}{t-b} &amp; &amp; -\frac{t+b}{t-b} \\
    &amp; &amp; \frac{2}{n-f} &amp; -\frac{n+f}{n-f}\\
    &amp; &amp; &amp; 1
\end{bmatrix}
\begin{bmatrix}
1&amp; &amp; &amp; \\
 &amp;1&amp; &amp; \\
 &amp; &amp;\frac{n+f}{n}&amp;-f\\
 &amp; &amp;\frac{1}{n}&amp; 
\end{bmatrix}$</span></p>

<p><span class="math-container">$M_{proj} = 
\begin{bmatrix}
    \frac{2n}{r-l}&amp; &amp; \frac{l+r}{l-r} &amp; \\
    &amp; \frac{2n}{t-b} &amp; \frac{b+t}{b-t} &amp; \\
    &amp; &amp; \frac{n+f}{n-f} &amp; \frac{2fn}{f-n}\\
    &amp; &amp; 1 &amp;
\end{bmatrix}$</span></p>

<p>If you have a free-moving camera you will need a camera matrix too, thus the final projection matrix centered on the camera is.</p>

<p><span class="math-container">$M_{proj} = M_{ortho}M_{persp}M_{camera}$</span></p>

CC BY-SA 4.0
2018-11-04T21:26:56.703Z
Thanks! But what does my matrix do, then? Doesn't it transform from a view space (with a perspective camera at the origin looking into -z) to a "clip space" (rectangular view volume) where far points have larger z values?
Supernormal
CC BY-SA 4.0
2018-11-04T21:33:52.040Z
If your far plane is behind the near plane, far points will have smaller values, but after applying the orthographic transformation matrix it will flip back.
Bloc97
CC BY-SA 4.0
2018-11-04T21:42:39.370Z
I misread your equation, what you have given is indeed a projection matrix, but a very simplified one, where r = -l, t = -b and n < f. Your two sets of equations are equivalent too, so the mistake is not in the matrix, it should be elsewhere.
Bloc97
CC BY-SA 4.0
2018-11-04T21:01:35.200Z
2018-11-04T21:40:54.783Z
2018-11-04T21:40:54.783Z
Bloc97
<p>I'm trying to compute a projection matrix to transform from view space to NDC with a near clip plane at -1 and far plane at +1. The general form of this matrix (disregarding aspect ratio and focal length) should be</p>

<p><span class="math-container">$\begin{bmatrix}1&amp;0&amp;0&amp;0\\0&amp;1&amp;0&amp;0\\0&amp;0&amp;A&amp;B\\0&amp;0&amp;-1&amp;0\end{bmatrix}$</span></p>

<p>I followed SongHo's guide at <a href="http://www.songho.ca/opengl/gl_projectionmatrix.html" rel="nofollow noreferrer">http://www.songho.ca/opengl/gl_projectionmatrix.html</a> which sets <span class="math-container">$A=-(f+n)/(f-n)$</span> and <span class="math-container">$B=-2fn/(f-n)$</span>. </p>

<p>However, setting the near clip plane at <span class="math-container">$n=-1$</span> and <span class="math-container">$f=-10$</span> (in view space) and using these <span class="math-container">$A$</span> and <span class="math-container">$B$</span>, I get points with <span class="math-container">$z$</span> values on the interval <span class="math-container">$[-1, -10]$</span> transformed to <span class="math-container">$[3.04, 1.04]$</span> (after homogenisation). </p>

<p>When I do the derivations myself, I'd like to set <span class="math-container">$A=(n+f)/(n-f)$</span> and <span class="math-container">$B=(-2fn)/(n-f)$</span> instead, which indeed transforms to <span class="math-container">$[-1,1]$</span> instead.</p>

<p>Am I doing something wrong?</p>

CC BY-SA 4.0
2018-11-04T20:32:14.830Z
A full perspective **projection** matrix is in the form of $M = M_{ortho}M_{persp}$ which is a orthographic matrix applied on a perspective matrix.
Bloc97
CC BY-SA 4.0
2018-11-04T21:39:40.610Z
Also, the two sets of equations you have given are equivalent, you might have a mistake elsewhere.
Bloc97
CC BY-SA 4.0
2018-11-04T22:07:08.950Z
Are they really equivalent, though? I mean, the first $B$ is $-2fn/(f-n) \neq -2fn/(n-f)$ (i.e., my $B$).
Supernormal
CC BY-SA 4.0
2018-11-04T22:19:29.187Z
There must be a typo somewhere, your $B = \frac{-2fn}{f-n}$ is correct. Prehaps the -1 below the $A$ is causing this problem, in my derivation below it is 1, not -1.
Bloc97
<3d><transformations><projections>
How to compute A and B in projection matrix
Supernormal
# How to compute A and B in projection matrix

(Asked by: username_0 on 2018-11-04)

I'm trying to compute a projection matrix to transform from view space to NDC with a near clip plane at -1 and far plane at +1. The general form of this matrix (disregarding aspect ratio and focal length) should be

$\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&A&B\\0&0&-1&0\end{bmatrix}$

I followed SongHo's guide at <http://www.songho.ca/opengl/gl_projectionmatrix.html> which sets $A=-(f+n)/(f-n)$ and $B=-2fn/(f-n)$.

However, setting the near clip plane at $n=-1$ and $f=-10$ (in view space) and using these $A$ and $B$, I get points with $z$ values on the interval $[-1, -10]$ transformed to $[3.04, 1.04]$ (after homogenisation).

When I do the derivations myself, I'd like to set $A=(n+f)/(n-f)$ and $B=(-2fn)/(n-f)$ instead, which indeed transforms to $[-1,1]$ instead.

Am I doing something wrong?

**Tags:** 3d, transformations, projections

> **Comments:**
> **username_1**: A full perspective **projection** matrix is in the form of $M = M_{ortho}M_{persp}$ which is a orthographic matrix applied on a perspective matrix.  
> **username_1**: Also, the two sets of equations you have given are equivalent, you might have a mistake elsewhere.  
> **username_0**: Are they really equivalent, though? I mean, the first $B$ is $-2fn/(f-n) \neq -2fn/(n-f)$ (i.e., my $B$).  
> **username_1**: There must be a typo somewhere, your $B = \frac{-2fn}{f-n}$ is correct. Prehaps the -1 below the $A$ is causing this problem, in my derivation below it is 1, not -1.  

---

## 1 answers

### Answer by username_1 on 2018-11-04. Score: 1

Note: I'll be using column vector notation (eg. $Ax=y$), thus if you are using row notation, transpose the matrix and multiply from the left instead of the right. (eg. $y=x^TA^T$)

A perspective transformation matrix transforms a view frustum into a rectangular view volume. Note that the resulting view volume might **not** be a cube and might **not** be centered at 0.

One *example* of a perspective transformation matrix. There are **many more**, and yours is included.

Let $l=left,\\ r = right,\\ b = bottom,\\ t = top,\\ n = near,\\ f = far$

$M\_{persp}=\begin{bmatrix}
1& & & \\
&1& & \\
& &\frac{n+f}{n}&-f\\
& &\frac{1}{n}&
\end{bmatrix}$

Since the resulting view volume is not the canonical view volume, we need to apply another matrix.

A orthographic transformation matrix transforms a rectangular view volume into a cubic view volume with two corners at (-1, -1, -1) and (1, 1, 1), which happens to be the canonical view volume.

Intuitively it will need one translation and one scaling.

$\text{T}\_{ortho} = S\left(\frac{2}{r-l},\frac{2}{t-b},\frac{2}{n-f}\right)T\left(\frac{-(l+r)}{2},\frac{-(b+t)}{2}\frac{-(n+f)}{2}\right)$

$M\_{ortho} =
\begin{bmatrix}
\frac{2}{r-l}& & & \\
& \frac{2}{t-b} & & \\
& & \frac{2}{n-f} & \\
& & & 1
\end{bmatrix}
\begin{bmatrix}
1 & & & \frac{-(l+r)}{2} \\
& 1 & & \frac{-(b+t)}{2} \\
& & 1 & \frac{-(n+f)}{2} \\
& & & 1
\end{bmatrix}
$

$M\_{ortho} =
\begin{bmatrix}
\frac{2}{r-l}& & & -\frac{l+r}{r-l} \\
& \frac{2}{t-b} & & -\frac{t+b}{t-b} \\
& & \frac{2}{n-f} & -\frac{n+f}{n-f}\\
& & & 1
\end{bmatrix}
$

Now you can combine them together to form a **perspective projection** matrix.

$M\_{proj} = M\_{ortho}M\_{persp}$

$M\_{proj} =
\begin{bmatrix}
\frac{2}{r-l}& & & -\frac{l+r}{r-l} \\
& \frac{2}{t-b} & & -\frac{t+b}{t-b} \\
& & \frac{2}{n-f} & -\frac{n+f}{n-f}\\
& & & 1
\end{bmatrix}
\begin{bmatrix}
1& & & \\
&1& & \\
& &\frac{n+f}{n}&-f\\
& &\frac{1}{n}&
\end{bmatrix}$

$M\_{proj} =
\begin{bmatrix}
\frac{2n}{r-l}& & \frac{l+r}{l-r} & \\
& \frac{2n}{t-b} & \frac{b+t}{b-t} & \\
& & \frac{n+f}{n-f} & \frac{2fn}{f-n}\\
& & 1 &
\end{bmatrix}$

If you have a free-moving camera you will need a camera matrix too, thus the final projection matrix centered on the camera is.

$M\_{proj} = M\_{ortho}M\_{persp}M\_{camera}$

> **Comments:**
> **username_0**: Thanks! But what does my matrix do, then? Doesn't it transform from a view space (with a perspective camera at the origin looking into -z) to a "clip space" (rectangular view volume) where far points have larger z values?  
> **username_1**: If your far plane is behind the near plane, far points will have smaller values, but after applying the orthographic transformation matrix it will flip back.  
> **username_1**: I misread your equation, what you have given is indeed a projection matrix, but a very simplified one, where r = -l, t = -b and n < f. Your two sets of equations are equivalent too, so the mistake is not in the matrix, it should be elsewhere.  

---

