Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glColor coloring all textures

I'm fairly new to OpenGL so maybe the answer will be obvious. I am currently trying to make a blue circle using GL_TRIANGLE_FAN in C++. My problem is that when I set the color using glColor4f, it sets all my other textures to have a blue color over them such as shown below (this is supposed to be silvery metal).

I draw the textures using the method shown below.

glLoadIdentity();
glTranslatef(x,y,0);

glBindTexture(GL_TEXTURE_2D, this->texture); 

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0,0,0);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(width,0,0);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(width,height,0);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(0,height,0);   
glEnd();

I'm not sure whether I just have to clear a flag for it to work, but I've been stuck for a few days now.

like image 745
Silvae Avatar asked Jan 21 '12 21:01

Silvae


3 Answers

Unbind the texture and set the color back to white after you're done drawing:

glLoadIdentity();
glTranslatef(x,y,0);

glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, this->texture); 

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0,0,0);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(width,0,0);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(width,height,0);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(0,height,0);   
glEnd();

glColor4f(1, 1, 1, 1);
glBindTexture(GL_TEXTURE_2D, 0);

And if you aren't rendering textures on the next object, disable texturing:

glDisable( GL_TEXTURE_2D );
like image 99
Robert Rouhani Avatar answered Nov 12 '22 04:11

Robert Rouhani


After drawing your blue circle, you should set the color back to white (default value) using glColor4f(1.f, 1.f, 1.f, 1.f);. Please note that by default the texture gets modulated by the currently set color (blue in your case) and that's the reason why your silver material gets a bluish tone (final color = blue color * texture color).

like image 38
FipS Avatar answered Nov 12 '22 05:11

FipS


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.

like image 1
Airstriker Avatar answered Nov 12 '22 04:11

Airstriker