用于 OpenGL ES 的多边形三角剖分为三角形带
我正在寻找一种快速多边形三角剖分算法,可以将不是很复杂的2D凹多边形(无孔)三角剖分成三角形条,准备发送到OpenGL ES以使用<代码>GL_TRIANGLE_STRIP。
我知道一些算法,但找不到适合我需要的算法:
- http:// www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
- 这个算法工作正常,但问题是它返回简单的三角形,您无法使用
GL_TRIANGLE_STRIP
绘制这些三角形,您需要使用GL_TRIANGLES
,这在大量的顶点。
- 这个算法工作正常,但问题是它返回简单的三角形,您无法使用
- http://code.google.com/p/iphone-glu/
- 它没有任何关联的示例,而且我找不到任何人在 iOS 上通过 OpenGL ES 2.0 成功使用它
- 我不知道它返回什么,而且它似乎还调用了我不想要的相应 OpenGL 命令 - 我只需要返回三角形
- 内存泄漏
我正在开发的平台是:iOS,OpenGL ES 2.0,cocos2d 2.0。
谁能帮我解决这样的算法吗?或者任何其他建议将不胜感激。
I am looking for a fast polygon triangulation algorithm that can triangulate not very complex 2D concave polygons (without holes) into triangle strips ready to be sent to OpenGL ES for drawing using GL_TRIANGLE_STRIP
.
I am aware of some algorithms but I couldn't find one that will fit my needs:
- http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
- this algorithm works ok but the problem is it returns simple triangles which you can't draw with
GL_TRIANGLE_STRIP
, you need to useGL_TRIANGLES
which isn't very efficient on a large number of vertices.
- this algorithm works ok but the problem is it returns simple triangles which you can't draw with
- http://code.google.com/p/iphone-glu/
- it doesn't have any example associated and I couldn't find anyone that has successfully used it on iOS with OpenGL ES 2.0
- I don't know what it returns and it seems like it also calls the corresponding OpenGL commands which I don't want - I only need the triangles back
- it leaks memory
The platform I am developing for is: iOS, OpenGL ES 2.0, cocos2d 2.0.
Can anyone help me with such an algorithm? Or any other advice will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在二维且没有孔的情况下,这相当容易。首先,您需要将多边形分解为一个或多个单调多边形。
单调多边形很容易变成三条带,只需按 y 对值进行排序,找到最顶部和最底部的顶点,然后就可以得到右侧和左侧的顶点列表(因为顶点以某种定义的顺序出现,例如顺时针顺序)。然后从最顶部的顶点开始,并以交替的方式从左侧和右侧添加顶点。
该技术适用于任何没有自相交边的二维多边形,其中包括一些带有孔的多边形的情况(但孔必须正确缠绕)。
您可以尝试使用此代码:
此代码不是最佳的,但它应该很容易理解。首先,创建一个凹多边形。然后创建顶点的“工作集”。在该工作集上,计算排列,根据顶点的 y 坐标对顶点进行排序。然后循环该排列,寻找分裂点。一旦找到分割点,就会创建一个新的单调多边形。然后,从工作集中删除新多边形中使用的顶点,并重复整个过程。最后,工作集包含最后一个无法分割的多边形。最后,渲染单调多边形以及三角形条带排序。它有点混乱,但我相信您会弄清楚的(这是 C++ 代码,只需将其放入 GLUT 窗口中,看看它会做什么)。
希望这有帮助...
In 2D and without holes, this is fairly easy. First, you need to break your polygon to one or more monotone polygons.
The monotone polygons are pretty simple to turn into tristrips, just sort the values by
y
, find the top-most and bottom-most vertex, and then you have lists of vertices to the right and to the left (because vertices come in some defined, say clockwise, order). Then you start with top-most vertex and add vertices from the left and from the right sides in alternating manner.This technique will work for any 2D polygons without self-intersecting edges, which includes some cases of polygons with holes (the holes must be correctly wound though).
You can try and play with this code:
This code is not optimal, but it should be easy to understand. At the beginnig, a concave polygon is created. Then a "working set" of vertices is created. On that working set, a permutation is calculated which sorts the vertices by their
y
coordinate. That permutation is then looped through, looking for split points. Once a split point is found, a new monotone polygon is created. Then, the vertices used in the new polygon are removed from the working set and the whole process repeats. Finally, the working set contains the last polygon which could not be split. At the end, the monotone polygons are rendered, along with triangle strip ordering. It is a little bit messy, but I'm sure you will figure it out (this is C++ code, just put it inside a GLUT window and see what it does).Hope this helps ...
您可以从 OpenGL 示例实现中提取曲面细分算法,如本文所述 http://choruscode.blogspot.de/2013/03/extracting-tessselation-from-opengl.html,它也有一个例子。
You can extract tesselation algorithm from OpenGL Sample Implementation, like described in this post http://choruscode.blogspot.de/2013/03/extracting-tesselation-from-opengl.html , it also has an example.