【发布时间】:2011-10-22 01:34:21
【问题描述】:
我正在尝试让我的渲染功能正常工作。我正在使用顶点数组。这是我的顶点结构。
struct Vertex
{
float x, y, z; // The x, y and z floating point values
float u, v; // The u - v texture coordinates
float padding[3]; // needs to be multiple of 32
};
这是我的渲染代码:
// Render the mesh
void WLD::render(GLuint* textures, long curRegion, CFrustum cfrustum)
{
int num = 0;
// Set up my indices
GLuint indicies[3];
// Cycle through the PVS
while(num < regions[curRegion].visibility.size())
{
int i = regions[curRegion].visibility[num];
if(!regions[i].dead && regions[i].meshptr != NULL)
{
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
indicies[0] = regions[i].meshptr->poly[j].vertIndex[0];
indicies[1] = regions[i].meshptr->poly[j].vertIndex[1];
indicies[2] = regions[i].meshptr->poly[j].vertIndex[2];
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &verticies[0].x);
// Texture index
int tex = regions[i].meshptr->poly[j].tex;
// Need to bind this to the polygon I render.
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &verticies[0].u);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, indicies);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
}
num++;
}
}
其中一个参数,GLuint* 纹理包含所有加载的纹理。所以行 int tex = region[i].meshptr->poly[j].tex; 返回的值是这个特定多边形的纹理索引。渲染时如何将其绑定到每个多边形?如果您有任何问题,请告诉我。
我知道我需要使用 glClientActiveTexture() 但一,它说它是未定义的,我找不到正确的标题,二,我不知道它是如何使用的。我找不到任何好的例子。那么,如果说多边形引用纹理索引 4,我该如何在使用 glClientActiveTexture 渲染时将其绑定到多边形。
【问题讨论】:
-
也许一个解决方案是创建一个纹理图集。唯一的问题是获得正确的纹理坐标,但这可以通过类和 XML 在客户端完成。这样你就不用担心切换纹理了。
-
您在滥用顶点数组。顶点数组的想法是,您在单个 glDrawElements 调用中批量处理大量三角形,而不是为每个三角形切换顶点数组指针并让 glDrawElements 只调用一个三角形。也不需要将顶点结构填充为 32 的倍数(无论如何?)。
-
@datenwolf 但在他的情况下,这似乎是必要的,因为他希望每个多边形都具有不同的纹理(尽管还有其他方法)。
-
@Christian Rau:他可以为此 opengl.org/wiki/Array_Texture 使用纹理数组,但我同意纹理图集可能是明智的解决方案。
-
@Moniker:如果您坚持原来的方法,请尝试首先对每个纹理的三角形进行排序,在渲染过程中尽可能少地交换纹理。这是并且一直是一个好主意 (tm)。
标签: c++ opengl vertex-array