多重纹理使整个场景变暗!
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要禁用所有纹理单元,以便所有纹理都不会影响场景的其余部分。从代码的外观来看,您可能只是忘记跟踪您的状态。
添加
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
如果您不将纹理单元 1 用于其他四边形,请尝试禁用它:
Try disabling texture unit 1 if you aren't using it for your other quads:
您可以根据每个纹理单元是否需要它来启用和禁用每个帧的 GL_TEXTURE_2D (就像其他答案所建议的那样)。或者,您可以取消绑定不需要的纹理,这应该要快得多:
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: