Hi Guys n Gals, I have a little problem and would appreciate any advice anyone can give…
I am working on my first demo and have a bug in the loadBMP function which causes problems with the drawing of the rest of the scene. I know it is with this function as when I remove it, everything is Rosy. After the following function has executed, the color on the screen changes, with the blue elements turning black and the white parts turning brown I have fiddled about with the glTexImage2D paramenter but dont really know much about them just yet so don’t seem to be getting any further.
Any help would be greatly appreciated, Thanks in advance…
BennUK
int Ctexture::LoadBMP(const char* szFileName)
{
HANDLE hFileHandle;
BITMAPINFO *pBitmapInfo = NULL;
unsigned long lInfoSize = 0;
unsigned long lBitSize = 0;
int nTextureWidth;
int nTextureHeight;
BYTE *pBitmapData;
// Open the Bitmap file
hFileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
// Check for open failure (most likely file does not exist).
if(hFileHandle == INVALID_HANDLE_VALUE)
return FALSE;
// File is Open. Read in bitmap header information
BITMAPFILEHEADER bitmapHeader;
DWORD dwBytes;
ReadFile(hFileHandle,&bitmapHeader,sizeof(BITMAPFILEHEADER),
&dwBytes,NULL);
__try {
if(dwBytes != sizeof(BITMAPFILEHEADER))
return FALSE;
// Check format of bitmap file
if(bitmapHeader.bfType != 'MB')
return FALSE;
// Read in bitmap information structure
lInfoSize = bitmapHeader.bfOffBits - sizeof(BITMAPFILEHEADER);
pBitmapInfo = (BITMAPINFO *) new BYTE[lInfoSize];
ReadFile(hFileHandle,pBitmapInfo,lInfoSize,&dwBytes,NULL);
if(dwBytes != lInfoSize)
return FALSE;
nTextureWidth = pBitmapInfo->bmiHeader.biWidth;
nTextureHeight = pBitmapInfo->bmiHeader.biHeight;
lBitSize = pBitmapInfo->bmiHeader.biSizeImage;
if(lBitSize == 0)
lBitSize = (nTextureWidth *
pBitmapInfo->bmiHeader.biBitCount + 7) / 8 *
abs(nTextureHeight);
// Allocate space for the actual bitmap
pBitmapData = new BYTE[lBitSize];
// Read in the bitmap bits
ReadFile(hFileHandle,pBitmapData,lBitSize,&dwBytes,NULL);
if(lBitSize != dwBytes)
{
if(pBitmapData)
delete [] (BYTE *) pBitmapData;
pBitmapData = NULL;
return FALSE;
}
}
__finally // Fail or success, close file and free working memory
{
CloseHandle(hFileHandle);
if(pBitmapInfo != NULL)
delete [] (BYTE *)pBitmapInfo;
}
// This is specific to the binary format of the data read in.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexImage2D(GL_TEXTURE_2D, 0, 4, nTextureWidth, nTextureHeight, 0,
GL_BGR_EXT, GL_UNSIGNED_BYTE, pBitmapData);
if(pBitmapData)
delete [] (BYTE *) pBitmapData;
return TRUE;
}