《X-opengl立方体的简单三维交互式几何变换实验报告代码23330.pdf》由会员分享,可在线阅读,更多相关《X-opengl立方体的简单三维交互式几何变换实验报告代码23330.pdf(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-立方体的简单三维交互式几何变换 立方体的简单三维交互式几何变换。功能:键盘的方向键实现立方体的上下左右平移;A 键,S 键分别实现向前,向后旋转;J 键,K 键分别实现放大,缩小;C 键退出。程序模块:1.重绘回调函数,在窗口首次创立或用户改变窗口尺寸时被调用。void reshape(int w,int h)glViewport(0,0,w,h);/指定视口的位置和大小 glMatri*Mode(GL_PROJECTION);glLoadIdentity();/glFrustum(-1.0,1.0,-1.0,1.0,3.1,10.0);/gluPerspective(45,1,0.1,10
2、.0);glOrtho(-2.0,2.0,-2.0,2.0,2.0,10.0);2.绘制一个立方体。void DrawBo*()glBegin(GL_QUADS);/前面 glColor3f(1,0,0);glVerte*3f(-1.0f,-1.0f,1.0f);/四边形的左下-glVerte*3f(1.0f,-1.0f,1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,1.0f);/四边形的右上 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的左上 /后面 glColor3f(0,1,0);glVerte*3f(-1.0f,-1.0f,-1.0f);/四
3、边形的右下 glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的左上 glVerte*3f(1.0f,-1.0f,-1.0f);/四边形的左下 /顶面 glColor3f(0,0,1);glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的左上 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的左下 glVerte*3f(1.0f,1.0f,1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的右上 /底面 glColor3f(1,1,0
4、);glVerte*3f(-1.0f,-1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,-1.0f,-1.0f);/四边形的左上 glVerte*3f(1.0f,-1.0f,1.0f);/四边形的左下 glVerte*3f(-1.0f,-1.0f,1.0f);/四边形的右下 /右面-glColor3f(0,1,1);glVerte*3f(1.0f,-1.0f,-1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,1.0f,1.0f);/四边形的左上 glVerte*3f(1.0f,-1.0f
5、,1.0f);/四边形的左下 /左面 glColor3f(1,0,1);glVerte*3f(-1.0f,-1.0f,-1.0f);/四边形的左下 glVerte*3f(-1.0f,-1.0f,1.0f);/四边形的右下 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的右上 glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的左上 glEnd();3该模块设置屏幕颜色和深度缓存,设置立方体的平移,缩放,旋转变换。void display()glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);/清空颜色和深度缓存 g
6、lMatri*Mode(GL_MODELVIEW);glLoadIdentity();/gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);-glTranslatef(0.0f+t*,0.0f+ty,-4.0f);/平移变换 glScalef(1+sf,1+sf,1+sf);/缩放变换 glRotatef(rat*,1.0f,0.0f,0.0f);/绕*轴的旋转变换 glRotatef(ratY,0.0f,1.0f,0.0f);/绕 Y 轴的旋转变换 glRotatef(ratZ,0.0f,0.0f,1.0f);/绕 Z 轴的旋转变换 DrawBo*(
7、);glFlush();glutSwapBuffers();4.该模块为设置深度缓存和启动深度测试模式。void init()glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);/启动深度测试模式 5.该模块为设置键盘的交互式操作。void myKeyboard(unsigned char key,int*,int y)if(key=a|key=A)rat*+=2;ratY+=2;ratZ+=2;-if(key=s|key=S)rat*-=2;ratY-=2;ratZ-=2;if(key=j|key=J)sf+=0.1;if(key=k|
8、key=K)sf-=0.1;if(key=c|key=C)e*it(0);glutPostRedisplay();/重新调用绘制函数 6.该模块为设置键盘方向键的交互式操作。void myspecialKeyboard(int key,int*,int y)if(key=GLUT_KEY_UP)ty+=0.1;if(key=GLUT_KEY_DOWN)ty-=0.1;if(key=GLUT_KEY_LEFT)t*-=0.1;if(key=GLUT_KEY_RIGHT)t*+=0.1;-glutPostRedisplay();7.主函数模块,实现设置显示模式 窗口大小,位置,调用函数操作。int
9、 main(int argc,char*argv)glutInit(&argc,argv);/初始 GLUT.glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB);/设定显示模式 glutInitWindowSize(400,400);/设定窗口大小 glutInitWindowPosition(50,100);/设定窗口位置 glutCreateWindow(立方体的简单三维交互式几何变换);/用前面指定参数创立 glutReshapeFunc(reshape);/指定重绘回调函数 glutDisplayFunc(display);/进展一
10、些初始化工作 glutKeyboardFunc(myKeyboard);/指定键盘回调函数 glutSpecialFunc(myspecialKeyboard);/指定键盘回调函数 init();glutMainLoop();/进入无穷循环,等待事件处理 功能截图:立方体的平移:上平移-下平移:左平移:右平移:放大:缩小:旋转:源程序:#include#include#include float rat*=60;float ratY=60;float ratZ=60;float t*=0;float ty=0;float sf=0;void DrawBo*()glBegin(GL_QUADS)
11、;/前面 glColor3f(1,0,0);glVerte*3f(-1.0f,-1.0f,1.0f);/四边形的左下-glVerte*3f(1.0f,-1.0f,1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,1.0f);/四边形的右上 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的左上 /后面 glColor3f(0,1,0);glVerte*3f(-1.0f,-1.0f,-1.0f);/四边形的右下 glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的左上 gl
12、Verte*3f(1.0f,-1.0f,-1.0f);/四边形的左下 /顶面 glColor3f(0,0,1);glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的左上 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的左下 glVerte*3f(1.0f,1.0f,1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的右上 /底面 glColor3f(1,1,0);glVerte*3f(-1.0f,-1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,-1.0f,-1.0f);/四边形的左上 glVe
13、rte*3f(1.0f,-1.0f,1.0f);/四边形的左下 glVerte*3f(-1.0f,-1.0f,1.0f);/四边形的右下 /右面-glColor3f(0,1,1);glVerte*3f(1.0f,-1.0f,-1.0f);/四边形的右下 glVerte*3f(1.0f,1.0f,-1.0f);/四边形的右上 glVerte*3f(1.0f,1.0f,1.0f);/四边形的左上 glVerte*3f(1.0f,-1.0f,1.0f);/四边形的左下 /左面 glColor3f(1,0,1);glVerte*3f(-1.0f,-1.0f,-1.0f);/四边形的左下 glVerte
14、*3f(-1.0f,-1.0f,1.0f);/四边形的右下 glVerte*3f(-1.0f,1.0f,1.0f);/四边形的右上 glVerte*3f(-1.0f,1.0f,-1.0f);/四边形的左上 glEnd();void display()glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);/清空颜色和深度缓存 glMatri*Mode(GL_MODELVIEW);glLoadIdentity();/gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);glTranslatef(0.0f+t*,0.
15、0f+ty,-4.0f);/平移变换 glScalef(1+sf,1+sf,1+sf);/缩放变换-glRotatef(rat*,1.0f,0.0f,0.0f);/绕*轴的旋转变换 glRotatef(ratY,0.0f,1.0f,0.0f);/绕 Y 轴的旋转变换 glRotatef(ratZ,0.0f,0.0f,1.0f);/绕 Z 轴的旋转变换 DrawBo*();glFlush();glutSwapBuffers();void reshape(int w,int h)/重绘回调函数,在窗口首次创立或用户改变窗口尺寸时被调用 glViewport(0,0,w,h);/指定视口的位置和大小
16、 glMatri*Mode(GL_PROJECTION);glLoadIdentity();/glFrustum(-1.0,1.0,-1.0,1.0,3.1,10.0);/gluPerspective(45,1,0.1,10.0);glOrtho(-2.0,2.0,-2.0,2.0,2.0,10.0);void init()glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);/启动深度测试模式 -void myKeyboard(unsigned char key,int*,int y)if(key=a|key=A)rat*+=2;ratY
17、+=2;ratZ+=2;if(key=s|key=S)rat*-=2;ratY-=2;ratZ-=2;if(key=j|key=J)sf+=0.1;if(key=k|key=K)sf-=0.1;if(key=c|key=C)e*it(0);glutPostRedisplay();/重新调用绘制函数 void myspecialKeyboard(int key,int*,int y)if(key=GLUT_KEY_UP)ty+=0.1;if(key=GLUT_KEY_DOWN)-ty-=0.1;if(key=GLUT_KEY_LEFT)t*-=0.1;if(key=GLUT_KEY_RIGHT)
18、t*+=0.1;glutPostRedisplay();int main(int argc,char*argv)glutInit(&argc,argv);/初始 GLUT.glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB);/设定显示模式 glutInitWindowSize(400,400);/设定窗口大小 glutInitWindowPosition(50,100);/设定窗口位置 glutCreateWindow(立方体的简单三维交互式几何变换);/用前面指定参数创立 glutReshapeFunc(reshape);/指定重绘回调函数 glutDisplayFunc(display);/进展一些初始化工作 glutKeyboardFunc(myKeyboard);/指定键盘回调函数 glutSpecialFunc(myspecialKeyboard);/指定键盘回调函数 init();glutMainLoop();/进入无穷循环,等待事件处理-