PDA

View Full Version : loading bmp with glut



happy
12-10-2002, 02:56 PM
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);
};

Stack Overflow
12-10-2002, 03:34 PM
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.

Stack Overflow
12-10-2002, 03:38 PM
If that still does not work, make sure you bitmap is 256x256 pixels format.

- VC6-OGL

happy
12-10-2002, 04:56 PM
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...

Stack Overflow
12-10-2002, 05:11 PM
I can e-Mail you a sample code of loading a bitmap?

- VC6-OGL

prashantgp
12-10-2002, 11:48 PM
send a copy to me also at pgp123@rediffmail.com

Stack Overflow
12-11-2002, 07:26 AM
Hello happy,

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

Stack Overflow
12-11-2002, 07:37 AM
Hello prashantgp,

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

- VC6-OGL

jirkamelich
12-11-2002, 08:50 AM
do you join rendering context (wglMakeCurrent(Canvas->Handle,hRC) ) ? before the loading textures ?

happy
12-11-2002, 03:49 PM
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]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

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

if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
{
free(TextureImage[0]->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 !

happy
12-11-2002, 03:53 PM
VC6-OGL > I didn't receive any mail yet ! To which mail you've sent the package?

Stack Overflow
12-11-2002, 04:41 PM
I don't have your e-Mail address, happy.

prashantgp
12-11-2002, 09:39 PM
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

happy
12-12-2002, 02:56 AM
VC6-OGL > sorry http://www.opengl.org/discussion_boards/ubb/smile.gif here is my mail :

happy_16@caramail.com

thanks a lot

Stack Overflow
12-18-2002, 06:46 PM
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

Stack Overflow
12-18-2002, 06:50 PM
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).]

prashantgp
03-04-2003, 08:16 PM
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:
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).]

HalcyonBlaze
03-04-2003, 09:00 PM
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

prashantgp
03-05-2003, 01:57 AM
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,9 4,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();

}

prashantgp
03-05-2003, 07:34 PM
Hi Halycon Blaze, VC6-OGl , Nexusone,shintanugp
i hope u had a look on my post.
regards,
pgp

03-06-2003, 03:12 AM
while not the correct way to do it
use
glColor3f(0.0f,0.0f,0.0f);

just before you draw your quad with the picture.

prashantgp
03-06-2003, 07:57 PM
Originally posted by The_Spider:
while not the correct way to do it
use
glColor3f(0.0f,0.0f,0.0f);

just before you draw your quad with the picture.

Hi Spider,
i tried it . it didn't work.
i dont know what other people are doing??
why r rtey not replying????

Stack Overflow
03-08-2003, 09:59 AM
Hello prashantgp,

I will look into the Bitmap problem and I will see if I can derive an answer. One thing that could help is if you send me your code, if you don't mind, and I will see if I can figure out what the problem is.

- VC6-OGL

shinpaughp
03-08-2003, 01:37 PM
prashantgp,
I am not precisely sure what you are asking. I assume you have a texture mapped to a quad or other set of polygons and overtop you are drawing lines which change position with each rendering pass. But, do you want the lines to blend with the background? Is that what you are asking? If so,

Try:




glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...
draw your lines
...
glDisable(GL_BLEND);


You might want to mess around with the parameters for glBlendFunc and see what you like best.

If that is not what you are asking, please rephrase.

Actually, having reread your post I think I was wrong in what you wanted. Use glGetError() after your glTexImage call and see if it gives an error.

[This message has been edited by shinpaughp (edited 03-08-2003).]

shinpaughp
03-08-2003, 09:07 PM
Looking at your code even closer, it doesn't appear as though you have a glTexImage(1D, 2D, or 3D) call anywhere. You should probably call it from your init function similarly to how happy did it in the code above. Unless of course the function is called from the implementation file associated with bitmap.h

foniks
03-09-2003, 04:11 PM
I could be wrong, but doesn't your video card have to support 24 bit color to display 24bit color BMP's?

I know the last couple of video cards I bought only supported 16 and 32 bit color.

I once had a similar problem and the solution was to convert the image to 16 bit color.

I could be WAAAAAAAY off though.