/* polygon modeler */ #define MAX_POLYGONS 8 #define MAX_VERTICES 10 typedef int bool; #define TRUE 1 #define FALSE 0 #include #include #ifdef __APPLE__ #include #else #include #endif void myMouse(int,int, int, int); void myMotion(int, int); void myDisplay(); void myReshape(int, int); void color_menu(int); void main_menu(int); int pick_polygon(int x, int y); void myinit(); /* globals */ /* polygon struct */ typedef struct polygon { int color; /* color index */ bool used; /* TRUE if polygon exists */ int xmin, xmax, ymin, ymax; /* bounding box */ float xc, yc; /* center of polygon */ int nvertices; /* number of vertices */ int x[MAX_VERTICES]; /* vertices */ int y[MAX_VERTICES]; } polygon; /* flags */ bool picking = FALSE; /* true while picking */ bool moving = FALSE; /* true while moving polygon */ int in_polygon = -1; /* not in any polygon */ int present_color = 0; /* default color */ GLsizei wh = 500, ww = 500; /* initial window size */ int draw_mode = 0; /* drawing mode */ GLfloat colors[8][3]={{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0},{0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 1.0}, {1.0, 0.0, 1.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}}; polygon polygons[MAX_POLYGONS]; void myReshape(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0,0,w,h); ww = w; wh = h; } void myinit() { int i; /* set clear color to grey */ glClearColor(0.5, 0.5, 0.5, 1.0); /* mark all polygons unused */ for(i = 0; i=0) { if(polygons[in_polygon].nvertices == MAX_VERTICES) { printf("exceeds maximum number vertices\n"); exit(0); } i = polygons[in_polygon].nvertices; polygons[in_polygon].x[i] = x; polygons[in_polygon].y[i] = y; polygons[in_polygon].nvertices++; } } if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN &&picking&&!moving) { /* delete polygon */ picking = FALSE; moving = FALSE; j = pick_polygon(x,y); if(j >= 0) { polygons[j].used = FALSE; in_polygon = -1; glutPostRedisplay(); } } } int pick_polygon(int x, int y) { /* find first polygon in which we are in bounding box */ int i; for(i=0; i=polygons[i].xmin)&&(x<=polygons[i].xmax)&&(y>=polygons[i].ymin)&&(y0) polygons[j].xmax += dx; else polygons[j].xmin += dx; if(dy>0) polygons[j].ymax += dy; else polygons[j].ymin += dy; glutPostRedisplay(); } } void color_menu(int index) { present_color = index; if(in_polygon>=0) polygons[in_polygon].color = index; } void main_menu(int index) { int i; switch(index) { case(1): /* create a new polygon */ { moving = FALSE; for(i=0; i=0) { polygons[in_polygon].xmax = polygons[in_polygon].xmin = polygons[in_polygon].x[0]; polygons[in_polygon].ymax = polygons[in_polygon].ymin = polygons[in_polygon].y[0]; polygons[in_polygon].xc = polygons[in_polygon].x[0]; polygons[in_polygon].yc = polygons[in_polygon].y[0]; for(i=1;ipolygons[in_polygon].xmax) polygons[in_polygon].xmax = polygons[in_polygon].x[i]; if(polygons[in_polygon].y[i]polygons[in_polygon].ymax) polygons[in_polygon].ymax = polygons[in_polygon].y[i]; polygons[in_polygon].xc += polygons[in_polygon].x[i]; polygons[in_polygon].yc += polygons[in_polygon].y[i]; } polygons[in_polygon].xc = polygons[in_polygon].xc/polygons[in_polygon].nvertices; polygons[in_polygon].yc = polygons[in_polygon].yc/polygons[in_polygon].nvertices; } in_polygon = -1; glutPostRedisplay(); break; } case(3): /* set picking mode */ { picking = TRUE; moving = FALSE; break; } case(4): /* set moving mode */ { moving = TRUE; break; } case(5): /* exit */ { exit(0); break; } } } void myDisplay() { /* display all active polygons */ int i, j; glClear(GL_COLOR_BUFFER_BIT); for(i=0; i