View all posts
If you want to be notified about future posts, please consider subscribing:
This blog post is a “port” into native website form of a LaTeX/PDF article that I wrote back in 2008, named Vertex Projection onto a Plane. Motivation for that was to play with LaTeX in WordPress. I have not introduced any corrections.
Projection is important graphics operation. We use projection to make our 3-dimensional world appear on a 2-dimensional screen, we use dot product to project one vector onto another. In this article we will derive matrices that project a vertex onto a plane.
If you still do not know what it is for I will give you a brief example. Imagine you want your object to cast a shadow onto a ground plane. You could achieve this by projecting all object’s vertices onto that plane. What you get is your object flattened on the plane just as it would cast a shadow. Such object could be projected from a specific direction (a directional light generating shadows) or from a specific point (a point light generating shadows). We will consider both cases.
1. Directional Projection
Let’s start with some formulas:
What we have here is the straight line equation with projection direction , vertex
we are projecting and the plane equation
we are projecting onto. Our main task is to find
.
To find we need to substitute
,
and
equations to the plane equation and solve for
. In other words we want to find such
for which a point on our straight line also lies on the plane. We have:
At this point we actually have the solution we were looking for. If we substitute this just calculated value to our initial straight line equation we will have a position
of vertex
projected onto a plane
along
direction.
Although we have a proper solution it would be much better to have it in a matrix form. Such a matrix form is convenient, for example, when we use vertex shaders. What we do is just passing the computed matrix to a shader and letting it do the projection in a fast GPU way.
To derive the projection matrix we need to solve ,
and
equations with substituting
and rearrange them a bit. Let’s first solve
for
:
Solving and
for
in the same way yields:
Now let’s introduce a helper variable such that:
Now:
You may not see it yet but we have just derived our matrix form! Take a look:
And that’s it. Our projection matrix has been derived. Note that the 4th column is made of vector. That means this matrix doesn’t do any perspective transformation so we may be sure that
will always be equal to 1. Dividing
,
and
by
is not necessary.
2. Point Projection
Point projection is very similar to directional. The only thing that is different is that the projection vector is unique for every object’s vertex and depends on projector’s (point) position
. Initial formulas:
So as you can see, the equations are very similar to those used in directional projection. The only change is that we consider projector’s position.
Just as in direction projection we must first find by substituting
,
and
to the plane equation:
Solving for
yields:
Solving and
for
in the same way yields:
And so we have – the matrix form is now easy to be written down. You may be worried about the denominator, as vertex appears there, so you may think it is not correct. If so you probably forgot about one – perspective division. Dividing by
will be necessary here. The final matrix:
After transformation all you have left is to divide ,
and
by
.
3. Acknowledgments
I would like to thank to Krzysztof Kluczek for helping me in developing these formulas (via gamedev.pl forum).