/* mouse2.c Laura Toma What it does: The user can enter a polygon by clicking on the mouse. */ #include "geom.h" #include #include #include #include #ifdef __APPLE__ #include #else #include #endif #include using namespace std; 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 mousepress(int button, int state, int x, int y); void timerfunc(); void initialize_polygon(); void print_polygon(vector poly); /* our coordinate system is (0,0) x (WINDOWSIZE,WINDOWSIZE) where the origin is the lower left corner */ /* global variables */ const int WINDOWSIZE = 750; //the current polygon vector poly; //coordinates of last mouse click double mouse_x=-10, mouse_y=-10; //initialized to a point outside the window //when this is 1, then clicking the mouse results in those points being stored in poly int poly_init_mode = 0; void draw_circle(double x, double y){ glColor3fv(blue); int precision = 100; double r = 4; double theta = 0; glBegin(GL_POLYGON); for(int i = 0; i < precision; i++){ theta = i * 2 * M_PI/precision; glVertex2f(x + r*cos(theta), y + r*sin(theta)); } glEnd(); } /* Usage void glutMouseFunc(void (*func)(int button, int state, int x, int y)); Description glutMouseFunc sets the mouse callback for the current window. When a user presses and releases mouse buttons in the window, each press and each release generates a mouse callback. The button parameter is one of GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. For systems with only two mouse buttons, it may not be possible to generate GLUT_MIDDLE_BUTTON callback. For systems with a single mouse button, it may be possible to generate only a GLUT_LEFT_BUTTON callback. The state parameter is either GLUT_UP or GLUT_DOWN indicating whether the callback was due to a release or press respectively. The x and y callback parameters indicate the window relative coordinates when the mouse button state changed. If a GLUT_DOWN callback for a specific button is triggered, the program can assume a GLUT_UP callback for the same button will be generated (assuming the window still has a mouse callback registered) when the mouse button is released even if the mouse has moved outside the window. */ void mousepress(int button, int state, int x, int y) { if(state == GLUT_DOWN) { mouse_x = x; mouse_y = y; //(x,y) are in wndow coordinates, where the origin is in the upper //left corner; our reference system has the origin in lower left //corner, this means we have to reflect y mouse_y = WINDOWSIZE - mouse_y; printf("mouse click at (x=%d, y=%d)\n", (int)mouse_x, (int)mouse_y); if (poly_init_mode ==1) { point2D p = {mouse_x, mouse_y}; poly.push_back(p); } } glutPostRedisplay(); } /* ****************************** */ /* initialize polygon stored in global variable poly */ void initialize_polygon() { //clear the vector, in case something was there poly.clear(); int n = 10; //size of polygon double rad = 100; double step = 2 * M_PI / n; point2D p; for (int i=0; i poly) { for (unsigned int i=0; i poly){ if (poly.size() == 0) return; //set color glColor3fv(yellow); int i; for (i=0; i