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")
Code:
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:
Code:
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.