I coded a function to load a BMP image without using the AuxBMP function. It is based from "OpenGL Game Progamming" book. The code is well compiled but I cannot load the texture. Here is the code to load a BMP file:
unsigned char *LoadBMP(char* pFilename,
bmpInfoHeader* bitmapInfoHr)
{
// Declaration of local variables
FILE* filePtr; // The file pointer
BITMAPFILEHEADER bitmapFileHeader; // Bitmap file header
unsigned char* pBitmapImage; // Bitmap image data
int imageIndex = 0; // Image index counter
unsigned char szTempRGB; // Swap variable
// Open a filename in "read binary" mode
filePtr = fopen(pFilename, "rb");
// Return nothing if file pointer does not exist
if(filePtr == NULL){return NULL;}
// Read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
// Verify 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(bitmapInfoHr, sizeof(bmpInfoHeader), 1, filePtr);
// Move file pointer to the beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
// Allcate enough memory for the bitmap image data
pBitmapImage = (unsigned char*)malloc(bitmapInfoHr->biSizeImage);
// Verify memory allocation
if(!pBitmapImage)
{
free(pBitmapImage);
fclose(filePtr);
return NULL;
}
// Read in the bitmap image data
fread(pBitmapImage, 1, bitmapInfoHr->biSizeImage, filePtr);
// Make sure bitmap image data was read
if(pBitmapImage == 0)
{
fclose(filePtr);
return NULL;
}
// Swap the R and B value to get RGB since the bitmap color format is in
// BGR
for(imageIndex = 0; imageIndex < bitmapInfoHr->biSizeImage;
imageIndex += 3)
{
szTempRGB = pBitmapImage[imageIndex];
pBitmapImage[imageIndex] = pBitmapImage[imageIndex + 2];
pBitmapImage[imageIndex + 2] = szTempRGB;
}
// Close the file and return the bitmap image data
fclose(filePtr);
return pBitmapImage;
}
For academic purpose, I did not make the LoadTexture() function. I loaded the texture in InitGL instead.
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
// glEnable(GL_CULL_FACE);
// glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
// Load our bitmap file
bmpData = LoadBMP("data/muandi3.bmp", &bmpInfoHeader);
glGenTextures(1, &iTexture); // Generate texture object
glBindTexture(GL_TEXTURE_2D, iTexture); // Bind the texture
// Load the texture image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpInfoHeader.biWidth,
bmpInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
bmpData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return true; // Initialization Went OK
}
I wonder where is the problem. I will code a similar code using the tutorial to compare.