9

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

Community
  • 1
  • 1
Vinzius
  • 2,890
  • 2
  • 26
  • 27
  • Hey there, so did you manage to achieve it using what you edited as Solution? Any other insights you got now, 7 years later? :) – DarkCygnus Apr 04 '18 at 23:22

6 Answers6

5

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

S.C. Madsen
  • 5,100
  • 5
  • 32
  • 50
  • 4
    So long as the polygon is convex! – Daniel Paull Oct 06 '10 at 13:08
  • @Daniel: Precisely! (did I not mention that? It would of course be easy to split the polygon into triangles and draw those using GL_TRIANGLES, but it sounded like he wanted to avoid that... – S.C. Madsen Oct 06 '10 at 13:12
  • Yep I'm working on a solution based on a code found here ( http://www.cocos2d-iphone.org/forum/topic/848 ) But it works only if the polygon is convex, like said Daniel. If anything works I tell you. – Vinzius Oct 06 '10 at 13:13
  • 1
    I remember seeing algorithms for splitting polygons into convex polygons, you could try Google. Anyway I hope you get your problem solved :-) – S.C. Madsen Oct 06 '10 at 13:16
  • I'm going to search thing about that too. tks. As soon as I get a good solution I put the code here and set the topic as solved ;-) If anyone found something already done, I take it too ! thanks for helping ;-) – Vinzius Oct 06 '10 at 13:17
  • Theres an SO discussion on decomposing to convex polygons here: http://stackoverflow.com/questions/694108/decomposition-to-convex-polygons – S.C. Madsen Oct 06 '10 at 13:25
  • Thanks for the answer that is very helpful especially with the graphical illustration. Yes, this works for all convex polygons, but it works for certain concave polygons too as long as the fan spokes and edges do not cross each other. For example, if we move point b and h far to the left of the starting point a to make the polygon concave, this method will still work. – Hong Jan 31 '12 at 13:41
4

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.

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • Ok but If possible I would like avoiding Cocos2D just for drawing polygons xD (but by the way, using Cocos2D means adding lot of size to the final archive ? can I create just one View above other UIView without problem ?) – Vinzius Oct 06 '10 at 12:45
  • You can use opengl and normal UIKit without a problem, cocos2d offers much more functionality than drawing polygons so if you only need simple polygons then it's not needed for sure :) – Krzysztof Zabłocki Oct 06 '10 at 13:16
  • It seems efficiently for polygon without holes (is saying the header file). So I'm going to study another C code which handle it ^^. If I code C => ObjC I will put the code. Maybe a response later or tomorrow ^^ – Vinzius Oct 06 '10 at 13:50
  • I'll begin with this library despite it doesn't support holes and then go to http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html A+ ;-) – Vinzius Oct 08 '10 at 09:48
3

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.

Nic Roets
  • 31
  • 2
2

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
Dong Ma
  • 1,073
  • 9
  • 16
  • Hi. Didn't find a good solution as you explained in your post... So for the moment I try to do without. If you find any solution I'm interested. – Vinzius Nov 01 '11 at 16:34
  • Thanks. I'm using the 3rd option now after modifying some local variable initialization bugs (initialize to 0). I'm not sure if there are any other bugs, but since it is not crashing now in my project, I think it is the best choice I can think of. (I found it so hard to login Stack Overflow with stackExchange ID, this is my first successful login since signup (I pinned the tab in chrome, so that each time I start chrome, it will try to login Stack Overflow...)) – Dong Ma Nov 12 '11 at 12:49
  • @DongMa, the 3rd option doesn't allow to download the source code any more. – Dmitriy Aug 23 '12 at 05:50
  • I'm NOT using any of the 3rd party libs in the end. NONE of them is robust enough to satisfy me. I'm using CoreGraphics now, although still looking for a light-weight and free 3rd party lib... – Dong Ma Mar 27 '14 at 12:12
0

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!

Petr B.
  • 879
  • 1
  • 8
  • 17
0

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.

Daniel Paull
  • 6,797
  • 3
  • 32
  • 41