View Full Version : help: Texture from files

11-18-2001, 05:56 AM
hey there!!
i'm new to opengl programming. I'm trying to apply textures from bitmap files to objects. I'm a bit confused by the different formats(BMP, PNG, TGA), and how to read the information from these files. I'm coding in C.
Plz help!

11-18-2001, 06:17 AM
NeHe's (http://nehe.gamedev.net) tutorials is what you need.

11-20-2001, 01:03 AM
The quickest hack is to use the TGA file format, and "know" about the image properties beforehand. For instance, if you have a 256x256 RGB (24-bit) image saved as a TGA file, you can load it with the following code (it simply skips the header, which is 18 bytes long):

unsigned char image[ 256*256*3 ];
FILE *f;

f = fopen( "mypic.tga", "rb" );
fseek( f, 18, SEEK_SET );
fread( image, 256*256*3, 1, f );
fclose( f );

Now you can upload it to texture memory with:

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_BGR, GL_UNSIGNED_BYTE, image );

Note: TGA stores the pixels as BGR, that is why we set format = GL_BGR.

Of course, you will want to add error checking and use memory allocation instead of statically declared variables (remember: you can free the memeory used by a texture once it has been uploaded to the graphics card using glTexImage2D).

To learn more about the contents of the 18-byte TGA file header (or any other file format), go to http://www.wotsit.org/

[This message has been edited by marcus256 (edited 11-20-2001).]