OpenGL triangle winding order

Started by
4 comments, last by BitMaster 11 years, 4 months ago
Hello,

I'm not sure I understand how the triangle winding order works. I've been reading http://www.arcsynthesis.org/gltut/Positioning/Tutorial%2004.html but I don't quite understand how it works.

Specifically these lines, where the bolded I do not understand:

No matter what size or shape the triangle is, you can classify the ordering of a triangle in two ways: clockwise or counter-clockwise. That is, if the order of the vertices from 1 to 2 to 3 moves clockwise in a circle, relative to the triangle's center, then the triangle is facing clockwise relative to the viewer. Otherwise, the triangle is counter-clockwise relative to the viewer.[/quote]




For reference, I'm trying to make a cube using glDrawElements, and I want to know how I should order them and why. Below is a naive attempt:


const float vertexPositions[] =
{
// front vertices
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
// back vertices
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f
};
const GLshort indexData[] =
{
// back
4, 5, 6,
7, 4, 5,
// right
1, 5, 6,
2, 1, 6,
// left
4, 0, 3,
7, 4, 3,
// top
4, 5, 1,
0, 4, 1,
// bottom
7, 6, 2,
3, 7, 2,
// front
0, 1, 2,
3, 0, 1
};
Advertisement
Imagine you paint a triangle on a piece of paper, labeling the vertices A, B, C. Pick a thin paper and pen which is clearly visible from both sides. Looking at it from one side you would see something like this:

A
|\
| \
B--C

Like this, the triangle ABC is in counterclockwise order. Now move your head around the paper so you see it from the other side (alternatively, just flip it over).
What you see from this position (minus the flipped BC) is:

A
/|
/ |
C--B

The triangle ABC is now in clockwise order (and would be culled normally).
Also take into account this deals with 2D space. Regarding 3D models, you'd determine whether a triangle is clockwise or not depending from where you're looking at. Generally you choose whether triangles when seen from the "front" (e.g. outside of the mesh) are clockwise or not and arrange vertices accordingly.

The main reason this is done is backface culling (to get rid of most triangles you'll never see anyway) and stuff like normals (which need to point towards one side of the triangle)
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
Which one is prefered, CW or CCW?

Imagine you paint a triangle on a piece of paper, labeling the vertices A, B, C. Pick a thin paper and pen which is clearly visible from both sides. Looking at it from one side you would see something like this:

A
|\
| \
B--C

Like this, the triangle ABC is in counterclockwise order. Now move your head around the paper so you see it from the other side (alternatively, just flip it over).
What you see from this position (minus the flipped BC) is:

A
/|
/ |
C--B

The triangle ABC is now in clockwise order (and would be culled normally).


What effect does this have when I have to specify in what order to draw the vertices?
For example, if I use CW and I draw vertices (0, 1, 2), and then try the same with (2, 1, 0), it wont be shown?
The triangle ABC is in CCW order in my first example (and would be rendered). The same triangle ABC is in CW order in my second example (and would be culled). The second example is the same situation as the first example, just viewed from the opposite direction. So the same triangle is visible or invisible depending on the view direction and that decision is made using the winding order of the triangle vertices projected into 2D.
On the other hand, the triangle CBA is in CW order in the first example and in CCW order in the second example.

Most renderers (that includes DirectX and OpenGL) expect a CCW face to be the front face and will simply cull backfaces. At least in OpenGL you can specify which winding is treated as the front face and how exactly to handle front- and backfaces.

This topic is closed to new replies.

Advertisement