GLUT

OpenGL Utility Toolkit

GLUT provides a simple interface for opening & managing windows, and getting input from the keyboard & mouse.

It also defines a basic program structure - an event loop, with callback functions.


GLUT documentation






GLUT Framework






GLUT Events

Events in GLUT include:






GLUT Functions - Setup

glutInit(sys.argv)
Initialize GLUT
sys.argv is the command-line arguments


glutInitDisplayMode(mode)
Tell GLUT what GL-related options the window should have
mode is a bitwise-or'ed (the | operator) subset of: (other options exist, but we won't be using them in this class)


glutInitWindowSize(x,y)
Set the initial size of the window (in pixels)


glutInitWindowPosition(x,y)
Set the window's initial location on the screen (in pixels, from the upper left corner)


glutCreateWindow(title)
Tell GLUT to create the window. The window will not actually appear immediately.
title is the name of the window.





GLUT Functions - Callbacks

glutDisplayFunc(function)
Gives the name of a function to call whenever the graphics must be drawn.


glutKeyboardFunc(function)
Gives the name of a function to call whenever a key is pressed.
function takes 3 arguments - key, x, y. key is the key that was pressed; x & y are the position of the mouse when the key was pressed.


glutKeyboardUpFunc(function)
Gives the name of a function to call whenever a key is released.
function takes 3 arguments - key, x, y. key is the key that was released; x & y are the position of the mouse when the key was pressed.


glutSpecialFunc(function)
Gives the name of a function to call whenever a 'special' key is pressed.
function takes 3 arguments - key, x, y. key is the key that was pressed, e.g. GLUT_KEY_UP, GLUT_KEY_HOME; x & y are the position of the mouse when the key was pressed.


glutIdleFunc(function)
Gives the name of a function to call when there are no events left to be handled.





GLUT Functions - Other

glutMainLoop()
Starts GLUT's event loop. This function never returns, so code after it will not be executed.


glutPostRedisplay()
Generates a "window needs redrawing" event, forcing GLUT to call your draw function
This can be called from an event callback function, to force redrawing when something changes. If called from the idle function, the window will be constantly redrawn.





GLUT Example

# Minimal program to open & clear a window.
# This program is intended to introduce GLUT.

import sys
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *


def draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glFlush()

def keyboard(key, x, y):
    if key == 'q':
        sys.exit(0)
    elif key == ' ':
        print 'Mouse is at', x, y

glutInit([])
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(200, 200)
glutInitWindowPosition(0,0)
glutCreateWindow(sys.argv[0])
glutDisplayFunc(draw)
glutKeyboardFunc(keyboard)
glutMainLoop()





glClearColor(red, green, blue, alpha)
Sets the color that glClear will fill the window with
Arguments should each be in the range 0.0 to 1.0



Create a Python program to do the following: