0
$\begingroup$

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?

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

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Supernormal Nov 4 '18 at 21:26
  • $\begingroup$ 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. $\endgroup$ – Bloc97 Nov 4 '18 at 21:33
  • $\begingroup$ 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. $\endgroup$ – Bloc97 Nov 4 '18 at 21:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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