/* view.c Laura Toma What it does: - Contains several functions to generate some test polygons to be triangulated. - Contains a function to draw a polygon. A polygon is stored using an array of vertices, in ccw order, in a global variable. The polygon is not necessarily convex. Intended use: Your code will use the test polygons and call the function that produces the triangulation with the test polygon as argument. The triangulation can be stored either as a set of triangles, or as a set of diagonals. You will probably not use the code that displays the test polygon, you will need to render the resulting triangulation instead. */ #include "geom.h" #include "rtimer.h" #include #include #include #include #ifdef __APPLE__ #include #else #include #endif GLfloat red[3] = {1.0, 0.0, 0.0}; GLfloat green[3] = {0.0, 1.0, 0.0}; GLfloat blue[3] = {0.0, 0.0, 1.0}; GLfloat black[3] = {0.0, 0.0, 0.0}; GLfloat white[3] = {1.0, 1.0, 1.0}; GLfloat gray[3] = {0.5, 0.5, 0.5}; GLfloat yellow[3] = {1.0, 1.0, 0.0}; GLfloat magenta[3] = {1.0, 0.0, 1.0}; GLfloat cyan[3] = {0.0, 1.0, 1.0}; GLint fillmode = 0; /* forward declarations of functions */ void display(void); void keypress(unsigned char key, int x, int y); void main_menu(int value); /* global variables */ const int WINDOWSIZE = 500; //the array of n points storing the test polygon. The variable n //stores its size. Good practice to initialize it to NULL, espe when //using asserts on pointers point2D* polygon = NULL; int n; //a convex n-gon of given size. The rendering function draw_polygon //assumes that the points are in teh range [0,WINSIZE] x [0, WINSIZE]. void generate_polygon_ngon() { printf("generating n-gon of size %d\n", n); int i; int rad = .7* WINDOWSIZE/2; //note rad can be at most WINDOWSIZE/2 double theta = 2 * M_PI / n; int center_x = WINDOWSIZE/2; int center_y = WINDOWSIZE/2; for (i=0; i\n"); exit(1); } n = atoi(argv[1]); printf("you entered n=%d\n", n); assert(n >0); //allocate global polygon array of n points polygon = (point2D*)malloc(n*sizeof(point2D)); assert(polygon); generate_polygon_ngon(); //generate_polygon_star(); //generate_polygon_star2(); print_points(polygon, n); Rtimer rt1; rt_start(rt1); //call the function that generates the triangulation rt_stop(rt1); //print the timing char buf [1024]; rt_sprint(buf,rt1); printf("Triangulating polygon: %s\n\n", buf); fflush(stdout); /* initialize GLUT */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOWSIZE, WINDOWSIZE); glutInitWindowPosition(100,100); glutCreateWindow(argv[0]); /* register callback functions */ glutDisplayFunc(display); glutKeyboardFunc(keypress); /* init GL */ /* set background color black*/ glClearColor(0, 0, 0, 0); /* here we can enable depth testing and double buffering and so on */ /* give control to event handler */ glutMainLoop(); return 0; } /* ****************************** */ /* draw the polygon stored in global variable polygon. The points are assumed to be in ccw around the boundary. Draw line segments from p[i] to p[i+1] */ void draw_polygon(){ // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //set color glColor3fv(yellow); //let's hope someone initialized this assert(polygon); int i; glBegin(GL_POLYGON); for (i=0; i