Figure B shows a diagram of two Polygons A & B along with their respective Normals (n1 & n2)
describing the way they are facing. V1 and V2 are the Vectors from the viewpoint to any point(vertex) in
the polygon. To make this clearer lets go through this step by step, starting with Polygon A. We are
going to test if it is visible to the viewer. We must: -
1. Calculate a Vector from the Viewpoint position to the position of any Vertex in the Polygon. Any
vertex in the polygon will do. In Figure B, the vertices I have used are labelled p1 & p2.
VECTOR v1=p1-view_pos;
2. Calculate the Dot Product of this new Vector v1 and the Normal of the Polygon n1
float result=DotProduct(v1,n1);
If you are unsure how to generate a Polygon Normal see Cross Product.
3. If the result is negative, the polygon is facing the viewer and should be drawn. If the result is
positive the polygon is not facing the viewer and should be discarded. If the result is 0, the
polygon is edge on to the viewer and can be treated as either.
The Dot Product works by taking the source point of each vector and bringing them together and
then calculating the cosine of the angle between them. To better understand this look at the inset
(diagram in the circle) below polygon a in the diagram. This shows how the two vectors are
bought together to share the same origin and shows the Normal of the polygon and the vector
from the camera to p1 are bought together to create an angle between them that is larger than 90
degrees. This means the Dot Product returns a Negative number and also means that the polygon
IS facing the Viewer.
Polygon B on the other hand is another story. When polygon normal and the vector from the
camera to p2 are moved to the same origin you can see from the inset diagram below polygon b
that the angle created is less than 90 degrees. The Dot Product returns Positive in this case
meaning that the polygon is facing away from the viewer and so should be Back Face culled.
IMPORTANT NOTE:
The Crucial thing to understand from the above diagram is that that BOTH vectors are moved to the same ORIGIN by the Dot
Product operation. Look at the two Inset diagrams above and make sure that you understand how the Polygon Normal Vector
and the Direction vector from the camera to the point on the plane(p1 or p2) are bought together to share the same origin.
I understand from my own experience that for the less mathematical people out there this can be difficult
to grasp. I am indeed one of those people that could not get any form of Trig into my head. When people
explained it to me, they still assumed I understood more mathematics than I did. I feel the best way to
learn anything is to play around with some code. So lets write a Back face checker function just to show
how easy it is. This way, even if you don’t understand it fully, at least you have the code for your own
applications.
Code for Check Backface Function. Note:- returns true if face is a backface and should not be drawn.
bool check_backface(VECTOR p1,VECTOR normal,VECTOR viewpos)
{
VECTOR v1;