OpenGL中用bmp图片做纹理贴图的三种方法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

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_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文件结构,包括文件头、信息头和数据,数据用于后面定义纹理longImageWidth=256;longImageHeight=256;GLubyteImage[256][256][3];voidReadHeader(FILE*fp,BITMAPFH*p_bitmapheader,BITMAPIH*p_bitmapinfo){fseek(fp,0,SEEK_SET);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_bitmapheader-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-biWidth,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(unsignedshort),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(unsignedlong),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_bitmapinfo-biClrImportant,sizeof(unsignedlong),1,fp);}voidReadBitmapFile(){BITMAPFHbitmapheader;BITMAPIHbitmapinfo;FILE*fp;fp=fopen(6.bmp,r);if(!fp){puts(Readfilefailed.);return;}ReadHeader(fp,&bitmapheader,&bitmapinfo);if(bitmapinfo.biBitCount!=24){puts(UNSUPPORT);return;}ImageWidth=bitmapinfo.biWidth;ImageHeight=bitmapinfo.biHeight;inti=bitmapheader.bfOffBits;while(ibitmapheader.bfSize){for(intj=0;jImageWidth;j++)for(intk=0;kImageHeight;k++){fseek(fp,i,SEEK_SET);fread(Image[j][k]+2,1,1,fp);fseek(fp,i+1,SEEK_SET);fread(Image[j][k]+1,1,1,fp);fseek(fp,i+2,SEEK_SET);fread(Image[j][k],1,1,fp);i=i+3;}}fclose(fp);}glTexImage2D(GL_TEXTURE_2D,0,3,ImageWidth,ImageHeight,0,GL_RGB,GL_UNSIGNED_BYTE,&Image[0][0][0]);OpenGL中不用AUX库来加载BMP图片作为纹理[转载](2007-02-1121:22:15)大家在OpenGL中一般是用aux库的auxDIBImageLoad()函数来加载BMP格式的图片来作为纹理,这个确实是比较简单易用,但aux库的性能不佳,经常会出问题,稳定性较差。所以一般最好不要用aux库,可以用glut库,但glut库没有纹理加载函数。而有些程序是直接读取BMP格式来获得图像数据,从而生成纹理的,这种方法虽然很强大,但比较复杂,需要了解BMP的格式,对初学者比较困难。而下面的一段程序是用windows的API函数LoadImage()来实现这个功能,这种实现方法很简单,可以说比auxDIBImageLoad()函数复杂不了多少。首先,加上头文件:#includegl\glext.h这是因为后面的GL_BGR_EXT是定义在这个头文件里的,因为BMP格式是按Blue,Green,Red顺序储存图像数据的,这与OpenGL中正好相反。GL_BGR_EXT就是完成两者之间的转换的。下面就是不用AUX库来加载BMP图片作为纹理的函数:boolLoadTexture(LPTSTRszFileName,GLuint&texid)//CreatesTextureFromABitmapFile{HBITMAPhBMP;//HandleOfTheBitmapBITMAPBMP;//BitmapStructureglGenTextures(1,&texid);//CreateTheTexturehBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),szFileName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);if(!hBMP)//DoesTheBitmapExist?returnFALSE;//IfNotReturnFalseGetObject(hBMP,sizeof(BMP),&BMP);//GetTheObject//hBMP:HandleToGraphicsObject//sizeof(BMP):SizeOfBufferForObjectInformation//&BMP:BufferForObjectInformationglPixelStorei(GL_UNPACK_ALIGNMENT,4);//PixelStorageMode(WordAlignment/4Bytes)//TypicalTextureGenerationUsingDataFromTheBitmapglBindTexture(GL_TEXTURE_2D,texid);//BindToTheTextureIDglTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//LinearMinFilterglTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//LinearMagFilterglTexImage2D(GL_TEXTURE_2D,0,3,BMP.bmWidth,BMP.bmHeight,0,GL_BGR_EXT,GL_UNSIGNED_BYTE,BMP.bmBits);DeleteObject(hBMP);//DeleteTheObjectreturnTRUE;//LoadingWasSuccessful}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功