/* Sierpinski Pentagon GL program */ /* This program illustrates simple use of mouse with OpenGL to start and stop program execution */ #include /* define a point data type */ typedef struct { float x,y;} point; point vertices[5]={{50,0},{0,250},{250,500},{500,250},{450,0}}; /* A pentagon */ int j; point new, old={75,50}; /* A random point */ void clear(void) { glClear(GL_COLOR_BUFFER_BIT); } void display(void) /* computes and plots a single new point */ { long random(); int i; j=random()%5; /* pick a vertex at random */ /* Compute point halfway between vertex and old point */ new.x = (old.x+vertices[j].x)/2; new.y = (old.y+vertices[j].y)/2; /* plot point */ glBegin(GL_POINTS); glVertex2f(new.x, new.y); glEnd(); /* replace old point by new */ old.x=new.x; old.y=new.y; 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(); } int main(int argc, char** argv) { 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(0.0, 500.0, 0.0, 500.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glutDisplayFunc(clear); glutMainLoop(); }