Skip to content

Geometry for 3D Rendering

Polygon Winding

Working out if a triangles points are in clockwise or anti-clockwise order can be used to backface cull a polygon.

// triangle is made up of points (x1,y1), (x2,y2), (x3,y3)

int edge1 = (x2 - x1) * (y2 + y1);
int edge2 = (x3 - x2) * (y3 + y2);
int edge3 = (x1 - x3) * (y1 + y3);

if (edge1 + edge2 + edge3 < 0) // triangle is facing away - don't draw it

Looking at this code, there's an error in the condition.

It's calculating the z component of each vector that makes up the triangle. If all the resulting z components are facing the right way, the triangle is facing the viewer.

The condition should probably read...

if (edge1 <= 0 && edge2 <= 0 && edge3 <= 0)

Dot and Cross Products

dot and cross products