/* Sierpinski Gasket GL program */ /* The Sierpinski gasket is defined recursively as follows: Start with three vertices in the place, defining a triangle. Pick a random point inside the triangle. Select any of the vertices of the triangle. Plot the point halfway between the random point and the randomly chosen vertex. Continue with this point as the new starting point. */ /* This program illustrates simple use of mouse with OpenGL to start and stop program execution */ #include #include /* define a point data type */ typedef struct { float x,y,z;} point; point vertices[4]={{0,0,0},{0,500,500},{500,-500,500},{-500,-500,500}}; /* A tetrahedron */ int j; point new={250,100,250}; void clear(void) { glClear(GL_COLOR_BUFFER_BIT); } void display(void) /* computes and plots a single new point */ { int rand(); double d0,d1,d2,d3; j=rand()%4; /* pick a vertex at random */ /* Compute point halfway between vertex and old point */ new.x = (new.x+vertices[j].x)/2; new.y = (new.y+vertices[j].y)/2; new.z = (new.z+vertices[j].z)/2; /* plot point */ glBegin(GL_POINTS); glColor3f(new.x/500.0,new.y/500.0,new.z/500.0); glVertex3f(new.x, new.y,new.z); glEnd(); /* replace old point by new */ glFlush(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON&state==GLUT_DOWN) glutIdleFunc(display); if(btn==GLUT_MIDDLE_BUTTON&state==GLUT_DOWN) glutIdleFunc(NULL); if(btn==GLUT_RIGHT_BUTTON&state==GLUT_DOWN) exit(0); } int main(int argc, char** argv) { double theta=0,radius=5000; glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("Sierpinski Gasket"); glutIdleFunc (display); glutMouseFunc (mouse); glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */ glColor3f(1.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-500.0, 500.0, -500.0, 500.0, -500.0, 500.0); glMatrixMode(GL_MODELVIEW); // glutDisplayFunc(clear); glutDisplayFunc(display); glutMainLoop(); }