Programming with OpenGL Part 3: Three dimensions Yuanfeng Zhou Shandong University
1 Programming with OpenGL Part 3: Three Dimensions Yuanfeng Zhou Shandong University
Review Keywords 1.Development 2. State machine 3. Functions(formats), callback function 4. Simple cube program 5.Simple viewing 6. OpenGL primitives(polygon) 7. Attributes(color)
2 Review •Keywords: 1.Development 2.State machine 3.Functions (formats), callback function 4.Simple cube program 5.Simple viewing 6.OpenGL primitives (polygon) 7.Attributes (color)
Objectives Develop a more sophisticated three dimensional example Sierpinski gasket: a fractal Introduce hidden -surface removal Plotting implicit functions
3 Objectives •Develop a more sophisticated threedimensional example - Sierpinski gasket: a fractal •Introduce hidden-surface removal •Plotting implicit functions
Random Sierpinski Gasket
Random Sierpinski Gasket 4
main code void main(int argc, char*x argv) / Standard glut initialization glutlnit (&argc, argv) glutlnitDisplayMode(GLUT SINGLE GLUT RGB) glutlnitWindowSize (500, 500); /*500 X 500 pixel window * glutInitWindowPosition(0,0); /*place window top left on display * glut CreateWindow ("Sierpinski Gasket); /*window title */ glutDisplay Func(display) / display callback invoked when window opened * myinit(; /*set attributes*/ glutMainLoopo; /*enter event loop *
main code void main(int argc, char** argv) { /* Standard GLUT initialization */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); /* 500 × 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow("Sierpinski Gasket"); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */ } 5
init code void myinit(void / attributes*/ glClear Color (1.0, 1.0, 1.0, 1.0); / * white background * glColor3f(1.0, 0.0,0.0); / draw in red*/ / set up viewing /*500x 50.0 camera coordinate window with origin lower left * gIMatrixMode(GL PROjECtIon) gILoadldentityo gluortho2D(0.0,50.0,0.0,50.0) gIMatrixMode(GL MODELVIEw
init code void myinit(void) { /* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing */ /* 50.0 × 50.0 camera coordinate window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 50.0, 0.0, 50.0); glMatrixMode(GL_MODELVIEW); } 6
display code void display (void) / A triangle * GLfloat vertices]2]={0.0.00}{25.050.0}{50.0.00 int i, j, k; GLfloat p[2]=0,0]; / an arbitrary initial point inside traingle * glClear(GL COLOR BUFFER BID); / clear the window * glBegin(GL POINTS): / compute and plots 5000 new points * for(k=0;k<10000k++) j=rando%3: / pick a vertex at random * Compute point halfway between selected vertex and old point * p[O]=(p[0]+vertices[OJ/2.0 p[1]=(p[1]+vertices[[1)/2.0 I plot new point * glVertex2fv(p) glEndo glFlusho; clear buffers*/
display code void display(void) { /* A triangle */ GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; int i, j, k; GLfloat p[2] ={0,0}; /* an arbitrary initial point inside traingle */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ glBegin(GL_POINTS); /* compute and plots 5000 new points */ for( k=0; k<10000; k++) { j=rand()%3; /* pick a vertex at random */ /* Compute point halfway between selected vertex and old point */ p[0] = (p[0]+vertices[j][0])/2.0; p[1] = (p[1]+vertices[j][1])/2.0; /* plot new point */ glVertex2fv(p); } glEnd(); glFlush(); /* clear buffers */ } 7
Results a Sierpinski Gasket 回X AAAA AAAA AAAA
Results 8
Three-dimensional Applications In OpenGL, two-dimensional applications are a special case of three-dimensional graphics Going to 3D Not much changes Use glvertex3*( Have to worry about the order in which polygons are drawn or use hidden-surface removal Polygons should be simple, convex, flat
9 Three-dimensional Applications •In OpenGL, two-dimensional applications are a special case of three-dimensional graphics •Going to 3D - Not much changes - Use glVertex3*( ) - Have to worry about the order in which polygons are drawn or use hidden-surface removal - Polygons should be simple, convex, flat
The gasket as a fractal Consider the filled area(black and the perimeter (the length of all the lines around the filled triangles) As we continue subdividing the area goes to zero but the perimeter goes to infinity This is not an ordinary geometric object It is neither two- nor three-dimensional It is a fractal(fractional dimension) object
10 The gasket as a fractal •Consider the filled area (black) and the perimeter (the length of all the lines around the filled triangles) •As we continue subdividing - the area goes to zero - but the perimeter goes to infinity •This is not an ordinary geometric object - It is neither two- nor three-dimensional •It is a fractal (fractional dimension) object