使用OpenGL的tesselator:
#include <gl/gl.h>
#include <gl/glu.h>
#include <vector>
using namespace std;
typedef vector< vector< GLdouble* > > contours;
contours poly;
//Initialize poly here
GLUtesselator* tess = gluNewTess();
gluTessCallback(tess, GLU_TESS_BEGIN, (void (CALLBACK*)())&BeginCallback);
gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK*)())&VertexCallback);
gluTessCallback(tess, GLU_TESS_END, (void (CALLBACK*)())&EndCallback);
gluTessCallback(tess, GLU_TESS_COMBINE, (void (CALLBACK*)())&CombineCallback);
gluTessCallback(tess, GLU_TESS_ERROR, (void (CALLBACK*)())&ErrorCallback);
gluTessNormal(tess, 0.0, 0.0, 1.0);
gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);
gluTessProperty(tess, GLU_TESS_BOUNDARY_ONLY, GL_FALSE); //GL_FALSE
gluTessBeginPolygon(tess, NULL);
for (UINT i = 0; i < poly.size(); ++i)
{
gluTessBeginContour(tess);
for (UINT j = 0; j < poly[i].size(); ++j)
{
gluTessVertex(tess, poly[i][j], poly[i][j]);
}
gluTessEndContour(tess);
}
gluTessEndPolygon(tess);
gluDeleteTess(tess); // Delete after tessellation
是用戶將要在如何繪製多邊形的限制?我的意思是他們會被阻止創建一條與現有相交的新線? – Robinson 2012-04-01 14:58:13
實際上線條可以互相交叉......無論如何,它們將被分組爲單獨的三角形。所以我認爲即使它們相交也是可行的。 – 2012-04-01 15:06:22
解決方案3工作得很好,除了無限循環問題:/。我有點爲無限循環做了一個「假」修復,它忽略了引起無限循環的頂點。 – 2012-04-01 15:19:49