In the last week our Computer Graphics class discussed how to infer projective mappings using a geometric approach. In this post I will show how the inferring can be done in a linear algebra way.

The main reference I use is the paper “Fundamentals of texture mapping and image warping”1. And I use Mathematica for symbolic computation.

## Theoretical background

In the section 2.2.3 of the paper1, a linear system is constructed to solve for elements of a projective mapping in 2D. This can be extended to 3D easily.

Let us denote the projective mapping to be determined as following:

Suppose we have a point $$(x, y, z)$$ in view space, and a point $$(u, v, w)$$ in NDC space, the equations can be written as following:

We apply these equations on all 8 vertices of the frustum, then construct and solve the linear system for the projective mapping $$\mathbf{M}$$.

## Implementation

First we build the $$\mathbf{M}$$, and an array of symbolic variables are stored in v for later use:

Then we construct matrices for points in view and NDC spaces:

Note that we use a scale factor k for the frustum in perspective projection. For the orthographic projection k should be $$1$$.

Next we define a function to generate one constraint for our linear system:

Now all data and tools are ready. We start to generate all constraints, and solve it.

The final result is:

Note that the solution is not unique, and it is parameterized by a single scalar($$m_0$$ in our case). This non-uniqueness can be seen from that we can multiply arbitrary non-zero factor on a point in homogeneous space without changing its meaning.

By letting $$m_0$$ equal $$\frac{2 n}{r - l}$$, we can get the conventional perspective projection matrix (right-handed, and maps to $$[-1, 1]^3$$, with positive $$n$$ and $$f$$):

1. Heckbert, Paul S. Fundamentals of texture mapping and image warping. MS thesis. University of California, Berkeley, 1989. 2