开发者

Multitexturing is darkening the entire scene!

I've implemen开发者_开发百科ted 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?


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


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
.....


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜