Jump to content
  • Advertisement
Sign in to follow this  
jake_Ghost

OpenGL problem with tga loader

This topic is 5005 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

when i run this code i get these errors....
--------------------Configuration: Maia - Win32 Debug--------------------
Compiling...
TGALoader.cpp
Linking...
TGALoader.obj : error LNK2001: unresolved external symbol "bool __cdecl LoadCompressedTGA(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct _iobuf *)" (?LoadCompressedTGA@@YA_NV?$basic_string@DU?$char_tra
its@D@std@@V?$allocator@D@2@@std@@PAU_iobuf@@@Z)
TGALoader.obj : error LNK2001: unresolved external symbol "bool __cdecl LoadUncompressedTGA(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct _iobuf *)" (?LoadUncompressedTGA@@YA_NV?$basic_string@DU?$char
_traits@D@std@@V?$allocator@D@2@@std@@PAU_iobuf@@@Z)
Debug/Maia.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

Maia.exe - 3 error(s), 0 warning(s)


heres the code....
#include <windows.h>		// Header File For Windows
#include <stdio.h>			// Header File For Standard Input/Output
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>
#include <gl\glaux.h>
#include "Tga.h"

TGAHeader tgaheader;									// TGA header
TGA tga;												// TGA image data
TGATex texture;

GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0};	// Uncompressed TGA Header
GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};	// Compressed TGA Header
bool LoadUncompressedTGA(std::string FileName, FILE * fTGA);	// Load an Uncompressed file
bool LoadCompressedTGA(std::string FileName, FILE * fTGA);		// Load a Compressed file

BOOL cTGA :: LoadGLTextures (std::string Filename, int TYPE)
{
	FILE * fTGA;												// File pointer to texture file
	fTGA = fopen(Filename.c_str(), "rb");								// Open file for reading

	if(fTGA == NULL)											// If it didn't open....
	{
		MessageBox(NULL, "Could not open texture file", "ERROR", MB_OK);	// Display an error message
		return false;														// Exit function
	}

	if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)					// Attempt to read 12 byte header from file
	{
		MessageBox(NULL, "Could not read file header", "ERROR", MB_OK);		// If it fails, display an error message 
		if(fTGA != NULL)													// Check to seeiffile is still open
		{
			fclose(fTGA);													// If it is, close it
		}
		return false;														// Exit function
	}

	if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)				// See if header matches the predefined header of 
	{																		// an Uncompressed TGA image
		LoadUncompressedTGA(Filename.c_str(), fTGA);						// If so, jump to Uncompressed TGA loading code
	}
	else if(memcmp(cTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)		// See if header matches the predefined header of
	{																		// an RLE compressed TGA image
		LoadCompressedTGA(Filename.c_str(), fTGA);							// If so, jump to Compressed TGA loading code
	}
	else																	// If header matches neither type
	{
		MessageBox(NULL, "TGA file be type 2 or type 10 ", "Invalid Image", MB_OK);	// Display an error
		fclose(fTGA);
		return false;																// Exit function
	}

	TGATex tmpTGA;

	TGATexture.push_back(tmpTGA);
    TexCountTGA += 1;

	// Generate Textures
	if (TYPE == 1)
	{
		glGenTextures(1,&TGATexture[TexCountTGA-1].imageDataNear);

		// Bind Nearest Texture
		glBindTexture(GL_TEXTURE_2D, TGATexture[TexCountTGA-1].imageDataNear);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, TGATexture[TexCountTGA-1].bpp / 8, TGATexture[TexCountTGA-1].width, TGATexture[TexCountTGA-1].height, 0, TGATexture[TexCountTGA-1].type, GL_UNSIGNED_BYTE, TGATexture[TexCountTGA-1].imageData);
	}
	else if(TYPE==2)
	{
		glGenTextures(1,&TGATexture[TexCountTGA-1].imageDataLin);

		// Bind Linear Texture
		glBindTexture(GL_TEXTURE_2D, TGATexture[TexCountTGA-1].imageDataLin);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, TGATexture[TexCountTGA-1].bpp / 8, TGATexture[TexCountTGA-1].width, TGATexture[TexCountTGA-1].height, 0, TGATexture[TexCountTGA-1].type, GL_UNSIGNED_BYTE, TGATexture[TexCountTGA-1].imageData);
	}
	else if(TYPE == 3)
	{
		glGenTextures(1,&TGATexture[TexCountTGA-1].imageDataMip);

		// Bind Mipmap Texture
		glBindTexture(GL_TEXTURE_2D, TGATexture[TexCountTGA-1].imageDataMip);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
		gluBuild2DMipmaps(GL_TEXTURE_2D, TGATexture[TexCountTGA-1].bpp / 8, TGATexture[TexCountTGA-1].width, TGATexture[TexCountTGA-1].height, TGATexture[TexCountTGA-1].type, GL_UNSIGNED_BYTE, TGATexture[TexCountTGA-1].imageData);
	}

	return true;															// All went well, continue on
}

bool LoadUncompressedTGA(TGATex * texture, std::string Filename, FILE * fTGA)	// Load an uncompressed TGA (note, much of this code is based on NeHe's 
{																			// TGA Loading code nehe.gamedev.net)
	if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)					// Read TGA header
	{										
		MessageBox(NULL, "Could not read info header", "ERROR", MB_OK);		// Display error
		if(fTGA != NULL)													// if file is still open
		{
			fclose(fTGA);													// Close it
		}
		return false;														// Return failular
	}	

	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 the 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

	if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32)))	// Make sure all information is valid
	{
		MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK);	// Display Error
		if(fTGA != NULL)													// Check if file is still open
		{
			fclose(fTGA);													// If so, close it
		}
		return false;														// Return failed
	}

	if(texture->bpp == 24)													// If the BPP of the image is 24...
		texture->type	= GL_RGB;											// Set Image type to GL_RGB
	else																	// Else if its 32 BPP
		texture->type	= GL_RGBA;											// Set image type to GL_RGBA

	tga.bytesPerPixel	= (tga.Bpp / 8);									// Compute the number of BYTES per pixel
	tga.imageSize		= (tga.bytesPerPixel * tga.Width * tga.Height);		// Compute the total amout ofmemory needed to store data
	texture->imageData	= (GLubyte *)malloc(tga.imageSize);					// Allocate that much memory

	if(texture->imageData == NULL)											// If no space was allocated
	{
		MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK);	// Display Error
		fclose(fTGA);														// Close the file
		return false;														// Return failed
	}

	if(fread(texture->imageData, 1, tga.imageSize, fTGA) != tga.imageSize)	// Attempt to read image data
	{
		MessageBox(NULL, "Could not read image data", "ERROR", MB_OK);		// Display Error
		if(texture->imageData != NULL)										// If imagedata has data in it
		{
			free(texture->imageData);										// Delete data from memory
		}
		fclose(fTGA);														// Close file
		return false;														// Return failed
	}

	// Byte Swapping Optimized By Steve Thomas
	for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)
	{
		texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=
		texture->imageData[cswap] ^= texture->imageData[cswap+2];
	}

	fclose(fTGA);															// Close file
	return true;															// Return success
}

bool LoadCompressedTGA(TGATex * texture,std::string FileName, FILE * fTGA)		// Load COMPRESSED TGAs
{ 
	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
	}

	if(texture->bpp == 24)													// If the BPP of the image is 24...
		texture->type	= GL_RGB;											// Set Image type to GL_RGB
	else																	// Else if its 32 BPP
		texture->type	= GL_RGBA;											// Set image type to GL_RGBA

	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
}

GLuint cTGA ::Texture(std::string Filename)
{
	int i, FileNum;

	for (i = 0;i < TexCountTGA; i++)
	{
		if (Filename == TGATexture.FileName) 
		{
			FileNum = i;
		}
	}

	return TGATexture[FileNum].imageDataMip;
}


GLuint cTGA :: Texture (std::string Filename, int TYPE)
{
	int i, FileNum;

	for (i = 0;i < TexCountTGA; i++)
	{
		if (Filename == TGATexture.FileName) 
		{
			FileNum = i;
		}
	}

	if (TYPE == 0)
	{
		return TGATexture[FileNum].imageDataNear;
	}
	else if (TYPE == 1)
	{
		return TGATexture[FileNum].imageDataLin;
	}
	else if (TYPE == 2)
	{
		return TGATexture[FileNum].imageDataMip;
	}
	return -1;
}

cTGA :: cTGA() {}

cTGA :: ~cTGA()
{
	 TGATexture.clear();
}

any hlep would be great thx jake

Share this post


Link to post
Share on other sites
Advertisement
add a #include <string>

and then a

using namespace std;

you're not including the headers for basic_string.


and here is another tip. when wrapping the LoadTga functions change up the mallocs with news.

Its good pratice to keep with language conventions.

Share this post


Link to post
Share on other sites
or you can try mine, i've modify it to load tga(comp./uncomp.), bmp and jpg
and i've write an alpha channel builer that will make an alpha channel for you with the colour of your choice.

here:
http://www.freewebs.com/vortex666/index.html

Share this post


Link to post
Share on other sites
Your functions are inconsistantly defined:


bool LoadUncompressedTGA(std::string FileName, FILE * fTGA);
bool LoadCompressedTGA(std::string FileName, FILE * fTGA);

//...

bool LoadUncompressedTGA(TGATex * texture, std::string Filename, FILE * fTGA)
{
//...
}

bool LoadCompressedTGA(TGATex * texture,std::string FileName, FILE * fTGA)
{
//...
}




Notice how when you declare the functions at first you're passing in a string and a FILE* but when you define them further down you include a TGATex* as well. Your compiler is obviously expecting the functions to have no TGATex* so when you define functions WITH a TGATex* it says "oh this isn't what I want" and then thinks "they must be elsewhere", so it compiles fine and then when it's linking everything it notices those functions aren't anywhere, and that's the error you're having. You need to update the forward declarations to the parameters in the definitions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!