How do you load bmp in OpenGL?
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.........
look at the tutorials in this site (nehe.gamedev.net). look for the early texture loading ones.
good luck
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.........
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.........
This is the function I use...
then just use GL_BGR_EXT instead of GL_RGB in glTexImage2d()
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()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement