Hardware
Graphics APIs (& other systems)
- OpenGL
- Direct3D
- Performer
- OpenInventor
- OpenSceneGraph
- Java3D
- Virtools
- OpenDX
- AVS
- Processing
- Flash
- Javascript <canvas>
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
OpenGL, Direct3D
Very basic commands for things like:
- Drawing shapes (points, lines, triangles)
- Setting colors
- Setting positions, rotations, sizes
- Defining a view
- Loading textures
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
Common features:
- Standard data structures for geometry & graphics state
- Automatic, optimized rendering
- View culling
- Level of detail
- Model loaders
- Math: vectors, matrices, quaternions
- Intersection testing
- Multiprocessing
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:
- glColor3f - 3 floating point arguments
- glColor3i - 3 integer arguments
- glColor3ub - 3 unsigned byte arguments
- glColor3fv - 1 argument, a vector of 3 floating point numbers
- etc.
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:
- GL_POINTS
- GL_LINES
- GL_TRIANGLES
- GL_TRIANGLE_STRIP
- etc.
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:
- Key pressed
- Key released
- Mouse button pressed
- Mouse button released
- Mouse moved
- Mouse entered/left window
- Window resized
- Window needs redrawing
- Menu selection
- Special devices - tablet, spaceball, dial/buttonbox
- Timer elapsed
- Idle (no events pending)
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:
- GLUT_SINGLE - single-buffered
- GLUT_DOUBLE - double-buffered
- GLUT_RGB - use RGB color
- GLUT_INDEX - use color indexing
- GLUT_DEPTH - allow depth-buffering
(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()
This document is by Dave Pape, and is released under a Creative Commons License.