- Forward


GLUT Basics
with Examples


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Introduction
Back SMYC Forward
  • What is GLUT?
    • A simple, cross-platform windowing system that can be used with OpenGL
  • Cross-Platform Alternatives:
    • CPW
    • NGL
    • OpenML
    • Simple Direct Media Layer (SDL)
Windowing Basics in GLUT
Back SMYC Forward
  • Initialization:
    • Initialize GLUT
    • Set the display mode
    • Set the window position and size
    • Create the window
    • Register the display callback function
    • Display the window and start event processing
  • The Display Callback:
    • Any void function with no parameters
    • Set by calling glutDisplayFunc(void (*f)(void))
    • Called whenever the window needs to be displayed
    • You can force a call by calling glutPostRedisplay()
Windowing Basics in GLUT (cont.)
Back SMYC Forward

Initialization

openglexamples/glut/window.c (Fragment: main)
 

A Display Callback

openglexamples/glut/window.c (Fragment: displaycallback)
 
Window Reshape Events
Back SMYC Forward
  • Parameters:
    • The new width and height of the window
  • Registration:
    • glutReshapeFunc(void (*f) (int width, int height))
Window Reshape Events (cont.)
Back SMYC Forward

A Reshape Callback

openglexamples/glut/window.c (Fragment: reshapecallback)
 

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

openglexamples/glut/window.c (Fragment: register-reshape)
 
Keyboard Events
Back SMYC Forward
  • Parameters:
    • The key that was pressed
    • The location of the mouse when the key was pressed
  • Registration:
    • glutKeyboardFunc(void (*f)(unsigned char key, int x, int y))
  • Modifiers:
    • Call int glutGetModifiers()
    • Bit flags can be checked with GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, and GLUT_ACTIVE_ALT
Special Keyboard Events
Back SMYC Forward
  • Parameters:
    • The key that was pressed
    • The location of the mouse when the key was pressed
  • Registration:
    • glutSpecialFunc(void (*f)(int key, int x, int y))
  • Key Definitions:
    • GLUT_KEY_F1 thru GLUT_KEY_F12, GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN, GLUT_KEY_PAGE_UP, GLUT_KEY_PAGE_DOWN, GLUT_KEY_HOME, GLUT_KEY_END, and GLUT_KEY_INSERT
Keyboard Events (cont.)
Back SMYC Forward

A Keyboard Callback

openglexamples/glut/keyboard.c (Fragment: keyboardcallback)
 
Keyboard Events (cont.)
Back SMYC Forward

A Special Key Callback

openglexamples/glut/keyboard.c (Fragment: specialcallback)
 
Keyboard Events (cont.)
Back SMYC Forward

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

openglexamples/glut/keyboard.c (Fragment: register-keyboard)
 
Mouse Events
Back SMYC Forward
  • Button Presses/Releases:
    • Register with glutMouseFunc(void (*f)(int button, int state, int x, int y))
    • button is either GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON
    • state is either GLUT_UP or GLUT_DOWN
  • Motion Events:
    • Register with glutPassiveMotionFunc(void (*f)(int x, int y))
    • Also called when the mouse is pressed/released
  • Drag Events:
    • Register with glutMotionFunc(void (*f)(int x, int y))
Mouse Events (cont.)
Back SMYC Forward

Callbacks

openglexamples/glut/mouse.c (Fragment: mousecallbacks)
 
Mouse Events (cont.)
Back SMYC Forward

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

openglexamples/glut/mouse.c (Fragment: register-mouse)
 
The Idle State
Back SMYC Forward
  • Uses:
    • "Background" processing
  • Registration:
    • glutIdleFunc(void (*f) (void))
Subwindows
Back SMYC Forward
  • Purpose:
    • Divide a window into regions, each with its own callbacks (and OpenGL context)
  • Creation/Destruction:
    • int glutCreateSubWindow(int parentWindow, int x, int y, int width, int height)
    • int glutDestroyWindow(int window)
  • Considerations:
    • When a window is created it becomes the "current" window
    • glutDisplayFunc() must be called for each window (when it is "current")
    • The OpenGL context must be initialized for each window (when it is "current")
Other Things
Back SMYC Forward
  • Window Title:
    • void glutSetWindowTitle(char *name)
  • Window Cursor:
    • void glutSetCursor(int cursor)
    • where cursor is one of: GLUT_CURSOR_INFO, GLUT_CURSOR_HELP, GLUT_CURSOR_WAIT, GLUT_CURSOR_TEXT, GLUT_CURSOR_CROSSHAIR, GLUT_CURSOR_NONE (or many others)
Rudimentary Menus
Back SMYC Forward
  • Creating and Destroying:
    • int glutCreateMenu(void (*callback)(int value))
    • void glutDestroyMenu(int menu)
  • Setting the Current Menu:
    • void setMenu(int menu)
Rudimentary Menus (cont.)
Back SMYC Forward
  • Adding Submenus and Menu Entries (to the current menu):
    • void glutAddSubMenu(char *name, int menu)
    • void glutAddMenuEntry(char *name, int valueToReturn)
  • Activating/Deactivating Mouse Buttons:
    • void glutAttachMenu(int button)
    • void glutDetachMenu(int button)
    • where button is either GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON
Rudimentary Menus (cont.)
Back SMYC Forward

Identifiers

openglexamples/glut/menu.c (Fragment: menuEntries)
 
Rudimentary Menus (cont.)
Back SMYC Forward

Callback

openglexamples/glut/menu.c (Fragment: menuClicked)
 
Rudimentary Menus (cont.)
Back SMYC Forward

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

openglexamples/glut/menu.c (Fragment: menuSetup)
 
There's Always More to Learn
Back -