Calculation of a perspective transformation matrix

3dmathmatrixprojection

Given a point in 3D space, how can I calculate a matrix in homogeneous coordinates which will project that point into the plane z == d, where the origin is the centre of projection.

Best Answer

OK, let's try to sort this out, expanding on Emmanuel's answer.

Assuming that your view vector is directly along the Z axis, all dimensions must be scaled by the ratio of the view plane distance d to the original z coordinate. That ratio is trivially d / z, giving:

x' = x * (d / z)
y' = y * (d / z)
z' = z * (d / z)    ( = d)

In homogenous coordinates, it's usual to start with P = [x, y, z, w] where w == 1 and the transformation is done thus:

P' = M * P

The result will have w != 1, and to get the real 3D coordinates we normalise the homogenous vector by dividing the whole thing by its w component.

So, all we need is a matrix that given [x, y, z, 1] gives us [x * d, y * d, z * d, z], i.e.

| x' |  =    | d   0   0   0 |  *  | x |
| y' |  =    | 0   d   0   0 |  *  | y |
| z' |  =    | 0   0   d   0 |  *  | z |
| w' |  =    | 0   0   1   0 |  *  | 1 |

which once normalised (by dividing by w' == z) gives you:

[ x * d / z, y * d / z,   d,   1 ]

per the first set of equations above