#include #include /* In this lab, we will run an OpenGL program which renders the 3D Sierpinski Gasket #1 Run this code as is. What is rendered? Click the left mouse button. What happens? #2 Where is the viewing volume set? What is it? #3 Turn on the Hidden-Surface removal, or z-buffer. (Hint: Look for the comments, there are three things to change). Note that this is NOT double-buffering! */ typedef GLfloat point3[3]; point3 v[]={ {0.0,0.0,1.0} , {0.0, 0.942809, -0.333333} , {-0.816497, -0.471405, -0.3333333} , {0.816497, -0.471405, -0.333333} }; GLint n=1; void triangle( point3 a , point3 b , point3 c ) { glBegin(GL_POLYGON); glNormal3fv(a); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd(); } void divide_triangle( point3 a, point3 b , point3 c, GLint m) { point3 v1, v2, v3; int j; 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); } } void tetrahedron( GLint m ) { glColor3f(1,0,0); divide_triangle(v[0],v[1],v[2],m); glColor3f(0,1,0); divide_triangle(v[3],v[2],v[1],m); glColor3f(0,0,1); divide_triangle(v[0],v[3],v[1],m); glColor3f(0,0,0); divide_triangle(v[0],v[2],v[3],m); } void display () { // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); tetrahedron(n); glFlush(); } void mouse (int btn, int state , int x, int y) { if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { n++; display(); } if (btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) { exit(0); } } int main (int argc, char **argv) { glutInit(&argc,argv); // glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); glutInitWindowSize(500,500); glutCreateWindow("3D Gasket"); glutDisplayFunc(display); glClearColor(1,1,1,0); // glEnable(GL_DEPTH_TEST); glutMouseFunc(mouse); glutMainLoop(); return 0; }