/* Sample Program Using Color Index "Overlay" VIZA 652 F. Parke 12/01/97 */ #include /* define standard I/O routines */ #include /* define OpenGL graphics routines */ #include /* define OpenGL utility routines */ #include /* define GLUT window and device routines */ #include /* define math library routines */ #define TRUE 1 #define FALSE 0 #define WIDTH 1000 /* window dimensions */ #define HEIGHT 750 #define NCOLORS 15 /* Number of Colors Used */ /* Define the RGB color values to be used */ #define RGBRED 1, 0, 0 #define RGBORANGE 1, 0.5, 0 #define RGBYELLOW 1, 1, 0 #define RGBYELLOWGREEN 0.5, 1, 0 #define RGBGREEN 0, 1, 0 #define RGBGREENCYAN 0, 1, 0.5 #define RGBCYAN 0, 1, 1 #define RGBCYANBLUE 0, 0.5, 1 #define RGBBLUE 0, 0, 1 #define RGBPURPLE 0.5, 0, 1 #define RGBMAGENTA 1, 0, 1 #define RGBVIOLET 1, 0, 0.5 #define RGBBLACK 0, 0, 0 #define RGBGREY .5,.5,.5 #define RGBWHITE 1, 1, 1 #define RGBDKGREY 0.20, 0.20, 0.20 /* for screen background */ /* Define Indices for the RGB color values */ #define IDXRED 0 #define IDXORANGE 1 #define IDXYELLOW 2 #define IDXYELLOWGREEN 3 #define IDXGREEN 4 #define IDXGREENCYAN 5 #define IDXCYAN 6 #define IDXCYANBLUE 7 #define IDXBLUE 8 #define IDXPURPLE 9 #define IDXMAGENTA 10 #define IDXVIOLET 11 #define IDXBLACK 12 #define IDXGREY 13 #define IDXWHITE 14 #define IDXDKGREY 15 /* for screen background */ /* Other Defines */ #define NRMLMASK 15 /* index mask for normal drawing */ #define OVLMASK 16 /* index mask for overlay drawing */ #define OVLWHITE 16 /* index of white color for overlay */ #define OVLCLEAR 0 /* index to clear overlay */ #define MAXCOLORS (OVLMASK * 2) #define R 0 #define G 1 #define B 2 /******************* Global Variables for Functions *********************/ int firstx, firsty; /* mouse coordinates on button press */ int lastx, lasty; /* last mouse drawing coordinates */ int drawing = FALSE; /* TRUE if button pressed in drawing area */ int colorindex = IDXWHITE; /* initial drawing color is white */ float colorarray[][3] = {{RGBRED}, {RGBORANGE}, {RGBYELLOW}, {RGBYELLOWGREEN},{RGBGREEN}, {RGBGREENCYAN}, {RGBCYAN}, {RGBCYANBLUE}, {RGBBLUE}, {RGBPURPLE}, {RGBMAGENTA}, {RGBVIOLET}, {RGBBLACK}, {RGBGREY},{RGBWHITE}, {RGBDKGREY}}; /******************* Procedures and Functions *********************/ /* Returns true if point (x, y) in the sketch pad area */ int insketchpad(int x, int y){ return(x > 0 && x < WIDTH && y > 0 && y < HEIGHT); } /* Draw an outline rectangle with diagonal (x0, y0) to (x1, y1) */ void outlinebox(x0, y0, x1, y1){ glBegin(GL_LINE_LOOP); glVertex2i(x0, y0); glVertex2i(x0, y1); glVertex2i(x1, y1); glVertex2i(x1, y0); glEnd(); } /* Draw a filled rectangle with diagonal (x0, y0) to (x1, y1) */ void fillbox(x0, y0, x1, y1){ glBegin(GL_POLYGON); glVertex2i(x0, y0); glVertex2i(x0, y1); glVertex2i(x1, y1); glVertex2i(x1, y0); glEnd(); } /* Draw Overlay Rectangle */ void orectangle(int x, int y){ glIndexi(OVLCLEAR); /* Erase last Overlay */ outlinebox(firstx, firsty, lastx, lasty); glIndexi(OVLWHITE); /* Draw new Overlay */ outlinebox(firstx, firsty, x, y); lastx = x; /* save mouse coordinates */ lasty = y; } /* Take drawing action based on new mouse position */ void drawMotion(int x, int y){ y = HEIGHT - y; if(drawing && insketchpad(x, y)) orectangle(x, y); /* Update overlay rectangle */ } /* Watch mouse button presses and handle them */ void handleButton(int button, int state, int x, int y){ y = HEIGHT - y; if(button != GLUT_LEFT_BUTTON) /* ignore if not left button */ return; if(state == GLUT_DOWN){ if(insketchpad(x, y)){ drawing = TRUE; firstx = lastx = x; /* initialize mouse coordinates */ firsty = lasty = y; glIndexMask(OVLMASK); /* set index mask to overlay */ glClearIndex(OVLCLEAR); /* set clear color */ glClear(GL_COLOR_BUFFER_BIT); /* clear overlay */ } } else{ glIndexMask(OVLMASK); /* set index mask to overlay */ glClearIndex(OVLCLEAR); /* set clear color */ glClear(GL_COLOR_BUFFER_BIT); /* clear overlay */ glIndexMask(NRMLMASK); /* restore index mask to normal */ glIndexi((colorindex++)%15); /* get new color */ fillbox(firstx, firsty, lastx, lasty); /* draw filled box */ drawing = FALSE; } } /* Draw the sketchpad */ void drawSketchpad(){ glClearIndex(IDXDKGREY); /* window cleared to dark grey */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ glIndexMask(NRMLMASK); /* set index mask to normal */ glIndexi(colorindex); /* set the drawing color index */ } /* Main program to open the main window and register the callbacks that draw the sketchpad, service the mouse buttons, and service mouse movements. */ void main(int argc, char* argv[]){ int i; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX); glutInitWindowSize(WIDTH, HEIGHT); glutCreateWindow("Index Overlay Example"); gluOrtho2D(0, WIDTH, 0, HEIGHT); glutDisplayFunc(drawSketchpad); /* drawing event callback */ glutMouseFunc(handleButton); /* mouse button event callback */ glutMotionFunc(drawMotion); /* mouse movement callback */ /* load the color index table with desired colors */ for(i = 0; i < NCOLORS + 1; i++) glutSetColor(i, colorarray[i][R], colorarray[i][G], colorarray[i][B]); /* load the rest of the index table with "off-white" */ for(i = NCOLORS + 1; i < MAXCOLORS; i++) glutSetColor(i, .75, .75, .75); glutMainLoop(); }