我正在尝试使我的渲染功能正常工作。我正在使用顶点数组。这是我的顶点结构。
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返回的值tex = region [i] .meshptr-> poly [j] .tex;是此特定多边形在纹理中的索引。渲染时如何将其绑定(bind)到每个多边形?如果您有任何疑问,请告诉我。
我知道我需要使用glClientActiveTexture(),但是其中之一说它是未定义的,而且我找不到正确的 header ,其二,我不知道它是如何使用的。我找不到任何好的例子。因此,如果说多边形引用了纹理索引4,那么在使用glClientActiveTexture进行渲染时,如何将其绑定(bind)到多边形。
最佳答案
您通常无法做到。通常,您不尝试渲染具有多个纹理的单个三角形流(一个glDraw*
调用)。好吧,无论如何,这不是您的意思。
单个绘制调用中的每个三角形必须与该绘制调用中的每个其他三角形都从相同的纹理集中提取。
通用解决方案是将图像合并为单个纹理。这通常称为“纹理图集”。在这种情况下,顶点上的纹理坐标描述了从纹理图集(它是单个纹理)中提取图像数据的位置。
如果您使用的是支持GL 4.0的硬件(D3D11)和GLSL,则可以使用一些技巧,根据任意计算在16个纹理之间动态选择。对于较早的硬件,您可以使用着色器在纹理之间进行选择。但是,这两种情况都比仅适本地批量处理数据要慢。