实验三 观察三维物体Word格式文档下载.docx
《实验三 观察三维物体Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《实验三 观察三维物体Word格式文档下载.docx(29页珍藏版)》请在冰豆网上搜索。
![实验三 观察三维物体Word格式文档下载.docx](https://file1.bdocx.com/fileroot1/2022-10/10/8857d318-a3bc-4e95-abe5-68ce7a6bb63b/8857d318-a3bc-4e95-abe5-68ce7a6bb63b1.gif)
(1)立方体的旋转方式由鼠标和键盘按键来控制:
按下鼠标左键,立方体绕x轴连续旋转;
按下鼠标左键+ctrl键,立方体绕y轴连续旋转;
按下鼠标右键,立方体绕z轴连续旋转。
(注意:
旋转的不动点在原点,正好是立方体的中心。
)
(2)如果旋转的不动点不在原点,而改为点P(1,1,1),如何实现立方体绕3个坐标轴轴的旋转?
(3)如果要求每按下一次鼠标按键或键盘按键,立方体旋转的角度增加5度,应如何修改程序?
4.自学教材4.2.7节。
编写一个交互式程序,使其可以通过鼠标(或键盘)和菜单实现一些glu和glut对象的旋转,平移和比例缩放。
(加自己的创意)
二.设计思想
本次试验主要是用户交互与三维物体观察的结合,主要用到的函数有
gluPerspective(fovy,aspect,near,far);
glOrtho(left,right,bottom,top,near,far);
gluLookAt(eyex,eyey,eyez,atx,aty,atz,upx,upy,upz);
通过这些函数的调用可以分别在正投影和透视投影下从不同的角度来观察三维物体。
三.程序清单
1.#include"
stdafx.h"
#include<
stdlib.h>
GL/glut.h>
GLdoublevertices[8][3]={-1.0,-1.0,1.0},?
-1.0,1.0,1.0},
{1.0,1.0,1.0},?
1.0,-1.0,1.0},{-1.0,-1.0,-1.0},
{-1.0,1.0,-1.0},{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};
GLdoublecolors[8][3]={{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},
{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}};
//定义顶点和颜色的全局数组
voidpolygon(inta,intb,intc,intd)
{
//根据索引列表绘制多边形
/*drawapolygonvialistofvertices*/
glBegin(GL_QUADS);
glColor3dv(colors[a]);
glVertex3dv(vertices[a]);
glColor3dv(colors[b]);
glVertex3dv(vertices[b]);
glColor3dv(colors[c]);
glVertex3dv(vertices[c]);
glColor3dv(colors[d]);
glVertex3dv(vertices[d]);
glEnd();
}
voidcolorcube()
/*mapverticestofaces*/
//利用表面绘制立方体
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
voiddisplay()
/*displaycallback,clearframebufferandzbuffer,/
anddraw,swapbuffers*/
//显示回调函数
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.0,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);
colorcube();
glutSwapBuffers();
voidmyReshape(intw,inth)
//设置投影方式
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
if(w<
=h)
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);
voidmain(intargc,char**argv)
glutInit(&
argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
//设置窗口的显示模式
glutInitWindowSize(500,500);
//指定窗口大小
glutCreateWindow("
E11414103叶传军"
);
//创建一个名为E11414103叶传军的窗口
glClearColor(0.0,0.0,0.0,0.0);
//指定窗口的背景色为黑色
//glShadeModel(GL_SMOOTH);
glShadeModel(GL_FLAT);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
//设置当前窗口的显示回调函数
//glEnable(GL_DEPTH_TEST);
/*Enablehidden--surface--removal*/
glutMainLoop();
//启动主GLUT事件处理循环
2.
(1)
#include"
GLdoublevertices[8][3]=
{{-1.0,-1.0,1.0},
{-1.0,1.0,1.0},
{1.0,1.0,1.0},
{1.0,-1.0,1.0},{-1.0,-1.0,-1.0},
GLdoublecolors[8][3]={{0.0,0.0,0.0},{1.0,0.0,0.0},
intx1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3=1,z3=0;
gluLookAt(x1,y1,z1,x2,y2,z2,x3,y3,z3);
//设置投影方式正投影
voidkeyboard(unsignedcharkey,intx,inty)
if(key=='
q'
)
{
x1++;
glutPostRedisplay();
}
elseif(key=='
Q'
x1--;
w'
y1++;