Skip to main content
gamedev.net
🔒 Locked

Loading TGA

Started by jazzman67 Mar 4, 2009 at 10:58 PM 3 replies 1.2k views
Original Post
jazzman67
jazzman67
I use eclipse with c++ plugin under linux. Been trying to load a TGA texture. Used the TGA loader at GPWiki(http://gpwiki.org/index.php/LoadTGACpp). My code terminates when gluBuild2DMipmaps is called. gluErrorString(gluBuild2DMipmaps( wasn't giving me anything in eclipse for some strange reason. so i transferred the code to VC++ under windows and get "gl_out_of_memory". I rewrite a more simple TGA loader from a book, and get the same error. googled the problem, didn't come up with much. Any help would be appreciated. I think the TGA Loaders are fine, so i'll post where glubuild is called and and rendering is done at the moment. Though its pretty simple.

void Room::LoadTextures()
{
	
	if(TGALoader.Load("Wall.tga")){
		
		glGenTextures(1, &w_Texture); 
		glBindTexture(GL_TEXTURE_2D, w_Texture);
	
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
		gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TGALoader.GetWidth(), TGALoader.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, TGALoader.GetImage());
	}
}



void Room::Render(){

	glBindTexture(GL_TEXTURE_2D, w_Texture);	
	glBegin(GL_QUADS);
	
	glTexCoord2f (0.0f, 0.0f);	glVertex3f(0.0f, 0.0f, 0.0f);
	glTexCoord2f (0.0f, 1.0f);	glVertex3f(0.0f, 5.0f, 0.0f);
	glTexCoord2f (1.0f, 1.0f);	glVertex3f(5.0f, 5.0f, 0.0f);
	glTexCoord2f (1.0f, 0.0f);	glVertex3f(5.0f, 0.0f, 0.0f);
	
	glDisable(GL_TEXTURE_2D);	
	glEnd();
}


CrazyCdn
CrazyCdn
Well I don't see an error in my 2 second scan. But save the TGA out again and see if it loads, display the pixels on the screen and see if the data is loading correctly...etc. All these should take a few minutes to impliment and try, also post the loader code.

Also use the debugger in visual studios or in eclipse. Step through the code one line at a time see whats happening.
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
D3DXVECTOR3
D3DXVECTOR3
Are yoy sure that the tga is loaded correctly? check the results if TGALoader.GetWidth(), TGALoader.GetHeight() and TGALoader.GetImage())
jazzman67
jazzman67
Thanx for the replies guys. Here is the TGA loader, coded from Allan Sherrod "Game Graphics Programming". I started debugging in eclipse, when *data is initialized, debugger says address out of bounds. Could this be it? Also the TGA is uncompressed at the moment, and the returning height and width are correct, 1024.

bool TGAImge::Load( char* file){		FILE *pfile;	int length = 0;	image = NULL;		if(!file)		return Error;		pfile = fopen(file, "rb");		if (!pfile)		return Error;			fseek(pfile, 0, SEEK_END);	length = ftell(pfile);	fseek(pfile, 0, SEEK_SET);		//Read data	char *data = new char[(length +1) * sizeof(char)];		if (!data)		return Error;			fread(data, 1, length, pfile);	data[length] = '\0';		fclose(pfile);		unsigned char tempColor;	unsigned char unCompressHeader[12] = {	0, 0, 2, 0, 0, 0,											0, 0, 0, 0, 0, 0,};	char *ptr = data;	long tgaSize = 0;		//No Compressed TGA	if(memcmp(unCompressHeader, ptr, sizeof(unCompressHeader)) != 0)	{		return Error;	}		ptr += sizeof(unCompressHeader);		//Caculate Stats	width = ptr[0] + ptr[1] * 256;	height = ptr[2] + ptr[3] * 256;	components = ptr[4] / 8;	tgaSize = width * height * components;		ptr += 6;		//Only RGB and RGBA	if(components != 3 && components != 4){		return Error;}				//Allocate data for the image	image = new unsigned char[sizeof(unsigned char) * tgaSize];	memcpy(image, ptr, sizeof(char) * tgaSize);		//Convert BGR to RGB	for( long index = 0; index < tgaSize; index += components)	{		tempColor = image[index];		image[index] = image[index + 2];		image[index +2] = tempColor;	}		delete[] data;		return Success;}
MARS_999
MARS_999
I don't have time to read through the code but here is my loader and I know that it works.

bool LoadTGAFile(const std::string& filename, unsigned char* imageData){	unsigned char colorSwap = 0;	unsigned char TGAheader[12]  = {0,0,2,0,0,0,0,0,0,0,0,0};	// Uncompressed TGA Header	unsigned char TGAcompare[12] = {0};                         // Used To Compare TGA Header	unsigned char header[6]      = {0};                         // First 6 Useful Bytes From The Header	unsigned int temp = 0;                                      // Temporary Variable				FILE* file = fopen(filename.c_str(), "rb");                         // Open The TGA File	if(file == NULL || 		fread(TGAcompare, 1, sizeof(TGAcompare), file) != sizeof(TGAcompare) || 		memcmp(TGAheader, TGAcompare, sizeof(TGAheader)) != 0 || 		fread(header, 1, sizeof(header), file) != sizeof(header))                    	{		return false;	}		unsigned int imageWidth  = header[1] * 256 + header[0];   // Determine The TGA Width      (highbyte*256+lowbyte)	unsigned int imageHeight = header[3] * 256 + header[2];  // Determine The TGA Height     (highbyte*256+lowbyte)    	if(imageWidth <= 0 || imageHeight <= 0 || (header[4] != 24 && header[4] != 32))	{		//kill app here		fclose(file);                                     		return false;	}		unsigned int bitsPerPixel  = header[4];							  // Grab The TGA's Bits Per Pixel (24 or 32)	unsigned int bytesPerPixel = bitsPerPixel / 8;					  // Divide By 8 To Get The Bytes Per Pixel	unsigned int imageSize = imageWidth * imageHeight * bytesPerPixel;// Calculate The Memory Required For The TGA Data		if(fread(imageData, 1, imageSize, file) != imageSize)    {		//kill app here		fclose(file);                                     		return false;	}		fclose(file);		for(unsigned int x = 0; x < imageSize; x +=bytesPerPixel)	{		colorSwap        = imageData[x];		imageData[x]     = imageData[x + 2];		imageData[x + 2] = colorSwap;	}	return true;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.