《report2郭小明 三维图形程序设计 电子科技大学.doc》由会员分享,可在线阅读,更多相关《report2郭小明 三维图形程序设计 电子科技大学.doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、实验报告课程名称:三维图形程序设计学院:计算机科学与工程专业:计算机科学与技术指导教师:曹跃学生姓名:郭小明学号:2011060100010实验成绩:日期:2013年5月3日电 子 科 技 大 学实 验 报 告 实 验 二 OpenGL建模与变换编程实验一实验室名称:A2二. 实验目的:1. 理解OpenGL视图变换、投影变换、模型-视图变换的相关原理;2. 掌握与变换相关的OpenGL API函数及其用法。三. 实验原理:OpenGL编程原理四. 实验内容:(一)编程任务1绘制钟表提示:1. 利用图元和建模变换来建立钟表的基本构件a. 表盘void createDial();b. 刻度voi
2、d createScale1();void createScale2();c时针、分针、秒针void createHourhand(); void createMinutehand(); void createSecondhand();2. 利用建模变换将钟表的基本构件组装在一起void createWatch();3. 控制钟表时针、分针和秒针的运动void processWatch(GLuint hour, GLuint minute, GLuint second);4. 获取本地时间使用标准库time.h中的time()、localtime()函数获取本地时间:struct tm* lo
3、calTime; int hour, minute, second;time_t curTime;time(&curTime);localTime = localtime(&curTime);hour=localTime-tm_hour;minute=localTime-tm_mins;second=localTime-tm_sec;5. 依据当前时间绘制钟表void displayWatch(void);(二)编程任务2P96 作业2编写一个程序,实现交互式的移动摄像机。显示一个或多个对象(如茶壶等),并确保摄像机在移动时总是朝向场景中的同一点。五、 实验器材(设备、元器件):Microso
4、ft Windows XP Professional 版本2002 Service Pack 3 VC+ 6.0七、实验数据及结果分析:(一)编程任务1绘制钟表#include #include #include #include void init()glClearColor (1.0, 0.0, 0.0, 0.0);gluOrtho2D(-100,100,-100,100);/时针void createHourhand(float angle)glRotatef(angle,0,0,1.0);glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);glColor3
5、f(1.0,1.0,1.0);glBegin(GL_POLYGON);glVertex3f(-10,0,0);glVertex3f(0,4,0);glVertex3f(25,0,0);glVertex3f(0,-4,0);glEnd();glRotatef(-angle,0,0,1.0);/分针void createMinutehand(float angle)glRotatef(angle,0,0,1.0);glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);glColor3f(1.0,1.0,1.0);glBegin(GL_POLYGON);glVertex3
6、f(-10,0,0);glVertex3f(0,3,0);glVertex3f(30,0,0);glVertex3f(0,-3,0);glEnd();glRotatef(-angle,0,0,1.0);/秒针void createSecondhand(float angle)/printf(adfadf);glRotatef(angle,0,0,1.0);glColor3f(1.0,0.0,0.0);glLineWidth(2.0);glBegin(GL_LINES);glVertex3f(-6,0,0);glVertex3f(35,0,0);glEnd();glRotatef(-angle,
7、0,0,1.0);int kedu_x = 10,kedu_y=2;/表盘上个长方形刻度相关参数void createDial_kedu(float angle,float offset_x,float offset_y)/glClear(GL_COLOR_BUFFER_BIT);glRotatef(angle,0.0,0.0,1.0);glTranslatef(offset_x-kedu_x/2,offset_y,0);glBegin(GL_POLYGON);glVertex3f(kedu_x/2,kedu_y/2,0);glVertex3f(-kedu_x/2,kedu_y/2,0);gl
8、Vertex3f(-kedu_x/2,-kedu_y/2,0);glVertex3f(kedu_x/2,-kedu_y/2,0);glEnd();glTranslatef(-offset_x+kedu_x/2,-offset_y,0);glRotatef(-angle,0.0,0.0,1.0);/创建表盘int D = 50;float PI = 3.14159;void createDial()glClear(GL_COLOR_BUFFER_BIT);glColor3f(0.0,1.0,0.0);int i = 0;glBegin(GL_POLYGON);for(;i24;i+)glVert
9、ex3f(D*cos(i*PI/12.0),D*sin(i*PI/12.0),0);glEnd();glLineWidth(3.0);glColor3f(0.0,0.0,0.0);glBegin(GL_LINE_LOOP);for(i=0;i24;i+)glVertex3f(D*cos(i*PI/12.0),D*sin(i*PI/12.0),0);glEnd();glLineWidth(1.0);glColor3f(0.0,0.0,0.0);glPointSize(8.0);glBegin(GL_POINTS);glVertex3f(0,D-2.5,0);glVertex3f(0,-D+2.5
10、,0);glVertex3f(D-2.5,0,0);glVertex3f(-D+2.5,0,0);glEnd();glPointSize(1.0);glBegin(GL_LINES);glVertex3f(-D,0,0);glVertex3f(D,0,0);glVertex3f(0,D,0);glVertex3f(0,-D,0);glEnd();createDial_kedu(30,D,0);createDial_kedu(60,D,0);createDial_kedu(120,D,0);createDial_kedu(150,D,0);createDial_kedu(-30,D,0);cre
11、ateDial_kedu(-60,D,0);createDial_kedu(-120,D,0);createDial_kedu(-150,D,0);/createDial_kedu();/建模变换,组装构件void createWatch(float hour,float minute,float second)createDial();glRotatef(90,0.0,0.0,1.0);createHourhand(-hour/12*360-minute/60*30);createMinutehand(-minute/60*360);createSecondhand(-second/60*3
12、60);glRotatef(-90,0.0,0.0,1.0);/下面代码为画表盘中间的灰色园glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);glColor3f(0.5,0.5,0.5);glBegin(GL_POLYGON);for(int i=0;itm_sec)Hour=localTime-tm_hour;Minute=localTime-tm_min;Second=localTime-tm_sec;glutPostRedisplay();void display()/createWatch(1,2,40);createWatch(Hour,Minute,
13、Second);glFlush();void timerProc(int id)processWatch();glutTimerFunc(1000,timerProc,1);int main(int argc,char * argv)glutInit(&argc,argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (500, 500);glutInitWindowPosition (100, 100);glutCreateWindow (argv0);processWatch();glutDisplayFu
14、nc(display);/glutIdleFunc(processWatch);glutTimerFunc(1000,timerProc,1);processWatch();init();glutMainLoop();return 0;(二)编程任务2P96 作业2#include #include /#include void init()glClearColor(1.0,0.0,0.0,0.0);glOrtho(-1,1,-1,1,-10,10);GLdouble eyex=0.0,eyey=0.0,eyez=0.8;/GLdouble centerx=0.0,centery=0.0,ce
15、nterz=0.0;GLdouble upx=0.0,upy=1.0,upz=0.0;int flag = 0;float angel1=0,angel2=0,angel3=0;float depth = 0;float change = 5;void change_look_point(unsigned char key,int x,int y)switch(key)case x:angel1 -= change;break;case X:angel1 += change;break;case y:angel2 -= change;break;case Y:angel2 += change;
16、break;case z:angel3 -= change;break;case Z:angel3 += change;break;case d:depth -= 0.1;break;case D:depth += 0.1;break;default:return;/glLoadIdentity();glutPostRedisplay();void display()glClear(GL_COLOR_BUFFER_BIT);glRotatef(angel1,1,0,0);glRotatef(angel2,0,1,0);glRotatef(angel3,0,0,1);glTranslatef(0
17、,0,depth);/gluLookAt(eyex,eyey,eyez,0,0,0,upx,upy,upz);glTranslatef(-0.4,0,0.0);glutWireTeapot(0.3);glTranslatef(0.8,0,0.0);glutWireTeapot(0.3);glTranslatef(-0.4,0,0.0);glFlush();int main(int argc,char * argv)glutInit(&argc,argv);glutCreateWindow(郭小明的移动摄像机);glutInitWindowSize(800,800);glutDisplayFun
18、c(display);glutKeyboardFunc(change_look_point);init();glutMainLoop();return 0;显示了两个茶壶对象:下面通过旋转x,y,z来改变视角,但是摄像机关注的点和方向没有发生变化X调整:Y调整Z调整八、总结、改进建议及心得体会:通过本次试验,我掌握了VC中OpenGL三维编程环境的建立过程;掌握了GLUT编写OpenGL应用程序的基本步骤,熟悉GLUT基于事件驱动的编程模型,理解OpenGL视图变换、投影变换、模型-视图变换的相关原理;掌握了掌握与变换相关的OpenGL API函数及其用法,通过修改不同的颜色,可以得到不用优美的图片。