Texture gets the color of my rectangle

Started by gnorriz, July 08, 2012, 21:25:07

Previous topic - Next topic

gnorriz

Hi

I am new to lwjgl and openGL. I have a little problem with my texture when im coloring my non textured rectangle. The texture is influenced by the color of the rectangle. See picture:

http://imageshack.us/photo/my-images/835/pinktexture.png/

The texture in the upper left corner should have a white background!
I bet it has something to do with the face that openGL is based on "states".

You can find the code here: https://gist.github.com/3072849

Thanks you for your time!
Sincerely Dennis

mattdesl

You've set the colour to be magenta, but then you've never changed it back to white before rendering your texture.

glColor affects all vertices after it. Textures are multiplied by the vertex colour (based on tex env modes), so to render an image without any tinting, you will want to use white as the vertex colour.

glColor4f(1f, 1f, 1f, 1f);
//draw texture

glColor4f(1f, 0f, 1f, 1f);
//draw coloured rectangle


Also, glColor4f expects a normalized float between zero and one. If you want to use unsigned bytes (0-255) then you'd need to use glColor4ub.

gnorriz

Oh, i have to change the color on textures aswell.

I see!

Thx for fast response!

gnorriz

Another thing.

With the same code; if i change the rectangle color to white ("glColor4f(1, 1f, 1f, 1f);") and ignore to render the texture (i keep "glEnable(GL_TEXTURE_2D);") then the rectangle will become grey instead.

If i comment out the "glEnable(GL_TEXTURE_2D);" row. The rectangle will become white...

How come?

Thanks you for your time!
Sincerely Dennis

oskar

If you have the texturing enabled, OpenGL will use the previously given texture coordinate and apply it on your triangle. The grey colour you're talking about is a mix between the texture value and the colour value you supplied. That's why when you disable texturing, OpenGL will only check the colour state to find out what colour the fragment should be.