将三角形条带转换为三角形?

3

我正在使用GPC镶嵌库,它输出三角形带。示例展示的渲染效果如下:

for (s = 0; s < tri.num_strips; s++)
{
    glBegin(GL_TRIANGLE_STRIP);
    for (v = 0; v < tri.strip[s].num_vertices; v++)
        glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y);
    glEnd();
}

问题在于这会渲染多个三角形条带。这对我来说是个问题。我的应用程序使用VBO进行渲染,特别是一个VBO对应一个多边形。我需要一种方法修改上述代码,使其看起来更像这样:
glBegin(GL_TRIANGLES);
for (s = 0; s < tri.num_strips; s++)
{
    // How should I specify vertices here?      
}
glEnd();

我该怎么做呢?

1个回答

11

每个多边形使用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();

我指的是每个多边形使用一个vbo,抱歉。 - jmasterx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接