您好,欢迎访问三七文档
《计算机图形学》实验报告学院:理学院专业:信息与计算科学班级:姓名学号指导教师实验时间4.实验地点计算机实验室成绩实验项目名称裁剪实验环境VC++6.0实验内容(1)理解直线裁剪的原理(Cohen-Surtherland算法、梁友栋算法)(2)利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。(3)调试、编译、修改程序。实验原理编码裁剪算法的主要思想是:对于每条线段,分为三种情况处理。(1)若线段完全在窗口之内,则显示该线段,称为“取”;(2)若线段明显在窗口之外,则丢弃该线段,称为“弃”;(3)若线段既不满足“取”的条件,也不满足“舍”的条件,则把线段分割为两段。其中一段完全在窗口之外,可弃之;对另一段则重复上述处理实验过程#includeGL/glut.h#includestdio.h#includestdlib.h#defineLEFT_EDGE1#defineRIGHT_EDGE2#defineBOTTOM_EDGE4#defineTOP_EDGE8voidLineGL(intx0,inty0,intx1,inty1){glBegin(GL_LINES);glColor3f(1.0f,0.0f,0.0f);glVertex2f(x0,y0);glColor3f(0.0f,1.0f,0.0f);glVertex2f(x1,y1);glEnd();}structRectangle{floatxmin,xmax,ymin,ymax;};Rectanglerect;intx0,y0,x1,y1;intCompCode(intx,inty,Rectanglerect){intcode=0x00;if(yrect.ymin)code=code|4;if(yrect.ymax)code=code|8;if(xrect.xmax)code=code|2;if(xrect.xmin)code=code|1;returncode;}intcohensutherlandlineclip(Rectanglerect,int&x0,int&y0,int&x1,int&y1){intaccept,done;floatx,y;accept=0;done=0;intcode0,code1,codeout;code0=CompCode(x0,y0,rect);code1=CompCode(x1,y1,rect);do{if(!(code0|code1)){accept=1;done=1;}elseif(code0&code1)done=1;else{if(code0!=0)codeout=code0;elsecodeout=code1;if(codeout&LEFT_EDGE){y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0);x=(float)rect.xmin;}elseif(codeout&RIGHT_EDGE){y=y0+(y1-y0)*(rect.xmax)/(x1-x0);x=(float)rect.xmax;}elseif(codeout&BOTTOM_EDGE){x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0);y=(float)rect.ymin;}elseif(codeout&TOP_EDGE){x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0);y=(float)rect.ymax;}if(codeout==code0){x0=x;y0=y;code0=CompCode(x0,y0,rect);}else{x1=x;y1=y;code1=CompCode(x1,y1,rect);}}}while(!done);if(accept)LineGL(x0,y0,x1,y1);returnaccept;}voidmyDisplay(){glClear(GL_COLOR_BUFFER_BIT);glColor3f(1.0f,0.0f,0.0f);glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax);LineGL(x0,y0,x1,y1);glFlush();}voidInit(){glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);rect.xmin=100;rect.xmax=300;rect.ymin=100;rect.ymax=300;x0=450,y0=0,x1=0,y1=450;printf(Presskey'c'toClip!\nPresskey'r'toRrstore!\n);}voidReshape(intw,inth){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);}voidkeyboard(unsignedcharkey,intx,inty){switch(key){case'c':cohensutherlandlineclip(rect,x0,y0,x1,y1);glutPostRedisplay();break;case'r':Init();glutPostRedisplay();break;case'x':exit(0);break;default:break;}}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);glutInitWindowPosition(100,100);glutInitWindowSize(640,480);glutCreateWindow(helloworld);Init();glutDisplayFunc(myDisplay);glutReshapeFunc(Reshape);glutMainLoop();}实验结果实验总结通过本次试验,理解直线裁剪的原理,利用VC+OpenGL实现直线的编码裁剪算法,在屏幕上用一个封闭矩形裁剪任意一条直线。
本文标题:计算机图形学裁剪
链接地址:https://www.777doc.com/doc-2042422 .html