c++ - 将三角形带转换为三角形?

标签 c++ c opengl

我正在使用 GPC 曲面 segmentation 库,它输出三角形 strip 。 该示例显示如下呈现:

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();
}

问题在于这会渲染多个三角形 strip 。这对我来说是个问题。我的应用程序使用 VBO 渲染,特别是 1 个多边形的 1 个 VBO。我需要一种方法来修改上面的代码,使其看起来更像这样:

glBegin(GL_TRIANGLES);
for (s = 0; s < tri.num_strips; s++)
{
    // How should I specify vertices here?      
}
glEnd();

我该怎么做?

最佳答案

perticularly 1 VBO for 1 polygon

哇哦。每个多边形 1 个 VBO 效率不高。杀死顶点缓冲区的全部原因。顶点缓冲区的想法是将尽可能多的顶点塞入其中。您可以将多个三角形带放入一个顶点缓冲区,或渲染存储在一个缓冲区中的单独图元。

I need a way to modify the above code so that instead it could look something more like this:

这应该有效:

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();

因为 trianglestrip 三角剖分是这样的(数字代表顶点索引):

0----2
|   /|
|  / |
| /  |
|/   |
1----3

注意:我假设三角形中的顶点存储顺序与我的图片中的顺序相同,并且您希望三角形顶点按逆时针顺序发送。如果您希望它们是 CW,则使用不同的代码:

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();

关于c++ - 将三角形带转换为三角形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3485034/

相关文章:

opengl - 为什么统一变量没有等效的 glBindAttribLocation() ?

c++ - 找不到时生成默认值

C++ 检查项目是否在数组中

c - 如何避免C变量的多重定义?

c - C 语言摩尔斯电码转换器

opengl - OpenGL 3.1 中已弃用 glLineStipple

c++ - UINT_MAX 宏的定义

c++ - 浮点类型表示

c - 使用 Ruby C API 在模块中定义类

c - 如何在 openGL 中使用 glTranslatef、glScalef、glRotatef