/*---------------------------------------------------------------------------------------- * This is the main display callback function. It sets up the drawing for * The 3D scene first then calls the Draw3D() function. After that it switches to * an orthographic projection and calls Draw2D(). */ void Draw(void) { /* * Clear the background */ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); /* * Allow drawing in full region of the screen */ glViewport(0,0,winw,winh); glPushMatrix(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, winw, 0, winh); poly.Shrink(40); glLineWidth(1); poly.draw(Vector2f(0,0)); glLineWidth(3); poly.handleShrinkErrors(); poly.draw(Vector2f(640,0)); glPopMatrix(); glPopMatrix(); glMatrixMode(GL_MODELVIEW); /* * Bring the back buffer to the front and vice-versa. */ glutSwapBuffers(); }
/*---------------------------------------------------------------------------------------- * \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(); }