Loading Textures

Hi I am just starting to learn OGL and I am
looking into loading textures. Which got me
thinking of something which I can’t seem to
figure out. I can load a texture from a
complete BMP file. (Found some code through NEHE
and it was quite simpel to understand) But I
have noticed that some people keep many texture
in one BMP file, so thinking I understand all I
have read so far, I decided to give it a shot…
ya right easy NOT.

Where can I find documentation on loading a
portion of a BMP file into a texture. What I
would like as a test is to Hold 9 textures in
one BMP file. Lets say the BMP file is 300*300
Pixels. Therefore I would have 9 100X100 images
that can be loaded as textures. Currently I see
creating a C++ object with the entry point
LoadBMPPortion(int TopLeft, int TopRight, int
Width, int height) is this a good approach and
how will this interact with glTexImage2D.

Thanks for your time.

Here is the code I currently use:
CBitmap image; // We’ll use this to load the texture

// First we load the bitmap
// if(image.loadBMP("100a_Chicago_skyline(4X).bmp") == false)
// if(image.loadBMP("100a_Chicago_skyline.bmp") == false)
if(image.loadBMP("Bitmap.bmp") == false)

glGenTextures(1, &g_Texture[0]);

glBindTexture(GL_TEXTURE_2D, g_TextureID);

glTexImage2D(GL_TEXTURE_2D, 0, image.getChannels(), image.getWidth()/4, image.getHeight()/4,
    0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.getLinePtr(0));

gluBuild2DMipmaps(GL_TEXTURE_2D, image.getChannels(), image.getWidth(), 
	image.getHeight(), GL_BGR_EXT, GL_UNSIGNED_BYTE, 


If you want glTexImage2D to pick a portion of an image, you’ll need to call glPixelStorei(GL_UNPACK_ROW_LENGTH, your_image_width). And of course, you’ll have to specify the right address as the last parameter to glTexImage2D, but that’s just generic 2d array stuff.

Or you could just leave them all in one texture and adjust the texture coordinates appropriately.


Thanks for the prompt response, I am just using
this as a learning tool, so yes I only plan to
do 2d (Baby Steps)

I am not sure I understand your reply. Where do
I call this function glPixelStorei(…) my guess
is before, but how will this interact with the
glTexImage2D(…). Can you please elaborate on
your response, and/or if possible provide some
psuedo code maybe by seeing it I’ll understand
better what you mean.

Thanks for your time

Oh One last thing // Second though

I am able to get portions of the BMP in staright C++ code using CreateBitmap(…) and pointing to a porting of the Original BML file. I the have a portion of the BMP file,When I output it straigh using no OGL code the image I get is correct, when I try to point to the Pixel info i seem to only get the first line of pixels. Can I therefore load a texture from a HBITMAP.


Originally posted by Bukwheat:
When I output it straigh using no OGL code the image I get is correct, when I try to point to the Pixel info i seem to only get the first line of pixels. Can I therefore load a texture from a HBITMAP.
I’m not sure what you’re trying to say here. What exactly do you do that causes you to see just the first line in a texture? Also, is the rest of the texture black, or random data, or is it valid pixels but in the wrong places? (If you don’t use glPixelStore, and the width you pass glTexImage is 1/3 the source width, every third line in the texture should be a consecutive line from the correct portion of the source, with the other lines being from portions to the side of the one you want.)

I’m not familiar with any of the classes you’re using, or HBITMAP. I’m just assuming that however you get the image, you have access to a standard row-major 2d array of pixel data.

Thanks for your help, I’ll read up some more to better understand all facets of the image. THe raw data part and th opengl part, the problem may be coming from the image manipulation than OGL itself.

If ever you come up with a suggestion on how this is done it will be welcomed.


And remember to use power-of-two sized textures. So your 300x300 texture should actually be 384x384 (=128x3):

glPixelStorei(GL_UNPACK_ROW_LENGTH, 128);
glTexImage2D(GL_TEXTURE_2D, 0, image.getChannels(), image.getWidth()/3, image.getHeight()/3,
0, GL_BGR_EXT, GL_UNSIGNED_BYTE, image.getLinePtr(0));