在OpenGL中渲染网格多边形 - 非常缓慢

3
我最近从中级模式转换并采用了新的渲染流程。肯定有些我没懂的地方,我认为这与索引有关。
这是我的图示:区域->网格->多边形数组->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。 - Satchmo Brown
2
将网格数据排列好,以便您可以在一个glDrawElements调用中绘制所有相同纹理的多边形。因此,您不需要循环每个多边形,而是循环类似于meshptr->textureCount的东西,其中您已经存储了该纹理的索引和纹理ID。并且您只需要在循环外或初始化时启用gl_texture_2d一次即可。另外,如果您控制网格内容,请尝试为整个网格(或几个网格)使用单个纹理。 - Ville Krumlinde
1个回答

8

如果您一次只渲染3个顶点,那么使用数组渲染是没有意义的。这个想法是通过单个调用发送数千个顶点。也就是说,您可以通过一个调用来渲染单个"多边形数组"或"网格"。


好的。所以,我想要渲染网格中的每个顶点(数百个多边形)。那么我应该如何修改我的代码?我应该使用哪种渲染方法?我可以获取顶点数组中的起始索引,然后获取该网格的最后一个多边形索引并渲染该范围。 - Satchmo Brown
如果对于每个网格,我都保留一个整数来表示渲染过程中的第一个顶点和最后一个顶点,那么只要该特定网格在视锥体内,我就可以使用glDrawRangeElements仅绘制该网格。唯一让我困惑的是我应该使用什么作为索引。我有一个巨大的顶点数组,我的起始顶点和结束顶点用于该网格,但不确定索引应该放在哪里。谢谢。 - Satchmo Brown
一种方法是改变你的数据结构以适应在所谓的“子网格”中存储多边形-每个材料的情况。然后,您只需将单个子网格的所有多边形放入glDrawElements调用中即可。顺便说一句,在考虑到您当前代码的状态时,您真的确定手动视锥体剔除是一个好事吗? - Paul-Jan
@Satchmo 在这种情况下,您必须保留网格的第一个和最后一个索引的整数值(而不是顶点),因为索引是指定绘制内容的关键。glDrawRangeElementsfirstlast 参数只是提示,并不限制实际绘制的内容。 - Christian Rau

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