《《虚拟现实与系统仿真》实验指导书22.docx》由会员分享,可在线阅读,更多相关《《虚拟现实与系统仿真》实验指导书22.docx(22页珍藏版)》请在三一办公上搜索。
1、虚拟现实与系统仿真实验指导书20132实验一 基于MFC的基本OpenGL应用程序生成一:实验目的1. 熟悉VC+2005集成开发环境;2. 熟悉基本MFC应用程序的生成;3. 掌握基于MFC的基本OpenGL应用程序生成。二:实验内容1. 建立基于MFC的基本OpenGL应用程序;2. 配置应用程序,添加OpenGL链接库文件;3. 建立OpenGL的基本环境。三:实验背景知识OpenGL是一种图形显示软件工具包,它为图形硬件提供软件接口。OpenGL为计算机动画提供由图形生成图像帧的工具。MFC提供的图形用户界面程序设计技术可以编写人机交互程序,OpenGL技术可以进行各种图形绘制。四:实
2、验步骤1. 文件-新建-项目 选择MFC应用程序 输入名称OpenGLPlat2. 右击项目名,选择属性,再选择链接器中的输入选项,附加依赖项:opengl32.lib; glu32.lib ;glaux.lib3选择类视图模式,右击View,再添加变量和头文件或直接在View头文件中添加变量:protected:HGLRC m_hRC;添加包含OpenGL头文件的语句:在COpenGLPlatView.cpp中添加代码#include #include #include 4右击属性 消息按钮 选择WM_CREATE 添加在int COpenGLPlatView:OnCreate(LPCREA
3、TESTRUCT lpCreateStruct)函数中添加代码/添加新的代码开始CClientDC dc(this); PIXELFORMATDESCRIPTOR pfd; memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR); pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion=1; pfd.dwFlags=PFD_DRAW_TO_WINDOW| PFD_SUPPORT_OPENGL| PFD_DOUBLEBUFFER; pfd.iPixelType=PFD_TYPE_RGBA; pfd.cColorB
4、its=24; pfd.cDepthBits=32; int pixelFormat=ChoosePixelFormat(dc.m_hDC,&pfd); SetPixelFormat(dc.m_hDC,pixelFormat,&pfd); m_hRC=wglCreateContext(dc.m_hDC);return 0;右击属性 消息按钮 选择WM_DESTROY 添加void COpenGLPlatView:OnDestroy()函数中添加代码 /添加新的代码开始wglMakeCurrent(NULL,NULL);wglDeleteContext(m_hRC);m_hRC=NULL;右击属
5、性 消息按钮 选择WM_SIZE 添加void COpenGLPlatView:OnSize(UINT nType, int cx, int cy) 函数中添加代码 /添加新的代码开始CClientDC dc(this);wglMakeCurrent(dc.m_hDC,m_hRC); glMatrixMode(GL_PROJECTION);glLoadIdentity();double d=10;double n=100;glOrtho(-cx/n,cx/n,-cy/n,cy/n,-d,d);glMatrixMode(GL_MODELVIEW);glViewport(0,0,cx,cy);wg
6、lMakeCurrent(NULL,NULL);5.在OpenGLPlatView.cpp中OnDraw函数中添加以下代码:void CMy1View:OnDraw(CDC* pDC) / 把 ”/*pDC*/” 改成 pDCCOpenGLPlatDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;/ TODO: 在此处为本机数据添加绘制代码wglMakeCurrent(pDC-m_hDC,m_hRC);glClearColor(1.0f,1.0f,1.0f,1.0f); glClearDepth(1.0);glClear
7、(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_COLOR_MATERIAL);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL);glEnable(GL_NORMALIZE);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glColor3f(1.0f,0.0f,0.0f);glRotated(60.0,1.0,1.0,1
8、.0); /茶壶auxWireTeapot(3.0);SwapBuffers(pDC-m_hDC);wglMakeCurrent(NULL,NULL);五:实验结果 实验二OpenGL基本图形元素和模型变换一:实验目的1. 掌握二次曲面的绘制函数及其应用方法。2. 掌握基本实体模型的绘制函数及其应用方法。二:实验内容1. 学习二次曲面的绘制函数及其应用方法。2. 学习基本实体模型的绘制函数及其应用方法。三:实验背景知识OpenGL提供了二次曲面和基本实体模型的绘制函数及其应用方法。这些二次曲面和基本实体模型在OpenGL内部实现中实际上也是将它们离散分割成为大量的多边形,然后通过绘制多边形的方
9、法实现对这些二次曲面和基本实体模型的绘制。四:实验程序 实验步骤14与上述实验1相同5在OpenGLPlatView.cpp中OnDraw函数中添加以下代码:wglMakeCurrent(pDC-m_hDC,m_hRC);glMatrixMode(GL_MODELVIEW); /二次曲面glColor3f(1.0f,0.0f,0.0f);GLUquadric* qd=gluNewQuadric();/创建二次曲面对象gluQuadricDrawStyle(qd,GLU_FILL);/设置二次曲面对象绘制模式GLU_LINE;GLU_SILHOUETTE;GLU_POINTglLoadIdent
10、ity();glRotated(-105.0,1.0,0.0,0.0);glTranslated(-6.0,0.0,-2.0);gluCylinder(qd,2.0,1.0,4.0,30,4);/(圆台,下底面半径,上底面半径,圆台高度,绕z轴分割瓣数,绕z轴方向层数)glLoadIdentity();glTranslated(-6.0,0.0,-2.0);gluDisk(qd,1.0,2.0,30,4);/平面圆盘(,内圆半径,外圆半径,扇形瓣数,环的个数)/.gluPartialDisk(qd,1.0,2.0,10,4,0,75);/拱状图形(,内圆半径,外圆半径,扇形瓣数,环的个数,起始
11、角,图形张开的角度)/.gluSphere(qd,2.0,30,6);/球(,球的半径,分割的瓣数,分割的层次)auxSolidTetrahedron(4.0);/实体模型auxWireTeapot();auxSolidTeapot();/auxWireSphere(3.0);auxSolidSphere(3.0);/auxWireCube(4.0);auxSolidCube(4.0);/auxWireBox(3.0,4.0,5.0);auxSolidBox(3.0,4.0,5.0);/auxWireTorus(1.0,3.0);/圆环auxSolidTorus(1.0,3.0);/auxWi
12、reCylinder(2.0,4.0);auxSolidCylinder(2.0,4.0);/auxWireIcosahedron(4.0);auxSolidIcosahedron(4.0);/二十面体 /auxWireOctahedron(4.0);auxSolidOctahedron(4.0);/八面体 /auxWireTetrahedron(4.0);auxSolidTetrahedron(4.0);/四面体 /auxWireDodecahedron(4.0);auxSolidDodecahedron(4.0);/十二面体 /auxWireCone(2.0,4.0);auxSolidCo
13、ne(2.0,4.0);/圆锥体 SwapBuffers(pDC-m_hDC); /双缓冲wglMakeCurrent(NULL,NULL); 实验三 光照与材质一:实验目的:1. 掌握利用OpenGL编写绘制具有真实感效果的图形方法。2. 掌握添加光源和设置颜色材料属性的方法。二:实验内容:1. 掌握添加光源的实现方法。2. 掌握设置颜色和材料属性的方法。三:实验要求通过本实验,掌握添加光源和设置颜色材料属性的方法,利用OpenGL编写绘制具有真实感效果的图形。四:实验程序实验步骤14与上述实验1相同5在OpenGLPlatView.cpp中OnDraw函数中添加以下代码:wglMakeCu
14、rrent(pDC-m_hDC,m_hRC);glClearColor(1.0f,1.0f,1.0f,1.0f);glClearDepth(1.0);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_COLOR_MATERIAL);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_AUTO_NORMAL);glEnable(GL_NORMALIZE);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS);/设置模型变换矩阵glMa
15、trixMode(GL_MODELVIEW);glLoadIdentity(); /设置光源参数GLfloat light_position=1.0f,1.0f,1.0f,0.0f;GLfloat light_ambient=0.2f,0.2f,0.2f,0.2f;GLfloat light_diffuse=0.5f,0.5f,0.2f,0.2f;GLfloat light_specular=0.5f,0.5f,0.5f,0.2f;glLightfv(GL_LIGHT0,GL_POSITION,light_position);glLightfv(GL_LIGHT0,GL_AMBIENT,lig
16、ht_ambient);glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);/设置材质参数GLfloat material_ambient=0.2f,0.2f,0.2f,0.2f;GLfloat material_diffuse=0.2f,0.8f,0.4f,0.8f;GLfloat material_specular=0.2f,0.8f,0.4f,0.8f;GLfloat material_emission=0.2f,0.2f,0.2f,1.0f;GLflo
17、at material_shiness=10.0f; glMaterialfv(GL_BACK,GL_AMBIENT,material_ambient);/FRONT_AND_BACKglMaterialfv(GL_BACK,GL_DIFFUSE,material_diffuse);glMaterialfv(GL_BACK,GL_SPECULAR,material_specular);glMaterialfv(GL_BACK,GL_EMISSION,material_emission);glMaterialfv(GL_BACK,GL_SHININESS,material_shiness);gl
18、Color3f(1.0f,0.0f,0.0f);/红色glRotated(60.0,1.0,1.0,1.0);auxSolidTeapot(2.0);SwapBuffers(pDC-m_hDC); /双缓冲wglMakeCurrent(NULL,NULL); 实验结果:实验四 纹理映射一:实验目的1. 掌握二维纹理映射方法。2. 掌握利用OpenGL编写绘制具有真实感效果的图形方法。二:实验内容掌握二维纹理映射方法。三:实验要求通过本实验,掌握掌握二维纹理映射方法。四:实验程序实验步骤14与上述实验1相同5在OpenGLPlatView.cpp中OnDraw函数中添加以下代码:wglMakeC
19、urrent(pDC-m_hDC,m_hRC);glClearColor(1.0f,1.0f,1.0f,1.0f);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_COLOR_MATERIAL);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0); glEnable(GL_AUTO_NORMAL);glEnable(GL_NORMALIZE);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS); glMatrixMode(GL_MODELVIEW);g
20、lLoadIdentity(); /设置光源参数GLfloat light_position=1.0f,1.0f,1.0f,0.0f;GLfloat light_ambient=0.2f,0.2f,0.2f,0.2f;GLfloat light_diffuse=0.5f,0.5f,0.2f,0.2f;GLfloat light_specular=0.5f,0.5f,0.5f,0.2f;glLightfv(GL_LIGHT0,GL_POSITION,light_position);glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);glLightfv(GL
21、_LIGHT0,GL_DIFFUSE,light_diffuse);glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);/设置材质参数GLfloat material_ambient=0.2f,0.2f,0.2f,0.2f;GLfloat material_diffuse=0.2f,0.8f,0.4f,0.8f;GLfloat material_specular=0.2f,0.8f,0.4f,0.8f;GLfloat material_emission=0.2f,0.2f,0.2f,1.0f;GLfloat material_shiness=10.0
22、f; glMaterialfv(GL_BACK,GL_AMBIENT,material_ambient);/FRONT_AND_BACKglMaterialfv(GL_BACK,GL_DIFFUSE,material_diffuse);glMaterialfv(GL_BACK,GL_SPECULAR,material_specular);glMaterialfv(GL_BACK,GL_EMISSION,material_emission);glMaterialfv(GL_BACK,GL_SHININESS,material_shiness);glRotated(80.0,1.0,1.0,1.0
23、);AUX_RGBImageRec *picture=auxDIBImageLoad(_T(C:tsIcon.bmp);/(此处图片路径为C盘根目录下)if(picture!=NULL)glEnable(GL_TEXTURE_2D);glPixelStorei(GL_UNPACK_ALIGNMENT,1);glTexImage2D(GL_TEXTURE_2D,/二维纹理 0,/纹理层次 3,/颜色分量个数 picture-sizeX,/纹理宽度 picture-sizeY,/纹理高度 0,/纹理边框宽度(或) GL_RGB,/像素数据格式 GL_UNSIGNED_BYTE,/像素数据类型 pi
24、cture-data);/存储图象数据glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);/循环glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);glTexEnvi(GL_TEXTURE_ENV,GL_TEXT
25、URE_ENV_MODE,GL_MODULATE);/GL_DECAL,GL_glBegin(GL_QUADS);glColor3f(0.0,1.0,0.0);glTexCoord2d(-2.0,-2.0);glVertex3d(-2.5,-2.5,3);glTexCoord2d(-2.0,2.0);glVertex3d(2.5,-2.5,3);glTexCoord2d(2.0,2.0);glVertex3d(2.5,2.5,3);glTexCoord2d(2.0,-2.0);glVertex3d(-2.5,2.5,3);glColor3f(1.0,1.0,0.0);glTexCoord2d(
26、0.0,0.0);glVertex3d(-2.5,2.5,0);glTexCoord2d(0.0,1.0);glVertex3d(2.5,2.5,0);glTexCoord2d(1.0,1.0);glVertex3d(2.5,2.5,3);glTexCoord2d(1.0,0.0);glVertex3d(-2.5,2.5,3);glColor3f(1.0,0.0,1.0);glTexCoord2d(0.4,0.4);glVertex3d(-2.5,-2.5,0);glTexCoord2d(0.4,0.6);glVertex3d(-2.5,-2.5,3);glTexCoord2d(0.6,0.6
27、);glVertex3d(-2.5,2.5,3);glTexCoord2d(0.4,0.4);glVertex3d(-2.5,2.5,0);glEnd();SwapBuffers(pDC-m_hDC); /双缓冲wglMakeCurrent(NULL,NULL); 实验五 基于OpenGL的动画编程一:实验目的1. 掌握VC中的文档视图架构。2. 掌握利用OpenGL编写动画效果的方法。二:实验内容掌握VC中的文档视图架构及动画程序编写。三:实验要求通过本实验,掌握利用OpenGL编写动画效果的方法。四:实验程序实验步骤14与上述实验1相同5.添加Star类在Star.h中添加以下代码#pra
28、gma once#include #include #include class Starpublic: Star(void); int m_n; int m_sign;/变形符:增加,-1:减少 double m_radiusCenter; double m_radiusMiddle; double m_radiusCorner; GLfloat m_redCenter,m_greenCenter,m_blueCenter; GLfloat m_redMiddle,m_greenMiddle,m_blueMiddle; GLfloat m_redCorner,m_greenCorner,m_
29、blueCorner; GLdouble m_angle;/旋转角度 GLdouble m_point263;/每个顶点坐标值 GLfloat m_color263;/每个顶点颜色值 void mb_adjustGLfloat(GLfloat& f,GLfloat min,GLfloat max); void mb_calculatePoint(void);/计算顶点坐标值 void mb_draw(void);/绘制图形 void mb_nextFrame(void);/计算下一帧public: Star(void); 在Star.cpp中添加以下代码: #include StdAfx.h#
30、include Star.h/ 构造函数:初始化各个成员变量Star:Star(void) m_radiusCenter=2.0; / 中心球半径 m_radiusMiddle=2.828; / 中间球半径,这里2sqrt(2)2.828 m_radiusCorner=3.464; / 角上球半径,这里2sqrt(3)3.464 m_redCenter=1.0f; / 中心球点颜色 m_greenCenter=0.0f; m_blueCenter=1.0f; m_redMiddle=0.0f; / 中间球点颜色 m_greenMiddle=1.0f; m_blueMiddle=0.0f; m_
31、redCorner=0.0f; / 角上球点颜色 m_greenCorner=0.0f; m_blueCorner=1.0f; m_angle=45.0; / 旋转角度 mb_calculatePoint( ); m_n=0; / 变形计数器 m_sign=1; / 变形符号: 1: 增加; -1: 减小 / 构造函数Star结束/ 析构函数Star:Star(void) / 析构函数Star结束/ 值调整函数:强制使得参数f的值位于区间min, max内void Star:mb_adjustGLfloat(GLfloat& f, GLfloat min, GLfloat max) if (f
32、max) f=max; / 函数mb_adjustGLfloat结束/ 计算顶点的坐标值和颜色值void Star:mb_calculatePoint(void) int i, j; int id1 =0, 2, 6, 8, 1, 3, 5, 7; int id2 =9, 11, 14, 16, 10, 12, 13, 15; int sign1 =-1, 1, -1, 1, 0, -1, 1, 0; int sign2 = 1, 1, -1, -1, 1, 0, 0, -1; for (i=0, j=4; i4; i+, j+) / 下面0.577是“sqrt(3)/3”的近似值 m_poi
33、ntid1i0=0.577*m_radiusCorner*sign1i; m_pointid1i1=0.577*m_radiusCorner*sign2i; m_pointid1i2=0.577*m_radiusCorner; m_colorid1i0=m_redCorner; m_colorid1i1=m_greenCorner; m_colorid1i2=m_blueCorner; / 下面0.707是“sqrt(2)/2”的近似值 m_pointid1j0=0.707*m_radiusMiddle*sign1j; m_pointid1j1=0.707*m_radiusMiddle*sig
34、n2j; m_pointid1j2=0.707*m_radiusMiddle; m_colorid1j0=m_redMiddle; m_colorid1j1=m_greenMiddle; m_colorid1j2=m_blueMiddle; / for循环结束 m_point40=0.0; m_point41=0.0; m_point42=m_radiusCenter; m_color40=m_redCenter; m_color41=m_greenCenter; m_color42=m_blueCenter; for (i=9; i17; i+) m_pointi2=0.0; for (i=
35、0, j=4; i4; i+, j+) / 下面0.707是“sqrt(2)/2”的近似值 m_pointid2i0=0.707*m_radiusMiddle*sign1i; m_pointid2i1=0.707*m_radiusMiddle*sign2i; m_colorid2i0=m_redMiddle; m_colorid2i1=m_greenMiddle; m_colorid2i2=m_blueMiddle; m_pointid2j0=m_radiusCenter*sign1j; m_pointid2j1=m_radiusCenter*sign2j; m_colorid2j0=m_re
36、dCenter; m_colorid2j1=m_greenCenter; m_colorid2j2=m_blueCenter; / for循环结束 for (i=0, j=17; i9; i+, j+) m_pointj0=m_pointi0; m_pointj1=m_pointi1; m_pointj2=-m_pointi2; m_colorj0=m_colori0; m_colorj1=m_colori1; m_colorj2=m_colori2; / for循环结束 / 函数mb_calculatePoint结束/ 绘制图形void Star:mb_draw(void) int i, j
37、, k, m; int id38= 0, 0, 1, 4, 3, 6, 4, 8, / 上表面三角形映射关系 3, 4, 4, 5, 6, 7, 7, 5, 4, 1, 2, 2, 4, 4, 8, 4 ; int face49= 0, 1, 2, 9, 10, 11, 17, 18, 19, / 侧面与表面的顶点映射关系 0, 3, 6, 9, 12, 14, 17, 20, 23, 6, 7, 8, 14, 15, 16, 23, 24, 25, 8, 5, 2, 16, 13, 11, 25, 22, 19 ; glBegin(GL_TRIANGLES); for (i=0; i8; i
38、+) / 上表面 for (j=0; j3; j+) m=idji; glColor3f (m_colorm0, m_colorm1, m_colorm2); glVertex3d(m_pointm0, m_pointm1, m_pointm2); / for循环结束 for (k=0; k4; k+) / 四个侧面 for (i=0; i8; i+) for (j=0; j3; j+) m=facekidji; glColor3f (m_colorm0, m_colorm1, m_colorm2); glVertex3d(m_pointm0, m_pointm1, m_pointm2); / for循环结束 for (i=0; i8; i+) / 下表面 for (j=0; jnMax) m_sign=-1; if (m_n0) m_sign=1; m_radiusCenter+=(d*m_sign*1.0); m_radiusMiddle-=(d*m_sign*2.0); m_radiusCorner+=(d*m_sign*3.0); GLfloat df=1.0f/nMax; m_redCenter-=(df*m_sign); / 中心球点颜色 mb_adjustGLfloat(m_redCenter, 0.0f, 1.0f); m_b