TGA fiiles

Started by
10 comments, last by AdamGL 18 years, 10 months ago
Sorry, I just can't seem to get it. I have Beginning OpenGL Game programming the book, but I can't seem to grasp the TGA loading part. Can anyone post a simple bit of code that loads a tga, with no linked lists. I would appreciate it. And if you could explain each part and what it does. I'm a newbie so please don't slap me for asking this. Adam L
bi-FreeSoftware
Advertisement
NeHe has TGA loading tutorial which is fairly detailed. ^^
If you need some TGA loading code I have a nice class that handles everything you need. So if you want it I'll give it to you.

Jake
Sure jake_Ghost, if you wouldn't mind also including descriptions of what certain things do in it that would be great!
bi-FreeSoftware
Here is a good website for a description of the TGA file format:

TGA Format

I also have code that I can post that I used from this site.
My TGA texture loader demo, if it can help you (written in C). It handles a lot of TGA types.
Alight if you guys could help me debug that would be great. What happens is I get a bunch of verical bars. Multicolored bars. Hippi bars. Anyways, I'll post the code. Its long so bear with me please.

tga.h
#include <windows.h>				// Standard Windows header#include <stdio.h>				// Standard Header For File I/O#include <gl\gl.h>				// Standard Header For OpenGL#include <gl\glu.h>typedef struct	{		GLubyte* imageData;			// Hold All The Color Values For The Image.		GLuint  bpp;				// Hold The Number Of Bits Per Pixel.					GLuint width;				// The Width Of The Entire Image.			GLuint height;				// The Height Of The Entire Image.			GLuint texID;				// Texture ID For Use With glBindTexture.			GLuint type;			 	// Data Stored In * ImageData (GL_RGB Or GL_RGBA)	} Texture;	typedef struct	{		GLubyte Header[12];			// File Header To Determine File Type	} TGAHeader;typedef struct	{		GLubyte header[6];			// Holds The First 6 Useful Bytes Of The File		GLuint bytesPerPixel;			// Number Of BYTES Per Pixel (3 Or 4)		GLuint imageSize;			// Amount Of Memory Needed To Hold The Image		GLuint type;				// The Type Of Image, GL_RGB Or GL_RGBA		GLuint Height;				// Height Of Image							GLuint Width;				// Width Of Image						GLuint Bpp;				// Number Of BITS Per Pixel (24 Or 32)	} TGA;TGAHeader tgaheader;				// Used To Store Our File HeaderTGA tga;					// Used To Store File Information// Uncompressed TGA HeaderGLubyte uTGAcompare[12] = {0,0, 2,0,0,0,0,0,0,0,0,0};// Compressed TGA HeaderGLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};// Load An Uncompressed Filebool LoadUncompressedTGA(Texture *, char *, FILE *);// Load A Compressed Filebool LoadCompressedTGA(Texture *, char *, FILE *);//load a tga filebool LoadTGA(Texture * texture, char * filename){     FILE * fTGA;					// Declare File Pointer	 fTGA = fopen(filename, "rb");			// Open File For Reading	 	 if(fTGA == NULL)				// If Here Was An Error	 {				return false;				// Return False	 }          if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)	 {				return false;				// Return False If It Fails	 }	          // If The File Header Matches The Uncompressed Header	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)	{		// Load An Uncompressed TGA		LoadUncompressedTGA(texture, filename, fTGA);	}	// If The File Header Matches The Compressed Header	else if(memcmp(cTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)	{													// Load A Compressed TGA		LoadCompressedTGA(texture, filename, fTGA);	}	else						// If It Doesn't Match Either One	{		fclose(fTGA);								return false;				// Return False	}	}     // Load An Uncompressed TGA!bool LoadUncompressedTGA(Texture * texture, char * filename, FILE * fTGA){     // Attempt To Read Next 6 Bytes	if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)	{														return false;				// Return False	}			texture->width  = tga.header[1] * 256 + tga.header[0];	// Calculate Height	texture->height = tga.header[3] * 256 + tga.header[2];	// Calculate The Width	texture->bpp = tga.header[4];				// Calculate Bits Per Pixel	tga.Width = texture->width;				// Copy Width Into Local Structure	tga.Height = texture->height;				// Copy Height Into Local Structure	tga.Bpp = texture->bpp;					// Copy Bpp Into Local Structure		// Make Sure All Information Is Valid	if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32)))	{				return false;				// Return False	}		if(texture->bpp == 24)				// Is It A 24bpp Image?		texture->type	= GL_RGB;		// If So, Set Type To GL_RGB	else						// If It's Not 24, It Must Be 32		texture->type	= GL_RGBA;		// So Set The Type To GL_RGBA			tga.bytesPerPixel = (tga.Bpp / 8);		// Calculate The BYTES Per Pixel	// Calculate Memory Needed To Store Image	tga.imageSize = (tga.bytesPerPixel * tga.Width * tga.Height);		// Allocate Memory	texture->imageData = (GLubyte *)malloc(tga.imageSize);	if(texture->imageData == NULL)			// Make Sure It Was Allocated Ok	{				return false;				// If Not, Return False	}		// Attempt To Read All The Image Data	if(fread(texture->imageData, 1, tga.imageSize, fTGA) != tga.imageSize)	{												return false;				// If We Cant, Return False	}		// Start The Loop	for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)	{		// 1st Byte XOR 3rd Byte XOR 1st Byte XOR 3rd Byte		texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=		texture->imageData[cswap] ^= texture->imageData[cswap+2];	}	fclose(fTGA);					// Close The File	return true;					// Return Success}bool LoadCompressedTGA(Texture * texture, char * filename, FILE * fTGA)	{                               if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)					// Attempt to read header	{		MessageBox(NULL, "Could not read info header", "ERROR", MB_OK);		// Display Error		if(fTGA != NULL)													// If file is open		{			fclose(fTGA);													// Close it		}		return false;														// Return failed	}	texture->width  = tga.header[1] * 256 + tga.header[0];					// Determine The TGA Width	(highbyte*256+lowbyte)	texture->height = tga.header[3] * 256 + tga.header[2];					// Determine The TGA Height	(highbyte*256+lowbyte)	texture->bpp	= tga.header[4];										// Determine Bits Per Pixel	tga.Width		= texture->width;										// Copy width to local structure	tga.Height		= texture->height;										// Copy width to local structure	tga.Bpp			= texture->bpp;											// Copy width to local structure	if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32)))	//Make sure all texture info is ok	{		MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK);	// If it isnt...Display error		if(fTGA != NULL)													// Check if file is open		{			fclose(fTGA);													// Ifit is, close it		}		return false;														// Return failed	}	tga.bytesPerPixel	= (tga.Bpp / 8);									// Compute BYTES per pixel	tga.imageSize		= (tga.bytesPerPixel * tga.Width * tga.Height);		// Compute amout of memory needed to store image	texture->imageData	= (GLubyte *)malloc(tga.imageSize);					// Allocate that much memory	if(texture->imageData == NULL)											// If it wasnt allocated correctly..	{		MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK);	// Display Error		fclose(fTGA);														// Close file		return false;														// Return failed	}	GLuint pixelcount	= tga.Height * tga.Width;							// Nuber of pixels in the image	GLuint currentpixel	= 0;												// Current pixel being read	GLuint currentbyte	= 0;												// Current byte 	GLubyte * colorbuffer = (GLubyte *)malloc(tga.bytesPerPixel);			// Storage for 1 pixel	do	{		GLubyte chunkheader = 0;											// Storage for "chunk" header		if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)				// Read in the 1 byte header		{			MessageBox(NULL, "Could not read RLE header", "ERROR", MB_OK);	// Display Error			if(fTGA != NULL)												// If file is open			{				fclose(fTGA);												// Close file			}			if(texture->imageData != NULL)									// If there is stored image data			{				free(texture->imageData);									// Delete image data			}			return false;													// Return failed		}		if(chunkheader < 128)												// If the ehader is < 128, it means the that is the number of RAW color packets minus 1		{																	// that follow the header			chunkheader++;													// add 1 to get number of following color values			for(short counter = 0; counter < chunkheader; counter++)		// Read RAW color values			{				if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel) // Try to read 1 pixel				{					MessageBox(NULL, "Could not read image data", "ERROR", MB_OK);		// IF we cant, display an error					if(fTGA != NULL)													// See if file is open					{						fclose(fTGA);													// If so, close file					}					if(colorbuffer != NULL)												// See if colorbuffer has data in it					{						free(colorbuffer);												// If so, delete it					}					if(texture->imageData != NULL)										// See if there is stored Image data					{						free(texture->imageData);										// If so, delete it too					}					return false;														// Return failed				}																						// write to memory				texture->imageData[currentbyte		] = colorbuffer[2];				    // Flip R and B vcolor values around in the process 				texture->imageData[currentbyte + 1	] = colorbuffer[1];				texture->imageData[currentbyte + 2	] = colorbuffer[0];				if(tga.bytesPerPixel == 4)												// if its a 32 bpp image				{					texture->imageData[currentbyte + 3] = colorbuffer[3];				// copy the 4th byte				}				currentbyte += tga.bytesPerPixel;										// Increase thecurrent byte by the number of bytes per pixel				currentpixel++;															// Increase current pixel by 1				if(currentpixel > pixelcount)											// Make sure we havent read too many pixels				{					MessageBox(NULL, "Too many pixels read", "ERROR", NULL);			// if there is too many... Display an error!					if(fTGA != NULL)													// If there is a file open					{						fclose(fTGA);													// Close file					}						if(colorbuffer != NULL)												// If there is data in colorbuffer					{						free(colorbuffer);												// Delete it					}					if(texture->imageData != NULL)										// If there is Image data					{						free(texture->imageData);										// delete it					}					return false;														// Return failed				}			}		}		else																			// chunkheader > 128 RLE data, next color reapeated chunkheader - 127 times		{			chunkheader -= 127;															// Subteact 127 to get rid of the ID bit			if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel)		// Attempt to read following color values			{					MessageBox(NULL, "Could not read from file", "ERROR", MB_OK);			// If attempt fails.. Display error (again)				if(fTGA != NULL)														// If thereis a file open				{					fclose(fTGA);														// Close it				}				if(colorbuffer != NULL)													// If there is data in the colorbuffer				{					free(colorbuffer);													// delete it				}				if(texture->imageData != NULL)											// If thereis image data				{					free(texture->imageData);											// delete it				}				return false;															// return failed			}			for(short counter = 0; counter < chunkheader; counter++)					// copy the color into the image data as many times as dictated 			{																			// by the header				texture->imageData[currentbyte		] = colorbuffer[2];					// switch R and B bytes areound while copying				texture->imageData[currentbyte + 1	] = colorbuffer[1];				texture->imageData[currentbyte + 2	] = colorbuffer[0];				if(tga.bytesPerPixel == 4)												// If TGA images is 32 bpp				{					texture->imageData[currentbyte + 3] = colorbuffer[3];				// Copy 4th byte				}				currentbyte += tga.bytesPerPixel;										// Increase current byte by the number of bytes per pixel				currentpixel++;															// Increase pixel count by 1				if(currentpixel > pixelcount)											// Make sure we havent written too many pixels				{					MessageBox(NULL, "Too many pixels read", "ERROR", NULL);			// if there is too many... Display an error!					if(fTGA != NULL)													// If there is a file open					{						fclose(fTGA);													// Close file					}						if(colorbuffer != NULL)												// If there is data in colorbuffer					{						free(colorbuffer);												// Delete it					}					if(texture->imageData != NULL)										// If there is Image data					{						free(texture->imageData);										// delete it					}					return false;														// Return failed				}			}		}	}	while(currentpixel < pixelcount);													// Loop while there are still pixels left	fclose(fTGA);																		// Close the file	return true;																		// return success}


Stuff to initialize once
Texture texture;    glEnable(GL_TEXTURE_2D);						// Enable Texture Mapping ( NEW )    glShadeModel(GL_SMOOTH);						// Enable Smooth Shading	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);					// Black Background	glClearDepth(1.0f);							// Depth Buffer Setup	glEnable(GL_DEPTH_TEST);						// Enables Depth Testing	glDepthFunc(GL_LEQUAL);							// The Type Of Depth Testing To Do	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);			// Really Nice Perspective Calculations    LoadTGA(&texture, "lightsaber.tga");        glGenTextures(1, &texture.texID);					// Create The Texture	// Typical Texture Generation Using Data From The Bitmap	glBindTexture(GL_TEXTURE_2D, texture.texID);	glTexImage2D(GL_TEXTURE_2D, 0, 3, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	


stuff in loop
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			// Clear Screen And Depth Buffer	    glLoadIdentity();		               		glBindTexture(GL_TEXTURE_2D, texture.texID);        glBegin(GL_QUADS);            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad		    glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad		    glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad		    glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);	// Top Left Of The Texture and Quad        glEnd();        SwapBuffers(hDC);


I'm not in a rush so take as long as you need to reply, if you reply at all. Sorry about all this.
bi-FreeSoftware
Quote:Original post by AdamGL
What happens is I get a bunch of verical bars. Multicolored bars. Hippi bars.

Does the problem happen with all .tga textures you try, or just with some of them? More specifically... do you get the same effect with both uncompressed and compressed textures, or just with one of these types..?
thanks man, that was a big waste of space up there. It shows up, the same type of tga uncompressed and 32 bit. How do I make a proper tga file. What aree my limitations?
bi-FreeSoftware
Wait, I found out why. My image was a 32 bit image. The others were 24 bit. Why can't I load the 32 bit one?
bi-FreeSoftware

This topic is closed to new replies.

Advertisement