loading bmp with glut

I have tried this to load a bmp picture but it doesn’t work! I’m using glut…

unsigned texture[1];

void LoadTexture()
{
AUX_RGBImageRec *texture1;
texture1 = auxDIBImageLoad(“wall.bmp”);
glGenTextures (1, &texture[0]);
glBindTexture (GL_TEXTURE_2D, texture[0]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, texture1->sizeX, texture1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, texture1->data);
};

Bitmap Loading Code:

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 );

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ( TextureImage[0] = LoadBMP( "Whatever.bmp" ) ) {
	Status = TRUE;

	glGenTextures( 1, &texture[0] );

	// Typical Texture Generation Using Data From The Bitmap
	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;

}

Code for Initialization:

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

glEnable( GL_TEXTURE_2D );

glBindTexture( GL_TEXTURE_2D, texture[0] );

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

Hope this helps.

If that still does not work, make sure you bitmap is 256x256 pixels format.

  • VC6-OGL

I’ve just tried what you said but unfortunately it didn’t work

I use a bmp 256*256, 24 bits : the file seams to be loaded but the texture is not represented on the drawing ! I have a white background…

I can e-Mail you a sample code of loading a bitmap?

  • VC6-OGL

send a copy to me also at pgp123@rediffmail.com

Hello happy,

I would like to e-Mail you the package, so if you have an e-Mail address please let me know.

Hello prashantgp,

The file has been sent to both of your e-Mail addresses, and the file is only 10kb.

  • VC6-OGL

do you join rendering context (wglMakeCurrent(Canvas->Handle,hRC) ) ? before the loading textures ?

wglMakeCurrent(Canvas->Handle,hRC) is used in win32

I’m using glut so there is no wglMakeCurrent
here is the entire code :


#include <stdio.h>
#include <gl/glut.h> // The GL Utility Toolkit (Glut) Header
#include <gl/glaux.h>

void terrain();

GLfloat light_position[] = { 0.0, 2.0, 0.0, 1.0 };
GLfloat mat_amb_diff_r[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_amb_diff_b[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 100.0 };
GLfloat mat_emission[] = {1, 1, 1, 1.0};

GLuint texture[1]; // Storage For One Texture ( NEW )

int b=0;

AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image
{
FILE *File=NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File=fopen(Filename,“r”); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}

int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture
memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if (TextureImage[0]=LoadBMP("wall.bmp"))
{
	Status=TRUE;							// Set The Status To TRUE
	glGenTextures(1, &texture[0]);					// Create The Texture

	// 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]-&gt;sizeX, TextureImage[0]-&gt;sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]-&gt;data);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	// Linear Filtering
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
}

if (TextureImage[0])							// If Texture Exists
{
	if (TextureImage[0]-&gt;data)					// If Texture Image Exists
	{
		free(TextureImage[0]-&gt;data);				// Free The Texture Image Memory
	}
	free(TextureImage[0]);						// Free The Image Structure
}
return Status;								// Return The Status

}

void init ( GLvoid ) // Create Some Everyday Functions
{
glClearColor (0,0,0,1);

if ( !LoadGLTextures() ) {
MessageBox( NULL, "Cannot Load - wall.bmp", "Error", MB_ICONINFORMATION | MB_OK );
exit( 0 );
}
glEnable (GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, texture[0] );


glClearDepth(1.0f);									// Depth Buffer Setup
glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do

glEnable ( GL_COLOR_MATERIAL );
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glShadeModel (GL_SMOOTH);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

}

void display ( void ) // Create The Display Function
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();

gluLookAt (0,5,20,0,0,0,0,1,0); 

b++;
glRotated (b,0,0,0);


glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff_r);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);

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

glutSwapBuffers ( );
// Swap The Buffers To Not Be Left With A Clear Screen
}

void reshape ( int w, int h ) // Create The Reshape Function (the viewport)
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION ); // Select The Projection Matrix
glLoadIdentity ( ); // Reset The Projection Matrix
if ( h==0 ) // Calculate The Aspect Ratio Of The Window
gluPerspective ( 45, ( float ) w, 1.0, 100.0 );
else
gluPerspective ( 45, ( float ) w / ( float ) h, 1.0, 100.0 );
glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix
glLoadIdentity ( ); // Reset The Model View Matrix
}

void keyboard ( unsigned char key, int x, int y ) // Create Keyboard Function
{
switch ( key ) {
case 27: // When Escape Is Pressed…
exit ( 0 ); // Exit The Program
break; // Ready For Next Case
default: // Now Wrap It Up
break;
}
}

void arrow_keys ( int a_keys, int x, int y ) // Create Special Function (required for arrow keys)
{
switch ( a_keys ) {
case GLUT_KEY_DOWN: // When Down Arrow Is Pressed…
glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window
break;
case GLUT_KEY_UP: // When Up Arrow Is Pressed…
glutFullScreen ( ); // Go Into Full Screen Mode
break;
default:
break;
}
}

void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together
{
glutInit ( &argc, argv ); // Erm Just Write It =)
init ();
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
glutInitWindowSize ( 640, 480 ); // If glutFullScreen wasn’t called this is the window size
glutInitWindowPosition (100, 100);
glutCreateWindow ( “NeHe’s OpenGL Framework” ); // Window Title (argv[0] for current directory as title)
//glutFullScreen ( ); // Put Into Full Screen
glutDisplayFunc ( display ); // Matching Earlier Functions To Their Counterparts
glutIdleFunc( display );
glutReshapeFunc ( reshape );
glutKeyboardFunc ( keyboard );
glutSpecialFunc ( arrow_keys );
glutMainLoop ( ); // Initialize The Main Loop
}


Thanks !

VC6-OGL > I didn’t receive any mail yet ! To which mail you’ve sent the package?

I don’t have your e-Mail address, happy.

Hi all,
Hello Andy(VC6-OGL),
Thank u very much for the help.
Ur program is running absolutely fine and I am getting the background picture. But when I try to load some other bmp file , I am not getting the picture instead entire thing is becoming white. I have attached the bmp , which I want to use as background. Will u pls help me in overcoming this problem. Regards
Bye bye
prashant

VC6-OGL > sorry here is my mail :

happy_16@caramail.com

thanks a lot

Sorry happy,

for not responding to your request, but I just sent you the e-Mail moments ago.

Does anyone else want the code. If so, just say “yes”.

  • VC6-OGL

Please make sure that all bitmaps loaded into this program are:

· 2x2
· 4x4
· 64x64
· 128x128
· 256x256 pixel format

  • VC6-OGL

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

Hi ,
back to the old problem.
will u pls tell me how to make a bmp to

· 2x2
· 4x4
· 64x64
· 128x128
· 256x256 pixel format

any of this if it is not in this format.

regards,
Prashant G.P

Originally posted by VC6-OGL:
[b]Please make sure that all bitmaps loaded into this program are:

· 2x2
· 4x4
· 64x64
· 128x128
· 256x256 pixel format

  • VC6-OGL

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

In any picture editor, you should be able to change the image properties. Inside there you should be able to set a pictures width and height in several units. You want to choose the pixel units.

  • Halcyon

i did that.
now texture is correctly fitting to my window.
but the problem is how to draw any objects on that.

once i bind the texture, i am drawing some moving lines . the problem is the color of texture is getting converted to whatever color i choose to draw line.
i am unable to figure it out.
pls help me out.

the code is as follows.

/*
Created by VC6-OGL
Main.cpp: Bitmap Loading
*/

#include <windows.h>
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <stdio.h>
#include “Bitmap.h”

int w = 600,h = 500;

int sw, pw, cw, state = 0;

int hsiyaw[51]={50,50,50,50,50,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95};

static char stringlabel[100];

void drawstring (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, s[i]);
}

void drawSmall (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_9_BY_15, s[i]);
}

void
idle (void)
{
float i = 0;
for (i = 0; i < 20000; i += 0.001)
{
}

if (state == 50)
state = 0;
state++;

glutSetWindow (cw);
glutPostRedisplay ();

}

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

glEnable( GL_TEXTURE_2D );
glClearColor ( 0.0, 0.0, 0.0, 0.0 );							// Clear Color - Black
glShadeModel ( GL_SMOOTH );										// Smooth Shade Model
//glEnable ( GL_DEPTH_TEST );										// Enable Depth Test

}

void
reshape (int w, int h)
{

// Prevent a divide by zero
if(h == 0)
	h = 1;

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-1.0, 1.0, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

void display( void )
{

int i = 0, j = 0;
glutSetWindow (pw);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity ();

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();

glColor3f (1.0, 0.0, 0.0);
glLineWidth (1.0);
glTranslatef (-(hsiyaw[state] * 0.1 * 0.4), 0.0, 0.0);
for (i = 0; i < 36; i += 1)
{
glBegin (GL_LINES);
glVertex2f (i * 0.40, 0.4);
glVertex2f (i * 0.40, -0.6);
glVertex2f (i * 0.20, -0.1);
glVertex2f (i * 0.20, 0.0);
glEnd ();
glRasterPos2f (i * 0.40, 0.6);
sprintf (stringlabel, “%d”, i);
drawSmall (stringlabel);
glFlush ();
}

j = 1;
for (i = 35; i >= 0; i -= 1)
{
glBegin (GL_LINES);
glVertex2f (-j * 0.40, 0.4);
glVertex2f (-j * 0.40, -0.6);
glVertex2f (-j * 0.20, -0.1);
glVertex2f (-j * 0.20, 0.0);
glEnd ();
glRasterPos2f (-j * 0.40, 0.6);
sprintf (stringlabel, “%d”, i);
drawSmall (stringlabel);
j += 1;
glFlush ();
}

glutSwapBuffers();

}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27: // On ‘Esc’
exit( 0 ); // Quit
break;
}
}

void main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize (w,h);
glutInitWindowPosition (150,10);
pw = glutCreateWindow (“Multi Function Display”);
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutIdleFunc (idle);

glutMainLoop();

}

Hi Halycon Blaze, VC6-OGl , Nexusone,shintanugp
i hope u had a look on my post.
regards,
pgp