How to Load A Bitmap From File

How to Load a Bitmap from File:

Created by VC6-OGL - Thanks to NeHe for Bitmap Loading Functions

#include <windows.h>
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <gl/glaux.h>
#include <stdio.h>

#pragma comment( lib, “glu32.lib” )
#pragma comment( lib, “glaux.lib” )

///////////////
GLuint texture[1];
///////////////

AUX_RGBImageRec *LoadBMP( char *Filename ) {
FILE *File = NULL;

if ( !Filename ) {
return NULL;
}

File = fopen( Filename,“r” );

if ( File ) {
fclose( File );
return auxDIBImageLoad( Filename );
}

return NULL;
}

int LoadGLTextures() {
int Status = FALSE;

AUX_RGBImageRec *TextureImage[1];

memset( TextureImage, 0, sizeof( void * )*1 );

if ( TextureImage[0] = LoadBMP( “Whatever.bmp” ) ) {
Status = TRUE;

glGenTextures( 1, &texture[0] );

glBindTexture( GL_TEXTURE_2D, texture[0] );
glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}

if ( TextureImage[0] ) {
if ( TextureImage[0] -> data ) {
free( TextureImage[0] -> data );
}

free( TextureImage[0] );
}

return Status;
}

void init( void ) {
if ( !LoadGLTextures() ) {
MessageBox( NULL, “Cannot Load - Whatever.bmp”, “Error”, MB_ICONINFORMATION | MB_OK );
exit( 0 );
}

glEnable( GL_TEXTURE_2D );
glClearColor ( 0.0, 0.0, 0.0, 0.0 );
glShadeModel ( GL_SMOOTH );
glEnable ( GL_DEPTH_TEST );
}

void reshape( int w, int h ) {
if( h == 0 )
h = 1;

float ratio = 1.0 * w / h;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glViewport( 0, 0, w, h );

gluPerspective( 25, ratio, 1, 1000 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f );
}

void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();

glBindTexture( GL_TEXTURE_2D, texture[0] );

glBegin( GL_POLYGON );
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( -1.33, -1.0f, 0.5f );
glTexCoord2f( 1.0f, 0.0f ); glVertex3f( 1.33, -1.0f, 0.5f );
glTexCoord2f( 1.0f, 1.0f ); glVertex3f( 1.33, 1.0f, 0.5f );
glTexCoord2f( 0.0f, 1.0f ); glVertex3f( -1.33, 1.0f, 0.5f );
glEnd();

glPopMatrix();

glutSwapBuffers();
}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit( 0 );
break;
}
}

void main( int argc, char **argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
glutEnterGameMode();
init();
glutDisplayFunc( display );
glutIdleFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );

glutMainLoop();
}

[This message has been edited by VC6-OGL (edited 12-11-2002).]

Take a look at www.gametutorials.com , that should satisfy you. By the way, I’ve heard that glaux is some sort of beta/demo library for quick effect demos but isn’t recomended for serious work

This Post was created for people to understand Bitmap Loading. The code is perfectly fine, but it’s hard trying to explain to everyone how to do it. So I created some code for people to copy and tell me if they like it or not.

Make sure your bitmap is setup as the following:

· 256*256 pixel
· 24 bit color

  • VC6-OGL

Why go though all that headacke. go to www.imagelib.org and downlaod devIL. It is opengl style syntax for image loading. ALL kinds of images. It will also set properties quite well for you.

Hi Andy,
when i tried with bmp of 256256 , it is doing fine.
but the bmp i want to use is not 256
256.
do u know how to change the properties of bmp
without hampering complexion of the picture.
thanks and regards
prashantgp

When you resize the image, it does reduce the chance of a picture looking nice.

In order to change the bitmap size, you can take it into your Paint program and resize it to 256*256. If you have a nice picture and you don’t want to resize it, you will not be able to use the “AUX_RGBImageRec” code. The code manages images like, 2x2, 4x4, 64x64, 128x128, 256x256 pixels. I will see if I can find another code that allows you to load a bitmap without a pixel size barrier.

  • VC6-OGL

[This message has been edited by VC6-OGL (edited 12-13-2002).]

[This message has been edited by VC6-OGL (edited 12-13-2002).]