# How to compute A and B in projection matrix

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?

• 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. Nov 4 '18 at 20:32
• Also, the two sets of equations you have given are equivalent, you might have a mistake elsewhere. Nov 4 '18 at 21:39
• Are they really equivalent, though? I mean, the first $B$ is $-2fn/(f-n) \neq -2fn/(n-f)$ (i.e., my $B$). Nov 4 '18 at 22:07
• 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. Nov 4 '18 at 22:19

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}$$

• 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? Nov 4 '18 at 21:26
• 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. Nov 4 '18 at 21:33
• 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. Nov 4 '18 at 21:42