/*---------------------------------------------------------------------------------------- * */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(winw,winh); glutInitWindowPosition(200,100); glutCreateWindow("03 - Mouse Motion"); int size=3; poly.Add(Vector2f(50*size,10*size)); poly.Add(Vector2f(50*size,130*size)); poly.Add(Vector2f(10*size,130*size)); poly.Add(Vector2f(10*size,90*size)); poly.Add(Vector2f(40*size,70*size)); poly.Add(Vector2f(10*size,50*size)); poly.Add(Vector2f(10*size,10*size)); glutDisplayFunc(Draw); glutReshapeFunc(Resize); glutMouseFunc(MouseButton); glutMotionFunc(MouseMotion); glutPassiveMotionFunc(MousePassiveMotion); glutMainLoop(); }
/*---------------------------------------------------------------------------------------- * \brief This function is called whenever a mouse button is pressed or released * \param button - GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, or GLUT_MIDDLE_BUTTON * \param state - GLUT_UP or GLUT_DOWN depending on whether the mouse was released * or pressed respectivly. * \param x - the x-coord of the mouse cursor. * \param y - the y-coord of the mouse cursor. */ void MouseButton(int button,int state,int x, int y) { /* * update the mouse position */ TheMouse.x = x; TheMouse.y = y; /* * has the button been pressed or released? */ if (state == GLUT_DOWN) { /* * This holds the location of the first mouse click */ if ( !(TheMouse.lmb || TheMouse.mmb || TheMouse.rmb) ) { TheMouse.xpress = x; TheMouse.ypress = y; poly.Add(Vector2f(x,winh-y)); poly.Shrink(20); } /* * Which button was pressed? */ switch(button) { case GLUT_LEFT_BUTTON: TheMouse.lmb = 1; break; case GLUT_MIDDLE_BUTTON: poly.vert.clear(); poly.shrink.clear(); TheMouse.mmb = 1; break; case GLUT_RIGHT_BUTTON: TheMouse.rmb = 1; break; } } else { /* * Which button was released? */ switch(button) { case GLUT_LEFT_BUTTON: TheMouse.lmb = 0; break; case GLUT_MIDDLE_BUTTON: TheMouse.mmb = 0; break; case GLUT_RIGHT_BUTTON: TheMouse.rmb = 0; break; } } /* * Force a redraw of the screen. If we later want interactions with the mouse * and the 3D scene, we will need to redraw the changes. */ glutPostRedisplay(); }