每个多边形使用1个VBO会非常低效。这样做就没有使用顶点缓冲区的意义了。使用顶点缓冲区的目的是尽可能多地将顶点装入其中。您可以在一个顶点缓冲区中放置多个三角形条带,或渲染存储在同一缓冲区中的单独基元。
我需要修改上述代码以使其类似于以下代码:
这应该可以正常工作:
glBegin(GL_TRIANGLES);
for (v= 0; v < tri.strip[s].num_vertices-2; v++)
if (v & 1){
glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
}
else{
glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
}
glEnd();
由于三角形带剖分是按照以下方式进行的(数字代表顶点索引):
0----2
| /|
| / |
| / |
|/ |
1----3
注意:我假设三角带中的顶点按照我的图片中的顺序存储,并且您希望三角形顶点被按逆时针顺序发送。如果您希望它们按顺时针顺序排列,则使用不同的代码:
glBegin(GL_TRIANGLES);
for (v= 0; v < tri.strip[s].num_vertices-2; v++)
if (v & 1){
glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
}
else{
glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y);
glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y);
}
glEnd();