Drawing polygons using triangles

Started by
1 comment, last by AEtheReal 14 years, 2 months ago
I have written a program for simple drawing which uses GDI+. I heard that using graphics library like OpenGL can speed up the drawing process, so now I am converting the code to use OpenGL instead of GDI+. For each function (e.g. FillEllipse, DrawImage, FillPolygon) I used in GDI+, I try to implement it using OpenGL. When I am trying to code FillPolygon, I encounter a big problem, which is completely out of my expectation. OpenGL can draw triangle strip and fan, which does not work when the polygon is convex. To make things even worse, the polygon may not be simple. There may be self-overlapping. In the FillPolygon call in GDI+, the area that is include an odd number of times is filled. For example, when the shape "8" is passed to the procedure, it fills the two loops. It is extremely important to cope with non-simple polygon in my program, since the polygon may undergo conplex transformations that cannot guarantee that the resultant polygon is simple. If the polygon is drawn incorrectly, the output may become flickery. However it seems very difficult to replicate this using OpenGL. I guess the only way to do this is to divide the polygon into triangles. I cannot think of any algorithm that can run in reasonable time. GDI+ can draw polygons with hundreds of vertices in 30fps. I hope to attain at least comparable result. I would like to ask if there is any fast algorithms to break an arbitrary polygon into triangles? Or are there any tricks to get around it?
Advertisement
Look at using the stencil buffer to implement your fill rules, you can simply submit your vertices as a triangle fan and the stencil will do the rest, cutting out the areas that should be convave/holes etc. Depending on the stencil rules used you can quite easily emulate a number of fill rules inclusing even-odd and non-zero.

General triangulation (with concavity, self intersection, and holes) is a much harder solution, and unless you have the opportunity to generate the triangulated data off-line it is very difficult to make fast enough for real time applications. If you are interested take a look at ear clipping or Seidel's algorithms.

It should be pretty easy to find information on both of the above.

Best of luck,

Alex

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

Thanks a lot!

Never thought that stencil buffer can work this way

This topic is closed to new replies.

Advertisement