# 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 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 ->
[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 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 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 now closed to further replies.

• ### Forum Statistics

• Total Topics
627704
• Total Posts
2978715
• ### Similar Content

• A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

-What I'm using:
C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.
-Questions
Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• 21
• 14
• 12
• 10
• 12