Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OpenGL ES - How to draw a filled concave Polygon?

I am creating a 2D Openstreetmap rendering application for Android using OpenGL ES.

Unfortunately I am struggling with drawing concave polygons consisting of approximately 50 or more points. In order to accomplish that my polygons are filled, I am using GL10.GL_TRIANGLE_FAN:

gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff); // size = 2 because 2D

gl.glDrawElements(GL10.GL_TRIANGLE_FAN, pointCount, GL10.GL_UNSIGNED_SHORT, indexBuff);

This approach works fine as long as my polygons are not too complex, for example drawing a polygon like this is no problem at all:

enter image description here

My problem is, that GL_TRIANGLE_FAN always starts at the first point (a in this case) and then uses 2 other points to draw a triangle, until the whole shape is complete.

If a more complex polygon has to be drawn, this can lead to issues as you can see here: (in this example, the river that flows through the city is a complex polygon) enter image description here

My question now is how to avoid this problem so that even complex polygons are drawn correctly and filled?

The problem lies in drawing concave polygons.

When using GL_TRIANGLE_STRIP instead of GL_TRIANGLE_FAN, the polygons are drawn, correctly, but only their points are connected together by triangles, the polygon itself is not filled.

like image 571
Philipp Jahoda Avatar asked Jan 08 '14 22:01

Philipp Jahoda


1 Answers

Since you are talking about OpenGL ES, it is much the same as the iOS issue here: OpenGL ES - How to Draw a filled Polygon?

In the above post, I've listed and tested 5 3rd party triangulation libs, but none of them is satisfying.

To conclude:

  1. OpenGL ES doesn't have triangulation, thus does not support drawing filled concave polygon (not to mention polygon with holes...)
  2. To Stick to OpenGL ES drawing, you have to find a 3rd party lib for triangulation (which is not easy...)
  3. Use graphics.Paint(Android) / CoreGraphics(iOS) to draw filled path, but the performance is low

I've been looking for a light-weight and free triangulation lib for quite a long time, but...

like image 132
Dong Ma Avatar answered Jan 02 '23 10:01

Dong Ma