tga loading problems

my tga loader isn’t loading tga’s! grrr… if you could take a look at my loader and spot any errors that i might have missed that would help me out alot. Thanx!

void tga: efine(){
glTexImage2D(GL_TEXTURE_2D,0,m_type,m_Width,m_Height,0,GL_RGB,GL_UNSIGNED_BYTE,m_data);
}
void tga::GenerateID(){
glGenTextures(1,&m_textID);
}

void tga::Bind(){
glBindTexture(GL_TEXTURE_2D,m_textID);
}

void tga::Filter(){
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

bool tga::Load(char *filename){
fstream file;
long x = 0;
unsigned char rgbSwitch;

file.open(filename,ios::in | ios::binary);
if(!file.is_open()){
	file.close();
	return false;
}else{
	file.seekg(2,ios::beg);
	file.read((char*)&m_type,sizeof(unsigned char));
	if((m_type != 2) | | (m_type != 3)){
		file.close();
		return false;
	}else{
		file.seekg(13,ios::cur);
		file.read((char*)&m_Width,sizeof(short int));
		file.read((char*)&m_Height,sizeof(short int));
		file.read((char*)&m_bites,sizeof(unsigned char));
		file.seekg(1,ios::cur);
		
		m_channels = m_bites / 8;
		m_size = m_Width * m_Height;
		m_size *= m_channels;
		m_data = new unsigned char[m_size];

		file.read((char*)&m_data,sizeof(unsigned char));

		while(x < m_size){
			rgbSwitch = m_data[x];
			m_data[x] = m_data[x + 2];
			m_data[x + 2] = rgbSwitch;
			x += m_channels;
		}
		file.close();
		return true;
	}
}

}

file.read((char*)&m_data,sizeof(unsigned char));

Your only reading in one byte of data.

i changed the line to:
file.read((char*)&m_data,sizeof(unsigned char)*m_size);
and it still doesnt work!

i think i found were the error could be happening:

if((m_type != 2) | | (m_type != 3)){
file.close();
return false;
}

i dont think im putting in the right value for m_type. i want to skip the first 2 bytes of data of the header and get the type. i try to do this using the file.seekg(2,ios::beg); but i think that im using it wrong and putting wrong data into m_type. how can i skip data in a file/fix this problem?

You want to check the third byte in the file for either 2 or 3. 2 being black and white, 3 being true color, both being uncompressed. Check the spec for the other identifiers. www.wotsit.org

EDIT:

You should be doing a logical AND (&&) rather than a logical OR.

Old GLman

[This message has been edited by Old GLman (edited 12-12-2002).]

Yeah, try wotsi.

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

You are right about your diagnose of the m_type values not being the appropriate ones…
The 1, 2, 3, 4 are simply the number of channels (bytes) you are reading per pixel. But there is no such thing in the header of a TGA file. Instead, you read the number of bits per pixel (bpp). So, you would have 8, 16, 24, 32.
Well, where do these 1, 2, 3, 4 come from?
You divide the bpp, which is the third unsigned char in the header as you are reading, and divide it by 8. 8 is the number of bits in a byte.

You will also have more problems if the tga is compressed or palletted or so and so and more sos.

8 -> grayscale
16 -> Why use it? Palletted maybe?
24 -> RGB
32 -> RGBA

If you want you can use one I wrote.
Download the normal map generator from: http://cheo.resnet.wayne.edu/miguel/
You can use and abuse it.

Good luck.

[This message has been edited by mancha (edited 12-15-2002).]