/******************************************************************* Example Callback Subroutines for CS480 Example Program ******************************************************************** Author: Stan Sclaroff Date: September, 9 2004 Comments: Subroutines to support GLUT library callbacks: menu support, redisplay/reshape, mouse actions. Used for drawing/modifying circular polygon. ********************************************************************/ #include #include #include #include #include "const.h" #include "types.h" #include "funcs.h" /* static global variables for storing state information */ static int currentButton; /* currently pressed mouse button */ static int fill = FALSE; /* area fill on or off, initially=off */ static polygonType *poly; /* polygon primitive */ static char concaveMsgBuff[300], inOutMsgBuff[300]; /* Msg to be rendered by opengl */ static int showIO = FALSE; /* toggle flag for inside/outside test pattern */ /* local functions */ void generateTestCase(); /* draw a string at a particular location (x,y) */ void drawString(int x, int y, char *string, void *font) { int len, i; glRasterPos2f(x, y); len = (int) strlen(string); for (i = 0; i < len; i++) { glutBitmapCharacter(font, string[i]); } } /* window size change */ void reshape(int width, int height) { /* change viewport dimensions */ glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, width, height, 0); glMatrixMode(GL_MODELVIEW); } /* redisplaying graphics */ void display(void) { int x, y, maxRow, maxCol; glPointSize(3); /* If requested, enable fill */ if(fill==TRUE) glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); else glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); /* clear the display */ glClear(GL_COLOR_BUFFER_BIT); if(concavePoly(poly)==FALSE) { drawConvexPoly(poly); strcpy(concaveMsgBuff, " Polygon is convex"); } else { drawConcavePoly(poly); strcat(concaveMsgBuff, " Polygon is concave"); } drawString(20,glutGet(GLUT_WINDOW_HEIGHT)-20, concaveMsgBuff,GLUT_BITMAP_TIMES_ROMAN_24); drawString(20,glutGet(GLUT_WINDOW_HEIGHT)-40, inOutMsgBuff,GLUT_BITMAP_TIMES_ROMAN_24); if (showIO == TRUE) { glPointSize(3); maxRow = glutGet(GLUT_WINDOW_HEIGHT); maxCol = glutGet(GLUT_WINDOW_WIDTH); glBegin(GL_POINTS); for (y=0; yn_verts = 0; glutPostRedisplay(); break; case 'q': case 'Q': case 27: /* Esc, q, or Q key = Quit */ exit(0); break; case 't': case 'T': /* loop through test cases */ generateTestCase(); glutPostRedisplay(); break; case 'f': case 'F': if(fill == TRUE) fill = FALSE; else fill = TRUE; glutPostRedisplay(); break; case 's': case 'S': if (showIO == TRUE) showIO = FALSE; else showIO = TRUE; glutPostRedisplay(); break; case 'p': case 'P': printVerts(poly); break; default: break; } } void initPoly() { if(poly == 0) poly = createPoly(); } void generateTestCase() { int numTestCase = 7; static int testCase = 0; switch (testCase) { case 0: poly->n_verts=0; addVert(poly, 50,200); addVert(poly,150, 50); addVert(poly,250,200); addVert(poly,150,125); break; case 1 : poly->n_verts = 0; addVert(poly, 50, 50); addVert(poly,250, 50); addVert(poly,250,200); addVert(poly,150,125); addVert(poly, 50,200); break; case 2: poly->n_verts=0; addVert(poly, 50, 200); addVert(poly, 50, 50); addVert(poly,200, 50); addVert(poly,200, 100); addVert(poly,100, 100); addVert(poly,100, 200); break; case 3: poly->n_verts=0; addVert(poly, 50, 50); addVert(poly,200, 50); addVert(poly,200, 200); addVert(poly,150, 200); addVert(poly,150, 100); addVert(poly, 50, 100); break; case 4: poly->n_verts=0; addVert(poly, 50, 100); addVert(poly,100, 100); addVert(poly,100, 50); addVert(poly,150, 50); addVert(poly,150, 100); addVert(poly,200, 100); addVert(poly,200, 150); addVert(poly,150, 150); addVert(poly,150, 200); addVert(poly,100, 200); addVert(poly,100, 150); addVert(poly, 50, 150); break; case 5: poly->n_verts = 0; addVert(poly,50,250); addVert(poly,50,150); addVert(poly,100,150); addVert(poly,150,50); addVert(poly,200,200); addVert(poly,250,50); addVert(poly,275,150); addVert(poly,275,250); case 6 : poly->n_verts=0; addVert(poly,50,50); addVert(poly,50,200); addVert(poly,75,200); addVert(poly,75,100); // try 75, 125 addVert(poly,100,100); addVert(poly,100,200); addVert(poly,125,200); addVert(poly,125,100); // try 125, 125 addVert(poly,150,100); addVert(poly,150,200); addVert(poly,175,200); addVert(poly,175,100); addVert(poly,275,100); addVert(poly,275,5); addVert(poly,250,5); addVert(poly,250,50); addVert(poly,225,50); addVert(poly,225,5); addVert(poly,200,5); addVert(poly,200,50); addVert(poly,175,50); addVert(poly,175,5); addVert(poly,150,5); addVert(poly,150,50); break; break; default: poly->n_verts=0; break; } testCase = (testCase + 1 ) % numTestCase; }