Archived

This topic is now archived and is closed to further replies.

TipplerP

the same code but 2 different mistakes...

Recommended Posts

TipplerP    122
I want to load TGA in my engine but i dont know much bout it! I took a little bit of Nate Miller and a little bit of Shninig knights tutos... the result is shit... I want to see how it works and how good tga''s are... With this class I want to texture a 3d wall but when I start the programm...the window is totaly white Ya cant see anything it is just white! Why?!
  
bool MW_TextureManager::DisplayTexture(bool TextureData)
{
    glBindTexture(GL_TEXTURE_2D, TextureID);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, texEnv);

    if(mipmap)
        gluBuild2DMipmaps(GL_TEXTURE_2D, Internal, width, height, Format,
        GL_UNSIGNED_BYTE, imageData);
    else
        glTexImage2D(GL_TEXTURE_2D, 0, Internal, width, height, 0, Format,
        GL_UNSIGNED_BYTE, imageData);
   
    if(TextureData)
        ReleaseTex();

	delete [] imageData;
    imageData = 0;
	
    return TRUE;
}

bool MW_TextureManager::LoadTGA(char *filename)
{
	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};
	GLubyte		TGAcompare[12];
	GLubyte		header[6];
	GLuint		bytesPerPixel;
	GLuint		imageSize;
	GLuint		temp;
	GLuint		type=GL_RGBA;

	FILE* file = fopen(filename, "rb");

	if(file==NULL							||
	   fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||
	   memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0		   ||
	   fread(header,1,sizeof(header),file)!=sizeof(header))			
	{
		if(file==NULL)
		{
			Log("%s does not exist.", filename);
			return false;							
		}
		else
		{
			fclose(file);
			Log("Could not load %s correctly, general failure.", filename);
			return false;						
		}
	}

	width  = header[1] * 256 + header[0];
	height = header[3] * 256 + header[2];
    
 	if(width	<=0	||
	   height<=0	||
		(header[4]!=24 && header[4]!=32))
	{
		fclose(file);					
		Log("%s''s height or width is less than zero, or the TGA is not 24 or 32 bits.", filename);
		return false;							
	}

	bpp		= header[4];
	bytesPerPixel	= bpp/8;
	imageSize		= width * height*bytesPerPixel;

	imageData=(byte *)malloc(imageSize);

	if(imageData==NULL ||
	   fread(imageData, 1, imageSize, file)!=imageSize)
	{
		if(imageData!=NULL)
			free(imageData);
		
		Log("Storate memory for %s does not exist or is corrupted.", filename);
		
		fclose(file);
		return false;
	}

	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)
	{
		temp=imageData[i];
		imageData[i] = imageData[i + 2];
		imageData[i + 2] = temp;
	}

	fclose (file);

	if(bpp==24)
		type=GL_RGB;
	if(bpp==8)
		type=GL_ALPHA;

	Log("Loaded %s correctly.", filename);
	return true;
}

bool MW_TextureManager::ReleaseTex()
{
	if(TextureID)
		glDeleteTextures(1,&TextureID);
	return TRUE;
}
  
what is wrong?! Thx

Share this post


Link to post
Share on other sites
didilabu2    122
Ok...I dont know what all that mess is, but it is very easy to load a tga for a texture. All you need is a tga header structure and a class like these:

//********************************

struct TargaHeader {
unsigned char idLength;
unsigned char colorMapType;
unsigned char imageType;
unsigned short cMapStart;
unsigned char cMapDepth;
unsigned short xOffset;
unsigned short yOffset;
unsigned short width;
unsigned short height;
unsigned char pixelDepth;
unsigned char imageDescriptor;
};// end TargaHeader

//********************************

class TextureManager {
private:
unsigned char* bytes;
int width, height;

void set(int id);

public:
TextureManager() {bytes=0;}
~TextureManager() {if(bytes)delete[]bytes;}

bool load(char* filename, int id);
};// end TextureManager

//********************************


Now the load function needs to know the filename, and what id for the texture you want (this is before I knew that opengl can generate text id''s for you - but you can change that). This example will only load 24bit targas, it first loads the header, then loads the image bytes (keep in mind they are stored in bgr order in 24bit, so you have to flip them to rgb). Here is the load function:

bool TextureManager::load(char* filename, int id) {
TargaHeader tHeader;

ifstream file(filename, ios::binary);
if (!file) return false;

file.read((char*)&tHeader, sizeof(TargaHeader));
width=tHeader.width;
height=tHeader.height;

// run some tests to make sure valid texture
if (tHeader.pixelDepth != 24) return false;
if (width != height) return false;
if (width != 64 && width != 128 && width != 256) return false;

int size = 3*tHeader.width*tHeader.height;
if (bytes)delete[]bytes;
bytes = new unsigned char[size];
file.read((char*)bytes, size);
// swap the r and b bits (image stored in bgr order)
for (int i = 0; i < size; i+=3){
unsigned char temp = bytes;
bytes[i] = bytes[i+2];
bytes[i+2] = temp;
}

set(id);

file.close();
return true;
}// end load


Next you have to tell opengl that you want to make a texture. The load function calls set() to do this. Here is set:

void TextureManager::set(int id) {
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bytes);
}// end set

Now in your program you can do this:

(somewhere in the beginning of your program)
glEnable(GL_TEXTURE_2D);
TextureManager t;
t.load("sometarga.tga", 1);

(inside your rendering function)
glBindTexture(GL_TEXTURE_2D, 1);

Then draw your vertices, faces, object, whatever it is.

Hope that helped.

Share this post


Link to post
Share on other sites