Hi there,
How to do a texture mapping with some RGBA data?(the RGBA data here i mean thet i read from a bitmap file or a png file, if it is a png file, i decode it to RGBA data first)
I try to do the texture mapping with the following codes, but it cant work and it just draws a rect with no texture.Can anybody help me? thanks a lot!
My render function is as follow:
void GL_Draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5.0f);
static GLshort vertex[12] = {-25,-25,0, 25,-25,0, -25,25,0, 25,25,0};
static GLshort texCoords[] = {0,0, 0,1, 1,0, 1,1};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_SHORT, 0, vertex);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_SHORT, 0, texCoords);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_texture);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_VERTEX_ARRAY);
eglSwapBuffers(display, surface);
}
and my generate texture function(only for bitmap file):
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_SHARE_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(BITMAPINFOHEADER)+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(BITMAPINFOHEADER)+dwNumColors*sizeof(RGBQUAD)+dwDataSize)
{
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)+sizeof(BITMAPINFOHEADER),NULL,FILE_BEGIN);
bResult=ReadFile(hFile,lpGlobal,dwPaletteSize+dwDataSize,&dwRead,NULL);
if(!bResult)
{
CloseHandle(hFile);
return;
}
glGenTextures(1, &g_texture);
glBindTexture(GL_TEXTURE_2D, g_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_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
GlobalUnlock(hGlobal);
}