【问题标题】:Rendering mesh polygons in OpenGL - very slow在 OpenGL 中渲染网格多边形 - 非常慢
【发布时间】:2011-10-22 22:00:18
【问题描述】:

我最近从中间模式切换并有一个新的渲染过程。一定有什么我不明白的地方。我认为这与指数有关。

这是我的图表:Region->Mesh->Polygon Array->3 个顶点索引,它引用了顶点的主列表。

这是我的渲染代码:

// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{

    int num = 0;

    // Set up rendering states
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // Set up my indices
    GLuint indices[3];

    // Cycle through the PVS
    while(num < regions[curRegion].visibility.size())
    {
        int i = regions[curRegion].visibility[num];

        // Make sure the region is not "dead"
        if(!regions[i].dead && regions[i].meshptr != NULL)
        {
            // Check to see if the mesh is in the frustum
            if(cfrustum.BoxInFrustum(regions[i].meshptr->min[0], regions[i].meshptr->min[2], regions[i].meshptr->min[1], regions[i].meshptr->max[0], regions[i].meshptr->max[2], regions[i].meshptr->max[1]))
            {
                // Cycle through every polygon in the mesh and render it
                for(int j = 0; j < regions[i].meshptr->polygonCount; j++)
                {   
                    // Assign the index for the polygon to the index in the huge vertex array
                    // This I think, is redundant
                    indices[0] = regions[i].meshptr->poly[j].vertIndex[0];
                    indices[1] = regions[i].meshptr->poly[j].vertIndex[1];
                    indices[2] = regions[i].meshptr->poly[j].vertIndex[2];

                    // Enable texturing and bind the appropriate texture
                    glEnable(GL_TEXTURE_2D);
                    glBindTexture(GL_TEXTURE_2D, textures[regions[i].meshptr->poly[j].tex]);

                    glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);

                    glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].u);

                    // Draw
                    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indices);
                }
            }
        }
    num++;
    }

    // End of rendering - disable states
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

对不起,如果我遗漏了什么。我非常感谢反馈和帮助。我什至会考虑付钱给擅长 OpenGL 和优化的人来帮助我解决这个问题。

【问题讨论】:

  • 我已经做到了,所以它只在需要当前未绑定的纹理时才绑定纹理。这将 FPS 提高了大约 10-15。
  • 排列您的网格数据,以便您可以在一次 glDrawElements 调用中绘制相同纹理的所有多边形。因此,不是循环每个多边形,而是循环类似 meshptr->textureCount 之类的东西,其中存储了该纹理的索引和纹理 ID。而且您只需要在循环外或初始化中启用 gl_texture_2d 一次。此外,如果您可以控制网格内容,请尝试对整个网格(或多个网格)使用单个纹理。

标签: c++ performance opengl render


【解决方案1】:

如果您一次只渲染 3 个顶点,则使用数组渲染毫无意义。这个想法是通过一个电话发送数千。也就是说,您只需一次调用即可渲染单个“多边形阵列”或“网格”。

【讨论】:

  • 好的。所以,我想渲染网格中的每个顶点(数百个多边形)。那么我应该如何修改我的代码呢?我应该使用哪种渲染方法?我可以获取顶点数组中的起始索引,然后获取该网格的最后一个多边形索引并渲染范围。
  • 也感谢您的回复。
  • 如果对于每个网格,我要在渲染过程中保留第一个顶点和最后一个顶点的整数。然后,我可以使用 glDrawRangeElements 仅绘制那个特定的网格,假设它在截锥体中。我唯一感到困惑的是我将用于索引的内容。我有巨大的顶点数组,网格的起始顶点和结束顶点,不确定索引适合的位置。谢谢。
  • 一种方法是改变你的数据结构,以适应首先存储多边形每个材料,在所谓的“子网格”中。然后,您只需将单个子网格的所有多边形放入 glDrawElements 调用中。附带说明一下,鉴于您的代码的当前状态,您真的确定您的手动平截头体剔除是一件好事吗?
  • @Satchmo 在这种情况下,您必须为网格的第一个和最后一个索引(而不是顶点)保留整数,因为索引表示绘制的内容。 glDrawRangeElements 的 first 和 last 参数只是提示,并不限制实际绘制的内容。
猜你喜欢
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
相关资源
最近更新 更多