计算机图形学实验三(纹理映射)

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

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

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

资源描述

实验三纹理映射一、实验目的(1)掌握OpenGl的三维图形制作方法(2)掌握VisualC++环境下的OpenGL图形开发二、实验内容在三维图像上添加纹理映射三、实验结果实验代码如下:/************************************************glut_applicationwritenbyyyb************************************************/#includestdlib.h#includeGL/glut.h#includeGL/glaux.h#includestdio.h#pragmacomment(lib,glaux.lib)#defineMAX_TEXTURES6//maxtexturesdisplayed/*createsaenumtypeformousebuttons*/enum{BUTTON_LEFT=0,BUTTON_RIGHT,BUTTON_LEFT_TRANSLATE,};intmButton=-1;intmOldY,mOldX;boolfullscreen;////////////////////////////////////floateye[3]={0.0f,0.0f,7.0f};floatrot[3]={45.0f,45.0f,0.0f};constintESC=27;intwireframe=0;constintGL_WIN_WIDTH=640;constintGL_WIN_HEIGHT=480;constintGL_WIN_INITIAL_X=0;constintGL_WIN_INITIAL_Y=0;/*arraytoholdtexturehandles*/GLuintg_TexturesArray[MAX_TEXTURES];BOOLLoadTextureGL(char*Filename,GLuint&texture)//加载一个bmp图片(new){AUX_RGBImageRec*TextureImage;//为纹理开辟存储空间FILE*File=NULL;//文件句柄if(!Filename)//确保文件名已经提供{returnNULL;//如果没有则返回NULL}File=fopen(Filename,r);//检查文件是否存在if(File)//文件存在吗?{fclose(File);//关闭File文件句柄TextureImage=auxDIBImageLoad(Filename);//载入图片并返回其指针}glGenTextures(1,&texture);//返回唯一的纹理名字来标识纹理,保存在texture中//使用来自位图数据生成的典型纹理glBindTexture(GL_TEXTURE_2D,texture);//生成纹理glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage-sizeX,TextureImage-sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage-data);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//线形滤波glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//线形滤波if(TextureImage)//纹理是否存在{if(TextureImage-data)//纹理图像是否存在{free(TextureImage-data);//释放纹理存储空间}free(TextureImage);//释放图像结构}returntrue;}boolLoadBMP(char*filename,GLuint&texture)//调贴图大小为2的幂{AUX_RGBImageRec*pImage=NULL;pImage=auxDIBImageLoad(filename);//装入位图if(pImage==NULL)returnfalse;//位图没装入返回错误glGenTextures(1,&texture);//生成贴图(纹理)glBindTexture(GL_TEXTURE_2D,texture);//捆绑贴图(纹理)gluBuild2DMipmaps(GL_TEXTURE_2D,4,//pImage-sizeX,//图形宽pImage-sizeY,//图形高GL_RGB,GL_UNSIGNED_BYTE,pImage-data//图形数据);free(pImage-data);//释放位图数据占据的内存资源free(pImage);returntrue;//返回成功}voidInit(){//glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//glShadeModel(GL_SMOOTH);glEnable(GL_DEPTH_TEST);glClearColor(0.0,0.0,0.0,1.0);//enabletextureandbindsatexturetothefirstelementofthearrayglEnable(GL_TEXTURE_2D);//LoadTextureGL(mybmp1.bmp,g_TexturesArray[0]);//LoadTextureGL(Cars.bmp,g_TexturesArray[1]);LoadBMP(1.bmp,g_TexturesArray[0]);LoadBMP(2.bmp,g_TexturesArray[1]);LoadBMP(3.bmp,g_TexturesArray[2]);LoadBMP(4.bmp,g_TexturesArray[3]);LoadBMP(5.bmp,g_TexturesArray[4]);LoadBMP(6.bmp,g_TexturesArray[5]);return;}//------------------------------------------------------------------------//Windowresizefunction//------------------------------------------------------------------------voidglutResize(intwidth,intheight){glViewport(0,0,width,height);glMatrixMode(GL_PROJECTION);glLoadIdentity();/*modifythislinetochangeperspectivevalues*/gluPerspective(45.0,(float)width/(float)height,1.0,300.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();}//------------------------------------------------------------------------//Functionthathandleskeyboardinputs//------------------------------------------------------------------------voidglutKeyboard(unsignedcharkey,intx,inty){switch(key){caseESC:exit(0);case'W':case'w':wireframe=!wireframe;}}voidclamp(float*v){inti;for(i=0;i3;i++)if(v[i]360||v[i]-360)v[i]=0;}//------------------------------------------------------------------------//Movesthescreenbasedonmousepressedbutton//------------------------------------------------------------------------voidglutMotion(intx,inty){if(mButton==BUTTON_LEFT){/*rotatesscreen*/rot[0]-=(mOldY-y);rot[1]-=(mOldX-x);clamp(rot);}elseif(mButton==BUTTON_RIGHT){eye[2]-=(mOldY-y)*0.05f;//hereImultiplybya0.2factorto//slowdownthezoomclamp(rot);}elseif(mButton==BUTTON_LEFT_TRANSLATE){eye[0]+=(mOldX-x)*0.01f;eye[1]-=(mOldY-y)*0.01f;clamp(rot);}mOldX=x;mOldY=y;}//------------------------------------------------------------------------//Functionthathandlesmouseinput//------------------------------------------------------------------------voidglutMouse(intbutton,intstate,intx,inty){if(state==GLUT_DOWN){mOldX=x;mOldY=y;switch(button){caseGLUT_LEFT_BUTTON:if(glutGetModifiers()==GLUT_ACTIVE_CTRL){mButton=BUTTON_LEFT_TRANSLATE;break;}else{mButton=BUTTON_LEFT;break;}caseGLUT_RIGHT_BUTTON:mButton=BUTTON_RIGHT;break;}}elseif(state==GLUT_UP)mButton=-1;}voidglutMenu(intvalue){switch(value){case1:glutFullScreen();return;case2:glutReshapeWindow(GL_WIN_WIDTH,GL_WIN_HEIGHT);return;case3:exit(0);}}voidglutSpecial(intvalue,intx,inty){switch(value){caseGLUT_KEY_F1:if(fullscreen)glutFullScreen();elseglutReshapeWindow(GL_WIN_WIDTH,GL_WIN_HEIGHT);fullscreen=!fullscreen;return;}}voidglutDisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);if(wireframe)glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);elseglPolygonMode(GL_FRONT_AND_BACK,GL_FILL);glPushMatrix();/*translatecameratothecoordinatesprovidedbyeyearray*/glTranslatef(-eye[0],-eye[1],-eye[2]);/*rotatesthescreenbytheanglesprovidedb

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

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

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

×
保存成功