TGA Loading Problems

I wrote my TGA engine based off of nehe’s lesson. I also have a BSP engine. So I extracted the Quake III pk3 files and had it read a BSP. When ever it reads a BSP it reads most of the images but a few of the TGA’s will not load with my TGA code. The error is it cannt find out if its an uncompressed or compressed True Color TGA. The type of TGA is an uncompressed True Color TGA, which my TGA engine supports. nehe’s code reads it but it looks like when you get a bad singal on your TV(black and white with lines).

Here is my code and some of the images it couldnt load. They loaded fine in GIMP.

Thanks
Nuke

I don’t have time at the moment to look at your source in detail but a quick glance reveals that your tga file header is wrong.

First of all, use:
#pragma pack(push,1)
typedef struct
{

} TGA;
#pragma pack(pop)

Otherwise you will have alignment problems.

Secondly, the TGA file format can be found on the web. The file name I have is TGA_FFS.RTF A quick search found this copy: http://honors.tntech.edu/~will/fileformat.virtualave.net/graphics/tga_ffs/tga_ffs.rtf

It goes over the file format quite nicely and (in my opinion) alot quicker for you to read that than for me to post a bunch of corrections. It covers the TGA Version 2.0 specs so you could later expand your reader if you want to.

Originally posted by JotDot:
#pragma pack(push,1)

There you go, you do learn something every day. That explains why the packing in my TGA header was screwy when I was trying to write it out as a single block. Thx JotDot…

I just stuffed the images you provided the link to through the TGA code I use and they worked OK. The code I use came from Texture Mapping Part 4

The easiest way to represent a TGA header is as an array of 9 unsigned shorts. short[6] is width, short[7] is height; you can mask out the other values too from the different shorts if you care.

It seems it will just be easyer to just redo the code based off the gametutorials code.

…or the GLFW TGA loader…

Anyway. My advise is to NEVER load/save structs, or arrays of elements other than 8 bits. You will almost always get problems with compiler/architecture dependencies such as alginment and endianity. I usually do it the “hard” way by writing dedicated routines for reading structures. In GLFW this is what I do:

    // Read TGA file header from file
    fread( buf, 18, 1, f );

    // Interpret header (endian independent parsing)
    h->idlen         = (int) buf[0];
    h->cmaptype      = (int) buf[1];
    h->imagetype     = (int) buf[2];
    h->cmapfirstidx  = (int) buf[3] | (((int) buf[4]) << 8);
    h->cmaplen       = (int) buf[5] | (((int) buf[6]) << 8);
    h->cmapentrysize = (int) buf[7];
    h->xorigin       = (int) buf[8] | (((int) buf[9]) << 8);
    h->yorigin       = (int) buf[10] | (((int) buf[11]) << 8);
    h->width         = (int) buf[12] | (((int) buf[13]) << 8);
    h->height        = (int) buf[14] | (((int) buf[15]) << 8);
    h->bitsperpixel  = (int) buf[16];
    h->imageinfo     = (int) buf[17];

    // Extract alphabits and origin information
    h->_alphabits = (int) (h->imageinfo & _TGA_IMAGEINFO_ALPHA_MASK) >>
                     _TGA_IMAGEINFO_ALPHA_SHIFT;
    h->_origin    = (int) (h->imageinfo & _TGA_IMAGEINFO_ORIGIN_MASK) >>
                     _TGA_IMAGEINFO_ORIGIN_SHIFT;

…at a glance.