Sign in to follow this  
Shawn619

OpenGL Texture mixed with previous colors

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:
[u][b]#1[/b][/u]
Use
Code:
[code]glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);[/code] This successfully removes the leaked colors onto the textures, but now the textures fails to work with basic OGL lighting.
[img]http://oi46.tinypic.com/2u7q7w3.jpg[/img]

[u][b]#2[/b][/u]
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.
[img]http://oi48.tinypic.com/44704.jpg[/img]

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

Share this post


Link to post
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 ->
[code]
Final color = glColor*texture*lighting;
[/code]
?

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

Share this post


Link to post
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:[list]
[*]GL_REPLACE: Then [i]final color = texture[/i].
[*]GL_MODULATE: Then [i]final color = primary color*texture[/i].
[/list]
The primary color depends on whether lighting is enabled or disabled:[list]
[*]With lighting enabled: [i]primary color = the color from the lighting equation based on light and material colors[/i].
[*]With lighting disabled: [i]primary color = glColor[/i].
[/list]

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this