Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the winding direction in an OpenGL triangle strip alternate from triangle to triangle?

I'm trying to clear up some inconsistencies I'm seeing in triangle strip vertex winding direction (clockwise and counter-clockwise). I'm drawing a trapezoid rotated 90 degrees counter-clockwise in OpenGL. Here's the relevant code:

unsigned char mIndices[] = { 0, 1, 2, 3, 4, 5 };
signed short mVertices[] = {
                                -50, 100, 0,    // A
                                -85, 65, 0,     // B
                                -50, 65, 0,     // C
                                -85, -65, 0,    // D
                                -50, -65, 0,    // E
                                -50, -100, 0,   // F
};

...

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_SHORT, 0, mVertices);

...

glDrawElements(GL_TRIANGLE_STRIP, sizeof(mVertices)/sizeof(mVertices[0]), GL_UNSIGNED_BYTE, mIndices);

From what I read here, the default front face in OpenGL is counter-clockwise, which means the first triangle in my strip should have it's vertices ordered counter-clockwise. Additionally, when drawing a triangle strip, the winding switches directions between counter-clockwise and clockwise from triangle to triangle, so my vertices are ordered in that manner. So from my code, the first triangle would be ABC, the second would be BCD, third CDE, and fourth DEF. However, this article (in the quote from the OpenGL Programming Guide) says that it would draw them as ABC, CBD, CDE, EDF (assuming v0 - A, v1 - B, etc) which means they all wind the same counter-clockwise direction.

If I'm understanding the A/B notation correctly from the OpenGL specification, the triangles would all wind the same direction, but I've seen the varying winding in a few different places. I suppose it's just an issue of semantics since the resulting shape is the same, but what is the actual winding order?

like image 731
charon00 Avatar asked Aug 01 '13 16:08

charon00


1 Answers

So from my code, the first triangle would be ABC, the second would be BCD, third CDE, and fourth DEF. However, this article (in the quote from the OpenGL Programming Guide) says that it would draw them as ABC, CBD, CDE, EDF (assuming v0 - A, v1 - B, etc) which means they all wind the same counter-clockwise direction.

What they both said is true. From a certain point of view ;)

The tutorial you cited is talking about it from the point of view of the order that the vertices are provided to the rasterizer. Which is exactly as stated. The rasterizer sees a single stream of ABCDEF... vertcies. Therefore, from that perspective, the rasterizer must switch its internal winding order on every other triangle in order to match the intent of the user.

The book you cited is talking about it from the point of view of how you think of the order of those triangles. While you provide them in that order, you want the winding to work out like ABC, CBD, CDE, etc. And to achieve this, you provide them in the ABCDEF... order.

like image 138
Nicol Bolas Avatar answered Nov 07 '22 13:11

Nicol Bolas