1、计算机图形学实验报告 实验一 直线的DDA算法一、【实验目的】1.掌握DDA算法的基本原理。2.掌握DDA直线扫描转换算法。3.深入了解直线扫描转换的编程思想。二、【实验内容】1.利用DDA的算法原理,编程实现对直线的扫描转换。2.加强对DDA算法的理解和掌握。三、【测试数据及其结果】四、【实验源代码】#include#include#include#includeGLsizei winWidth=500;GLsizei winHeight=500;void Initial(void) glClearColor(1.0f,1.0f,1.0f,1.0f); glMatrixMode(GL_PRO
2、JECTION); gluOrtho2D(0.0,200.0,0.0,150.0);void DDALine(int x0,int y0,int x1,int y1) glColor3f(1.0,0.0,0.0); int dx,dy,epsl,k; float x,y,xIncre,yIncre; dx=x1-x0; dy=y1-y0; x=x0; y=y0; if(abs(dx)abs(dy) epsl=abs(dx); else epsl=abs(dy); xIncre=(float)dx/(float)epsl; yIncre=(float)dy/(float)epsl; for(k=
3、0;k=epsl;k+) glPointSize(3); glBegin(GL_POINTS); glVertex2i(int(x+0.5),(int)(y+0.5); glEnd(); x+=xIncre; y+=yIncre; void Display(void) glClear(GL_COLOR_BUFFER_BIT); DDALine(100,100,200,180); glFlush();void winReshapeFcn(GLint newWidth, GLint newHeight) glMatrixMode(GL_PROJECTION); glLoadIdentity();
4、gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight); glClear(GL_COLOR_BUFFER_BIT); winWidth=newWidth; winHeight=newHeight;int main(int argc,char*argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(400,300); glutInitWindowPosition(100,120); glutCreateWin
5、dow(line); Initial(); glutDisplayFunc(Display); glutReshapeFunc(winReshapeFcn); glutMainLoop(); return 0;实验二 Bresenham绘制直线和圆一、【实验目的】1.掌握Bresenham算法扫描转换圆和直线的基本原理。二、【实验内容】1.利用Bresenham算法扫描转换圆和直线的基本原理编程实现对圆和直线的扫描转换。三、【测试数据及其结果】四、【实验源代码】绘制直线:#include#include#include#includeGLsizei winWidth=500;GLsizei w
6、inHeight=500;void lineBres(int x0, int y0, int xEnd, int yEnd) glColor3f(0.0, 0.0, 1.0); int dx=fabs(xEnd-x0), dy=fabs(yEnd-y0); int p=2*dy-dx; int twoDy=2*dy, twoDyMinusDx=2*(dy-dx); int x, y; if (x0xEnd) x=xEnd; y=yEnd; xEnd=x0; else x=x0; y=y0; glPointSize(6); glBegin(GL_POINTS); glVertex2i(x, y)
7、; glEnd(); while (xxEnd) x+; if (p0) p+=twoDy; else y+; p+=twoDyMinusDx; glPointSize(2); glBegin(GL_POINTS); glVertex2i(x, y); glEnd(); void init (void) glClearColor(1.0, 1.0, 1.0, 1.0); glShadeModel(GL_FLAT);void display (void) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); lineBres(10, 10, 400,
8、 300); glFlush();void winReshapeFcn(GLint newWidth, GLint newHeight) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight); glClear(GL_COLOR_BUFFER_BIT); winWidth=newWidth; winHeight=newHeight;void main(int argc, char* argv) glutInit(&argc, argv)
9、; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(10, 10); glutInitWindowSize(winWidth, winHeight); glutCreateWindow(lineBres); init(); glutDisplayFunc(display); glutReshapeFunc(winReshapeFcn); glutMainLoop();绘制圆:#includevoid init() glClearColor(0,0,0,0);void MidBresenhamCircle(i
10、nt r) int x,y,d; x=0; y=r; d=1-r; glBegin(GL_LINE_STRIP); while(x=y)glVertex2f(x,y); if(d0) d+=2*x+3; else d+=2*(x-y)+5; y-; x+; glEnd();void display() glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1,0,0); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(
11、45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glRotated(45,0,0,1); MidBresenhamCircle(8); glutSwapBuffers();void reshape(int w,int h) gl
12、Viewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-10,10,-10,10);int main(int argc,char*argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); glutCreateWindow(扫描转换圆); glutDisplayFunc(display); g
13、lutReshapeFunc(reshape); glutMainLoop(); return 0;实验三 反走样及五环的绘制一、【实验目的】1.了解走样和反走样的内容,熟练掌握用opengl实现图形的反走样。?2.学会用反走样消除走样现象。3.学会五环的绘制方法。二、【实验内容】1.通过学习反走样相关课程,用opengl实现光栅图形的反走样。2.绘制五环。三、【测试数据及其结果】四、【实验源代码】反走样:#include#pragma comment(linker,/subsystem:windows /entry:mainCRTStartup)GLuint lineList; /指定显示列
14、表void Initial() glClearColor(1.0f,1.0f,1.0f,0.0f); glLineWidth(12.0f); glColor4f(0.0,0.6,1.0,1.0); lineList=glGenLists(1); /获得一个显示列表标识 glNewList(lineList,GL_COMPILE); /定义显示列表 glBegin(GL_LINE_LOOP); glVertex2f(1.0f,1.0f); glVertex2f(4.0f,2.0f); glVertex2f(2.0f,5.0f); glEnd(); glEndList();void ChangeS
15、ize(GLsizei w,GLsizei h) if(h=0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); /指定设置投影参数 glLoadIdentity(); if(w=h) gluOrtho2D(0.0,5.0,0.0,6.0*(GLfloat)h/(GLfloat)w); else gluOrtho2D(0.0,5.0*(GLfloat)w/(GLfloat)h,0.0,6.0); glMatrixMode(GL_MODELVIEW); /指定设置模型视图变换参数 glLoadIdentity();void Displ
16、ayt(void) glClear(GL_COLOR_BUFFER_BIT); glCallList(lineList); /调用显示列表 glFlush();void Displayw(void) glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_LINE_SMOOTH); /使用反走样 glEnable(GL_BLEND); /启用混合函数 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); /指定混合函数 glCallList(lineList); /调用显示列表 glFlush();void main(v
17、oid) glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(300,300); glutCreateWindow(原始图形); glutDisplayFunc(Displayt); glutReshapeFunc(ChangeSize); Initial(); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(300,300); glutInitWindowSize(300,300); glutCreateWindow(反走样图形); gl
18、utDisplayFunc(Displayw); glutReshapeFunc(ChangeSize); Initial(); glutMainLoop();五环:#include#include #pragma comment(linker,/subsystem:windows /entry:mainCRTStartup)const float PI=3.1415;void DrawCircle(GLfloat radius) GLfloat x,y,z; glBegin(GL_LINE_LOOP); for (int alpha=0;alpha360;alpha+) x=radius*c
19、os(alpha*PI/180); y=radius*sin(alpha*PI/180); z=0; glVertex3f(x,y,z); glEnd();void Display() glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0,-25); glColor3f(0,1,0); glLineWidth(3); DrawCircle(3.0); glPopMatrix(); glPushMatrix(); glTranslatef(7,0,0); glColor3f(
20、1,0,0); DrawCircle(3.0); glPopMatrix(); glPushMatrix(); glTranslatef(-7,0,0); glColor3f(0,0,1); DrawCircle(3.0); glPopMatrix(); glPushMatrix(); glTranslatef(-3.5,-3.5,0); glColor3f(0.3,0.5,0.7); DrawCircle(3.0); glPopMatrix(); glPushMatrix(); glTranslatef(3.5,-3.5,0); glColor3f(0.7,0.0,0.3); DrawCir
21、cle(3.0); glPopMatrix(); glutSwapBuffers();void reshape(int w,int h) glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,GLdouble(w)/h,1,100); glMatrixMode(GL_MODELVIEW);void main(int argc,char *argv) glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
22、 glutInitWindowPosition(10,10); glutInitWindowSize(500,500); glutCreateWindow(Test); glutDisplayFunc(Display); glutReshapeFunc(reshape); glutMainLoop();实验四 多视区一、【实验目的】1.熟练掌握各种裁剪算法和二维观察变换。?2.学会在屏幕坐标系下创建多个视区、指定视区的宽度和高度,了解二维观察变换中包含窗口到视区的映射。二、【实验内容】1.在一个显示窗口内指定多个视区,分别显示具有相同坐标、不同颜色和不同显示模式的各种图形面。?2.在书本给定程
23、序基础上,对程序做一些改变并在视区中绘制各种图形。三、【测试数据及其结果】四、【实验源代码】#include#includeconst float PI=3.1415;void initial(void) glClearColor(1.0,1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-10.0,10.0,-10.0,10.0);void triangle(GLsizei mode) if(mode=1) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); else
24、 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glBegin(GL_TRIANGLES); glVertex2f(0.0,5.0); glVertex2f(5.0,-5.0); glVertex2f(-5.0,-5.0); glEnd();void polygon(GLsizei mode) if(mode=1) glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glBegin(GL_POLYGON); glVertex2f(2
25、.0,7.0); glVertex2f(5.0,3.0); glVertex2f(4.0,0.0); glVertex2f(0.0,0.0); glVertex2f(1.0,4.0); glEnd();void DrawCircle(GLfloat r) GLfloat x,y,z; glBegin(GL_LINE_LOOP); for (int alpha=0;alpha360;alpha+) x=r*cos(alpha*PI/180); y=r*sin(alpha*PI/180); z=0; glVertex3f(x,y,z); glEnd();void Display() glClear
26、(GL_COLOR_BUFFER_BIT); glColor3f(1.0,0.0,0.0); glViewport(0,0,100,100); triangle(1); glColor3f(0.0,0.0,1.0); glViewport(100,0,100,100); triangle(2); glColor3f(1.0,0.0,0.0); glViewport(0,100,100,100); polygon(2); glViewport(100,100,100,100); DrawCircle(5); glFlush();void main(void) glutInitDisplayMod
27、e(GLUT_SINGLE|GLUT_RGB); glutInitWindowPosition(10,10); glutInitWindowSize(400,200); glutCreateWindow(多视区); initial(); glutDisplayFunc(Display); glutMainLoop();实验五 分子模型一、【实验目的】1.熟练掌握二维、三维几何变换矩阵和透视投影的相关知识从而用opengl实现分子模型的运动。?2.熟练掌握opengl中相关函数的调用和实现。二、【实验内容】1.显示分子模型:红色大球表示原子,三个黄色小球表示电子,分别绕原子旋转,采用透视投影变换
28、显示电子旋转过程。2.启用深度测试和模型视图矩阵完成分子动画。三、【测试数据及其结果】四、【实验源代码】#includeGLint angleSelf=0;void Initial() glEnable(GL_DEPTH_TEST); glClearColor(1.0f,1.0f,1.0f,1.0f);void ChangeSize(int w,int h) if(h=0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLfloat fAspect; fAspect=(float)w/(float)h; gluPerspective(45.0,fAspect,1,500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();void Display(void) static float fElect1=0.0f; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f,0.0f,-250.0f); glColor3f(1.0f,0.0f,0.0f);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1