三角剖分一切并将三角形扔进一个大的VA/VBO。
编辑:GLUtesselator包装器:
struct TessContext
{
~TessContext()
{
for( size_t i = 0; i < combined.size(); ++i )
{
delete[] combined[i];
}
}
vector< Eigen::Vector2d > pts;
vector< GLdouble* > combined;
};
#define APIENTRY __stdcall
void APIENTRY tess_begin( GLenum type ) {}
void APIENTRY tess_edgeFlag( GLboolean flag ) {}
void APIENTRY tess_end() {}
void APIENTRY tess_vertex( void *data, TessContext* ctx )
{
GLdouble* coord = (GLdouble*)data;
ctx->pts.push_back( Eigen::Vector2d( coord[0], coord[1] ) );
}
void APIENTRY tess_combine( GLdouble coords[3], void *vertex_data[4], GLfloat weight[4], void **outData, TessContext* ctx )
{
GLdouble* newVert = new GLdouble[3];
ctx->combined.push_back( newVert );
newVert[0] = coords[0];
newVert[1] = coords[1];
newVert[2] = coords[2];
*outData = newVert;
}
template< typename Vec >
vector< Vec > Triangulate
(
const vector< Vec >& aSimplePolygon
)
{
vector< GLdouble > coords;
for( size_t i = 0; i < aSimplePolygon.size(); ++i )
{
coords.push_back( aSimplePolygon[i].x() );
coords.push_back( aSimplePolygon[i].y() );
coords.push_back( 0 );
}
GLUtesselator* tess = gluNewTess();
gluTessCallback( tess, GLU_TESS_BEGIN, (GLvoid (APIENTRY *)()) tess_begin );
gluTessCallback( tess, GLU_TESS_EDGE_FLAG, (GLvoid (APIENTRY *)()) tess_edgeFlag );
gluTessCallback( tess, GLU_TESS_VERTEX_DATA, (GLvoid (APIENTRY *)()) tess_vertex );
gluTessCallback( tess, GLU_TESS_END, (GLvoid (APIENTRY *)()) tess_end );
gluTessCallback( tess, GLU_TESS_COMBINE_DATA, (GLvoid (APIENTRY *)()) tess_combine );
gluTessNormal( tess, 0.0, 0.0, 1.0 );
TessContext ctx;
gluTessBeginPolygon( tess, &ctx );
gluTessBeginContour( tess );
for( size_t i = 0; i < aSimplePolygon.size(); ++i )
{
gluTessVertex( tess, &coords[i*3], &coords[i*3] );
}
gluTessEndContour( tess );
gluTessEndPolygon( tess );
gluDeleteTess(tess);
vector< Vec > ret( ctx.pts.size() );
for( size_t i = 0; i < ret.size(); ++i )
{
ret[i].x() = ctx.pts[i].x();
ret[i].y() = ctx.pts[i].y();
}
return ret;
}
使用 Eigen,但没有用于任何有趣的事情。