Hardware






Graphics APIs (& other systems)






Graphics APIs

API = Application Programming Interface

Python, C++, Java, Fortran, etc are languages

OpenGL, Direct3D, etc are graphics APIs

A graphics API can be used with any language, if there's a binding






Lower-Level APIs

Far Cry 2 by UbiSoft

OpenGL, Direct3D

Very basic commands for things like:

Most data management handled by application






Scene Graph APIs

A scene graph is a tree used to represent a graphical scene

"Nodes" represent things like groupings, transformations, lights, geometry






Scene Graph APIs

SGI's Performer Town

Common features:






Visual Programming

Instead of writing code, plug together higher-level components






OpenGL

OpenGL uses the model of a pipeline
Commands and data are fed into the pipeline, processing is done, and the result (an image) comes out the other end (the display)

OpenGL libraries:
  • GL
  • GLU
  • GLX / WGL / AGL
  • GLUT





GLUT

OpenGL consists of commands related to drawing 2D & 3D objects (e.g. draw a triangle, define material properties, define a texture).

Drawing takes place in some sort of window, controlled by an operating system.

OpenGL avoids including any sort of functions to create or manipulate windows, or to do other user interface tasks (reading keyboard/mouse, etc).

GLUT (GL Utility Toolkit) provides functions for windowing and interaction.
It defines a simple interface that hides the OS-specific details of these tasks.

GLUT functions can be used in the same way under Unix, MacOS, and Windows, making GLUT-based programs more portable.






GLUT Callbacks

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

callback - a function that you provide for other code to call when needed; the "other code" is typically in a library

GLUT uses callbacks for drawing, keyboard & mouse input, and other events.

Whenever the window must be redrawn, your drawing callback is called.
Whenever an input event occurs, your corresponding callback is called.






GLUT Program Structure






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 == chr(27):
        sys.exit(0)

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





OpenGL Functions

GL function names follow a standardized scheme


All begin with "gl", followed by the basic name of the command


When there's only one possible form of arguments:






OpenGL Functions

When arguments can take multiple forms:

e.g. the Color function has the forms:






OpenGL Constants

Constants are defined for certain function arguments

Constant names are all in capital letters, in the form:

e.g. possible arguments for glBegin() are:






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.



glutMouseFunc(function)
Gives the name of a function to call whenever a mouse button is pressed or released.
function takes 4 arguments - button, state, x, y. button is the button that was pressed - GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUFFON; state is either GLUT_DOWN or GLUT_UP; x & y are the position of the mouse when the button's state changed.



glutTimerFunc(msecs, function, value)
Gives the name of a function to call when msecs (or more) milliseconds have elapsed. value is an argument to pass to function.



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.
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()


Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.