《计算机图形学实验.doc》由会员分享,可在线阅读,更多相关《计算机图形学实验.doc(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流计算机图形学实验【精品文档】第 15 页华北电力大学实 验 报 告 实验名称 OpenGL基本图元绘制实验 课程名称 计算机图形学 专业班级: 学生姓名: 学 号: 成 绩:指导教师: 实验日期:2015-4-28(实验报告如打印,纸张用A4,左装订;页边距:上下2.5cm,左2.9cm, 右2.1cm;字体:宋体小四号,1.25倍行距。)* 封面左侧印痕处装订一、实验目的及要求二、所用仪器、设备三、实验原理四、实验方法与步骤五、实验结果与数据处理 六、讨论与结论(对实验现象、实验故障及处理方法、实验中存在的问题等进行分析和讨论,对实验的进一步想法或改进
2、意见)七、所附实验输出的结果或数据一、 目的与要求1 掌握计算机图形学及交互式计算机图形学的定义,了解OpenGL的功能及工作流程,掌握基于OpenGL Glut库的程序框架。2 掌握基本的二维线画图元的绘制算法及属性,掌握OpenGL基本图元的绘制。3 理解二维、三维图形的绘制流程,掌握二维图形和三维图形的图形变换。4 了解形体的真实感表示的内容,包括消隐技术、简单光照明模型、多边形的明暗绘制技术以及纹理映射技术。5 要求使用OpenGL及GLUT库在Visual C+环境下编写图形绘制程序实现基本图元绘制。6 要求对绘制的简单场景综合利用几何变换或gluLookAt函数实现交互式三维观察程
3、序。二、 主要内容1 OpenGL基本图元绘制实验在两个具有不同属性的窗口中分别显示旋转的正方形,要求正方形内部颜色逐渐变化。观察单缓存和双缓存绘制模式的区别,利用鼠标或菜单可终止/启动图形旋转。明确程序包括哪些函数,各个函数的功能以及整个流程,从而为进一步做综合性的图形绘制实验奠定基础。2 OpenGL三维观察综合实验对于绘制的立方体或球体通过键盘移动视点,利用鼠标或键盘控制立方体的旋转方向,从不同角度观察各面颜色不同的三维物体,观察光源变化引起的物体表面变化情况,通过本实验加深理解计算机图形学中的三维图形绘制流程的工作原理和OpenGL三维观察流程及相应的函数实现。三、所用仪器、设备Win
4、dows 系统,Visual C+,OpenGL及GLUT库四、实验方法与步骤(一)先配置环境,把相关文件放到相应的文件夹C:Program FilesMicrosoft Visual StudioVC98IncludeGL C:WINDOWSsystem32 C:Program FilesMicrosoft Visual StudioVC98Lib(二)再通过VC+进行编译,源代码如下实验一:#include #include #define DEG_TO_RAD 0.017453static GLfloat theta = 0.0;static GLint m = 0;int single
5、b,doubleb;void myinit() glClearColor(1.0,1.0,1.0,1.0);/glMatrixMode(GL_PROJECTION); /gluOrtho2D(0.0,500.0,0.0,500.0); void displays() glClear(GL_COLOR_BUFFER_BIT);/glColor3f(1.0, 0.0, 0.0); if (m=0) glColor3f(1.0, 0.0, 0.0);m+=1;m=m%3; else if (m=1) glColor3f( 0.0,1.0, 0.0);m+=1;m=m%3;else if (m=2)
6、glColor3f( 0.0, 0.0,1.0);m+=1;m=m%3;glBegin(GL_POLYGON); /glVertex2f(cos(DEG_TO_RAD*theta), sin(DEG_TO_RAD*theta); /glVertex2f(-sin(DEG_TO_RAD*theta), cos(DEG_TO_RAD*theta); /glVertex2f(-cos(DEG_TO_RAD*theta), -sin(DEG_TO_RAD*theta); /glVertex2f(sin(DEG_TO_RAD*theta), -cos(DEG_TO_RAD*theta);glVertex
7、2f(cos(DEG_TO_RAD*theta), sin(DEG_TO_RAD*theta); glVertex2f(cos(DEG_TO_RAD*theta+DEG_TO_RAD*120), sin(DEG_TO_RAD*theta+DEG_TO_RAD*120); glVertex2f(cos(DEG_TO_RAD*theta+DEG_TO_RAD*240), sin(DEG_TO_RAD*theta+DEG_TO_RAD*240);glEnd(); glFlush();void displayd() glClear(GL_COLOR_BUFFER_BIT);/glColor3f(1.0
8、, 0.0, 0.0); if (m=0) glColor3f(1.0, 0.0, 0.0);m+=1;m=m%3; else if (m=1) glColor3f( 0.0,1.0, 0.0);m+=1;m=m%3;else if (m=2) glColor3f( 0.0, 0.0,1.0);m+=1;m=m%3;glBegin(GL_POLYGON); /glVertex2f(cos(DEG_TO_RAD*theta), sin(DEG_TO_RAD*theta); /glVertex2f(-sin(DEG_TO_RAD*theta), cos(DEG_TO_RAD*theta); /gl
9、Vertex2f(-cos(DEG_TO_RAD*theta), -sin(DEG_TO_RAD*theta); /glVertex2f(sin(DEG_TO_RAD*theta), -cos(DEG_TO_RAD*theta);glVertex2f(cos(DEG_TO_RAD*theta), sin(DEG_TO_RAD*theta); glVertex2f(cos(DEG_TO_RAD*theta+DEG_TO_RAD*120), sin(DEG_TO_RAD*theta+DEG_TO_RAD*120); glVertex2f(cos(DEG_TO_RAD*theta+DEG_TO_RA
10、D*240), sin(DEG_TO_RAD*theta+DEG_TO_RAD*240);glEnd(); glutSwapBuffers();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w 360.0)theta = theta - 360.0;glutSetWindow(singleb);glutPostWindowRedisplay(singleb); glutSetWindow(doubleb);glutPostWindow
11、Redisplay(doubleb); void mouse(int btn, int state, int x, int y)if(btn = GLUT_LEFT_BUTTON & state = GLUT_DOWN)glutIdleFunc(SpinDisplay);if(btn = GLUT_MIDDLE_BUTTON & state = GLUT_DOWN)glutIdleFunc(NULL);void mykey(unsigned char key, int x, int y)if(key=Q|key=q) exit(0);void quit_menu(int id)if (id=1
12、) exit(0);int _tmain(int argc, _TCHAR* argv)/glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);singleb = glutCreateWindow(single square);myinit();glutDisplayFunc(displays); glutReshapeFunc(myReshape); glutIdleFunc(SpinDisplay);glutMouseFunc(mouse);glutKeyboardFunc(mykey);glutInitDispl
13、ayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowPosition(310,0);doubleb = glutCreateWindow(double square);myinit();glutDisplayFunc(displayd); glutReshapeFunc(myReshape); glutIdleFunc(SpinDisplay);glutMouseFunc(mouse);glutCreateMenu(quit_menu);glutAddMenuEntry(quit,1);glutAttachMenu(GLUT_RIGHT_BUTTON);
14、glutMainLoop(); return 0;实验二:#include stdafx.h#include #include GLfloat vertices3 = -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;static GLfloat theta = 0.0,0.0,0.0;static GLint axis = 2;static GLdouble viewer= 0.0, 0.0, 5.0; void pol
15、ygon(int a, int b, int c , int d) glBegin(GL_POLYGON); glVertex3fv(verticesa); glVertex3fv(verticesb); glVertex3fv(verticesc); glVertex3fv(verticesd); glEnd();void colorcube() /正y前面? glColor3f(1,1,1); polygon(4,5,6,7); /正y背3面? glColor3f(1.0,0,0); polygon(0,3,2,1); glColor3f(0,1,0); polygon(2,3,7,6);
16、 glColor3f(0,0,1); polygon(0,4,7,3); glColor3f(1,1,0); polygon(1,2,6,5); glColor3f(0,1,1); polygon(0,1,5,4);void display() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearDepth(1); glLoadIdentity(); /更新?视点?位?置? gluLookAt(viewer0,viewer1,viewer2, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glRotatef(th
17、eta0, 1.0, 0.0, 0.0); glRotatef(theta1, 0.0, 1.0, 0.0); /* 旋y转a立方?体? */ glRotatef(theta2, 0.0, 0.0, 1.0); colorcube(); glutSwapBuffers();void keys(unsigned char key, int x, int y)/* 用? x, X, y, Y, z, and Z 键 移?动视点? */ if(key = x) viewer0-= 1.0; if(key = X) viewer0+= 1.0; if(key = y) viewer1-= 1.0; i
18、f(key = Y) viewer1+= 1.0; if(key = z) viewer2-= 1.0; if(key = Z) viewer2+= 1.0; display();void myReshape(int w, int h) glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w 360.0 ) thetaaxis -= 360.0; display();int _tmain(int argc, _TCHAR* argv) /glutInit(&argc, argv); glutInit
19、DisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(300, 300); glutCreateWindow(cube); glutDisplayFunc(display); glutReshapeFunc(myReshape); glutMouseFunc(mouse); glutKeyboardFunc(keys); glEnable(GL_DEPTH_TEST); glutMainLoop();return 0;五、实验结果与数据处理六、讨论与结论通过本次试验,我进一步加深了对于基本画图算法的理解。另外,通过实验训练了自己的编程能力,同时熟悉了OpenGL绘图的函数和流程,也进一步巩固了相关的知识。对图形的变换开始并不了解但是经过自己看书知道图形变换并不是那么难的,知道图形变换的几个函数。在这个坐标系中,可以对物体实施平移glTranslatef()、旋转glRotatef()和放大缩小glScalef()。做了实验之后收获很多,结合课堂上老师的讲解,对计算机图形学从许多方面都有了一定的理解。对更加深入学习计算机打下比较好的基础。