Sign in to follow this  
Ainokea

OpenGL Texture mapping issue(TGA files)

Recommended Posts

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[i];							// Temporarily Store The Value At Image Data 'i'
		texture->imageData[i] = texture->imageData[i + 2];	// Set The 1st Byte To The Value Of The 3rd Byte
		texture->imageData[i + 2] = temp;					// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
	}

	fclose (file);											// Close The File

	// Build A Texture From The Data
	glGenTextures(1, &texture[0].ID);					// Generate OpenGL texture IDs

	glBindTexture(GL_TEXTURE_2D, texture[0].ID);			// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
	
	if (texture[0].bpp==24)									// Was The TGA 24 Bits
	{
		type=GL_RGB;										// If So Set The 'type' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, type, texture[0].width, texture[0].height, 0, type, GL_UNSIGNED_BYTE, texture[0].imageData);

    textureSlots.push_back(texture);

	return true;											// Texture Building Went Ok, Return True
	
}    

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just in case anyone cared the problem was the size of the texture. It had a resolution that was to big, I will have to look into that to fix...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this