您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 计算机图形学实验C++代码
1一、bresenham算法画直线#includeglut.h#includemath.h#includestdio.hvoiddraw_pixel(intix,intiy){glBegin(GL_POINTS);glVertex2i(ix,iy);glEnd();}voidBresenham(intx1,inty1,intxEnd,intyEnd){intdx=abs(xEnd-x1),dy=abs(yEnd-y1);intp=2*dy-dx;inttwoDy=2*dy,twoDyMinusDx=2*dy-2*dx;intx,y;if(x1xEnd){x=xEnd;y=yEnd;xEnd=x1;}else{x=x1;y=y1;}draw_pixel(x,y);while(xxEnd){x++;if(p0)p+=twoDy;else{y++;p+=twoDyMinusDx;draw_pixel(x,y);}}}voiddisplay(){glClear(GL_COLOR_BUFFER_BIT);Bresenham(0,0,400,400);glFlush();2}voidmyinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,500.0,0.0,500.0);}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow(CG_test_Bresenham_Lineexample);glutDisplayFunc(display);myinit();glutMainLoop();}二、中点法绘制椭圆#includeglut.h#includemath.h#includestdio.hinlineintround(constfloata){returnint(a+0.5);}voidsetPixel(GLintxCoord,GLintyCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}voidellipseMidpoint(intxCenter,intyCenter,intRx,intRy){intRx2=Rx*Rx;intRy2=Ry*Ry;inttwoRx2=2*Rx2;inttwoRy2=2*Ry2;intp;intx=0;inty=Ry;intpx=0;intpy=twoRx2*y;voidellipsePlotPoints(int,int,int,int);3ellipsePlotPoints(xCenter,yCenter,x,y);p=round(Ry2-(Rx2*Ry)+(0.25*Rx2));while(pxpy){x++;px+=twoRy2;if(p0)p+=Ry2+px;else{y--;py-=twoRx2;p+=Ry2+px-py;}ellipsePlotPoints(xCenter,yCenter,x,y);}p=round(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);while(y0){y--;py-=twoRx2;if(p0)p+=Rx2-py;else{x++;px+=twoRy2;p+=Rx2-py+px;}ellipsePlotPoints(xCenter,yCenter,x,y);}}voidellipsePlotPoints(intxCenter,intyCenter,intx,inty){setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);setPixel(xCenter+x,yCenter-y);setPixel(xCenter-x,yCenter-y);}voiddisplay(){glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(200,200,50,30);glFlush();}voidmyinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);4glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,300.0,0.0,300.0);}voidmain(intargc,char**argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(300,300);glutInitWindowPosition(200.0,200.0);glutCreateWindow(circleMIdexample);glutDisplayFunc(display);myinit();glutMainLoop();}三、抛物线#includeglut.h#includemath.h#includestdio.hinlineintround(constfloata){returnint(a+0.5);}voidsetPixel(GLintxCoord,GLintyCoord){glBegin(GL_POINTS);glVertex2i(xCoord,yCoord);glEnd();}voidellipseMidpoint(intxCenter,intyCenter,inta,intb){intp;intx=xCenter;inty=yCenter;intpx=0,py=0;voidellipsePlotPoints(int,int,int,int);ellipsePlotPoints(xCenter,yCenter,px,py);p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;do{if(p0){x=x+1;y=y;p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;}else{x=x+1;y=y-1;5p=yCenter+a*(x+1-xCenter)*(x+1-xCenter)+b*(x+1-xCenter)-y-0.5;}px=x-xCenter;py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);}while(pxpy);for(;;){if(p0){x=x-1;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;}else{x=x;y=y+1;p=yCenter+a*(x+0.5-xCenter)*(x+0.5-xCenter)+b*(x+0.5-xCenter)-y-1;}px=x-xCenter;py=y-yCenter;ellipsePlotPoints(xCenter,yCenter,px,py);};}voidellipsePlotPoints(intxCenter,intyCenter,intx,inty){setPixel(xCenter+x,yCenter+y);setPixel(xCenter-x,yCenter+y);}voiddisplay(){glClear(GL_COLOR_BUFFER_BIT);ellipseMidpoint(150,150,1,0);glFlush();}voidmyinit(){glClearColor(0.8,1.0,1.0,1.0);glColor3f(0.0,0.0,1.0);glPointSize(1.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,300.0,0.0,300.0);}voidmain(intargc,char**argv){6glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutInitWindowPosition(200.0,200.0);glutCreateWindow(circleMIdexample);glutDisplayFunc(display);myinit();glutMainLoop();}四、基本图元输出#includeglut.h#includemath.h#includestdio.hvoidPolygon(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6){glBegin(GL_POLYGON);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glVertex2iv(p6);glEnd();}voidTriangles(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6){glBegin(GL_TRIANGLES);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p6);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glEnd();}voidTrianglefan(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6){glBegin(GL_TRIANGLE_FAN);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p3);glVertex2iv(p4);glVertex2iv(p5);glVertex2iv(p6);glEnd();7}voidTrianglestrip(int*p1,int*p2,int*p3,int*p4,int*p5,int*p6){glBegin(GL_TRIANGLE_STRIP);glVertex2iv(p1);glVertex2iv(p2);glVertex2iv(p6);glVertex2iv(p3);glVertex2iv(p5);glVertex2iv(p4);glEnd();}voidglRect_s(GLinta,GLintb,GLintc,GLintd){glRecti(a,b,c,d);}voiddisplay(){intp1[]={60,170};intp2[]={100,100};intp3[]={180,100};intp4[]={220,170};intp5[]={180,240};intp6[]={100,240};intp7[]={60,100};glClear(GL_COLOR_BUFFER_BIT);//Triangles(p1,p2,p3,p4,p5,p6);//Polygon(p1,p2,p3,p4,p5,p6);//glRect_s(160,30,10,100);Trianglestrip(p1,p2,p3,p4,
本文标题:计算机图形学实验C++代码
链接地址:https://www.777doc.com/doc-4307142 .html