2

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

enter image description here

William
  • 111
  • 2
  • 5
  • Not directly answering your question, but did you consider using `glFrontFace(GL_CW)` to specify that your front faces have CW winding order? – Reto Koradi Jul 14 '16 at 03:00
  • Yes, but then I would have to keep track of which winding order each polygon has, and make the gl call every time the winding order changes. By the time I do that, I figure I might as well disable culling altogether. – William Jul 14 '16 at 12:20
  • That doesn't match the problem description in your question. In the question, you're saying that the winding order changes because you're applying a reflection in the transformation. Now you're saying that each polygon can have a different winding order? – Reto Koradi Jul 14 '16 at 15:56
  • All of my polygons start out with CCW winding order, but if I reflect any of them, their winding order changes. For example, if I have polygons p1,..,p10, and reflect p10, then p1,...,p9 will have CCW winding order and p10 will have CW winding order. I want to know how to restore p10's winding order to CCW by permuting its vertices. – William Jul 14 '16 at 17:55

1 Answers1

2

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.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Your solution works, but I'd really like to find a solution that permutes the vertices instead of the indices. That way I could have a static final index buffer that all my polygons of a certain type share. – William Jul 15 '16 at 15:19