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

2

7

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?

Plath answered 1/8, 2013 at 16:43 Comment(0)
V
7

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.

Verbal answered 1/8, 2013 at 17:42 Comment(0)
A
5

Rest assured that the triangles resulting from a triangle strip are all wound in the same order (otherwise it would be a total mess and you couldn't use simple things like back-face culling or two-sided rendering at all), that's part of the magic of a triangle strip and why just drawing a simple triangle starting at each consecutive vertex wouldn't work (well, it would, but it would result in alternating winding orders, which is never ever (ever) a good idea). So the second article (from the OpenGL programming guide) is correct and the first article is either wrong, has its semantics messed up or was misinterpreted by you (though I'd guess the latter, given the quality of that tutorial, but maybe Nicol Bolas himself can shed some light on it).

EDIT: Ok, looking at the end of the first article:

Notice how it alternates between clockwise and counter-clockwise. This means that, regardless of what face you consider front, and what face you cull, you'll always lose about half of the faces.

However, OpenGL is rather intelligent about this. Triangle strips do face culling differently. For every second triangle, the one who's winding order is opposite from the first triangle's order, the winding order is considered backwards for culling purposes.

So if you have set the front face to be clockwise, and have face culling cull back-facing triangles, everything will work exactly as you expect so long as the order of the first triangle is correct. Every even numbered triangle will be culled if it has a clockwise winding, and every odd numbered triangle will be culled if it has a counter-clockwise winding.

It actually says that while the winding order would theoretically be alternating, OpenGL accounts for that and "fixes" it for triangle strips, resulting in the winding order practically (for all facing considerations, like culling or the gl_FrontFacing fragment shader variable) being the same for each triangle. So both articles are correct and you merely didn't read the first one till the end. Though the article could have made it more clear that it isn't just done for culling purposes, but all facing purposes and thus practically unnoticable to you.

Arguable answered 1/8, 2013 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.