JMU
GLUT Basics
with Examples


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Introduction
Windowing Basics in GLUT
Windowing Basics in GLUT (cont.)

Initialization

openglexamples/glut/window.c (Fragment: main)
        /**
 * The entry point of the application.
 *
 * This function contains calls to GLUT.
 *
 * @param argc  The number of command line arguments
 * @param argv  The array of command line arguments
 * @return      A status code
 */
int main(int argc, char** argv) {
  // Initialize GLUT
  glutInit(&argc, argv);

  // Set the display mode.  In this case, tell GLUT to
  // use single buffering and an RGB color model
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

  // Set the size of the window (in pixels)
  glutInitWindowSize(640, 480);

  // Set the position of the window (in screen coordinates)
  glutInitWindowPosition(0, 0);

  // Create a window with the given title
  glutCreateWindow("A GLUT Window");

  // Register the display callback function
  glutDisplayFunc(display);


  // Display window and start event processing
  glutMainLoop();

  return 0;
}
        

A Display Callback

openglexamples/glut/window.c (Fragment: displaycallback)
        /**
 * The display callback (i.e., the function that is called
 * each time the window needs to be displayed).
 *
 * This function would normally contain calls to OpenGL.  
 */
void display() {
  printf("+display()\n");

  // Calls to OpenGL go here

  printf("-display()\n");
}
        
Window Reshape Events
Window Reshape Events (cont.)

A Reshape Callback

openglexamples/glut/window.c (Fragment: reshapecallback)
        /**
 * The reshape callback (i.e., the function that is called
 * each time the window is re-sized).
 *
 * @param width   The new width
 * @param height  The new height
 */
void reshape(int width, int height) {
  printf("+reshape(%d,%d)\n", width, height);

  // Appropriate code goes here

  printf("+reshape(%d,%d)\n", width, height);
}
        

Registration [e.g., in main()]

openglexamples/glut/window.c (Fragment: register-reshape)
          // Register the reshape callback
  glutReshapeFunc(reshape);
        
Keyboard Events
Special Keyboard Events
Keyboard Events (cont.)

A Keyboard Callback

openglexamples/glut/keyboard.c (Fragment: keyboardcallback)
        /**
 * The keyboard callback  (i.e., the function that is called
 * each time a "normal" key is pressed).
 *
 * @param key   The ASCII code of the key that was pressed
 * @param x     The x-position of the mouse when the key was pressed
 * @param y     The y-position of the mouse when the key was pressed
 */
void keyPressed(unsigned char key, int x, int y) {
  int alt, ctrl, modifiers, shift;

  printf("+keyPressed(%d,%d,%d)\n", key, x, y);

  modifiers = glutGetModifiers();
  alt = modifiers & GLUT_ACTIVE_ALT;
  ctrl = modifiers & GLUT_ACTIVE_CTRL;
  shift = modifiers & GLUT_ACTIVE_SHIFT;
  printf("            %d %d %d \n", alt, ctrl, shift);

  // Appropriate code goes here

  printf("-keyPressed(%d,%d,%d)\n", key, x, y);
}
        
Keyboard Events (cont.)

A Special Key Callback

openglexamples/glut/keyboard.c (Fragment: specialcallback)
        /**
 * The special-key callback  (i.e., the function that is called
 * each time a "special" key is pressed).
 *
 * @param key   The key that was pressed (e.g., GLUT_KEY_UP)
 * @param x     The x-position of the mouse when the key was pressed
 * @param y     The y-position of the mouse when the key was pressed
 */
void specialKeyPressed(int key, int x, int y) {
  int alt, ctrl, modifiers, shift;

  printf("+specialKeyPressed(%d,%d,%d)\n", key, x, y);

  modifiers = glutGetModifiers();
  alt = modifiers & GLUT_ACTIVE_ALT;
  ctrl = modifiers & GLUT_ACTIVE_CTRL;
  shift = modifiers & GLUT_ACTIVE_SHIFT;
  printf("            %d %d %d \n", alt, ctrl, shift);

  // Appropriate code goes here

  printf("-specialKeyPressed(%d,%d,%d)\n", key, x, y);
}
        
Keyboard Events (cont.)

Registration [e.g., in main()]

openglexamples/glut/keyboard.c (Fragment: register-keyboard)
          // Register the keyboard callbacks
  glutKeyboardFunc(keyPressed);
  glutSpecialFunc(specialKeyPressed);
        
Mouse Events
Mouse Events (cont.)

Callbacks

openglexamples/glut/mouse.c (Fragment: mousecallbacks)
        /**
 * The mouse callback (i.e., the function that is called
 * each time a mouse button is pressed or released).
 *
 * @param button The button (e.g., GLUT_LEFT_BUTTON)
 * @param state  The state (e.g., GLUT_UP or GLUT_DOWN)
 * @param x      The x-position of the mouse
 * @param y      The y-position of the mouse
 */
void mouseClicked(int button, int state, int x, int y) {
  printf("+mouseClicked(%d,%d,%d,%d)\n", button, state, x, y);

  // Appropriate code goes here

  printf("-mouseClicked(%d,%d,%d,%d)\n", button, state, x, y);
}

/**
 * The motion callback (i.e., the function that is called
 * each time the mouse is dragged).
 *
 * @param x      The x-position of the mouse
 * @param y      The y-position of the mouse
 */
void mouseDragged(int x, int y) {
  printf("+mouseDragged(%d,%d)\n", x, y);

  // Appropriate code goes here

  printf("-mouseDragged(%d,%d)\n", x, y);
}

/**
 * The passive-motion callback (i.e., the function that is called
 * each time the mouse is moved).
 *
 * @param x      The x-position of the mouse
 * @param y      The y-position of the mouse
 */
void mouseMoved(int x, int y) {
  printf("+mouseMoved(%d,%d)\n", x, y);

  // Appropriate code goes here

  printf("-mouseMoved(%d,%d)\n", x, y);
}

        
Mouse Events (cont.)

Registration [e.g., in main()]

openglexamples/glut/mouse.c (Fragment: register-mouse)
          // Register the mouse callbacks
  glutMouseFunc(mouseClicked);
  glutPassiveMotionFunc(mouseMoved);
  glutMotionFunc(mouseDragged);
        
The Idle State
Subwindows
Other Things
Rudimentary Menus
Rudimentary Menus (cont.)
Rudimentary Menus (cont.)

Identifiers

openglexamples/glut/menu.c (Fragment: menuEntries)
        // Menu entries
enum MenuEntry {
  MENU_COPY,
  MENU_CUT,
  MENU_HELP,
  MENU_PASTE
};
        
Rudimentary Menus (cont.)

Callback

openglexamples/glut/menu.c (Fragment: menuClicked)
        /**
 * The menu callback
 *
 * @param menu   The menu entry that was selected
 */
void menuClicked(int menu) {
  printf("+menuClicked(%d)\n", menu);

  // Appropriate code goes here

  printf("-menuClicked(%d)\n", menu);
}
        
Rudimentary Menus (cont.)

Setup [e.g., in main()]

openglexamples/glut/menu.c (Fragment: menuSetup)
          // Create the main menu
  mainMenu = glutCreateMenu(menuClicked);

  // Create the "File" menu
  fileMenu = glutCreateMenu(menuClicked);

  // Add menu items to the "File" menu (which is the current menu)
  glutAddMenuEntry("Copy", MENU_COPY);
  glutAddMenuEntry("Cut", MENU_CUT);
  glutAddMenuEntry("Paste", MENU_PASTE);

  // Make the main menu the current menu
  glutSetMenu(mainMenu);

  // Add the "File" menu to the main menu (which is again the current menu)
  glutAddSubMenu("File", fileMenu);

  // Add a menu item to the main menu (which is still the current menu)
  glutAddMenuEntry("Help", MENU_HELP);

  // Attach the menu to the right button
  glutAttachMenu(GLUT_RIGHT_BUTTON);