資源描述:
《OpenGL中用bmp圖片做紋理貼圖的三種方法.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、OpenGL中用bmp圖片做紋理貼圖的三種方法方法一:首先獲取位圖句柄HBITMAPhBmp=(HBITMAP)::LoadImage(AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP1),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);然后根據(jù)位圖句柄得到位圖信息BITMAPBM;::GetObject(hBmp,sizeof(BM),&BM);最后根據(jù)位圖信息中的RGB值建立紋理gluBuild2DMipmaps(GL_TEX
2、TURE_2D,3,BM.bmWidth,BM.bmHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE,BM.bmBits);方法二:首先用OpenGL輔助庫獲得位圖信息AUX_RGBImageRec*TextureImage[1];TextureImage[0]=auxDIBImageLoad("1.bmp");然后建立紋理gluBuild2DMipmaps(GL_TEXTURE_2D,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,GL_
3、RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);方法三:從底層做,需要了解bmp文件的結(jié)構(gòu),首先讀取bmp文件結(jié)構(gòu),包括文件頭、信息頭和數(shù)據(jù),數(shù)據(jù)用于后面定義紋理longImageWidth=256;longImageHeight=256;GLubyteImage[256][256][3];voidReadHeader(FILE*fp,BITMAPFH*p_bitmapheader,BITMAPIH*p_bitmapinfo){fseek(fp,0,SEEK_SET)
4、;fread(&p_bitmapheader->bfType,sizeof(unsignedshort),1,fp);fseek(fp,2,SEEK_SET);fread(&p_bitmapheader->bfSize,sizeof(unsignedlong),1,fp);fseek(fp,6,SEEK_SET);fread(&p_bitmapheader->bfReserved1,sizeof(unsignedshort),1,fp);fseek(fp,8,SEEK_SET);fread(&p_bitma
5、pheader->bfReserved2,sizeof(unsignedshort),1,fp);fseek(fp,10,SEEK_SET);fread(&p_bitmapheader->bfOffBits,sizeof(unsignedlong),1,fp);fseek(fp,14,SEEK_SET);fread(&p_bitmapinfo->biSize,sizeof(unsignedlong),1,fp);fseek(fp,18,SEEK_SET);fread(&p_bitmapinfo->biWid
6、th,sizeof(unsignedlong),1,fp);fseek(fp,22,SEEK_SET);fread(&p_bitmapinfo->biHeight,sizeof(unsignedlong),1,fp);fseek(fp,26,SEEK_SET);fread(&p_bitmapinfo->biPlanes,sizeof(unsignedshort),1,fp);fseek(fp,28,SEEK_SET);fread(&p_bitmapinfo->biBitCount,sizeof(unsign
7、edshort),1,fp);fseek(fp,30,SEEK_SET);fread(&p_bitmapinfo->biCompression,sizeof(unsignedlong),1,fp);fseek(fp,34,SEEK_SET);fread(&p_bitmapinfo->biSizeImage,sizeof(unsignedlong),1,fp);fseek(fp,38,SEEK_SET);fread(&p_bitmapinfo->biXPelsPerMeter,sizeof(unsignedl
8、ong),1,fp);fseek(fp,42,SEEK_SET);fread(&p_bitmapinfo->biYPelsPerMeter,sizeof(unsignedlong),1,fp);fseek(fp,46,SEEK_SET);fread(&p_bitmapinfo->biClrUsed,sizeof(unsignedlong),1,fp);fseek(fp,50,SEEK_SET);fread(&p_