#include #include /* In this lab, we will create an OpenGL program to look at the 2D Sierpinski Gasket #1 Note that the WINDOW pre-processor #define is 500. Set up your window using this parameter, so the window is 500 x 500, but with the origin, (0,0) in the center. #2 Determine coordinates for an isoceles triangle that will fit in your window. By isoceles we mean equal length edges. You should then enter these coordinates in the vertices array. It's 3 rows (for 3 points), and 2 columns (x and y coord). You should also pick one, initial point in your triangle, and enter the coordinates in the array p. #3 Complete the loop rendering 100 points inside the triangle, based on our in-class discussion (and textbook). Note that the vector version of glVertex2f is used. #4 Modify your code so that clicking on the left mouse button will render an additional 100 points, preserving the points already rendered. */ // make your intial window size a pre-processor definition #define WINDOW 500 GLint clear=1; GLfloat vertices[3][2]={ {-0.87*WINDOW/4 , -WINDOW/4} , { 0.87*WINDOW/4 , -WINDOW/4} , { 0 , WINDOW/4} }; GLfloat p[2] = { 0 , 0 }; void myInit() { glClearColor(1,1,1,0); glColor3f(1,0,0); glPointSize(2); // clipping window here glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-WINDOW/2,WINDOW/2,-WINDOW/2,WINDOW/2); glMatrixMode(GL_MODELVIEW); } void display () { if (clear==1) glClear(GL_COLOR_BUFFER_BIT); GLint j,k; glBegin(GL_POINTS); for (k=0; k<100; k++) { // first, pick a random vertex from 0, 1, or 2 j = rand()%3; // now, compute the next point p[0] = (p[0] + vertices[j][0])/2; p[1] = (p[1] + vertices[j][1])/2; // display the new point glVertex2fv(p); } glEnd(); glFlush(); } void mouse (int btn, int state , int x, int y) { if (btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) { clear=0; 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 ); glutInitWindowSize(WINDOW,WINDOW); glutCreateWindow("2D Gasket"); glutDisplayFunc(display); glClearColor(1.0,1.0,1.0,0.0); glutMouseFunc(mouse); myInit(); glutMainLoop(); return 0; }