计算机图形学实验报告计算机图形学课程实验报告姓名:学号:计算机图形学实验报告目录DDA算法绘制直线实验一实验一直线的DDA算法一、【实验目的】1.掌握DDA算法的基本原理。2.掌握DDA直线扫描转换算法。3.深入了解直线扫描转换的编程思想。二、【实验内容】1.利用DDA的算法原理,编程实现对直线的扫描转换。2.加强对DDA算法的理解和掌握。三、【测试数据及其结果】四、【实验源代码】#includestdlib.hDDA算法绘制直线实验一#includemath.h#includeGL/glut.h#includestdio.hGLsizeiwinWidth=500;GLsizeiwinHeight=500;voidInitial(void){glClearColor(1.0f,1.0f,1.0f,1.0f);glMatrixMode(GL_PROJECTION);gluOrtho2D(0.0,200.0,0.0,150.0);}voidDDALine(intx0,inty0,intx1,inty1){glColor3f(1.0,0.0,0.0);intdx,dy,epsl,k;floatx,y,xIncre,yIncre;dx=x1-x0;dy=y1-y0;x=x0;y=y0;if(abs(dx)abs(dy))epsl=abs(dx);elseepsl=abs(dy);xIncre=(float)dx/(float)epsl;yIncre=(float)dy/(float)epsl;for(k=0;k=epsl;k++){glPointSize(3);glBegin(GL_POINTS);glVertex2i(int(x+0.5),(int)(y+0.5));glEnd();x+=xIncre;y+=yIncre;}}voidDisplay(void){glClear(GL_COLOR_BUFFER_BIT);DDALine(100,100,200,180);glFlush();}DDA算法绘制直线实验一voidwinReshapeFcn(GLintnewWidth,GLintnewHeight){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));glClear(GL_COLOR_BUFFER_BIT);winWidth=newWidth;winHeight=newHeight;}intmain(intargc,char*argv[]){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(400,300);glutInitWindowPosition(100,120);glutCreateWindow(line);Initial();glutDisplayFunc(Display);glutReshapeFunc(winReshapeFcn);glutMainLoop();return0;}Bresenham算法实验二实验二Bresenham绘制直线和圆一、【实验目的】1.掌握Bresenham算法扫描转换圆和直线的基本原理。二、【实验内容】1.利用Bresenham算法扫描转换圆和直线的基本原理编程实现对圆和直线的扫描转换。三、【测试数据及其结果】Bresenham算法实验二四、【实验源代码】绘制直线:#includestdlib.h#includemath.h#includeGL/glut.h#includestdio.hGLsizeiwinWidth=500;GLsizeiwinHeight=500;voidlineBres(intx0,inty0,intxEnd,intyEnd){glColor3f(0.0,0.0,1.0);intdx=fabs(xEnd-x0),dy=fabs(yEnd-y0);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*(dy-dx);intx,y;if(x0xEnd){x=xEnd;y=yEnd;xEnd=x0;}else{Bresenham算法实验二x=x0;y=y0;}glPointSize(6);glBegin(GL_POINTS);glVertex2i(x,y);glEnd();while(xxEnd){x++;if(p0)p+=twoDy;else{y++;p+=twoDyMinusDx;}glPointSize(2);glBegin(GL_POINTS);glVertex2i(x,y);glEnd();}}voidinit(void){glClearColor(1.0,1.0,1.0,1.0);glShadeModel(GL_FLAT);}voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);lineBres(10,10,400,300);glFlush();}voidwinReshapeFcn(GLintnewWidth,GLintnewHeight){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));glClear(GL_COLOR_BUFFER_BIT);winWidth=newWidth;winHeight=newHeight;}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowPosition(10,10);glutInitWindowSize(winWidth,winHeight);glutCreateWindow(lineBres);init();Bresenham算法实验二glutDisplayFunc(display);glutReshapeFunc(winReshapeFcn);glutMainLoop();}绘制圆:#includegl/glut.hvoidinit(){glClearColor(0,0,0,0);}voidMidBresenhamCircle(intr){intx,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();}voiddisplay(){glClearColor(1,1,1,1);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1,0,0);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);glRotated(45,0,0,1);MidBresenhamCircle(8);glRotated(45,0,0,1);MidBresenhamCircle(8);glutSwapBuffers();}voidreshape(intw,inth){Bresenham算法实验二glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-10,10,-10,10);}intmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);glutInitWindowSize(400,400);glutInitWindowPosition(100,100);glutCreateWindow(扫描转换圆);glutDisplayFunc(display);glutReshapeFunc(reshape);glutMainLoop();return0;}反走样及五环的绘制实验三实验三反走样及五环的绘制一、【实验目的】1.了解走样和反走样的内容,熟练掌握用opengl实现图形的反走样。2.学会用反走样消除走样现象。3.学会五环的绘制方法。二、【实验内容】1.通过学习反走样相关课程,用opengl实现光栅图形的反走样。2.绘制五环。三、【测试数据及其结果】反走样及五环的绘制实验三四、【实验源代码】反走样:#includegl/glut.h#pragmacomment(linker,/subsystem:\windows\/entry:\mainCRTStartup\)GLuintlineList;//指定显示列表voidInitial(){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();}voidChangeSize(GLsizeiw,GLsizeih)反走样及五环的绘制实验三{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);elsegluOrtho2D(0.0,5.0*(GLfloat)w/(GLfloat)h,0.0,6.0);glMatrixMode(GL_MODELVIEW);//指定设置模型视图变换参数glLoadIdentity();}voidDisplayt(void){glClear(GL_COLOR_BUFFER_BIT);glCallList(lineList);//调用显示列表glFlush();}voidDisplayw(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();}voidmain(void){glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(300,300);glutCreateWindow(原始图形);glutDisplayFunc(Dis