OpenGL Texture mixed with previous colors

This topic is 1432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

My previous colors are mysteriously being mixed together with my texture. I first use glColor4f to produce a green and red (alpha) colors for some quads. Then i make a quad that holds just my texture. And from this, somehow the green from drawing previous quads has leaked onto the texture ?
I found two options, none of which work:
#1
Use
Code:
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); This successfully removes the leaked colors onto the textures, but now the textures fails to work with basic OGL lighting.

#2
Using no glTexEnvi (Which is what OpenGL FAQ suggested i do in this situation)
This leaks the previous colors onto the texture, but now successfully works with basic OGL lighting.

Problem: I can't get the texture to work with the lighting, without the mixup of colors. Edited by Shawn619

Share on other sites
What about glColor3f(1.0f, 1.0f, 1.0f) before rendering the texture then?

Share on other sites
Ah that works! Much appreciated.

Can somebody tell me how the end-color of a fragment actually gets calculated in OpenGL?

Is it ->
 Final color = glColor*texture*lighting; 
?

If so, is that why multiplying the glColor white (all 1's) has no effect on final color? Edited by Shawn619

Share on other sites
It depends on how the combiners are set up. Assuming only the basic combiners and nothing fancy, you have the following common setups:

• GL_REPLACE: Then final color = texture.
• GL_MODULATE: Then final color = primary color*texture.

The primary color depends on whether lighting is enabled or disabled:

• With lighting enabled: primary color = the color from the lighting equation based on light and material colors.
• With lighting disabled: primary color = glColor.

Share on other sites
Ah ok that makes sense. Thank you all ! Edited by Shawn619

Share on other sites
When You have enabled glEnable(GL_COLOR_MATERIAL) and changed a color by glColor, then all your textures will be affected by this color. This is because by default the texture is displayed in modulate mode (glColor multiplied by texture colors).

If you don't have lighting enabled, to restore the original texture color is simple - just set glColor to white: glColor4f(1.0, 1.0, 1.0, 1.0).
The problem comes, when you have lighting enabled on your texture. Then, setting the color to white or changing texture mode to REPLACE doesn't help at all - your lighting effects will be removed! (which nobody seems to be noticing!) The reason for this is because with enabling GL_COLOR_MATERIAL by default you're getting behaviour, where glColor commands changes both Ambient and Diffuse colours at the same time - thus your ambient and diffuse material properties will be affected (lost). So all you have to do, to restore the material state (and thus lighting effects), which you had before applying glEnable(GL_COLOR_MATERIAL), is the following:

glDisable(GL_COLOR_MATERIAL); //disable color influence
GLfloat ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; //default material has this ambient color!
GLfloat diffuse[] = { 0.8f ,0.8f ,0.8f, 1.0f }; //default material has this diffuse color!
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); //restore default material ambient color
glMaterialfv(GL_FRONT, GL_AMBIENT, diffuse); //restore default material diffuse color

Please note, what are the default ambient and diffuse colors for default material! There is no pure white there!

This way, all the textures, that you use from this point will be drawn as intended (with the correct color and lighting effects).
Took me some time to find this stuff, so I suppose it's nice to mention it here.

Share on other sites

Two years and counting; leave the old threads to rest in peace. Closing.

Share on other sites

This topic is 1432 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632630
• Total Posts
3007521
• Who's Online (See full list)

There are no registered users currently online

×