PDA

View Full Version : about open gl texture!



davie
04-30-2010, 12:43 AM
Hi there,

I use the auxDIBImageLoad function to load a bitmap file and do a texture mapping, all works well. but when i read the bitmap file by myself, the texture's color changed. I dont know why and my function is as follow:


void LoadTexture(TCHAR *strFilePath)
{
HANDLE hFile;
HBITMAP hBitMap;

BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;

// First,Open the bitmap file to get the necessary information.
hFile=CreateFile(strFilePath,GENERIC_READ,FILE_SHA RE_READ,
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
return;
}

DWORD dwRead;
BOOL bResult;

// get the information of BITMAPFILEHEADER
bResult=ReadFile(hFile,&bf,sizeof(BITMAPFILEHEADER ),&dwRead,NULL);
if(!bResult)
{
CloseHandle(hFile);
return;
}
// get the information of BITMAPINFOHEADER
bResult=ReadFile(hFile,&bi,sizeof(BITMAPINFOHEADER ),&dwRead,NULL);
if(!bResult)
{
CloseHandle(hFile);
return;
}

DWORD dwNumColors;
if(bi.biClrUsed!=0)
{
dwNumColors=bi.biClrUsed;
}
else
{
switch(bi.biBitCount)
{
case 1:
dwNumColors=2;
break;
case 4:
dwNumColors=16;
break;
case 8:
dwNumColors=256;
break;
case 16:
dwNumColors=256*256;
break;
case 24:
dwNumColors=0;
break;
default:
CloseHandle(hFile);
return;
}
}

if(bf.bfOffBits!=sizeof(BITMAPFILEHEADER)+sizeof(B ITMAPINFOHEADER)+dwNumColors*sizeof(RGBQUAD))
{
CloseHandle(hFile);
return;
}

DWORD dwPaletteSize;
DWORD dwLineBytes,dwDataSize;

dwPaletteSize=dwNumColors*sizeof(RGBQUAD);
dwLineBytes=(bi.biWidth*bi.biBitCount+31)/32*4;
dwDataSize=dwLineBytes*bi.biHeight;

if(bf.bfSize!=sizeof(BITMAPFILEHEADER)+sizeof(BITM APINFOHEADER)+dwNumColors*sizeof(RGBQUAD)+dwDataSi ze)
{
return;
}

// Next,allocate memory to store the bitmap file's data(content).
HGLOBAL hGlobal=GlobalAlloc(GHND,dwPaletteSize+dwDataSize) ;
LPVOID lpGlobal=GlobalLock(hGlobal);

SetFilePointer(hFile,sizeof(BITMAPFILEHEADER)+size of(BITMAPINFOHEADER),NULL,FILE_BEGIN);
bResult=ReadFile(hFile,lpGlobal,dwPaletteSize+dwDa taSize,&dwRead,NULL);
if(!bResult)
{
CloseHandle(hFile);
return;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, 3, bi.biWidth, bi.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, lpGlobal);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

GlobalUnlock(hGlobal);
}

Thanks a lot!

davie
05-02-2010, 05:13 PM
any help? what the auxDIBImageLoad function does when it read a bitmap file? I think there are some errors, but i'm not sure.

switch(bi.biBitCount)
{
case 16:
dwNumColors=256*256;
break;
case 24:
case 32:
dwNumColors=0;
break;
default:
CloseHandle(hFile);
return;
}

for 24 bpp:
glTexImage2D(GL_TEXTURE_2D, 0, 3, bi.biWidth, bi.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, lpGlobal);
or
glTexImage2D(GL_TEXTURE_2D, 0, 3, bi.biWidth, bi.biHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, lpGlobal);

for 32 bpp:
glTexImage2D(GL_TEXTURE_2D, 0, 3, bi.biWidth, bi.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, lpGlobal);
or
glTexImage2D(GL_TEXTURE_2D, 0, 3, bi.biWidth, bi.biHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, lpGlobal);

but i dont know how to tell a bitmap is RGB(A) or BGR(A)? Can anybody help me? Thanks a lot!

Fugitive
05-02-2010, 05:19 PM
When you say it is 'wrong', what do you mean? Show us a picture of what is right, and a picture of what is wrong.

davie
05-03-2010, 06:04 PM
Hi Fugitive,
Thanks for your reply!

when i read the bitmap file by myself, the texture's color changes and it is not the same as the original bitmap file.
Some area becomes yellow color. With auxDIBImageLoad has no this
problem.