PDA

View Full Version : code not working!!! :-(



01-14-2001, 07:06 PM
hi there
here is a code for displaying a bmp image in opengl. it gives no error and a few warnings. i've made it in VC++ IDE using opengl. but it does not actually load the bitmap.
(1) what should be the value of info parameter passed in LoadDIBitmap function
(2) or any other clue which makes it work

here is the code:

#include<windows.h>
#include<stdio.h>
#include<GL/glut.h>

void *bits;


void *LoadDIBitmap(char *filename,BITMAPINFO **info)
{
FILE *fp; /* Open file pointer */
/* Bitmap pixel bits */
long bitsize, /* Size of bitmap */
infosize; /* Size of header information */
BITMAPFILEHEADER header; /* File header */

/*
* Try opening the file; use "rb" mode to read this *binary* file.
*/

if ((fp = fopen(filename, "rb")) == NULL)
return (NULL);

/*
* Read the file header and any following bitmap information…
*/

if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
{
/*
* Couldn't read the file header - return NULL…
*/

fclose(fp);
return (NULL);
};

if (header.bfType != 'MB') /* Check for BM reversed… */
{
/*
* Not a bitmap file - return NULL…
*/

fclose(fp);
return (NULL);
};

infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
*info = (BITMAPINFO *)malloc(infosize);
if (*info == NULL)
{
/*
* Couldn't allocate memory for bitmap info - return NULL…
*/

fclose(fp);
return (NULL);
};

if (fread(*info, 1, infosize, fp) < infosize)
{
/*
* Couldn't read the bitmap header - return NULL…
*/

free(*info);
fclose(fp);
return (NULL);
};

/*
* Now that we have all the header info read in, allocate memory for the
* bitmap and read *it* in…
*/

if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth *(*info)->bmiHeader.biBitCount + 7) / 8 * abs((*info)->bmiHeader.biHeight);

if ((bits = malloc(bitsize)) == NULL)
{
/*
* Couldn't allocate memory - return NULL!
*/

free(*info);
fclose(fp);
return (NULL);
};

if (fread(bits, 1, bitsize, fp) < bitsize)
{
/*
* Couldn't read bitmap - free memory and return NULL!
*/

free(*info);
free(bits);
fclose(fp);
return (NULL);
};

/*
* OK, everything went fine - return the allocated bitmap…
*/

fclose(fp);
return (bits);
}


void RepaintWindow(RECT *rect) /* I - Client area rectangle */
{
GLint xoffset, /* X offset of image */
yoffset; /* Y offset of image */
GLint xsize, /* X size of scaled image */
ysize; /* Y size of scaled image */
GLfloat xscale, /* Scaling in X direction */
yscale; /* Scaling in Y direction */
BITMAPINFO *BitmapBits;
BITMAPINFO *BitmapInfo;

BitmapBits=bits;


/*
* Reset the viewport and clear the window to white
*/

glViewport(0, 0, rect->right, rect->bottom);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, rect->right - 1.0, 0.0, rect->bottom - 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

/*
* If we have loaded a bitmap image, scale it to fit the window…
*/

if (BitmapBits != NULL)
{
xsize = rect->right;
ysize = BitmapInfo->bmiHeader.biHeight * xsize / BitmapInfo->bmiHeader.biWidth;

if (ysize > rect->bottom)
{
ysize = rect->bottom;
xsize = BitmapInfo->bmiHeader.biWidth * ysize / BitmapInfo->bmiHeader.biHeight;
};

xscale = (float)xsize / (float)BitmapInfo->bmiHeader.biWidth;
yscale = (float)ysize / (float)BitmapInfo->bmiHeader.biHeight;

xoffset = (rect->right - xsize) * 0.5;
yoffset = (rect->bottom - ysize) * 0.5;

glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glPixelZoom(xscale, yscale);
glRasterPos2i(xoffset, yoffset);
glDrawPixels(BitmapInfo->bmiHeader.biWidth,
BitmapInfo->bmiHeader.biHeight,
GL_RGB, GL_UNSIGNED_BYTE, BitmapBits);

};

glFinish();
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
}

void display(void)
{

char *str="bubbles.bmp";
RECT *rect;
BITMAPINFO *in;

rect->bottom=100;
rect->right=100;

LoadDIBitmap(str,in);
RepaintWindow(rect);

glFinish();
}

void reshape(int w,int h)
{ glViewport(0.0,0.0,(GLsizei) w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);

glLoadIdentity();
gluPerspective(60.0,(GLfloat) w/(GLfloat) h,1.0,20.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

------------*------------

thanks a ton
anxious

Sim
01-14-2001, 08:23 PM
Hello!

I don't know whats wrong with your code, but
if you just want to load a BMP, I can help you, try to use this function:
(based on nehes code)

GLuint LoadGLTexture(char *filename,int filter) // Load Bitmaps And Convert To Textures
{

GLuint tempadd; // Storage for one texture

AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL

if (filter > 2) //Invaild filter
{

exit(1);
}

if (TextureImage[0]=auxDIBImageLoad(filename))
{
glGenTextures(1, &tempadd); //Setup storage

glBindTexture(GL_TEXTURE_2D, tempadd);

if (filter==0)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}

if (filter==1)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}

if (filter==2)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}

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 tempadd; // Return The Texture Structure
}


It is easy to use:

Gluint newtex;
newtex = LoadGLTexture("bubbles.bmp",2);
glBindTexture(GL_TEXTURE_2D,newtex);

That's all, your texture is now active.



[This message has been edited by Sim (edited 01-14-2001).]