实验三观察三维物体Word下载.docx
《实验三观察三维物体Word下载.docx》由会员分享,可在线阅读,更多相关《实验三观察三维物体Word下载.docx(14页珍藏版)》请在冰点文库上搜索。
按下鼠标右键,立方体绕z轴连续旋转。
(注意:
旋转的不动点在原点,正好是立方体的中心。
)
(2)如果旋转的不动点不在原点,而改为点P(1,1,1),如何实现立方体绕3个坐标轴轴的旋转
(3)如果要求每按下一次鼠标按键或键盘按键,立方体旋转的角度增加5度,应如何修改程序
4.自学教材节。
编写一个交互式程序,使其可以通过鼠标(或键盘)和菜单实现一些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"
"
#include<
>
GL/>
GLdoublevertices[8][3]={,,},,,},
{,,},,,},{,,},
{,,},{,,},{,,}};
GLdoublecolors[8][3]={{,,},{,,},
{,,},{,,},{,,},
{,,},{,,},{,,}};
1)
#include"
GLdoublevertices[8][3]=
{{,,},
{,,},
{,,},
{,,},{,,},
GLdoublecolors[8][3]={{,,},{,,},
//定义顶点和颜色的全局数组
intx1=1,y1=1,z1=1,x2=0,y2=0,z2=0,x3=0,y3=1,z3=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(x1,y1,z1,x2,y2,z2,x3,y3,z3);
colorcube();
glutSwapBuffers();
voidmyReshape(intw,inth)
//设置投影方式正投影
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
if(w<
=h)
glOrtho,,*(GLfloat)h/(GLfloat)w,
*(GLfloat)h/(GLfloat)w,,;
else
glOrtho*(GLfloat)w/(GLfloat)h,
*(GLfloat)w/(GLfloat)h,,,,;
voidkeyboard(unsignedcharkey,intx,inty)
if(key=='
q'
)
{
x1++;
glutPostRedisplay();
}
elseif(key=='
Q'
x1--;
w'
y1++;
W'
y1--;
e'
z1++;
E'
z1--;
a'
x2++;
A'
x2--;
s'
y2++;
S'
y2--;
d'
z2++;
D'
z2--;
z'
x3++;
Z'
z3--;
x'
y3++;
X'
y3--;
c'
z3++;
C'
voidmain(intargc,char**argv)
glutInit(&
argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
//设置窗口的显示模式
glutInitWindowSize(500,500);
//指定窗口大小
glutCreateWindow("
E叶传军"
);
//创建一个名为E叶传军的窗口
glClearColor,,,;
//指定窗口的背景色为黑色
//glShadeModel(GL_SMOOTH);
glShadeModel(GL_FLAT);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
//设置当前窗口的显示回调函数
glutKeyboardFunc(keyboard);
//glEnable(GL_DEPTH_TEST);
/*Enablehidden--surface--removal*/
glutMainLoop();
//启动主GLUT事件处理循环
(2)
//设置投影方式透视投影
glFrustum,,*(GLfloat)h/(GLfloat)w,
glFrustum*(GLfloat)w/(GLfloat)h,
(3)
四.结果分析
掌握了三维物体的观察方法,以及对视角的一些调整函数。