2011-08-09 49 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繪製特定的網格,並將其視爲平截頭體。唯一讓我困惑的是我將用於索引。我有我的巨大頂點數組,我的起點頂點爲網格的頂點,不知道索引適合在哪裏。謝謝。 –