2011-08-09 48 views
3

我最近从中间模式切换,并有一个新的渲染过程。必须有一些我不了解的东西。我认为这与指数有关。在OpenGL中渲染网格多边形 - 非常慢

这是我的图: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和优化的人来帮助我。

+0

我已经做到了,所以它只在调用当前未绑定的纹理时绑定纹理。这增加了大约10-15的FPS。 –

+2

排列网格数据,以便可以在一个glDrawElements调用中绘制具有相同纹理的所有多边形。因此,不是循环每个多边形,而是循环一些类似于meshptr-> textureCount的地方,其中存储了该纹理的索引和纹理ID。你只需要在循环之外或初始化时启用gl_texture_2d。此外,如果您有对网格内容的控制,请尝试为整个网格(或几个网格)使用单个纹理。 –

回答

8

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

+0

好的。所以,我想渲染网格中的每个顶点(数百个多边形)。那么我应该如何修改我的代码呢?我应该使用哪种渲染方法?我可以在顶点数组中获取起始索引,然后获取该网格的最后一个多边形索引并呈现范围。 –

+0

谢谢您的回复。 –

+0

如果对于每个网格物体,我将在渲染过程中保留第一个顶点和最后一个顶点的整数。然后,我可以使用glDrawRangeElements绘制特定的网格,并将其视为平截头体。唯一让我困惑的是我将用于索引。我有我的巨大顶点数组,我的起点顶点为网格的顶点,不知道索引适合在哪里。谢谢。 –