How do you load bmp in OpenGL?

Started by
4 comments, last by opengl15 22 years, 1 month ago
I need help loading BMP files in opengl/GLUT because I have been trying sinse the past week and know hope at all so if somebody can help then that will be nice. Andrew rulz and u know it.........
Andrew rulz and u know it.........
Advertisement
look at the tutorials in this site (nehe.gamedev.net). look for the early texture loading ones.

good luck
I tried NeHe tutorials but no hope at all for me......Please somebody help me on how to load bmp files so i can be able to texture map my work....

Here''s my code that I tried but dont work?

//////Texture Information
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;
unsigned int texture;

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;

//open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if(filePtr == NULL)
return NULL;

//read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

//verify that this is a bitmap by checking for the universal bitmap id
if(bitmapFileHeader.bfType !=BITMAP_ID)
{
fclose(filePtr);
return NULL;
}

//read the bitmap information header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1 ,filePtr);

//move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

//allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

//verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

//read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

//make sure the bitmap date was read
if (bitmapImage ==NULL)
{
fclose(filePtr);
return NULL;
}

//swape the R and B values to get RGB sinse the bitmap color format is in BGR
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}

//close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;

}

GLuint mapTexture(void)
{
unsigned char texels[]={255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255};
GLuint textureName; glGenTextures(1, &textureName);
glBindTexture(GL_TEXTURE_2D, textureName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, texels); return textureName;
}

void init(void)
{
glClearColor(0.0f ,0.0f ,0.0f ,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glClearDepth(1.0f);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);

//load Bitmap file
bitmapData = LoadBitmapFile("C:\\Documents and Settings\\Owner\\Desktop\\DEV save\\paint.bmp", &bitmapInfoHeader);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

//load the texture image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);

Andrew rulz and u know it.........
Andrew rulz and u know it.........
This is the function I use...

BYTE *swLoadBitmap(const char *filename, int &w, int &h){    FILE *fp;              BYTE *bits;    int bitsize;      	int infosize;	int bajs;    BITMAPFILEHEADER header;       	BITMAPINFO **info;    if((fp = fopen(filename, "rb")) == NULL)        return (NULL);    if(fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)    {		fclose(fp);        return (NULL);    }    if(header.bfType != ''MB'')    {        fclose(fp);        return (NULL);    }	infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);    if((*info = (BITMAPINFO *)malloc(infosize)) == NULL)    {        fclose(fp);        return (NULL);    }    if((int)fread(*info, 1, infosize, fp) < infosize)    {        free(*info);        fclose(fp);        return (NULL);    }    if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)        bitsize = ((*info)->bmiHeader.biWidth *                   (*info)->bmiHeader.biBitCount + 7) / 8 *  	           abs((*info)->bmiHeader.biHeight);    if((bits = new GLubyte[bitsize]) == NULL)    {        fclose(fp);        return (NULL);    }    if((bajs = fread(bits, 1, bitsize, fp)) < bitsize)    {        fclose(fp);        return (bits);    }    fclose(fp);	w = (*info)->bmiHeader.biWidth;	h = (*info)->bmiHeader.biHeight;	return (bits);} 


then just use GL_BGR_EXT instead of GL_RGB in glTexImage2d()
Can somebody post a whole peice of code to show me how to do it(loading bmp files in opengl) because its starting to be a pain in the ass.

Andrew rulz and u know it.........
Andrew rulz and u know it.........
Load any picture format there is with DevIL

openil.sourceforge.net
www.EberKain.comThere it is, Television, Look Listen Kneel Pray.

This topic is closed to new replies.

Advertisement