资源描述:
《opengl中用bmp图片做纹理贴图的三种方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、OpenGL中用bmp图片做纹理贴图的三种方法方法一:首先获取位图句柄HBITMAPhBmp=(HBITMAP)::LoadImage(AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP1),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);然后根据位图句柄得到位图信息BITMAPBM;::GetObject(hBmp,sizeof(BM),&BM);最后根据位图信息中的RGB值建立纹理gluBuild2DMipmaps(GL_TEXTURE
2、_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_RGB,GL_U
3、NSIGNED_BYTE,TextureImage[0]->data);方法三:从底层做,需要了解bmp文件的结构,首先读取bmp文件结构,包括文件头、信息头和数据,数据用于后面定义纹理longImageWidth=256;longImageHeight=256;GLubyteImage[256][256][3];voidReadHeader(FILE*fp,BITMAPFH*p_bitmapheader,BITMAPIH*p_bitmapinfo){fseek(fp,0,SEEK_SET);fread(&p_bi
4、tmapheader->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_bitmapheader->bfReser
5、ved2,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->biWidth,sizeof(unsignedlo
6、ng),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(unsignedshort),1,fp);fseek(fp,
7、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(unsignedlong),1,fp);fseek(fp,42,SEEK_
8、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_