当前位置: 编程技术>c/c++/嵌入式
VC运用OPENGL加载BMP纹理图的实现方法汇总
来源: 互联网 发布时间:2014-10-25
本文导语: OpenGL是专业的图形程序接口,功能非常强大,调用方便的底层图形库。本文汇总了运用OPENGL加载BMP纹理图的几个实现方法。 方法一: 首先获取位图句柄: HBITMAP hBmp = (HBITMAP) ::LoadImage (AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP1), ...
OpenGL是专业的图形程序接口,功能非常强大,调用方便的底层图形库。本文汇总了运用OPENGL加载BMP纹理图的几个实现方法。
方法一:
首先获取位图句柄:
HBITMAP hBmp = (HBITMAP) ::LoadImage (AfxGetResourceHandle(),MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0,LR_CREATEDIBSECTION);
然后根据位图句柄得到位图信息:
BITMAP BM; ::GetObject (hBmp, sizeof (BM), &BM);
最后根据位图信息中的RGB值建立纹理:
gluBuild2DMipmaps( GL_TEXTURE_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_UNSIGNED_BYTE, TextureImage[0]->data);
方法三:
从底层做,需要了解bmp文件的结构,首先读取bmp文件结构,包括文件头、信息头和数据,数据用于后面定义纹理
long ImageWidth=256; long ImageHeight=256; GLubyte Image[256][256][3]; void ReadHeader(FILE *fp , BITMAPFH * p_bitmapheader , BITMAPIH *p_bitmapinfo) { fseek(fp, 0, SEEK_SET) ; fread( &p_bitmapheader->bfType,sizeof(unsigned short), 1, fp ); fseek(fp, 2, SEEK_SET) ; fread( &p_bitmapheader->bfSize,sizeof(unsigned long), 1, fp ); fseek(fp, 6, SEEK_SET) ; fread( &p_bitmapheader->bfReserved1,sizeof(unsigned short), 1, fp ); fseek(fp, 8, SEEK_SET) ; fread( &p_bitmapheader->bfReserved2,sizeof(unsigned short), 1, fp ); fseek(fp, 10, SEEK_SET) ; fread( &p_bitmapheader->bfOffBits,sizeof(unsigned long), 1, fp ); fseek(fp, 14, SEEK_SET) ; fread( &p_bitmapinfo->biSize, sizeof(unsigned long), 1, fp ); fseek(fp, 18, SEEK_SET) ; fread( &p_bitmapinfo->biWidth, sizeof(unsigned long), 1, fp ); fseek(fp, 22, SEEK_SET) ; fread( &p_bitmapinfo->biHeight, sizeof(unsigned long), 1, fp ); fseek(fp, 26, SEEK_SET) ; fread( &p_bitmapinfo->biPlanes, sizeof(unsigned short), 1, fp ); fseek(fp, 28, SEEK_SET) ; fread( &p_bitmapinfo->biBitCount, sizeof(unsigned short), 1, fp ); fseek(fp, 30, SEEK_SET) ; fread( &p_bitmapinfo->biCompression, sizeof(unsigned long), 1, fp ); fseek(fp, 34, SEEK_SET) ; fread( &p_bitmapinfo->biSizeImage, sizeof(unsigned long), 1, fp ); fseek(fp, 38, SEEK_SET) ; fread( &p_bitmapinfo->biXPelsPerMeter, sizeof(unsigned long), 1, fp ); fseek(fp, 42, SEEK_SET) ; fread( &p_bitmapinfo->biYPelsPerMeter, sizeof(unsigned long), 1, fp ); fseek(fp, 46, SEEK_SET) ; fread( &p_bitmapinfo->biClrUsed, sizeof(unsigned long), 1, fp ); fseek(fp, 50, SEEK_SET) ; fread( &p_bitmapinfo->biClrImportant, sizeof(unsigned long), 1, fp ); } void ReadBitmapFile() { BITMAPFH bitmapheader ; BITMAPIH bitmapinfo ; FILE *fp; fp = fopen("6.bmp" , "r") ; if(!fp) { puts("Read file failed.") ; return; } ReadHeader(fp, &bitmapheader , &bitmapinfo) ; if(bitmapinfo.biBitCount != 24) { puts("UNSUPPORT") ; return; } ImageWidth = bitmapinfo.biWidth; ImageHeight = bitmapinfo.biHeight; int i=bitmapheader.bfOffBits; while(i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。