INT32GP Graphics Programming: Tutorial #02
More OpenGL and GLUT

B1.11 3:00pm Tue 30 Jul 02
B1.11 12midday Wed 31 Jul 02
Continue to build on your program from tutorial #01 or start with square2.c (this has no keyboard function).
Creating a GLUT menu
Add a GLUT main menu to quit the program and a submenu that controls the OpenGL drawing mode (GL_POINTS, GL_LINE_LOOP, GL_LINES, GL_POLYGON). The menu is activated by clicking the right mouse button.
  1. At the top of the program (before the function code), define a constant integer to represent the menu choice to quit the program and declare that the default drawing mode is a filled polygon.
  2. /*
    ** menu management
    */
    #define QUIT -999
    GLenum mode = GL_POLYGON;
  3. Add the following function above setUpGLUT.
  4. void setUpMenus(){
       int mMenu;
       mMenu=setUpModeMenu();
       glutCreateMenu(mainMenu);
       glutAddSubMenu("specify primitive mode",mMenu);
       glutAddMenuEntry("quit",QUIT);
       glutAttachMenu(GLUT_RIGHT_BUTTON);
    }
  5. Call this function from setUpGLUT, just before registering callbacks. If you put the call in the wrong position, you may run into trouble.
  6. Write the code for setting up the mode menu (above setUpGLUT).
  7. int setUpModeMenu(){
       int id;
       id=glutCreateMenu(modeMenu);
       glutAddMenuEntry("vertices",GL_POINTS);
       glutAddMenuEntry("line loop",GL_LINE_LOOP);
       glutAddMenuEntry("filled polygon",GL_POLYGON);
       glutAddMenuEntry("lines",GL_LINES);
       return id;
    }
  8. Write the code for the main menu and the mode menu (above setUpModeMenu). Note the call to glutPostRedisplay. This causes GLUT to call your display function again. Never call display directly.
  9. void mainMenu(int choice){
       if(choice==QUIT)
       	exit(0);
    }
    void modeMenu(int choice){
       mode=choice;
       glutPostRedisplay();
    }
  10. Replace the renderView function with the following code. This renders the square in red using the mode chosen from the menu (filled polygon by default).
  11. void renderView(){
       glColor3fv(colour[red]);
       render(square,numVertices,mode);
    }
Enhance the keyboard function to control the size of vertices (GL_POINTS)
When the user presses 'p' (lowercase), increase the size of vertices (GL_POINTS) up to a maximum value. Decrease the size
  1. Create a global variable for pointSize and set up maximum and minimum values.
  2. GLfloat pointSize = 1.0;
    GLfloat MAX_POINT = 50.0;
    GLfloat MIN_POINT = 1.0;
  3. Add the following (or similar) code to the switch statement in the keyboard function.
  4.       case 'p':
          	if(pointSize < MAX_POINT)
                pointSize+=1.0;
             break;
          case 'P':
          	if(pointSize > MIN_POINT)
                pointSize-=1.0;
             break;
  5. After the switch statement, tell GLUT to redisplay the window.
  6. glutPostRedisplay();
  7. Insert the following code in an appropriate place in renderView
  8. glPointSize(pointSize);
  9. Does the program behave as expected when you hold down the 'p' key?
Attempt the following
  1. Set up another sub menu to control the background (clear colour). Make sure there is a choice of (at least) black, white and grey.
  2. Create more drawing colours - at least orange and pink. Use the keyboard to control the current drawing colour. Pressing a key should cause the program to circle through all the available colours. Use a global variable to specify the current colour.
  3. Control the width of lines (similar to controlling size of points).glLineWidth(GLfloat width) Default width is 1.0.
  4. When the 'm' key is pressed, print the x and y position of the mouse to the command line. Are they what you expected?
  5. Consult the GLUT API and the OpenGL Programming Guide to continue your own investigation of their capablities. The OpenGL Programming Guide is also available as an online book on the Silicon Graphics machines in B111 (perhaps not all of them?).
Fran Soddell      last updated 29 July 2002 F.Soddell@bendigo.latrobe.edu.au