OpenGL ES - How to Draw a filled Polygon?
Asked Answered
A

6

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

Acacia answered 6/10, 2010 at 12:36 Comment(1)
Hey there, so did you manage to achieve it using what you edited as Solution? Any other insights you got now, 7 years later? :)Hutt
S
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.

Scyros answered 6/10, 2010 at 12:42 Comment(4)
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 ?)Acacia
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 :)Waspish
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 ^^Acacia
I'll begin with this library despite it doesn't support holes and then go to cs.unc.edu/~dm/CODE/GEM/chapter.html A+ ;-)Acacia
B
6

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:https://static.mcmap.net/file/mcmap/ZG-Ab5ovK1c1cw2jbRUQa7M0Kmykc3/~cse472/step6_stuff/step6.1.gif

Bonniebonns answered 6/10, 2010 at 13:2 Comment(7)
So long as the polygon is convex!Lithiasis
@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...Bonniebonns
Yep I'm working on a solution based on a code found here ( cocos2d-iphone.org/forum/topic/848 ) But it works only if the polygon is convex, like said Daniel. If anything works I tell you.Acacia
I remember seeing algorithms for splitting polygons into convex polygons, you could try Google. Anyway I hope you get your problem solved :-)Bonniebonns
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 ;-)Acacia
Theres an SO discussion on decomposing to convex polygons here: #694608Bonniebonns
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.Doiron
S
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.

Scyros answered 6/10, 2010 at 12:42 Comment(4)
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 ?)Acacia
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 :)Waspish
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 ^^Acacia
I'll begin with this library despite it doesn't support holes and then go to cs.unc.edu/~dm/CODE/GEM/chapter.html A+ ;-)Acacia
F
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.

Faviolafavonian answered 21/1, 2011 at 22:2 Comment(3)
I discovered and fixed a number of bugs. Please wait for my next commit before trying it.Faviolafavonian
I committed a new version. Look in the .h file before you start.Faviolafavonian
Didn't try yet, but very soon (I have to finish a report before). I will mail/MP you when I tried ;-)Acacia
S
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
Spencerianism answered 27/10, 2011 at 10:35 Comment(4)
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.Acacia
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...))Spencerianism
@DongMa, the 3rd option doesn't allow to download the source code any more.Metalline
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...Spencerianism
L
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.

Lithiasis answered 6/10, 2010 at 13:18 Comment(3)
Thanks, I will take a look to see if it's can be used :-)Acacia
Tried to compile the project but it seems having "some" problems ^^ still thanksAcacia
glu tessellation is now deprecated, what's the modern way of achieving this on non-ES opengl?Craggie
W
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!

Woll answered 31/1, 2014 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.