Load TGA files

Is there any easy way to load a TGA to a 2D quad?
The function I use now distort the TGA file and make the color
messed up.

GLuint loadTexture(const char * fname, int w, int h)
{
	GLuint texture;
	unsigned char * data;
	FILE * file;
	file = fopen(fname, "rb");
		if(file == NULL) return 0;
		data = (unsigned char *) malloc (w * h * 3);
		fread(data, w * h * 3, 1, file);
	fclose(file);
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture); 
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	free(data);
	return texture;
}

I want to load a TGA and make it as easy as possible to make a
specific color on the texture transparent.

Anyone has any idea?

That code does not load a TGA at all, it simply reads raw data from a file. A TGA contains a header, where width and height is stored and other information, like whether it is RLE compressed and how many channels it has. Also TGAs are usually stored in BGRA format not RGBA.

Google for “TGA file format” or “TGA loader”, there should be plenty of code out there. Many OpenGL tutorials include TGA loaders, since it is a very popular format.

Jan.

Very good TGA file format

When we worked on the new tutorials for NeHe we decided to create a new free use tga textureloader, it’s not the final one that will soon be released, but the final one will use the same load engine witen by yours truly,
However you can download it as it was in the first beta form here.

Look in this forums for freeimage or image magick libraries