#ifdef WIN32 #include "windows.h" #include "windowsx.h" #endif #include #include #include // max vertices in polygon #define MAX_VERTICES 100 #define WINDOW_WIDTH 512 #define WINDOW_HEIGHT 384 #define WINDOW_ORIG_X 256 #define WINDOW_ORIG_Y 192 // point structure definition typedef struct { GLint x; GLint y; } GLintPoint; // globals GLboolean g_bDragging; GLboolean g_bPolyComplete; GLintPoint g_aVertices[MAX_VERTICES]; GLint g_nVertices = 0; GLintPoint g_LastLine[2]; // function prototypes int main(int argc, char** argv); void Init(void); void OnDisplay(void); void OnReshape(int w, int h); void OnMouseClick(int button, int state, int x, int y); void OnMouseMove(int x, int y); int main(int argc, char** argv) { // initialize the display glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (WINDOW_WIDTH, WINDOW_HEIGHT); glutInitWindowPosition (WINDOW_ORIG_X, WINDOW_ORIG_Y); glutCreateWindow (argv[0]); // user-specific initialization Init(); // register callbacks glutDisplayFunc(OnDisplay); glutReshapeFunc(OnReshape); glutMouseFunc(OnMouseClick); glutMotionFunc(OnMouseMove); // start the main event loop glutMainLoop(); return 0; } // Init(). User-specific initialization. void Init(void) { glClearColor (1.0, 1.0, 1.0, 1.0); glPointSize(5.0); glLogicOp(GL_XOR); g_nVertices = 0; g_bDragging = false; g_bPolyComplete = true; } // The OnDisplay() callback is invoked in response to an expose or a resize event. void OnDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // redraw the polygon if necessary ////////////////////////////////////////// if (g_nVertices != 0) { int i; // draw the polygon vertices in black glColor3f(0.0, 0.0, 0.0); glBegin(GL_POINTS); for (i=0; i 2) { // connect the last and first vertices of the polygon glColor3f(0.0, 0.0, 0.0); glBegin(GL_LINES); glVertex2i(g_aVertices[g_nVertices - 1].x, g_aVertices[g_nVertices - 1].y); glVertex2i(g_aVertices[0].x, g_aVertices[0].y); glEnd(); glFlush(); // draw the complete polygon in red glColor3f(1.0, 0.0, 0.0); glBegin(GL_POLYGON); for (int i=0; i