我想通过在固定管道中使用多纹理功能来混合多达 4 个纹理。我可以成功使用 2 个纹理单元,但是当我使用超过两个纹理单元时,会出现奇怪的伪影(某些纹理丢失或纹理坐标错误)。我不知道我做错了什么。
代码非常简单:
glGetIntegerv(GL_MAX_TEXTURE_UNITS, &TextureUnits);
if (TextureUnits > 4)
TextureUnits = 4;
// set textures parameters: texCoords and multitexturing combination
for (int j = 0; j < TextureUnits; ++j)
{
glActiveTexture(GL_TEXTURE0 + j);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
if (j == 0)
{
glEnable(GL_TEXTURE_2D);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
else
{
// Interpolate :
// Arg0 * Arg2 + Arg1 * (1 - Arg2)
// Set arguments to obtain the fallowing blending equation:
// SRCrgb * SRCalpha + DSTrgb * (1 - SRCalpha)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_INTERPOLATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
}
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0 + j);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &Mesh->Vertices[0].TexCoords);
}
...
for (int i = 0; i < TextureUnits; ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glDisable(GL_TEXTURE_2D);
}
// use multitexturing and mulipasses when there are needed.
count = ; // number of textures to blend It could be from 1 to 4.
do
{
tex = 0;
while (index < count && tex != TextureUnits)
{
texture = textures[index];
glActiveTexture(GL_TEXTURE0 + tex);
glEnable(GL_TEXTURE_2D);
setTextureMatrix(texture);
glBindTexture(GL_TEXTURE_2D, texture->getGLTexture());
index++;
tex++;
}
if (tex < TextureUnits)
{
glActiveTexture(GL_TEXTURE0 + tex);
glDisable(GL_TEXTURE_2D);
}
glDrawElements(...);
} while (index < count);