Texture mapping issue(TGA files)

Started by
9 comments, last by Ainokea 19 years, 2 months ago
For some odd reason once I converted to TGA files for textures I can no longer load and use any texture other then one I have called test.tga nor can I use that one if I edit that one. Here is my texture mapping code:

bool TextureManager::CreateTexture(std::string fileName , Texture *texture)
{
	unsigned char		Theader[12]={0,0,2,0,0,0,0,0,0,0,0,0};	// Uncompressed TGA Header
	unsigned char		Tcompare[12];								// Used To Compare TGA Header
	unsigned char		header[6];									// First 6 Useful Bytes From The Header
	unsigned int		bytesPerPixel;								// Holds Number Of Bytes Per Pixel Used In The TGA File
	unsigned int		imageSize;									// Used To Store The Image Size When Setting Aside Ram
	unsigned int		temp;										// Temporary Variable
	unsigned int		type=GL_RGBA;								// Set The Default GL Mode To RBGA (32 BPP)

	FILE *file = fopen(fileName.c_str(), "rb");						// Open The TGA File

	if(	file==NULL ||										// Does File Even Exist?
		fread(Tcompare,1,sizeof(Tcompare),file)!=sizeof(Tcompare) ||	// Are There 12 Bytes To Read?
		memcmp(Theader,Tcompare,sizeof(Theader))!=0				||	// Does The Header Match What We Want?
		fread(header,1,sizeof(header),file)!=sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file == NULL)									// Did The File Even Exist? *Added Jim Strong*
		{
            return false;									// Return False
         }
		else
		{
			fclose(file);									// If Anything Failed, Close The File
			return false;									// Return False
		}
	}

	texture->width  = header[1] * 256 + header[0];			// Determine The TGA Width	(highbyte*256+lowbyte)
	texture->height = header[3] * 256 + header[2];			// Determine The TGA Height	(highbyte*256+lowbyte)
    
 	if(	texture->width	<=0	||								// Is The Width Less Than Or Equal To Zero
		texture->height	<=0	||								// Is The Height Less Than Or Equal To Zero
		(header[4]!=24 && header[4]!=32))					// Is The TGA 24 or 32 Bit?
	{
		fclose(file);										// If Anything Failed, Close The File
		return false;										// Return False
	}

	texture->bpp	= header[4];							// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel	= texture->bpp/8;						// Divide By 8 To Get The Bytes Per Pixel
	imageSize		= texture->width*texture->height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	texture->imageData=(unsigned char *)malloc(imageSize);		// Reserve Memory To Hold The TGA Data

	if(	texture->imageData==NULL ||							// Does The Storage Memory Exist?
		fread(texture->imageData, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if(texture->imageData!=NULL)						// Was Image Data Loaded
			free(texture->imageData);						// If So, Release The Image Data

		fclose(file);										// Close The File
		return false;										// Return False
	}

	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)		// Loop Through The Image Data
	{														// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temp=texture->imageData;							// Temporarily Store The Value At Image Data 'i'
		texture->imageData = texture->imageData;	<span class="cpp-comment">// Set The 1st Byte To The Value Of The 3rd Byte</span>
		texture-&gt;imageData = temp;					<span class="cpp-comment">// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)</span>
	}

	fclose (file);											<span class="cpp-comment">// Close The File</span>

	<span class="cpp-comment">// Build A Texture From The Data</span>
	glGenTextures(<span class="cpp-number">1</span>, &amp;texture[<span class="cpp-number">0</span>].ID);					<span class="cpp-comment">// Generate OpenGL texture IDs</span>

	glBindTexture(GL_TEXTURE_2D, texture[<span class="cpp-number">0</span>].ID);			<span class="cpp-comment">// Bind Our Texture</span>
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	<span class="cpp-comment">// Linear Filtered</span>
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	<span class="cpp-comment">// Linear Filtered</span>
	
	<span class="cpp-keyword">if</span> (texture[<span class="cpp-number">0</span>].bpp==<span class="cpp-number">24</span>)									<span class="cpp-comment">// Was The TGA 24 Bits</span>
	{
		type=GL_RGB;										<span class="cpp-comment">// If So Set The 'type' To GL_RGB</span>
	}

	glTexImage2D(GL_TEXTURE_2D, <span class="cpp-number">0</span>, type, texture[<span class="cpp-number">0</span>].width, texture[<span class="cpp-number">0</span>].height, <span class="cpp-number">0</span>, type, GL_UNSIGNED_BYTE, texture[<span class="cpp-number">0</span>].imageData);

    textureSlots.push_back(texture);

	<span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;											<span class="cpp-comment">// Texture Building Went Ok, Return True</span>
	
}    

</pre></div><!–ENDSCRIPT–> 
______________________________________________________________________________________With the flesh of a cow.
Advertisement
Did you save with/without compression? There are different types of TGA. Check the options in whatever program you use to save them.
Quote:Original post by Vampyre_Dark
Did you save with/without compression? There are different types of TGA. Check the options in whatever program you use to save them.

No compression.
______________________________________________________________________________________With the flesh of a cow.
Just in case the way I load and draw it matter:

//OpenGL initilization:void Init(){        manager.CreateTexture("Data/tile.tga",&texture); //its loading tile.tga which is not test.tga so it doesnt work...	glEnable(GL_TEXTURE_2D);							glShadeModel(GL_SMOOTH);								glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	glClearDepth(1.0f);								glEnable(GL_DEPTH_TEST);								glDepthFunc(GL_LEQUAL);									glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);}//Rendering:int Render()								{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer	glLoadIdentity();									// Reset The View	glTranslatef(0.0f,0.0f,-5.0f);	glRotatef(xrot,1.0f,0.0f,0.0f);	glRotatef(yrot,0.0f,1.0f,0.0f);	glRotatef(zrot,0.0f,0.0f,1.0f);	manager.UseTexture(&texture); //This simply passes a call to glBindTexture	glBegin(GL_QUADS);		// Front Face		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		// Back Face		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);		// Top Face		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);		//manager.DeleteTexture(&texture);		//manager.UseTexture(&texture2);        // Bottom Face		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		// Right face		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);		// Left Face		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);	glEnd();}
______________________________________________________________________________________With the flesh of a cow.
Where does it actually fail? Does the loadTexture routine return true/false? I'd step throught it in the debugger to see if anything obvious sticks out.
Are you saving these .tga files as RGB, RGBA, or greyscale? Greyscale images are loaded a bit differently than RGB, RGBA
Quote:Original post by evolutional
Where does it actually fail? Does the loadTexture routine return true/false? I'd step throught it in the debugger to see if anything obvious sticks out.

It returns true and I would step it through a debugger if I how to use one.

Im so lost that I think I will learn how to use the GDB ddebugger that comes with Dev-C++.
______________________________________________________________________________________With the flesh of a cow.
Quote:Original post by MARS_999
Are you saving these .tga files as RGB, RGBA, or greyscale? Greyscale images are loaded a bit differently than RGB, RGBA

I don't know how to tell, can you tell me?
______________________________________________________________________________________With the flesh of a cow.
What does the loaded texture look like? If it kind of looks like a diagonal rainbow, you should check your constraints on byte alignment to make sure that you are loading 24-bit files correctly.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Quote:Original post by Thunder_Hawk
What does the loaded texture look like? If it kind of looks like a diagonal rainbow, you should check your constraints on byte alignment to make sure that you are loading 24-bit files correctly.

Other then the one called test.tga it doesnt load it at all.
______________________________________________________________________________________With the flesh of a cow.

This topic is closed to new replies.

Advertisement