# How are these two projection matrices related?

I have two 3D perspective projection matrices $$A,B$$ with standard projection parameters $$k=\cot(\theta/2)$$, where $$\theta$$ is the field of view, $$n$$ is the $$z$$-near value, $$f$$ is the $$z$$-far value, and $$a$$ is the width-to-height aspect ratio: $$A=\begin{bmatrix} k&0&0&0\\ 0&ka&0&0\\ 0&0&\frac{f}{f-n}&-\frac{fn}{f-n}\\ 0&0&1&0 \end{bmatrix}$$ and $$B=\begin{bmatrix} k&0&0&0\\ 0&ka&0&0\\ 0&0&\frac{f}{f-n}&-1\\ 0&0&\frac{fn}{f-n}&0 \end{bmatrix}.$$

I want to know if there is a transformation $$T$$ that will result in $$TA=B$$ and $$TB=A$$. I also would like to know what $$T$$ represents geometrically. I believe it has something to do with coordinate system handedness. However, after putting pen to paper, I can't come to a conclusion.

For more context, $$A$$ is the perspective projection provided in Matt Phar's PBRT (they use a left-handed coordinate system) and $$B$$ is the perspective projection provided in your typical OpenGL graphics lesson (I assume OpenGL uses right-handed coordinates when applying the projective matrix).

There are several things going on here.

I want to know if there is a transformation $$T$$ that will result in $$TA=B$$ and $$TB=A$$.

There is not, because what you would need to do is transpose the matrix, which cannot be expressed by multiplying by another matrix. Something to keep in mind is that it is common to store matrices in column-major order, and when that is done, if the elements are written out in an array literal or function arguments, they will appear transposed. So, you have probably encountered merely an accident of notation, and your $$B$$ should be written transposed:

$$B=\begin{bmatrix} k&0&0&0\\ 0&ka&0&0\\ 0&0&\frac{f}{f-n}&\frac{fn}{f-n}\\ 0&0&-1&0 \end{bmatrix}.$$

Now the only differences are signs; let's discuss that next. Actually, you have fewer differences than a typical OpenGL perspective matrix would have — I'd usually expect to see

$$B=\begin{bmatrix} k&0&0&0\\ 0&ka&0&0\\ 0&0&\frac{f + n}{n-f}&\frac{2fn}{n-f}\\ 0&0&-1&0 \end{bmatrix}.$$

Notice that the subtraction in the denominators is reversed, so the signs of the results are opposite. I'm going to assume that the “typical OpenGL graphics lesson” contains this and not what you wrote.

I believe it has something to do with coordinate system handedness.

Correct in part. Remember that if you negate any one axis, the handedness is changed. In an OpenGL conventional coordinate system, in eye space (the 3D coordinates that are being transformed for display by the projection matrix), the Z axis points towards the viewer, but in the conventional use of the clip and NDC spaces (after being transformed by the projection matrix), the Z axis points away from the viewer. So, there must be a negation of Z coordinates in the projection matrix; the value in the 3rd row and 3rd column is negative, and the off-diagonal elements which define the relationship between Z and W are also negative.

(I assume OpenGL uses right-handed coordinates when applying the projective matrix).

OpenGL does not actually care which handedness you use — it's all a matter of convention, because the projection matrix can do whatever it wants.

The remaining difference, the source of the $$2$$ in the matrix, is that OpenGL's NDC space differs from what most other graphics APIs use. NDC (normalized device coordinates) is the coordinate system where all “pixels that are actually drawn” live; the portions of the scene that are clipped are exactly those which live outside of the bounds in NDC space.

In OpenGL, the NDC space is a cube — the coordinate ranges are from -1 to 1 in all three axes. In most other systems, the X and Y ranges are -1 to 1, but the Z range is 0 to 1. That accounts for the final differences in the matrix — it's producing a different range of Z. In typical use of OpenGL, the perspective matrix maps points on the near plane to a NDC Z value of -1.0; elsewhere, the near plane would have a NDC Z value of 0.0.

This is often considered a design mistake on OpenGL's part; there are no technical advantages to the -1 to 1 system, and there are some for the 0 to 1 system (to do with the depth buffer precision available at various depths).

• A delightfully well-explained and helpful answer. Thank you very much! Commented Jun 1 at 16:19