【文档说明】计算机图形学computer-graphics课件6.ppt,共(47)页,2.585 MB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-77495.html
以下为本文档部分文字说明:
1ProgrammingwithOpenGLPart3:ThreeDimensionsYuanfengZhouShandongUniversity2Review•Keywords:1.Development2.Statemachine3.Functi
ons(formats),callbackfunction4.Simplecubeprogram5.Simpleviewing6.OpenGLprimitives(polygon)7.Attributes(co
lor)3Objectives•Developamoresophisticatedthree-dimensionalexample-Sierpinskigasket:afractal•Introducehidden-surfaceremoval•Plottingimplicit
functionsRandomSierpinskiGasket4maincodevoidmain(intargc,char**argv){/*StandardGLUTinitialization*/glutInit(&argc,argv);glutInitDisplayM
ode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);/*500×500pixelwindow*/glutInitWindowPosition(0,0);/*pla
cewindowtopleftondisplay*/glutCreateWindow("SierpinskiGasket");/*windowtitle*/glutDisplayFunc(display);/*displaycallbackinvokedwhenwindowop
ened*/myinit();/*setattributes*/glutMainLoop();/*entereventloop*/}5initcodevoidmyinit(void){/*attribu
tes*/glClearColor(1.0,1.0,1.0,1.0);/*whitebackground*/glColor3f(1.0,0.0,0.0);/*drawinred*//*setupviewing*//*50.0×50.0cameracoordinatewin
dowwithoriginlowerleft*/glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0.0,50.0,0.0,50.0);glMatrixMode(GL_MODELVIEW);}6displaycode
voiddisplay(void){/*Atriangle*/GLfloatvertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}};inti,j,k;GLfloatp[2]={0,0};/*anarbitr
aryinitialpointinsidetraingle*/glClear(GL_COLOR_BUFFER_BIT);/*clearthewindow*/glBegin(GL_POINTS);/*computeandplots5000newpoints*/for(k=0;
k<10000;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*/}7Results89Three-dimensionalApplic
ations•InOpenGL,two-dimensionalapplicationsareaspecialcaseofthree-dimensionalgraphics•Goingto3D-Notmuchchanges-Use
glVertex3*()-Havetoworryabouttheorderinwhichpolygonsaredrawnorusehidden-surfaceremoval-Polygonsshouldbesimple,c
onvex,flat10Thegasketasafractal•Considerthefilledarea(black)andtheperimeter(thelengthofallthelinesaroundthefilledtrian
gles)•Aswecontinuesubdividing-theareagoestozero-buttheperimetergoestoinfinity•Thisisnotanordinarygeometri
cobject-Itisneithertwo-northree-dimensional•Itisafractal(fractionaldimension)objectFractalGeometry(From1
975)•Thegasketisself-similar.Thatis,itismadeupofsmallercopiesofitself.11FractalGeometry1213SierpinskiGask
et(2D)•Startwithatriangle•Connectbisectorsofsidesandremovecentraltriangle•Repeat14Example•FivesubdivisionsFifteen15GasketProgram#inclu
de<GL/glut.h>/*initialtriangle*/GLfloatv[3][2]={{-1.0,-0.58},{1.0,-0.58},{0.0,1.15}};intn;/*numberofrecursivesteps*/16Drawonetrianglev
oidtriangle(GLfloat*a,GLfloat*b,GLfloat*c)/*displayonetriangle*/{glVertex2fv(a);glVertex2fv(b);glVertex2fv(c);}17TriangleSubdivisio
nvoiddivide_triangle(GLfloat*a,GLfloat*b,GLfloat*c,intm){/*trianglesubdivisionusingvertexnumbers*/GLfloatv0[2],v1[2],v
2[2];intj;if(m>0){for(j=0;j<2;j++)v0[j]=(a[j]+b[j])/2;for(j=0;j<2;j++)v1[j]=(a[j]+c[j])/2;for(j=0;j<2;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);/*dra
wtriangleatendofrecursion*/}18displayandinitFunctionsvoiddisplay(){glClear(GL_COLOR_BUFFER_BIT);glBegin(GL_TRIANGLES);divide_tr
iangle(v[0],v[1],v[2],n);glEnd();glFlush();}voidmyinit(){glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-2.0,2.0,-2.0,2.0);glMatrixMod
e(GL_MODELVIEW);glClearColor(1.0,1.0,1.0,1.0)glColor3f(0.0,0.0,0.0);}19mainFunctionintmain(intargc,char**argv){cout<<"请输入迭代次数";cin>>n;glutInit
(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(500,500);glutCreateWindow("SierpinskiGas
ket");glutDisplayFunc(display);myinit();glutMainLoop();}20EfficiencyNoteByhavingtheglBeginandglEndinthedisplaycallbackrat
herthaninthefunctiontriangleandusingGL_TRIANGLESratherthanGL_POLYGONinglBegin,wecallglBeginandglEndonlyoncefortheentiregasketratherthanonc
eforeachtriangle21Movingto3D•Wecaneasilymaketheprogramthree-dimensionalbyusingGLfloatv[3][3]glVertex3fglOrtho•Butthatwouldnotbeveryinteresting•I
nstead,wecanstartwithatetrahedron223DGasket•Wecansubdivideeachofthefourfaces•Appearsasifweremoveasolidtet
rahedronfromthecenterleavingfoursmallertetrahedra23Exampleafter5iterations24trianglecodevoidtriangle(GLfloat*a,GLfloat*b,GLfloat*c){glVertex3fv(a);
glVertex3fv(b);glVertex3fv(c);}25subdivisioncodevoiddivide_triangle(GLfloat*a,GLfloat*b,GLfloat*c,intm){G
Lfloatv1[3],v2[3],v3[3];intj;if(m>0){for(j=0;j<3;j++)v1[j]=(a[j]+b[j])/2;for(j=0;j<3;j++)v2[j]=(a[j]+c[j])/2;for(j=
0;j<3;j++)v3[j]=(b[j]+c[j])/2;divide_triangle(a,v1,v2,m-1);divide_triangle(c,v2,v3,m-1);divide_triangle(b,v3,v1,m
-1);}else(triangle(a,b,c));}26tetrahedroncodevoidtetrahedron(intm){glColor3f(1.0,0.0,0.0);divide_triangle(v[0],v[1],v[2],m);glColor3f(0.0,1.0,
0.0);divide_triangle(v[3],v[2],v[1],m);glColor3f(0.0,0.0,1.0);divide_triangle(v[0],v[3],v[1],m);glColor3f(0.0,0.0,0.0);divide_triangle(v[0],v[2],v[
3],m);}ReshapecodevoidmyReshape(intw,inth){glViewport(0,0,w,h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if(w<=h)glOrtho(-2.0,2.0,-2.0*(GL
float)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();}2728AlmostCorrect•Becausethetrianglesaredrawnintheorderthe
yaredefinedintheprogram,thefronttrianglesarenotalwaysrenderedinfrontoftrianglesbehindthemgetthiswantthis29Hidden-SurfaceRemova
l•Wewanttoseeonlythosesurfacesinfrontofothersurfaces•OpenGLusesahidden-surfacemethodcalledthez-bufferalgorithmthats
avesdepthinformationasobjectsarerenderedsothatonlythefrontobjectsappearintheimage30Usingthez-bufferalgorith
m•Thealgorithmusesanextrabuffer,thez-buffer,tostoredepthinformationasgeometrytravelsdownthepipeline•It
mustbe-Requestedinmain.c•glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH)-Enabledininit.c•glEnable(GL_DEPTH_TEST)-Clearedinthedisplaycallback
•glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)maincodevoidmain(intargc,char**argv){cout<<"请输入迭代次数";cin>>n;glutInit(&argc,arg
v);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);glutInitWindowSize(500,500);glutCreateWindow("3DGasket");glutReshapeFunc(myReshape
);glutDisplayFunc(display);glEnable(GL_DEPTH_TEST);glClearColor(1.0,1.0,1.0,1.0);glutMainLoop();}3132SurfacevsVolumeSubdvision
•Inourexample,wedividedthesurfaceofeachface•Wecouldalsodividethevolumeusingthesamemidpoints•Themidpointsdefinefoursmallertetrahedrons,oneforeach
vertex•Keepingonlythesetetrahedronsremovesavolumeinthemiddle•Seetextforcode33subdivisioncodevoiddivide_tetr
a(GLfloat*a,GLfloat*b,GLfloat*c,GLfloat*d,intm){GLfloatmid[6][3];intj;if(m>0){/*computesixmidpoints*/for(j=0;j<3;j++)m
id[0][j]=(a[j]+b[j])/2;for(j=0;j<3;j++)mid[1][j]=(a[j]+c[j])/2;for(j=0;j<3;j++)mid[2][j]=(a[j]+d[j])/2;for(j=0;j<3;j++)mid[3
][j]=(b[j]+c[j])/2;for(j=0;j<3;j++)mid[4][j]=(c[j]+d[j])/2;for(j=0;j<3;j++)mid[5][j]=(b[j]+d[j])/2;/*create
4tetrahedronsbysubdivision*/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);/*drawtetrahedronatendofrecursio
n*/}34VolumeSubdivisionPlottingimplicitfunctions•2D:f(x,y)=0;•3D:f(x,y,z)=0;•Spherex^2+y^2+z^2-1=0;•Advantages:•Smooth•Caneasilytodeci
deonepointisinimplicitsurfaceornot•Topologychangefreely•Disadvantage•Hardtorender,raycastingisslow35P
lottingimplicitfunctions•Solvingmethod:•Polygonization(Marchingcubes)362D:MarchingSquares372D:Marching
Squares382D:MarchingSquares392D:MarchingSquares•x=xi+(a-c)△x/(a-b)402D:MarchingSquares•f(x,y)=(x^2+y^2+a^2)^2-4a^2x^2-b^4•a=0.49,b=0.541S
amplingonimplicitsurface42f=x^4-10*r^2*x^2+y^4-10*r^2*y^2+z^4-10*r^2*z^2r=0.13Samplingonimplicitsurface43Samplin
gonimplicitsurface44Samplingonimplicitsurface45Shellspacetriangulation46Marchingcubes47