多重纹理使整个场景变暗!

发布于 2024-11-17 22:14:59 字数 1562 浏览 5 评论 0原文

我已经使用 3 个四边形 A、B 和 C 实现了一个场景。四边形 C 位于四边形 B 后面。四边形 B 位于四边形 A 之上。

除了四边形 B 之外,所有四边形都使用相同的纹理。四边形 B 使使用 alpha 贴图和所有其他四边形正在使用的基础纹理。

下面的代码示例是我如何使用固定功能多重纹理绘制 Quad B。

GLuint alphaGLTexture; 
SDL_SurfaceToGLTexture(aTexture, &alphaGLTexture);

GLuint baseGLTexture;
SDL_SurfaceToGLTexture(baseTexture, &baseGLTexture);

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, alphaGLTexture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, baseGLTexture); 

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

glTranslatef(0.0f, 0.0f, 0.0f);

double u_coord = 0.0f, v_coord = 0.0f;

// Renders Quad B
glActiveTexture(GL_TEXTURE0)
glBegin(GL_QUADS);

// remainder of code modifies u_coord, v_coord and draws the other glVertex3f's.
glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(posax, posay, posaz);

当我将多纹理 alpha 贴图应用到 Quad B 时,纹理四边形 A 和 C 会改变颜色,并且最终比 Quad B 更暗。

当我删除对 GL_TEXTURE1 的任何提及时,一切都渲染得很好,没有变暗,尽管我丢失了 Quad B 上的 alpha 映射 有什么

建议或提示吗?我的 glTexEnvi 调用有问题吗?

I've implemented a scene using 3 quads, A, B, and C. Quad C is behind quad B. Quad B sits on top of Quad A.

All quads are using the same texture with the exception of Quad B. Quad B makes use of an alpha map and the base texture all the other Quads are using.

The code sample below is how I'm drawing Quad B with fixed function multi-texturing.

GLuint alphaGLTexture; 
SDL_SurfaceToGLTexture(aTexture, &alphaGLTexture);

GLuint baseGLTexture;
SDL_SurfaceToGLTexture(baseTexture, &baseGLTexture);

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, alphaGLTexture);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);

glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, baseGLTexture); 

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE);

glTranslatef(0.0f, 0.0f, 0.0f);

double u_coord = 0.0f, v_coord = 0.0f;

// Renders Quad B
glActiveTexture(GL_TEXTURE0)
glBegin(GL_QUADS);

// remainder of code modifies u_coord, v_coord and draws the other glVertex3f's.
glMultiTexCoord2f(GL_TEXTURE0, u_coord, v_coord);
glMultiTexCoord2f(GL_TEXTURE1, u_coord, v_coord);
glVertex3f(posax, posay, posaz);

When I apply a multitextured alpha map to Quad B, the textured Quads A and C change color and end up darker than Quad B.

When I remove any mention to GL_TEXTURE1 everything renders nicely, no dimming, though I lose my alpha mapping on Quad B.

Any recommendations or tips? Is there something wrong with my glTexEnvi calls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-11-24 22:14:59

您需要禁用所有纹理单元,以便所有纹理都不会影响场景的其余部分。从代码的外观来看,您可能只是忘记跟踪您的状态。

添加

glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D); // or whatever texture target used

glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D); // or whatever texture target used

glActiveTexture(GL_TEXTURE…);
glDisable(GL_TEXTURE_2D); // or whatever texture target used

You need to disable all of the texture units so that none of the textures influences the rest of the scene. By the looks of your code you probably just forgot to keep track of your states.

Add

glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D); // or whatever texture target used

glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D); // or whatever texture target used

glActiveTexture(GL_TEXTURE…);
glDisable(GL_TEXTURE_2D); // or whatever texture target used
橘虞初梦 2024-11-24 22:14:59

如果您不将纹理单元 1 用于其他四边形,请尝试禁用它:

// draw Quad B
....

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);

// draw Quad A
.....

// draw Quad C
.....

Try disabling texture unit 1 if you aren't using it for your other quads:

// draw Quad B
....

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);

// draw Quad A
.....

// draw Quad C
.....
伤感在游骋 2024-11-24 22:14:59

您可以根据每个纹理单元是否需要它来启用和禁用每个帧的 GL_TEXTURE_2D (就像其他答案所建议的那样)。或者,您可以取消绑定不需要的纹理,这应该要快得多:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE0); //make sure to set the active texture unit back to 0

You could enable and disable GL_TEXTURE_2D each frame depending on whether each texture unit requires it (like other answers have suggested). Alternatively, you could just unbind the textures that you don't need, which should be much faster:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,0);

glActiveTexture(GL_TEXTURE0); //make sure to set the active texture unit back to 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文