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).