I can't Texture!

I am having a terrible time trying to learn how to texture map a bmp file to a polygon. Every book I read is like “its simple, we’ll assume you know how to load in the bmp file. Now that that is done, use these wonderful OpenGL functions to map it onto your polygon!”. Wait a second! Load the bmp? Huh? I’m a quasi-good programmer in C++. I’ve been buying and reading several books trying to learn this little technique and cant figure it out. I understand all the GL stuff you can do, but how do I get the bmp file loaded so I can use it??? Please email me if there are some easy to read resources out there on that, or if you have a good explanation. Thanks.

Ace Corbain


You have two options at this point :

1/ you want to load the bmp yourself
1.1/ you want to learn the BMP file format : you’ll find information here http://www.wotsit.org . Once you know the file format, you can load the data into memory to be used as a texture in GL.
1.2/ you don’t care about the format, and want to use one of the wonderful Win32 API functions : LoadImage(). Look through the MSDN and you’ll figure out how it works.

2/ you prefer to use a library to handle this. I personnaly use DevIL (formerly known as OpenIL). It handles a lot of image formats and does just what I need :wink: Have a look here : http://www.imagelib.org

at hene.gamedev.net they have a good tutorial on this. Heres some simple code for doing it. The hard part is loading the damned image file. GLUAX has a nice function for loading bmp’s. It is of course the only reason any why anyone ever uses GLUAX anymore, just for that one function.
glGenTextures(1, &barID); // Create The Texture
glBindTexture(GL_TEXTURE_2D, barID);
gluBuild2DMipmaps ( GL_TEXTURE_2D, GL_RGB, bar->sizeX,
bar->sizeY, GL_RGB, GL_UNSIGNED_BYTE, bar->data);
statusBarObj = glGenLists(1); //generate our new list
glNewList ( statusBarObj, GL_COMPILE ); //compile it
//glMaterialf(GL_FRONT_AND_BACK, GL_AMBIENT, 1.0);
glBindTexture (GL_TEXTURE_2D, barID);
glColor3f (1.0,1.0,1.0);
glBegin ( GL_QUADS );
glVertex3f(1.0,0.5,0.0); glTexCoord2d( 1.0f,0.0f);
glVertex3f(1.0,1.0,0.0); glTexCoord2d( 1.0f,1.0f);
glVertex3f(0.0,1.0,0.0); glTexCoord2d( 0.0f,1.0f);
glTexCoord2d( 0.0f,0.0f);

[This message has been edited by dheitbri (edited 12-07-2001).]

[This message has been edited by dheitbri (edited 12-07-2001).]

Originally posted by dheitbri:
at hene.gamedev.net

He meant http://nehe.gamedev.net

Ok, thanks so far for the info guys. I have been able to confirm that my problem lies in not understanding bmp format and the process of loading it. I’m looking into DevIL and glaux at this point. I’m still not quite sure how to use the texture once it is loaded. For instance, I load an image “bah.bmp” in one of these nifty loading functions, how now do I refer to it with Open GL’s nifty functions? And going into further depth, if I want to use more than one texture in a program, I can only guess I use an array, so I must refer to the textures from the array. Bottom line, is there a variable the functions will set or what? Thanks again for the library referrals. My email is mushka70@aol.com if you have any more info.


Use glbindtexture to swap between textures. say it’s 1D or 2D ( gl_texture_1d or gl_texture_2d) and give the place where you stored your new texture.

Do you have opengl superbible 2e edition?
It explains a lot about opengl ( also for textures ) and I think there will be told you something about “behind textures”,

got it

I know I’m close. I’m probably just overlooking one small detail. If anyone is willing to look at my .cpp file let me know. It is a simple program that just draws a cube and puts NeHe’s bmp on it. The problem I’m getting is that it is all black meaning it hasn’t been loaded properly, but I can’t figure out why. Anyway, if you can spare a few to check out my cpp file, gimme an email and I’ll send it ur way. mushka70@aol.com Thanks for all the help so far guys. Should texturing be THIS hard? I’m starting to doubt my abilities in programming.

Ace Corbain

Did you check texture coordinates?

Also, are you sure your image is 2^n*2^m ?

Is TEXTURE_2D enabled?

Just some thoughts…

Don’t use GLAUX.

Maybe try a glDrawPixels with the image to ensure you have a valid array of data.

On this page there’s a gamma.zip file. It has decent code to load a 24bit BMP image and display it. You could use this code if your image is 24 bit.

OK! Now I got textures working. Here’s what I’m trying to accomplish. I want to create a sky box around an object that I can rotate around with my mouse. I got all the mechanics worked out and everything. One problem is occurring though. My object has material properties associated with it. However, the skybox reacts with the material properties. Why is this? I have the skybox being drawn in its own function. So is there something I can do to make the textures not react with the material properties of the object? Plus, it is really dark in there, even with several lights.

Ace Corbain

openGl is a state machine. what this means is that, if you set the material properties, everything else that gets drawn after that uses those material properties. so, if you do this:


when you draw the skyBox, you havent changed the materials. this holds true even if the glMaterial call is in the drawObject() function. openGl does not know what function you are drawing from, so there is no way for it to set the material back when drawObject() returns (also, there is no “default” material, you must define a material, or weird things happen)

at the begining of the skybox function, set its material properties (you want diffuse to be white, and turn off specular lighting with:

glMaterialf(Gl_FRONT, GL_SHININESS, 0.0);

that should give you the results you are looking for…

Yeah, that’s one of the things that I picked up on when playing with OpenGL from the beginning. I defined a material property for the texture that is all white and it actually took care of the problem of being to dark. Thanks for your help sprial. It still flickers a little, but not as much. It almost looks like my light position is changing when I animate certain things. Oh well, atleast I can see the background now, and its not too dark. Thanks again.

Ace Corbain