.TGA files in OpenGL [Archive] - C Board

PDA

View Full Version : .TGA files in OpenGL


...
10-25-2003, 10:30 PM
im trying to get .tga files to display to the screen in OpenGL. i am fairly new to OpenGL, so i might be doing something obviously wrong but not know it.

i first load the files into memory, like this:

(NOTE: this is copied almost exactly from "OpenGL Game Programming")



typedef struct
{
unsigned char imageTypeCode;
short imageWidth;
short imageHeight;
unsigned char bitCount;
unsigned char *imageData;
} TGAFILE;

//...


BOOL LoadTGA( char *fileName, TGAFILE *tgaFile )
{
FILE *filePtr;
UINT imageSize;
int colorMode; // 3 for RGB, 4 for RGBA

// open the file in "read binary" mode
filePtr = fopen( fileName, "rb" );
if ( !filePtr )
return FALSE;

// read first two unnecessary bytes
unsigned char unneeded;
fread( &unneeded, sizeof(unsigned char), 1, filePtr );
fread( &unneeded, sizeof(unsigned char), 1, filePtr );

fread( &tgaFile->imageTypeCode, sizeof(unsigned char), 1, filePtr );

// type code must be 2 or 3
if ( tgaFile->imageTypeCode != 2 && tgaFile->imageTypeCode != 3 )
{
fclose( filePtr );
return FALSE;
}

short unneeded2;
// more unneeded info
fread( &unneeded2, sizeof(short), 1, filePtr );
fread( &unneeded2, sizeof(short), 1, filePtr );
fread( &unneeded, sizeof(unsigned char), 1, filePtr );
fread( &unneeded2, sizeof(short), 1, filePtr );
fread( &unneeded2, sizeof(short), 1, filePtr );

// image width and height
fread( &tgaFile->imageWidth, sizeof(short), 1, filePtr );
fread( &tgaFile->imageHeight, sizeof(short), 1, filePtr );

// bit depth
fread( &tgaFile->bitCount, sizeof(unsigned char), 1, filePtr );

// more unneeded info
fread( &unneeded, sizeof(unsigned char), 1, filePtr );

// for colorMode, 3 = BGR, 4 = BGRA
colorMode = tgaFile->bitCount / 8;
imageSize = tgaFile->imageWidth * tgaFile->imageHeight * colorMode;

// allocate memory for image data
tgaFile->imageData = (unsigned char *) malloc( sizeof(unsigned char) * imageSize );

// read in the image data
fread( tgaFile->imageData, sizeof(unsigned char), imageSize, filePtr );

unsigned char colorSwap;

// change BGR to RGB so that OpenGL can use it
for ( UINT i = 0; i < imageSize; i += colorMode )
{
colorSwap = tgaFile->imageData[i];
tgaFile->imageData[i] = tgaFile->imageData[i + 2];
tgaFile->imageData[i + 2] = colorSwap;
}

fclose( filePtr );

return TRUE;
}




then, inside of my program, i draw the image to the screen like so:


void DrawPic()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );

for ( int i = 0; i < 50; ++i )
{
// draw a tga file to the screen
glRasterPos2i( rand() % 800, rand() % 600 );
glDrawPixels( testfile->imageWidth, testfile->imageHeight,
GL_RGB, GL_UNSIGNED_SHORT, testfile->imageData );
}


glFlush();
SwapBuffers(display.hdc);
}





i have stepped through it in the debugger and it seems to load the file successfully, but it doesnt show anything to the screen.

JaWiB
10-26-2003, 02:35 PM
Can't help with the OpenGL part, but some of the loading code seems odd:


// change BGR to RGB so that OpenGL can use it
for ( UINT i = 0; i < imageSize; i += colorMode )
{
colorSwap = tgaFile->imageData[i];
tgaFile->imageData[i] = tgaFile->imageData[i + 2];
tgaFile->imageData[i + 2] = colorSwap;
}


Which makes sense if this is correct:


// for colorMode, 3 = BGR, 4 = BGRA
colorMode = tgaFile->bitCount / 8;


But all the tga files I've worked with so far use RGB and here:


int colorMode; // 3 for RGB, 4 for RGBA


You also say RGB? I'd have to look at the tga file documentation to make sure thats correct

Another thing is that most image files (as far as I know) are stored bottom up, so if you do get it to display, then it will be upside down :)

And I'm not sure why it uses fread to skip over parts of the file that don't need to be read...why not fseek?

...
10-26-2003, 02:48 PM
like i said, most of that stuff i copied straight from the book.

i tried commenting out the for loop where i swap the colors and it didnt make a difference.