Texture mixed with previous colors

Started by
5 comments, last by Brother Bob 9 years, 5 months ago
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.
2u7q7w3.jpg

#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.
44704.jpg

Problem: I can't get the texture to work with the lighting, without the mixup of colors.
Advertisement
What about glColor3f(1.0f, 1.0f, 1.0f) before rendering the texture then?
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?
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.
Ah ok that makes sense. Thank you all !
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.

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

This topic is closed to new replies.

Advertisement