PDA

View Full Version : LoadBMP: Some BMP images render OK, others not...?



torbjorn
09-18-2002, 03:27 AM
Hi!

I use the LoadBMP()-function to display some BMP-files.

I am modifying some existing code and some existing bitmaps.

The code should work fine, because when I render the old bitmaps (that I have not made myself), the images are shown nicely.

But when I try to make my own .bmp-files, then I get into trouble. Only a white square is shown, and not the image.

I have tried using 24-bit, 256-color and 8-bit versions of the BMP-images, but with the same results!

How come?


Thanks,
Torbjørn

Gavin
09-18-2002, 03:33 AM
How are you displaying them, or rather trying to? drawpixels?

torbjorn
09-18-2002, 03:47 AM
I found the error :-)

I did a search for the the rendering function that I use, at it seems to be collected from http://nehe.gamedev.net/tutorials/lesson.asp?l=06

I also discovered my error here - I should use images with height and width that are power of 2 (64, 256...).

That's a bit cumbersome, I think...How can I go about this?

These are the functions that I use:
/******************************************/
// Loads a bitmap image
AUX_RGBImageRec *LoadBMP(char *Filename)
{
// File handler
FILE *File=NULL;
// Make shure a file name was given
if (!Filename)
{
return NULL;
}
// Check to see if the file exists
File=fopen(Filename,"r");
// Make shure the file was loaded
if (File)
{
fclose(File);
// Load the bitmap and return a pointer
return auxDIBImageLoad(Filename);
}
// if load failed return null
return NULL;
}

// Load bitmap and convert to texture - height and width MUST be power of 2 (64,128,256...)
LoadGLTextures(char * fname)
{
// Status indicator
int Status=FALSE;
// Create storage space for the texture
AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP(fname))
{
Status=TRUE;
// Create the texture
glGenTextures(2, &texture[0]);

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);

// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Linear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
// Linear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

// If texture exists
if (TextureImage[0])
{
// If texture image exists
if (TextureImage[0]->data)
{
// Free the image memory
free(TextureImage[0]->data);
}
// Free the image structure
free(TextureImage[0]);
}
return Status;
}
/*******************************************/

nexusone
09-18-2002, 04:00 AM
I use TGA file loader, works well.
Your other option would be to find another BMP loader routine to use. There are other people out there who have writen image loading libs.

Some of the lib's let you load GIF,JPG etc.


Originally posted by torbjorn:
I found the error :-)

I did a search for the the rendering function that I use, at it seems to be collected from http://nehe.gamedev.net/tutorials/lesson.asp?l=06

I also discovered my error here - I should use images with height and width that are power of 2 (64, 256...).

That's a bit cumbersome, I think...How can I go about this?

These are the functions that I use:
/******************************************/
// Loads a bitmap image
AUX_RGBImageRec *LoadBMP(char *Filename)
{
// File handler
FILE *File=NULL;
// Make shure a file name was given
if (!Filename)
{
return NULL;
}
// Check to see if the file exists
File=fopen(Filename,"r");
// Make shure the file was loaded
if (File)
{
fclose(File);
// Load the bitmap and return a pointer
return auxDIBImageLoad(Filename);
}
// if load failed return null
return NULL;
}

// Load bitmap and convert to texture - height and width MUST be power of 2 (64,128,256...)
LoadGLTextures(char * fname)
{
// Status indicator
int Status=FALSE;
// Create storage space for the texture
AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP(fname))
{
Status=TRUE;
// Create the texture
glGenTextures(2, &texture[0]);

// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);

// Generate The Texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

// Linear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
// Linear filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
}

// If texture exists
if (TextureImage[0])
{
// If texture image exists
if (TextureImage[0]->data)
{
// Free the image memory
free(TextureImage[0]->data);
}
// Free the image structure
free(TextureImage[0]);
}
return Status;
}
/*******************************************/

dabeav
09-18-2002, 04:06 AM
I recomend using DevIL, www.imagelib.org (http://www.imagelib.org)
It will load just about any image format, and will setup OpenGL texture objects, and mippmapps quite easily, with like 2-3 simple function calls.

Gavin
09-18-2002, 04:37 AM
Are you trying to display an image as opposed to texture mapping? Well anyway try mipmapping or you can 'pad' your non 2^n bmp file with (0,0,0,0) (for rbga) to make it a 2^n image, then when applying the texture scale it appropriately.

torbjorn
09-19-2002, 04:06 AM
Thanks, guys!

Guess I'll settle with my LoadBMP for now. Refinements will come later :-)

Torbjørn