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()
Photorealism
Perception
Color
Colors in the physical world can be any wavelength, or combination
of wavelengths, of light
Color | Wavelength |
Violet | 420 nm
|
Blue | 470 nm
|
Green | 530 nm
|
Yellow | 580 nm
|
Orange | 620 nm
|
Red | 700 nm
|
The Eye
Rods & cones absorb light, send signal to brain
Color Perception
Any visible wavelength is perceived the same as some
combination of 3 basic colors
(roughly blue, green, and red)
RGB Color
RGB = Red , Green , Blue
Each component (R, G, or B), ranges from a minimum (no intensity)
to a maximum (full intensity), typically 0.0 to 1.0.
Color on the Computer
glColor3f(1.0, 0.5, 0.0)
Computer numbers have a finite resolution - how many distinct values can be represented
24 bit color = 8 bits red + 8 bits green + 8 bits blue
(a.k.a. 8 bits per component)
8 bits = 256 possible values
32 bit color usually means 8 bits red + 8 bits green + 8 bits blue + 8 bits alpha
16 bit color can be 5 bits red + 6 bits green + 5 bits blue
HDRI: High Dynamic Range Imaging - uses 16 or 32 bits per component
RGB Photography
Prokudin-Gorskii, ca. 1910
RGB Photography
CMY Color
CMY = Cyan , Magenta , Yellow
C = 1.0 - R
M = 1.0 - G
Y = 1.0 - B
CMYK = Cyan , Magenta , Yellow , Black
HSV Color
HSV = Hue , Saturation , Value
Color Gamuts
Luminance
The "brightness" of a color.
Formula, used in NTSC television standard, based on human perception:
0.30 * R + 0.59 * G + 0.11 * B
Real-world Luminance
Background | Luminance |
Moonless overcast night sky | 0.00003 cd/m^2 |
Moonlit clear night sky | 0.03 |
Twighlight sky | 3 |
Overcast day sky | 300 |
Day sky with sunlit clouds | 30,000 |
Rods & cones adapt to average level of illumination
Rods most sensitive at low levels (scotopic vision)
Cones more sensitive at higher levels (photopic vision)

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