Question

I tried googling and searching on stack but I didn't find anything :-(
( Mapping irregular shapes or other polygons (cartoons, sprites) to triangles in OpenGL ES )

I want to draw a filled polygon in OpenGL ES on iPad (2D, don't need 3D) (I'm new to OpenGL && OpenGL ES) It seems simple on OpenGL but with ES with just can draw triangle.

But I can't find any good tutorial about this :-( Does it exist any wrapper / library or anything else that could help me ?

I don't want to use CoreGraphics, it's too slow for what I'm doing ^^
And if possible, avoiding too (big) library like Cocos2D.

Edit : Working On the Problem, here are some useful links (will edit)
Links
- Polygon Triangulation
http://www.vterrain.org/Implementation/Libs/triangulate.html
http://en.wikipedia.org/wiki/Polygon_triangulation (WIKI) http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml (polygon WITHOUT holes) http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html (polygon WITH holes, C code)

Solution
I will try using polygon tessellation to get triangles. At first I'm going to try ( http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml ) and as I need polygon with holes, the second one ( http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html ). If anyone has a better way, PLEASE, tell me with a comment ^^

Thanks :-)

Was it helpful?

Solution

Cocos2D is cool library wrapping OpenGL and has many useful features ( mainly for games but not limited to ).

For polygon tessellation use : http://flipcode.net/archives/Efficient_Polygon_Triangulation.shtml I've used it before and it worked well.

OTHER TIPS

I haven't tried using OpenGL ES, but judging from a quick look in the documentation you should be able to draw a convex polygon using e.g. a "triangle fan":

glVertexPointer(2, ..., arrayOfCoordinates)
...
glDrawElements(GL_TRIANGLE_FAN, ... , arrayOfIndices);

You can think of a "triangle fan" as the spokes of a bicycle-wheel dividing the area of the wheel into "triangles" (the outer edge of a bicycle wheel is of course round, but I hope you get the idea).

UPDATE: I found a small diagram on the web:http://www.cse.msu.edu/~cse472/step6_stuff/step6.1.gif

On the Wikipedia Polygon Triangulation Talk page I argue that more triangles will in fact be faster.

I have written a triangulation engine that supports holes and runs in O(n log (n)) time. I tested it under Gdk and then made an Android app from it.

What is your final choice? I recently tested 5 of the libs listed in below link: http://vterrain.org/Implementation/Libs/triangulate.html

But NONE of them is satisfying...

  1. iphone-glu: (http://code.google.com/p/iphone-glu/)

    • Bugs in the algorithm? there are tiny holes not filled, or sometimes draw outside of the polygon
  2. Triangulte: (http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml)

    • doesn't support holes…
  3. Triangulation: (http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html)

    • Buggy, sometimes runs out of bounds, or negative index etc...
  4. poly2tri: (http://code.google.com/p/poly2tri/)

    • Buggy, crash in certain cases
  5. openglespolygon: (https://github.com/nroets/OpenGlEsPolygon)

    • imperfect algorithm? there are tiny holes (not filled) along the edges of the polygon

In non-ES OpenGL one would tend to use the tessellator from the GL Utility (glu) library. I found this project which aims to make glu available for the iPhone and it claims to support polygon tessellation - if it works, then it would be a good option.

When you need do triangulation of polygons with holes, you can try use GPC. There is function for tringulation called gpc_polygon_to_tristrip.

For render use GL_TRIANGLE_STRIP.

But read gpc license first!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top