我最近從中間模式切換,並有一個新的渲染過程。必須有一些我不瞭解的東西。我認爲這與指數有關。在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和優化的人來幫助我。
我已經做到了,所以它只在調用當前未綁定的紋理時綁定紋理。這增加了大約10-15的FPS。 –
排列網格數據,以便可以在一個glDrawElements調用中繪製具有相同紋理的所有多邊形。因此,不是循環每個多邊形,而是循環一些類似於meshptr-> textureCount的地方,其中存儲了該紋理的索引和紋理ID。你只需要在循環之外或初始化時啓用gl_texture_2d。此外,如果您有對網格內容的控制,請嘗試爲整個網格(或幾個網格)使用單個紋理。 –