【发布时间】: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