1、虚拟现实与系统仿真实验指导书20142实验一 基于 MFC 的基本 OpenGL 应用程序生成一:实验目的1. 熟悉VC+2008集成开发环境;2. 熟悉基本MFC应用程序的生成;3. 掌握基于MFC的基本OpenGL应用程序生成。二:实验内容1. 建立基于MFC的基本OpenGL应用程序;2. 配置应用程序,添加OpenGL链接库文件;3. 建立OpenGL的基本环境。三:实验背景知识OpenGL是一种图形显示软件工具包,它为图形硬件提供软件接口。OpenGL为计算机动画提供由图形生成图像帧的工具。MFC提供的图形用户界面程序设计技术可以编写人机交互程序,OpenGL技术可以进行各种图形绘制
2、。说明:如果使用的VS2008开发环境,下载OpenGL 安装包所需文件:http:/ GLAUX.H GLU.H glut.h GLAUX.LIB GLU32.LIB glut32.lib glut.lib OPENGL32.LIB glaux.dll glu32.dll glut32.dll glut.dll opengl32.dll VS2008不带 GL.H, GLAUX.h, glu.h, glaux.lib, glu32.lib, opengl32.lib 这些文件。通过上面所给地址下载后,把下载的文件分配放在系统中不同的文件夹下,具体配置方法如下:(1) 把 glaux.dll、
3、glut32.dll、glut.dll 拷贝到C:WINDOWSsystem32目录下,system32目录下应该已经有 opengl32.dll、glu32.dll了。 (2) 把 GL.H、GLAUX.h、glu.h、glut.h 拷贝到 C:Program FilesMicrosoft Visual Studio 9.0VCincludegl (3) 把 GLAUX.LIB、Glu32.lib、glut32.lib、Opengl32.lib,glut.lib 拷贝到 C:Program FilesMicrosoft Visual Studio 9.0VClib四:实验步骤1. 文件-新建
4、 -项目 选择 MFC应用程序 输入名称OpenGLPlat2. 右击项目名,选择属性,再选择链接器中的输入选项,附加依赖项:opengl32.lib glu32.lib glaux.lib3选择类视图模式,右击View,再添加变量和头文件或直接在View头文件中添加变量:protected:HGLRC m_hRC;添加包含OpenGL 头文件的语句:在COpenGLPlatView中添加代码#include #include #include 4右击属性 消息按钮 选择WM_CREATE 添加在int COpenGLPlatView:OnCreate(LPCREATESTRUCT lpCre
5、ateStruct)函数中添加代码/添加新的代码开始CClientDC dc(this);PIXELFORMATDESCRIPTOR pfd;memset(pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);pfd.nVersion=1;pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;pfd.iPixelType=PFD_TYPE_RGBA;pfd.cColorBits=24;pfd.cDepthBits=32;int pixelFormat=ChoosePixelFormat(d
6、c.m_hDC,SetPixelFormat(dc.m_hDC,pixelFormat,m_hRC=wglCreateContext(dc.m_hDC);return 0;右击属性 消息按钮 选择WM_DESTROY 添加void COpenGLPlatView:OnDestroy()函数中添加代码/添加新的代码开始wglMakeCurrent(NULL,NULL);wglDeleteContext(m_hRC);m_hRC=NULL;右击属性 消息按钮 选择WM_SIZE 添加void COpenGLPlatView:OnSize(UINT nType, int cx, int cy) 函数
7、中添加代码/添加新的代码开始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);wglMakeCurrent(NULL,NULL);5.在OpenGLPlatView.cpp中OnDraw 函数中添加以下代码:void CMy1View:On
8、Draw(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);glClear(GL_COLOR_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glColor3f(1.0f,0.0f,0.0f);glRot
9、ated(60.0,1.0,1.0,1.0); /茶壶auxWireTeapot(3.0);SwapBuffers(pDC-m_hDC);wglMakeCurrent(NULL,NULL);五:实验结果实验二 OpenGL 基本图形元素和模型变换一:实验目的1. 掌握二次曲面的绘制函数及其应用方法。2. 掌握基本实体模型的绘制函数及其应用方法。二:实验内容1. 学习二次曲面的绘制函数及其应用方法。2. 学习基本实体模型的绘制函数及其应用方法。三:实验背景知识OpenGL提供了二次曲面和基本实体模型的绘制函数及其应用方法。这些二次曲面和基本实体模型在OpenGL内部实现中实际上也是将它们离散分割
10、成为大量的多边形,然后通过绘制多边形的方法实现对这些二次曲面和基本实体模型的绘制。四:实验程序实验步骤 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_SILH
11、OUETTE;GLU_POINTglLoadIdentity();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);/拱状
12、图形(,内圆半径,外圆半径,扇形瓣数,环的个数,起始角,图形张开的角度)/./gluSphere(qd,2.0,30,6);/球(,球的半径,分割的瓣数,分割的层次)/auxSolidTetrahedron(4.0);/实体模型auxWireTeapot(4.0);auxSolidTeapot(4.0);/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
13、);/圆环auxSolidTorus(1.0,3.0);/auxWireCylinder(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);/十二面体/a
14、uxWireCone(2.0,4.0);auxSolidCone(2.0,4.0);/圆锥体SwapBuffers(pDC-m_hDC); /双缓冲wglMakeCurrent(NULL,NULL); 实验三 光照与材质一:实验目的:1. 掌握利用OpenGL编写绘制具有真实感效果的图形方法。2. 掌握添加光源和设置颜色材料属性的方法。二:实验内容:1. 掌握添加光源的实现方法。2. 掌握设置颜色和材料属性的方法。三:实验要求通过本实验,掌握添加光源和设置颜色材料属性的方法,利用OpenGL编写绘制具有真实感效果的图形。四:实验程序实验步骤 14 与上述实验 1 相同5 在 OpenGLPla
15、tView.cpp 中 OnDraw 函数中添加以下代码:wglMakeCurrent(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_TE
16、ST);glDepthFunc(GL_LESS);/设置模型变换矩阵glMatrixMode(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_positio
17、n);glLightfv(GL_LIGHT0,GL_AMBIENT,light_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 materi
18、al_emission=0.2f,0.2f,0.2f,1.0f;GLfloat 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_BA
19、CK,GL_SHININESS,material_shiness);glColor3f(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 在 O
20、penGLPlatView.cpp 中 OnDraw 函数中添加以下代码 :wglMakeCurrent(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);glDep
21、thFunc(GL_LESS);glMatrixMode(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_LI
22、GHT0,GL_AMBIENT,light_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
23、.2f,0.2f,1.0f;GLfloat 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,ma
24、terial_shiness);glRotated(80.0,1.0,1.0,1.0);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_R
25、GB,/像素数据格式GL_UNSIGNED_BYTE,/像素数据类型picture-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_NEARE
26、ST);glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_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);
27、glColor3f(1.0,1.0,0.0);glTexCoord2d(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);glVertex
28、3d(-2.5,-2.5,3);glTexCoord2d(0.6,0.6);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); if (picture!=NULL)delete picture-data;delete picture; / if 结构结束/ 添加新的代码结束。/ /实验五 基于 OpenGL 的动画编程一:实验目的1. 掌握VC中的文档视图架构。2. 掌握利用OpenGL
29、编写动画效果的方法。二:实验内容掌握VC中的文档视图架构及动画程序编写。三:实验要求通过本实验,掌握利用OpenGL编写动画效果的方法。四:实验程序实验步骤 14 与上述实验 1 相同5.添加 Star 类在 Star.h 中添加以下代码#pragma 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_redCen
30、ter,m_greenCenter,m_blueCenter;GLfloat m_redMiddle,m_greenMiddle,m_blueMiddle;GLfloat m_redCorner,m_greenCorner,m_blueCorner;GLdouble m_angle;/旋转角度GLdouble m_point263;/每个顶点坐标值GLfloat m_color263;/每个顶点颜色值void mb_adjustGLfloat(GLfloatvoid mb_calculatePoint(void);/计算顶点坐标值void mb_draw(void);/绘制图形void mb_
31、nextFrame(void);/计算下一帧public:Star(void); 在Star.cpp中添加以下代码 :#include “StdAfx.h“#include “Star.h“/ 构造函数:初始化各个成员变量Star:Star(void)m_radiusCenter=2.0; / 中心球半径m_radiusMiddle=2.828; / 中间球半径,这里2sqrt(2)2.828m_radiusCorner=3.464; / 角上球半径,这里2sqrt(3)3.464m_redCenter=1.0f; / 中心球点颜色m_greenCenter=0.0f;m_blueCenter
32、=1.0f;m_redMiddle=0.0f; / 中间球点颜色m_greenMiddle=1.0f;m_blueMiddle=0.0f;m_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
33、Star:mb_adjustGLfloat(GLfloat / 函数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; inMax)m_sign=-1;if (m_n360)m_an
34、gle=0.0;mb_calculatePoint( ); / 函数mb_nextFrame结束6在COpenGLPlatView类开头添加#include “Star.h“在view中添加一个WM_TIMER函数void COpenGLPlatView:OnTimer(UINT_PTR nIDEvent)/ TODO: 在此添加消息处理程序代码和/或调用默认值COpenGLPlatDoc* pDoc=GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;pDoc-m_star.mb_nextFrame();Invalidate();CView:On
35、Timer(nIDEvent);在COpenGLPlatDoc类中添加m_star变量:public:Star m_star;7.在view中的OnCreate函数中添加时间触发:int COpenGLPlatView:OnCreate(LPCREATESTRUCT lpCreateStruct)if (CView:OnCreate(lpCreateStruct) = -1)return -1;/ TODO: 在此添加您专用的创建代码.SetTimer(1,33,NULL);return 0;8.在view中的OnDestroy函数中添加代码:KillTimer(1);9.在OpenGLPla
36、tView.cpp中OnDraw 函数中添加以下代码void CMy4View:OnDraw(CDC* pDC)CMy4Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;wglMakeCurrent(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_L
37、IGHT0);glEnable(GL_AUTO_NORMAL);glEnable(GL_NORMALIZE);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LESS);glMatrixMode(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
38、=0.5f,0.5f,0.5f,0.2f;glLightfv(GL_LIGHT0,GL_POSITION,light_position);glLightfv(GL_LIGHT0,GL_AMBIENT,light_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
39、,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.0f;glMaterialfv(GL_BACK,GL_AMBIENT,material_ambient);/FRONT_AND_BACKglMaterialfv(GL_BACK,GL_DIFFUSE,material_diffuse);glMaterialfv(GL_BACK,GL_SPECULAR,material_specular);glMa
40、terialfv(GL_BACK,GL_EMISSION,material_emission);glMaterialfv(GL_BACK,GL_SHININESS,material_shiness);glRotated(pDoc-m_star.m_angle, 1.0, 1.0, 1.0);pDoc-m_star.mb_draw();/ TODO: 在此处为本机数据添加绘制代码SwapBuffers(pDC-m_hDC);wglMakeCurrent(NULL,NULL);实验六 OpenGL 交互与 动画一:实验目的:1. 掌握利用glut实现OpenGL鼠标键盘交互的方法。2. 掌握利用g
41、lut实现OpenGL动画的方法。二:实验内容:1. 利用鼠标点击、拖动和键盘按键实现一个矩形的移动。2. 使用双缓存绘制旋转正方形。3. 反弹方块动画。三:实验要求通过本实验,掌握鼠标点击、拖动和键盘按键的响应方法,利用空闲函数和定时器函数实现动态绘制的方法,利用双缓存机制连贯地显示动画图形的方法。四:实验步骤1:实现一个利用鼠标点击、拖动和键盘按键移动矩阵的程序。1)File-New-Project 选择Win32应用程序 输入名称HelloOpenGL2)点击“OK”后,在后续的对话框中选中Empty project,然后点击Finish。3)右击项目名,选择属性(property),再
42、选择链接器(Linker)中的输入选项(Input),附加依赖项(Additional Dependencies):opengl32.lib glu32.lib glaux.lib4)右击项目名下的源文件,选择添加(ADD),再选择新建项 (New Item),在弹出来的对话框中选择C+ File(.cpp),输入文件名test,然后点击添加(ADD),即新建源文件。5)在源文件test.cpp中输入如下代码:#include #include float x1 = -0.25,y1 = -0.25;/float width = 0.5,height = 0.5;float xMin = -1
43、,xMax = 1;float yMin = -1,yMax = 1;float widthWindows = 400,heightWindows=400;float colorR = 0.5,colorG = 0.5,colorB = 0.5;void display(void)glClearColor(0.0f,0.0f,0.0f,0.0f);glClearDepth(1.0);glClear (GL_COLOR_BUFFER_BIT);/绘制一个矩形,z轴位置为-1.0fglColor3f (colorR, colorG, colorB);glRectf(x1,y1,x1+width,y
44、1+height);glFlush ();void reshape (int w, int h)glViewport (0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode (GL_PROJECTION);glLoadIdentity ();/* 规定二维视景区域,参数分别为left,right,bottom,top */glOrtho(xMin, xMax, yMin, yMax,0.0, 10.0);void MyMouse(int button,int state,int x,int y)if(state = GLUT_DOWN)switch(butt
45、on)case GLUT_LEFT_BUTTON:colorR += 0.1;if(colorR 1.0)colorR = 0.0;glutPostRedisplay();break;case GLUT_MIDDLE_BUTTON:colorG += 0.1;if(colorG 1.0)colorG = 0.0;glutPostRedisplay();break;case GLUT_RIGHT_BUTTON:colorB += 0.1;if(colorB 1.0)colorB = 0.0;glutPostRedisplay();break;void MyMotion(int x, int y)
46、x1 = (float)x / widthWindows *(xMax - xMin) + xMin - width / 2;y1 = (float)(heightWindows - y) / heightWindows *(yMax - yMin) + yMin - width / 2;glutPostRedisplay();void MyKeyboard(unsigned char key, int x, int y) switch(key) case W:case w:/ 矩形坐标变量修改使得矩形上移y1 += 0.1;if(y1 = yMax - height)y1 = yMax -
47、height;glutPostRedisplay();break; case S:case s: / 矩形坐标变量修改使得矩形下移y1-= 0.1;if(y1 = xMax - width)x1 = xMax - width;glutPostRedisplay();break;int main(int argc, char* argv)glutInit(glutInitDisplayMode (GLUT_SINGLE);glutInitWindowSize (400, 400); glutInitWindowPosition (100, 100);glutCreateWindow (argv0
48、);glutDisplayFunc(display); glutReshapeFunc(reshape);glutKeyboardFunc(MyKeyboard);glutMouseFunc(MyMouse);glutMotionFunc(MyMotion);glutMainLoop();return 0;2实现一个使用双缓存绘制的旋转正方形。实验步骤1)-4)同前一个程序的设置一样。5)在源文件test.cpp中输入如下代码:#include #include static GLfloat spin = 0.0;void display(void)glClear(GL_COLOR_BUFFER_BIT);glPushMatrix();glRotatef(spin, 0.0, 0.0, 1.0);glColor3f(1.0, 1.0, 1.0);glRectf(-25.0, -25.0, 25.0, 25.0);glPopM