Texture problems.

I have a texture problem, look at below(url), the left image is original and the right one is mapped on quad.

<a href=“Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos” target=“_blank”>
Click to link to the image
</a>

The format is BMP 24 bits with height and width same and in power of 2 dimension.

And in case code needed,

void LoadBitmap(char * filename, int texture_ID)
{
ifstream bmpfile;
GLubyte *bitmap;
int width, height;
int size, offset;

bmpfile.open(filename);
if(bmpfile == NULL)
{
MessageBox( NULL, “Cannot open bitmap file”,
“ERROR”, MB_OK | MB_ICONSTOP);
return;
}

bmpfile.seekg(0x0012);
bmpfile.read(reinterpret_cast<char*>(&width), 4);
bmpfile.read(reinterpret_cast<char*>(&height), 4);

bmpfile.seekg(0x0022);
bmpfile.read(reinterpret_cast<char*>(&size), 4);

bmpfile.seekg(0x000A);
bmpfile.read(reinterpret_cast<char*>(&offset), 4);

if( width != height && width != 64 && width != 128 &&
width != 256 && width != 512)
{
MessageBox( NULL, “Bitmap dimension not compatible”,
“ERROR”, MB_OK | MB_ICONSTOP);
return;
}

bitmap = new GLubyte[size];

bmpfile.seekg(offset);
bmpfile.read(reinterpret_cast<char*>(bitmap), size);

GLubyte temp;
for(int i = 0; i < size; i += 3)
{
temp = bitmap[i];
bitmap[i] = bitmap[i+2];
bitmap[i + 2] = temp;
}

glBindTexture(GL_TEXTURE_2D, texture_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);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap);

delete bitmap;
}

[This message has been edited by Questions Burner (edited 12-09-2001).]

Hi !
After a quick look I couldn’t find any problem, have you checked the bmpfile so there isn’t a problem in that one ?

As a side note, shouldn’t the if use or(| |) instead of and(&&) to check is width != height or width != 256 or …

Mikael

There’s no logical error here because if I use | |, the test will failed if one of the condition reached even if it’s power of 2 dimension.

And about the bmp file, it’s all ok because I can open it and view it. I’ve tested the program with many bmp file and different image give my different result as you already seen.

HELP!!!

I think your loading code is wrong. It has to do with data alignment.

I’ll check if I can find my (very old) bmp loader somewhere.

I had a quick look at my old code. You shouldn’t have problems with alignment because your bmps are 2^n wide. There can be problems when the bmp’s width is not divisible by 4 (ie each scan line is not DWORD aligned : in this case you pad it when it’s in memory, but you normally don’t have to write the file with padding bytes). Anyway, your bmps are guaranteed to be properly aligned because of their 2^n width.

Are you sure your filestream is in binary mode? If I remember well, streams are opened by default in text mode. If this is your case, this would explain why your textures are messed up : when reading a ’
’ char in the stream, it will put char(10) and char(13) in your buffer. This will completely ruin your image because everything after will be shifted.

Thank you very much, this problem has been bugging me quite some time and now it’s finally solve.

I open the file in binary mode and everything works fine now.