PDA

View Full Version : Funny Bitmap Problem



rmmlg
05-15-2005, 02:32 PM
I have finally gotten OpenGL to recognize importing my bitmaps to some extent. The problem now is that it is completely screwing them up. For example, the image to be imported (left) comes out as (right):

http://neverland.ncssm.edu/~mclinkor/bitmap.bmp

Here is the relevant code:

int paint()
{
//InitGL();
//glTranslatef(0, 0,-5.0f);
glEnable(GL_TEXTURE_2D);
//draw a square with specified texture coordinates
double xvals[] = {cur_x_cross, cur_x_cross + 10, cur_x_cross + 10, cur_x_cross};
double yvals[] = {cur_y_cross, cur_y_cross, cur_y_cross + 10, cur_y_cross + 10};
float svals[] = {0.0f, 0.0f, 1.0f, 1.0f};
float tvals[] = {1.0f, 0.0f, 0.0f, 1.0f};

glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(cur_x_cross, cur_y_cross,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(cur_x_cross + 30, cur_y_cross,0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(cur_x_cross + 30, cur_y_cross + 30,0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(cur_x_cross, cur_y_cross + 30,0);

/*for (int i=0; i < 4; i++)
{
glTexCoord2f(svals[i], tvals[i]);
glVertex2f(xvals[i], yvals[i]);
}*/
glEnd();
glDisable(GL_TEXTURE_2D);
//glFlush();
glutSwapBuffers();

return TRUE;
}

void LoadBitmap(UINT textureArray[], LPSTR strFileName, int ID)
{
if(!strFileName) return;

AUX_RGBImageRec *pBitMap = auxDIBImageLoad(strFileName);

glGenTextures(1, &amp;textureArray[ID]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitMap->sizeX, pBitMap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitMap->data);

}

int InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glEnable(GL_TEXTURE_2D);
LoadBitmap(TextureArray, "untitled.bmp", 0);

return TRUE;
}

dvm
05-15-2005, 04:50 PM
I don't know I've never used glaux for loading my bitmaps before. Are you sure your bitmap is 24 bits (GL_RGB)? Double check on that.
Also you could use your own bitmap loading code, it's not that hard. You could either look at nehe (http://nehe.gamedev.net) 's tutorials or search the source code for OpenGL game programming which has a nice C function. It should be on chapter 6.

05-15-2005, 06:37 PM
Check to see if your bitmap is in multiples of 2 such as 256 X 256 or 512 X 512. Otherwise the bitmap will screw up like that.

05-16-2005, 12:23 PM
Have you tried GL_RGBA?

Your resulting image 'looks' like it's skipping the alpha channel.

Dunno.

T101
05-16-2005, 11:52 PM
AFAIK bmp does not support an alpha channel.
But here's some things to consider:

1. I believe you need to bind the texture ID before you upload the texture. glGenTextures is not enough to do that - add this before the gluBuild2Dmipmaps call:

glBindTexture(GL_TEXTURE_2D, texture[ID]); 2. If the height and width of the bitmap are not powers of two, that usually does not result in this behaviour. Either nothing is uploaded (no texture displayed), or the texture is padded to the next power of two.

3. Are you sure you're not just looking at garbage here? I suggest printing an error message if the file cannot be opened. (i.e. when pBitMap==0)

Relic
05-17-2005, 02:23 AM
The image didn't work for me, but your texture coordinates are definitely wrong.

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(cur_x_cross, cur_y_cross,0);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(cur_x_cross + 30, cur_y_cross,0);
glTexCoord2f(0.0f, 1.0f); // BUG! Same as first.
glVertex3f(cur_x_cross + 30, cur_y_cross + 30,0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(cur_x_cross, cur_y_cross + 30,0);
glEnd();OpenGL's default winding is counter-clockwise.
I would do this (which is also in line with the glTexImage2D image orientation):

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(cur_x_cross, cur_y_cross,0);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(cur_x_cross + 30, cur_y_cross,0);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(cur_x_cross + 30, cur_y_cross + 30,0);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(cur_x_cross, cur_y_cross + 30,0);
glEnd();