/* mouse1.c Laura Toma What it does: Shows how to use the mouse in OpenGL. First the mouse is registered via a callback function. Once registered, this function will be called on any mouse event in the window. The user can use this function to respond to mouse events. This example will print the coordinates of the point where the mouse is clicked, and will draw a small blue disk at the point where the mouse is pressed. */ #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 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 window 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 pressed at (%d,%d)\n", mouse_x, mouse_y); } glutPostRedisplay(); } /* ****************************** */ /* initialize polygon stored in global variable poly */ //Note: use our local coordinate system (0,0) to (WINSIZE,WINSIZE), //with the origin in the lower left corner. 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