I have the following code:
BOOL LoadBitmap(LPTSTR szFileName, GLuint &texture)
{
HANDLE fileHandle;
//open the bitmap file
fileHandle = CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if( fileHandle == INVALID_HANDLE_VALUE )
{
MessageBox(hWnd,“Cannot open file”,“ERROR”,MB_OKCANCEL | MB_ICONASTERISK);
exit(0);
return FALSE;
}
BITMAPFILEHEADER bmpHeader;
BYTE *pBitmapData;
BITMAPINFO *pBitmapInfo;
DWORD dwBytesToRead;//read in the bitmap header information
ReadFile(fileHandle,&bmpHeader,sizeof(BITMAPFILEHEADER),&dwBytesToRead,NULL);
int texWidth, texHeight;
try{
if( dwBytesToRead != sizeof(BITMAPFILEHEADER) )
{
return FALSE;
}
//check format of bitmap file if ( bmpHeader.bfType != 'MB') { return FALSE; }
/*read in bitmap information structure*/ unsigned long lInfoSize; unsigned long lBitSize;
lInfoSize = bmpHeader.bfOffBits - sizeof(BITMAPFILEHEADER); pBitmapInfo = (BITMAPINFO *) malloc(sizeof(BYTE)*lInfoSize); ReadFile(fileHandle,pBitmapInfo,lInfoSize,&dwBytesToRead,NULL);
if(dwBytesToRead != lInfoSize){ free(pBitmapInfo); CloseHandle(fileHandle); return FALSE; } texWidth = pBitmapInfo->bmiHeader.biWidth; texHeight = pBitmapInfo->bmiHeader.biHeight; lBitSize = pBitmapInfo->bmiHeader.biSizeImage;
if( lBitSize == 0) { lBitSize = (texWidth * pBitmapInfo->bmiHeader.biBitCount + 7 ) / 8 * abs(texHeight); } /*allocate space for teh actual bitmap*/ pBitmapData = (BYTE *) malloc (sizeof(BYTE) * lBitSize);
/*read in the bitmap bits*/
ReadFile(fileHandle,pBitmapData,lBitSize,&dwBytesToRead,NULL);
if( lBitSize != dwBytesToRead) { if(pBitmapData) free(pBitmapData); pBitmapData = NULL; return FALSE; }
}
catch(exception &ex)
{
MessageBox(hWnd,ex.what(),“Error”,MB_OKCANCEL);
exit(0);
}
CloseHandle(fileHandle);
if( pBitmapInfo != NULL)
free(pBitmapInfo);
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glPixelStorei(GL_UNPACK_SKIP_ROWS,0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS,0);
glTexImage2D(GL_TEXTURE_2D,0,3,texWidth,texHeight,0, GL_RGB, GL_UNSIGNED_BYTE,pBitmapData);
if(pBitmapData)
free(pBitmapData);
return TRUE;
}
But I always end up fails at opening the file.
I use the visual studio 2008.
Thank you if you could help me abit.