多重纹理问题
我想通过使用固定的多重纹理功能来混合最多 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);
I would like to blend up to 4 textures by using multitexturing feature in fixed
pipe line. I can successful use 2 texture units but when I use more then two
texture units there strange artifact occurred (some texture are missing or
wrong texture coords). I don't know what I am doing wrong.
The code is very simple:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
之后:
TextureUnits 的值是多少?我认为是2。
after this:
what is the value of TextureUnits? I think it is 2.