Thread: .TGA files in OpenGL

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    .TGA files in OpenGL

    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.
    Last edited by ...; 10-25-2003 at 10:32 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Can't help with the OpenGL part, but some of the loading code seems odd:

    Code:
    // 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:

    Code:
    // 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:

    Code:
    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?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    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.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. first opengl game, problems.
    By n3v in forum Game Programming
    Replies: 1
    Last Post: 07-11-2006, 08:22 PM
  4. compiler won't find the included opengl files
    By cerin in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2005, 12:52 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM