//File: group.cpp //Name: Mark Maynard Brad Bolen //Date: October 31, 2002 #include #include #include class GLfloatPoint { public: GLfloat x; GLfloat y; int section; }; GLint overlaySupport, transparent, red, white, blue, green; const int MAXNUMBERPOINTS = 100; GLfloatPoint A[MAXNUMBERPOINTS]; int numberPoints = 0; //Pre: A contains the points to draw and drawPicture is a defined function that draws the points. //Post: The picture from the points in A have been drawn to the overlay. void redrawOverlay(void); //Pre: A window has been created and the frame set up to include the valid points. //Post: Several points producing a polygon have been drawn to the screen in the normal plane. void display(void); //Pre: A window has been created. //Post: The background color has been set. The matrix has been loaded and the frame has been set. void init (void); //Pre: value contains a valid integer. //Post: If value is 1 then the overlay is shown. If it is 2, the overlay is hidden. Otherwise, nothing is done. void choice_selected(int value); //Pre: A and numberPoints have been declared as an array of GLfloatPoint and int respectively. //Post: The points and the section of the picture that they are in have been read into A. An error is returned if there is a failure in the read. int readInPointsFromFile(char file[]); //Pre: A contains the list of points to draw along with the section of the picture that they are in. numberPoints contains the number of points in A. //Post: The points contained in A have been drawn to the screen. void drawPicture(); /////////////////////////////////// MAIN //////////////////////////////// int main(int argc, char** argv) { int overlaySupport, transparent, red, white, blue, green; if(argc < 2){ cout << "Error: Usage - " << argv[0] << " filename" << endl; return EXIT_FAILURE; } int error = readInPointsFromFile(argv[1]); if(error == 1){ cout << "Error: Reading in points from file." << endl; return EXIT_FAILURE; } glutInit(&argc, argv); glutInitWindowSize (800, 400); glutInitWindowPosition (101, 350); glutCreateWindow ("Overlay Usage"); init(); glutDisplayFunc(display); glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX); overlaySupport = glutLayerGet(GLUT_OVERLAY_POSSIBLE); if(overlaySupport){ //Set up the overlay and hide it initially. glutEstablishOverlay(); glutHideOverlay( ); glOrtho(0.0,29.0,0.0,19.0,-1.0,1.0); transparent = glutLayerGet(GLUT_TRANSPARENT_INDEX); glClearIndex(transparent); red = (transparent + 1) % glutGet(GLUT_WINDOW_COLORMAP_SIZE); blue = (transparent + 2) % glutGet(GLUT_WINDOW_COLORMAP_SIZE); green = (transparent + 3) % glutGet(GLUT_WINDOW_COLORMAP_SIZE); glutSetColor(red, 1.0, 0.0, 0.0); // Red glutSetColor(blue, 0.0, 0.0, 1.0); // Blue glutSetColor(green, 0.0, 1.0, 0.0); // Green glutOverlayDisplayFunc(redrawOverlay); }else { cout << "Sorry overlays are not supported by this machine." << endl; } //Add menu entries and attach menu. glutCreateMenu(choice_selected); glutAddMenuEntry("Show Overlay",1); glutAddMenuEntry("Hide Overlay", 2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMainLoop(); return 0; } ///////////////////////////////////////////////////////////////////////// void redrawOverlay(){ glutUseLayer(GLUT_OVERLAY); glClear (GL_COLOR_BUFFER_BIT); //clear all pixels drawPicture(); glFlush(); } void display(void){ glutUseLayer(GLUT_NORMAL); glClear (GL_COLOR_BUFFER_BIT); //clear all pixels glPointSize(3.0); //change size to 3: //Plot some points: glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); //Make one polygon glVertex3f (0.0, 0.0, 0.0); glVertex3f (1.0, 0.0, 0.0); glColor3f(0.0, 0.0, 1.0); glVertex3f (1.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glEnd(); glFlush(); } void init (void){ //select clearing (background) color glClearColor (0.0, 0.0, 0.0, 0.0); //initialize viewing values glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } void choice_selected(int value){ switch(value){ case 1: glutShowOverlay( ); //Show overlay if selected break; case 2: glutHideOverlay( ); //Hide overlay is selected break; } } int readInPointsFromFile(char file[]){ ifstream infile; //Return error code if file open fails. infile.open(file); if(infile.fail( )) return 1; infile >> A[numberPoints].x >> A[numberPoints].y >> A[numberPoints].section; ++numberPoints; //Read in points to A until the end of the file while((!infile.eof( )) && (!infile.fail( )) && (numberPoints < MAXNUMBERPOINTS)){ infile >> A[numberPoints].x >> A[numberPoints].y >> A[numberPoints].section; ++numberPoints; //If any type of file error occurs, immediately return with an error code. if(infile.fail( ) == true) return 1; } //Clean up --numberPoints; infile.close( ); return 0; } void drawPicture(){ int i, pictureSection = 0; glColor3f(1.0, 1.0, 1.0); //Draws each section of the picture by drawing one line strip at a time. glBegin(GL_LINE_STRIP); for(i=0; i