资源描述:
《opengl立方体地简单三维交互式几何变换实验报告材料+代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、实用标准文案立方体的简单三维交互式几何变换这个学期对opengl的学习,使我对计算机图形学的一些算法过程有了更多的了解。因为对三维图形的显示比较感兴趣,就做了立方体的简单三维交互式几何变换。功能:键盘的方向键实现立方体的上下左右平移;A键,S键分别实现向前,向后旋转;J键,K键分别实现放大,缩小;C键退出。程序模块:1.该模块为绘制一个立方体。voidDrawBox(){glBegin(GL_QUADS);//前面glColor3f(1,0,0);glVertex3f(-1.0f,-1.0f,1.0f);//四边形的左下gl
2、Vertex3f(1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左上//后面glColor3f(0,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,-1.0f);//四边形的右上精彩文档实用标准文案glVertex3f(1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(1.0f,-1
3、.0f,-1.0f);//四边形的左下//顶面glColor3f(0,0,1);glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glVertex3f(-1.0f,1.0f,1.0f);//四边形的左下glVertex3f(1.0f,1.0f,1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上//底面glColor3f(1,1,0);glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,-1.0f,
4、-1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下//右面glColor3f(0,1,1);glVertex3f(1.0f,-1.0f,-1.0f);//四边形的右下glVertex3f(1.0f,1.0f,-1.0f);//四边形的右上glVertex3f(1.0f,1.0f,1.0f);//四边形的左上glVertex3f(1.0f,-1.0f,1.0f);//四边形的左下//左面glColor3
5、f(1,0,1);精彩文档实用标准文案glVertex3f(-1.0f,-1.0f,-1.0f);//四边形的左下glVertex3f(-1.0f,-1.0f,1.0f);//四边形的右下glVertex3f(-1.0f,1.0f,1.0f);//四边形的右上glVertex3f(-1.0f,1.0f,-1.0f);//四边形的左上glEnd();}2.该模块设置屏幕颜色和深度缓存,设置立方体的平移,缩放,旋转变换。voiddisplay(){glClear(GL_COLOR_BUFFER_BIT
6、GL_DEPTH_BUFF
7、ER_BIT);//清空颜色和深度缓存glMatrixMode(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+tx,0.0f+ty,-4.0f);//平移变换glScalef(1+sf,1+sf,1+sf);//缩放变换glRotatef(ratX,1.0f,0.0f,0.0f);//绕X轴的旋转变换glRotatef(ratY,0.0f,1.0f,0.0f);//绕Y轴的旋转
8、变换glRotatef(ratZ,0.0f,0.0f,1.0f);//绕Z轴的旋转变换DrawBox();glFlush();精彩文档实用标准文案glutSwapBuffers();}3.该模块为重绘回调函数,在窗口首次创建或用户改变窗口尺寸时被调用。voidreshape(intw,inth){glViewport(0,0,w,h);//指定视口的位置和大小glMatrixMode(GL_PROJECTION);glLoadIdentity();//glFrustum(-1.0,1.0,-1.0,1.0,3.1,10.0)
9、;//gluPerspective(45,1,0.1,10.0);glOrtho(-2.0,2.0,-2.0,2.0,2.0,10.0);}4.该模块为设置深度缓存和启动深度测试模式。voidinit(){glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_