views:

104

answers:

3

Hello !

I tried googling and searching on stack but I didn't find anything :-(
( http://stackoverflow.com/questions/2842339/mapping-irregular-shapes-or-other-polygons-cartoons-sprites-to-triangles-in-op )

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

A: 

Hi,

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

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

Cheers, Krzysztof Zabłocki

Krzysztof Zabłocki
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
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
yeah I only need polygons, but thanks ;-)
Vinzius
i added link to working code for tessalation
Krzysztof Zabłocki
thanks, will take a look too :)
Vinzius
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
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
+2  A: 

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
So long as the polygon is convex!
Daniel Paull
@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
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
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
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
Theres an SO discussion on decomposing to convex polygons here: http://stackoverflow.com/questions/694108/decomposition-to-convex-polygons
S.C. Madsen
A: 

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
Thanks, I will take a look to see if it's can be used :-)
Vinzius
Tried to compile the project but it seems having "some" problems ^^ still thanks
Vinzius