PDA

View Full Version : glTexImage2D causes problem to opengl color



blueepl
05-20-2006, 10:45 AM
Hi,
in my project i load a bitmap image and then i put it into a texture using glTexImage2D. After i do this, when i draw something e.x a circle, i use black color it is black, if i try any other color the results are not what they should be. For example if i draw white the output color is dark red (that depends on the colors of the image). This seems unreasonable but maybe you know something i dont.

when the image has only black and white colors i have no problem. The problem occures when i load a coloured image.

The code i use to load the image is not mine, i found it ready but i dont think its the problem cause i tried other codes with the same result.

with this code i make it a texture
ImageLoad("bitmap.bmp", in_image);

glGenTextures(1, &texture[2]);

glBindTexture(GL_TEXTURE_2D, texture[2]);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, 4, in_image->sizeX, in_image->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, in_image->data);

free(in_image->data);
free(in_image);

So if i try to draw a white rectangle like this
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(0.0f,size_winy,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(size_winx,0.0f,0.0f);
glVertex3f(size_winx,size_winy,0.0f);
glEnd();

some other color is drawn(depending on the image).

If you can help me understand why is that and fix it, i'll be very gratefull.
Thanx

05-20-2006, 10:47 AM
Hi,

Try using GL_BGR(A)_EXT as your source format for glTexImage2D.

blueepl
05-20-2006, 11:08 AM
Using this format caused the texture to invert colors. Now the if the image is red it becomes blue. Anyway this didnt solve the problem since now when i draw white it comes out as dark blue

05-20-2006, 11:17 AM
I see. Well, just so you know, bmp images are stored in BGR(A) format, so GL_RGB won't work as an source format for glTexImae2D, unless you byte-swap the data before uploading. This may not be your only problem, but it's probably among them, unless there's more going on behind the scenes.

blueepl
05-20-2006, 12:26 PM
It seems that i just had to add
glBindTexture(GL_TEXTURE_2D,0);
before drawing anything because it binds the texture and draws it with the polygons. I'm not sure why is that since i didnt bind it anywhere but that did the trick:)
Thanks anyway

05-20-2006, 12:40 PM
Always glad to help a citizen.

ZbuffeR
05-20-2006, 03:26 PM
glDisable(Gl_TEXTURE2D)

Serge K
05-26-2006, 08:53 PM
Originally posted by blueepl:
It seems that i just had to add
glBindTexture(GL_TEXTURE_2D,0);
before drawing anything because it binds the texture and draws it with the polygons. I'm not sure why is that since i didnt bind it anywhere but that did the trick:)
Thanks anyway What about this line of your code?

glBindTexture(GL_TEXTURE_2D, texture[2]);

To create the texture you have to bind it...