Reputation: 111
I'm rendering a polygon in OpenGL with a vertex array called vertices
and a final
index buffer called DRAW_ORDER
with CCW winding. I have back-face culling enabled, and I make draw calls using glDrawElements(GL_TRIANGLES, DRAW_ORDER.capacity(), GL_UNSIGNED_SHORT, DRAW_ORDER)
.
When I reflect vertices
in the x or y axis via matrix transformation, the polygon gets culled because the reflection reverses the orientation of the vertices, so that they no longer match the CCW winding order of DRAW_ORDER
.
I can prevent the problem by disabling culling, but for performance sake I would rather find a way to restore the orientation of vertices
via permutation. For example, if the polygon were a triangle, I could simply swap the second and third vertices after a reflection to restore CCW orientation. How can I extend this approach to a polygon with an arbitrary number of vertices and indices?
//PSEUDO-CODE FOR TRIANGLE:
final DRAW_ORDER = {0,1,2};
vertices = { {0,0}, {1,0}, {0,1} };
reflect(vertices);
swap(vertices,1,2);
EDIT: Here's a solution that seems to work for convex polygons, but not concave.
//Reverse the order of the vertices so, for example,
//vertices {v1,v2,v3,v4,v5} become {v5,v4,v3,v2,v1}
for(int start = 0, end = vertices.length-1; start<end; start++, end--){
swap(vertices,start,end);
}
You can see in the image below how the solution works for an ellipse (which is convex) but not a star (which is concave).
Upvotes: 2
Views: 1354
Reputation: 54642
To invert the winding order by a permutation of the indices, you can simply reverse the order of the indices.
So for your triangle example, if the original order is (0, 1, 2), the reverse order is (2, 1, 0). Since all cyclic permutations are equivalent for defining a polygon, other valid orders would be (1, 0, 2) and (0, 2, 1). But using the reverse is as good as anything.
As another example, for a polygon with 5 vertices, using indices (3, 17, 6, 8, 11), the reverse is (11, 8, 6, 17, 3), which can be used as the set of indices when you want to render the polygon with the opposite winding order.
Upvotes: 2