PDA

View Full Version : Texture problems.



Questions Burner
12-09-2001, 09:41 AM
I have a texture problem, look at below(url), the left image is original and the right one is mapped on quad.

<a href="http://www.geocities.com/bachew2000/forumq.html" 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*>(&amp;width), 4);
bmpfile.read(reinterpret_cast<char*>(&amp;height), 4);

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

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

if( width != height &amp;&amp; width != 64 &amp;&amp; width != 128 &amp;&amp;
width != 256 &amp;&amp; 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).]

mikael_aronsson
12-09-2001, 11:06 PM
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(&#0124; &#0124;) instead of and(&&) to check is width != height or width != 256 or ....

Mikael

Questions Burner
12-09-2001, 11:23 PM
There's no logical error here because if I use &#0124; &#0124;, 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. http://www.opengl.org/discussion_boards/ubb/confused.gif

HELP!!!

kehziah
12-10-2001, 12:58 AM
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.

kehziah
12-10-2001, 02:46 AM
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 '\n' 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.

Questions Burner
12-10-2001, 03:15 AM
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.

http://www.opengl.org/discussion_boards/ubb/biggrin.gif