您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 交互式图形学-基于OPenGL自顶向下(第五版)课后实例程序全
怎么上传不了啊!!!!!A.1Sierpinski镂垫程序/*two-dimensionalSierpinskigasket*//*generatedusingrandomlyselectedvertices*//*andbisection*/#includeGL/glut.h/*youmayhavetochangetheincludetoglut.horelsewheredependingonwhereitisstoredonyoursystem*//*glut.husuallyhasincludedforgl.handglu.h*/voidmyinit(void){/*attributes*/glClearColor(1.0,1.0,1.0,1.0);/*whitebackground*/glColor3f(1.0,0.0,0.0);/*drawinred*//*setupviewing*//*50.0×50.0cameracoordinatewindowwithoriginlowerleft*/glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,50.0,0.0,50.0);glMatrixMode(GL_MODELVIEW);}voiddisplay(void){/*Atriangle*/GLfloatvertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};inti,j,k;intrand();/*standardrandomnumbergenerator*/GLfloatp[2]={7.5,5.0};/*anarbitraryinitialpointinsidetraingle*/glClear(GL_COLOR_BUFFER_BIT);/*clearthewindow*/交互式计算机图形学——基于OpenGL的自顶向下方法(第4版)586glBegin(GL_POINTS);/*computeandplots5000newpoints*/for(k=0;k5000;k++){j=rand()%3;/*pickavertexatrandom*//*Computepointhalfwaybetweenselectedvertexandoldpoint*/p[0]=(p[0]+vertices[j][0])/2.0;p[1]=(p[1]+vertices[j][1])/2.0;/*plotnewpoint*/glVertex2fv(p);}glEnd();glFlush();/*clearbuffers*/}voidmain(intargc,char**argv){/*StandardGLUTinitialization*/glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);/*default,notneeded*/glutInitWindowSize(500,500);/*500×500pixelwindow*/glutInitWindowPosition(0,0);/*placewindowtopleftondisplay*/glutCreateWindow(SierpinskiGasket);/*windowtitle*/glutDisplayFunc(display);/*displaycallbackinvokedwhenwindowopened*/myinit();/*setattributes*/glutMainLoop();/*entereventloop*/}A.2生成Sierpinski镂垫的递归程序/*RecursivesubdivisionoftriangletoformSierpinskigasket*//*numberofrecursivestepsgivenoncommandline*/#includeGL/glut.h#includestdlib.h/*initialtriangle*/GLfloatv[3][2]={{-1.0,-0.58},{1.0,-0.58},{0.0,1.15}};附录A实例程序587intn;voidtriangle(GLfloat*a,GLfloat*b,GLfloat*c)/*displayonetriangle*/{glVertex2fv(a);glVertex2fv(b);glVertex2fv(c);}voiddivide_triangle(GLfloat*a,GLfloat*b,GLfloat*c,intm){/*trianglesubdivisionusingvertexnumbers*/GLfloatv0[2],v1[2],v2[2];intj;if(m0){for(j=0;j2;j++)v0[j]=(a[j]+b[j])/2;for(j=0;j2;j++)v1[j]=(a[j]+c[j])/2;for(j=0;j2;j++)v2[j]=(b[j]+c[j])/2;divide_triangle(a,v0,v1,m-1);divide_triangle(c,v1,v2,m-1);divide_triangle(b,v2,v0,m-1);}elsetriangle(a,b,c);/*drawtriangleatendofrecursion*/}voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);divide_triangle(v[0],v[1],v[2],n);glEnd();glFlush();}voidmyinit(){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-2.0,2.0,-2.0,2.0);glMatrixMode(GL_MODELVIEW);glClearColor(1.0,1.0,1.0,1.0);glColor3f(0.0,0.0,0.0);}voidmain(intargc,char**argv){n=atoi(argv[1]);/*orsetnumberofsubdivisionstepshere*/交互式计算机图形学——基于OpenGL的自顶向下方法(第4版)588glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutCreateWindow(SierpinskiGasket);glutDisplayFunc(display);myinit();glutMainLoop();}A.3三维Sierpinski镂垫的递归程序/*Recursivesubdivisionofatetrahedrontoform3DSierpinskigasket*//*numberofrecursivestepsgivenoncommandline*/#includestdlib.h#includeGL/glut.h/*initialtetrahedron*/GLfloatv[4][3]={{0.0,0.0,1.0},{0.0,0.942809,-0.33333},{-0.816497,-0.471405,-0.333333},{0.816497,-0.471405,-0.333333}};GLfloatcolors[4][3]={{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{0.0,0.0,0.0}};intn;voidtriangle(GLfloat*va,GLfloat*vb,GLfloat*vc){glVertex3fv(va);glVertex3fv(vb);glVertex3fv(vc);}voidtetra(GLfloat*a,GLfloat*b,GLfloat*c,GLfloat*d){glColor3fv(colors[0]);triangle(a,b,c);glColor3fv(colors[1]);triangle(a,c,d);glColor3fv(colors[2]);triangle(a,d,b);glColor3fv(colors[3]);triangle(b,d,c);}voiddivide_tetra(GLfloat*a,GLfloat*b,GLfloat*c,GLfloat*d,intm){GLfloatmid[6][3];intj;附录A实例程序589if(m0){/*computesixmidpoints*/for(j=0;j3;j++)mid[0][j]=(a[j]+b[j])/2;for(j=0;j3;j++)mid[1][j]=(a[j]+c[j])/2;for(j=0;j3;j++)mid[2][j]=(a[j]+d[j])/2;for(j=0;j3;j++)mid[3][j]=(b[j]+c[j])/2;for(j=0;j3;j++)mid[4][j]=(c[j]+d[j])/2;for(j=0;j3;j++)mid[5][j]=(b[j]+d[j])/2;/*create4tetrahedronsbysubdivision*/divide_tetra(a,mid[0],mid[1],mid[2],m-1);divide_tetra(mid[0],b,mid[3],mid[5],m-1);divide_tetra(mid[1],mid[3],c,mid[4],m-1);divide_tetra(mid[2],mid[4],d,mid[5],m-1);}elsetetra(a,b,c,d);/*drawtetrahedronatendofrecursion*/}voiddisplay(void){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glBegin(GL_TRIANGLES);divide_tetra(v[0],v[1],v[2],v[3],n);glEnd();glFlush();}voidmyReshape(intw,inth){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w=h)glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,2.0*(GLfloat)h/(GLfloat)w,-10.0,10.0);elseglOrtho(-2.0*(GLfloat)w/(GLfloat)h,2.0*(GLfloat)w/(GLfloat)h,-2.0,2.0,-10.0,10.0);glMatrixMode(GL_MODELVIEW);glutPostRedisplay();}voidmain(intargc,char**argv){n=atoi(argv[1]);/*orenternumberofsubdivisionstepshere*/交互式计算机图形学——基于OpenGL的自顶向下方法(第4版)590glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(500,500);glutCreateWindow(3DGasket);glutReshapeFunc(myReshape);glutDisplayFunc(display);glEnable(GL_DEPTH_TEST);glClearColor(1.0,1
本文标题:交互式图形学-基于OPenGL自顶向下(第五版)课后实例程序全
链接地址:https://www.777doc.com/doc-2750333 .html