Sign in to follow this  

TGA Texture Resource Problem

This topic is 4838 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

I recently spent some time writing a TGA loader, and then trying to write a resource TGA loader. I accomplished this by creating a class that could accept either a string representing the TGA file location or an unsigned integer representing the resource ID. However, I have run into some problems with it. I can load the resource texture and even draw it, but the colors are a little bit lighter than they are supposed to be. I've tried running various different ways, and have come to the conclusion that it must be the texture class code as opposed to the implementation settings (blending, alpha testing, etc.). Could anyone point out what I messed up? (Loading a resource from a file is also currently broken). Note that the class can only load a 32 bit uncompressed TGA image, that will hopefully be fixed in the future.
	
// CTexture.h

#ifndef __TEXTURE_H__
#define __TEXTURE_H__

struct PIXEL
{
	unsigned char red;
	unsigned char green;
	unsigned char blue;
	unsigned char alpha;

	PIXEL( unsigned char _red = 0, unsigned char _green = 0, unsigned char _blue = 0, unsigned
		char _alpha = 0 ) :	red(_red), green(_green), blue(_blue), alpha(_alpha)
	{
		// Nothing else to do
	}
};

#pragma pack(push,1)
struct _TARGA 
{
	unsigned char  idlength;
	unsigned char  colourmaptype;
	unsigned char datatypecode;
	short int colourmaporigin;
	short int colourmaplength;
	unsigned char  colourmapdepth;
	short int x_origin;
	short int y_origin;
	short width;
	short height;
	unsigned char  bitsperpixel;
	unsigned char  imagedescriptor;
	PIXEL data;
};
#pragma pack(pop,1)

class CTexture
{
public:

	static unsigned int numTextures;

	CTexture();
	CTexture( unsigned int resourceID );
	CTexture( const char* fileName );
	~CTexture();

	void Bind() const;
	bool Initialize( const char* fileName );
	bool Initialize( unsigned int resourceID );

private:
	unsigned int id;

	bool LoadResourceTGA( _TARGA* targa, PIXEL* data, unsigned int resourceID );
	bool LoadFileTGA( const char* fileName );
};


#endif



#include "CTexture.h"

#include <windows.h>
#include <gl/gl.h>
#include <stdio.h>

unsigned int CTexture::numTextures = 0;

CTexture::CTexture()
{
	// Nothing else to do
}

CTexture::CTexture( unsigned int resourceID )
{
	_TARGA* tga = (_TARGA*)LockResource( LoadResource(NULL, 
		FindResource(NULL, MAKEINTRESOURCE(resourceID), "TGA") ) );

	if ( tga )
	{
		glGenTextures(1, &numTextures);
		id = numTextures;
		LoadResourceTGA( tga, &tga->data, id );
	}
}

CTexture::CTexture( const char* fileName )
{	
	glGenTextures(1, &numTextures);
	id = numTextures;

	LoadFileTGA( fileName );
}

CTexture::~CTexture()
{
	glDeleteTextures(1, &id);
}

void CTexture::Bind() const
{
	glBindTexture(GL_TEXTURE_2D, id);
}

bool CTexture::Initialize( const char* fileName )
{
	glGenTextures(1, &numTextures);
	id = numTextures;

	if ( ! LoadFileTGA( fileName ) )
	{
		return false;
	}

	return true;
}

bool CTexture::Initialize( unsigned int resourceID )
{
	_TARGA* tga = (_TARGA*)LockResource( LoadResource(NULL, 
		FindResource(NULL, MAKEINTRESOURCE(resourceID), "TGA") ) );

	if ( tga )
	{	
		glGenTextures(1, &numTextures);
		id = numTextures;
		
		if ( ! LoadResourceTGA( tga, &tga->data, id ) )
		{
			return false;
		}
	}
	
	return true;
}

bool CTexture::LoadResourceTGA( _TARGA* targa, PIXEL* data,  unsigned int resourceID )
{
	PIXEL* pixels = new PIXEL[targa->height*targa->width];

	for ( int height = 0; height < targa->height; ++height )
	{
		for ( int width = 0; width < targa->width; ++width )
		{
			pixels[(targa->height - height - 1) * targa->width + width].blue  = data->red;
			pixels[(targa->height - height - 1) * targa->width + width].green = data->green;
			pixels[(targa->height - height - 1) * targa->width + width].red   = data->blue;
			pixels[(targa->height - height - 1) * targa->width + width].alpha = data->alpha;

			data++;
		}
	}

	glBindTexture(GL_TEXTURE_2D, resourceID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, targa->width, targa->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
	
	delete[] pixels;

	return true;
}

bool CTexture::LoadFileTGA( const char *fileName )
{
	_TARGA* tga = new _TARGA;
	FILE* file = NULL;
	PIXEL* pixels;

	if ( ! fileName )
	{
		return false;
	}
	
	file = fopen(fileName,"rb");

	if ( ! file )
	{
		return false;
	}
	
	fread(tga, 1, sizeof(_TARGA) - sizeof(PIXEL), file);

	pixels = new PIXEL[tga->width * tga->height];

	fread(pixels, 1, tga->width * tga->height * sizeof(PIXEL), file);

	for ( int height = 0; height < tga->height; ++height )
	{
		for ( int width = 0; width < tga->width; ++width )
		{
			pixels[(tga->height - height - 1) * tga->width + width].blue   = tga->data.red;
			pixels[(tga->height - height - 1) * tga->width + width].green = tga->data.green;
			pixels[(tga->height - height - 1) * tga->width + width].red  = tga->data.blue;
			pixels[(tga->height - height - 1) * tga->width + width].alpha = tga->data.alpha;

			&tga->data;
		}
	}

	glBindTexture(GL_TEXTURE_2D, id);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tga->width, tga->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

	delete [] pixels;
	delete tga;
	fclose(file);

	return true;


Share this post


Link to post
Share on other sites

This topic is 4838 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.

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