In 2D and without holes, this is pretty simple. First, you need to split your polygon into one or more monochrome polygons .
Monotone polygons are pretty simple to turn into tristrips, just sort the y
values, find the top and bottom vertices, and then you have lists of vertices on the right and left (because the vertices fall into some specific order, for example, clockwise). Then you start at the top of the vertex and add the vertices on the left and right with alternating image.
This method will work for any two-dimensional polygons without self-intersecting edges, which includes some cases of polygons with holes (holes should be properly wound, though).
You can try playing with this code:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(-.5f, -.5f, 0); std::vector<Vector2f> my_polygon; my_polygon.push_back(Vector2f(-0.300475f, 0.862924f)); my_polygon.push_back(Vector2f(0.302850f, 1.265013f)); my_polygon.push_back(Vector2f(0.811164f, 1.437337f)); my_polygon.push_back(Vector2f(1.001188f, 1.071802f)); my_polygon.push_back(Vector2f(0.692399f, 0.936031f)); my_polygon.push_back(Vector2f(0.934679f, 0.622715f)); my_polygon.push_back(Vector2f(0.644893f, 0.408616f)); my_polygon.push_back(Vector2f(0.592637f, 0.753264f)); my_polygon.push_back(Vector2f(0.269596f, 0.278068f)); my_polygon.push_back(Vector2f(0.996437f, -0.092689f)); my_polygon.push_back(Vector2f(0.735154f, -0.338120f)); my_polygon.push_back(Vector2f(0.112827f, 0.079634f)); my_polygon.push_back(Vector2f(-0.167458f, 0.330287f)); my_polygon.push_back(Vector2f(0.008314f, 0.664491f)); my_polygon.push_back(Vector2f(0.393112f, 1.040470f));
This code is not optimal, but it should be easy to understand. At the beginning, a concave polygon is created. Then a "working set" of vertices is created. On this working set, a permutation is computed that sorts the vertices by their y
coordinate. This permutation then loops around looking for split points. After the split point is detected, a new monotonic polygon is created. Then the vertices used in the new polygon are removed from the working set, and the whole process is repeated. Finally, the working set contains the last polygon that cannot be broken. At the end, monotone polygons are displayed, as well as the order of the triangular strip. It's a bit dirty, but I'm sure you figure it out (this is C ++ code, just put it in the GLUT window and see what it does).
Hope this helps ...