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).]