Sign in to follow this  
AEtheReal

OpenGL Drawing polygons using triangles

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this