basic texture loading in glut

excuses for just how basic this is, i’m currently just learning openGL at university and it turns out that there’s not enough time for them to teach us texturing

i’ve looked around and i think i’d be comfortable using a texture but i’m really unclear as to how you load one…i’m using glut because our programs have to be platform independant

i think i have to make a BMPLoad or TGALoad function but what are the advantages of the two file formats? and can anyone point me in the direction of how to load a file and store it for use as a texture? (without using auxDIBImageLoad)

thanks in advance

nehe has a tutorial on loading tga files nehe.gamedev.net

I’ve used the Image Format Library for Windows and IRIX. I haven’t found a version for Linux yet. IFL supports many formats (e.g. TIFF, GIF, PNG, JFIF(JPEG), SGI, BMP, raw formats, etc.). See http://www.sgi.com/products/evaluation/pc_ifl_1.3.1/.

I’ve also written a cross-plaform image loader for SGI formats (e.g. INT, INTA, RGB, RGBA). I’ll send it to you with example if you email me at brcain@yahoo.com (remove *).

I’ve also wondered if there are GIMP libraries available. But, I haven’t looked into it yet.

Bitmap and Targa images are almost identical in the way they store data. The only advantage between them is Targa images can carry with it an additional alpha channel, which allows for some neat blending effects. Anyway, the idea for loading them is:

  1. open the file
  2. load important data from the file (height, width, bpp, etc.)
  3. skip to the data (always at the end of the header info)
  4. copy the data to memory
  5. switch red and blue values
  6. use data as a texture

Step 5 will give you headaches until you really understand it. Bitmap and Targa files store pixel data in the sequence BGR or BGRA (blue, green, red, alpha), but OpenGL wants the data in the form RGB or RGBA (red, green, blue, alpha.) There are two ways around this: you can convert it yourself, or tell OpenGL to. The latter is less reliable since some systems will not have extension necessary to understand BGR and BRGA formats. Also, be wary of different pixel formats. For example, 15 bit color stores a pixel as one short value, where the highest bit is never used, and the lower bits are blue, green, and red (5 bits each). Using this format will only work on little endian systems. On big endian systems, the short will come in like this: gggrrrrr 0bbbbbgg, where each letter represents a bit of the color. You’ll have to swap the dat around to reach 0bbbbbgg gggrrrrr format. This is becoming really complicated, but if you need platform independance, it’s unfortunately necessary.

Even with all of these ideas, it’s not hard to write a bitmap and/or targa file loader:

  1. check endian-ness:
    in C:

#define LITTLE_ENDIAN 0
#define BIG_ENDIAN 1
int GetEndian()
{
unsigned long value=0x12345678;
unsigned char *ptr =(char *)&value;
if (ptr[0]==0x12) return BIG_ENDIAN;
return LITTLE_ENDIAN;
}

  1. load file
  2. get important information (include palette for 8 bit files)
  3. allocate necessary memory
  4. load one pixel worth of data:
    8 bit color = 1 char
    15 bit color = 1 short*
    16 bit color = 1 short*
    24 bit color = 3 chars
    32 bit color = 4 chars OR 1 long*
  5. if data type is order specific (*), and system is big endian, swap byte order
  6. swap red and blue values (can be tricky in 15/16 bit formats)
  7. store pixel in data
  8. repeat from step 4 until end of file or end of allocated memory reached
  9. close file
  10. use data as texture

That’s the only way I know of to load an image file AND maintaining platform independence.

i read nehe, it was kinda my main source of information but it uses auxDIBImageLoad which has apparently vanished from glaux

thanks for the link brcain, the thing is that i don’t want to rely on libraries or APIs to do things for me…sorry if i sound arsey but i’d prefer to do it all myself…the work will eventually end up in coursework and they generally frown on using anything more than glut…it is appreciated though, im downloading it and will have a good look through it in a bit

ok, i read what you wrote Nychold about five times…i think i get the concept now (i’m a bit slow)…it makes sense and is hugely appreciated, thanks…i’ll go and read up on file formats, since i’ll be making the textures i guess i can use 24bpp and get basic functionality for the time being

thanks to all, this is a great help

Nehe also has a TGA loading routine which is not part of any library. Not sure the lesson number but it is in the 30’s.

Also you can go a google search on the TGA file format, and write your own routine to load a TGA or BMP.

I have a glut example with a TGA file loader, for loading a texture on my website, www.angelfire.com/linux/nexusone/

[This message has been edited by nexusone (edited 02-19-2004).]

ah sweet…thank you, i just got a file format book from the library so i’m all set i think