Computer Graphics
Lecture 5 - Programming with OpenGL
John Shearer
Culture Lab – space 2
[email protected]
http://di.ncl.ac.uk/teaching/csc3201/
Computer Graphics 1
Objectives
•Development of the OpenGL API
•OpenGL Architecture
–OpenGL as a state machine
•Functions
–Types
–Formats
•Fundamental OpenGL primitives
•Attributes
•Simple program
Computer Graphics 2
Early History of APIs
•IFIPS (1973) formed two committees to come up with
a standard graphics API
–Graphical Kernel System (GKS)
•2D but contained good workstation model
–Core
•Both 2D and 3D
–GKS adopted as IS0 and later ANSI standard (1980s)
•GKS not easily extended to 3D (GKS-3D)
–Far behind hardware development
Computer Graphics 3
PHIGS and X
•Programmers Hierarchical Graphics System (PHIGS)
–Arose from CAD community
–Database model with retained graphics (structures)
•X Window System
–DEC/MIT effort
–Client-server architecture with graphics
•PEX combined the two
–Not easy to use (all the defects of each)
Computer Graphics 4
SGI and GL
•Silicon Graphics (SGI) revolutionized the
graphics workstation by implementing the
pipeline in hardware (1982)
•To access the system, application
programmers used a library called GL
•With GL, it was relatively simple to program
three dimensional interactive applications
Computer Graphics 5
OpenGL
The success of GL lead to OpenGL (1992), a
platform-independent API that was
–Easy to use
–Close enough to the hardware to get excellent
performance
–Focus on rendering
–Omitted windowing and input to avoid window system
dependencies
Computer Graphics 6
OpenGL Evolution
•Originally controlled by an Architectural Review Board (ARB)
–Members included SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,…….
–Relatively stable
•Though recent changes have caused some disruption
•Evolution reflects new hardware capabilities
–3D texture mapping and texture objects
–Vertex programs
–Allows for platform specific features through extensions
–ARB replaced by Kronos
Computer Graphics 7
Kronos
•The Khronos Group was founded in January 2000 by a number
of leading media-centric companies, including 3Dlabs, ATI,
Discreet, Evans & Sutherland, Intel, NVIDIA, SGI and Sun
Microsystems, dedicated to creating open standard APIs to
enable the authoring and playback of rich media on a wide
variety of platforms and devices.
•Standards include:
–OpenGL (2D & 3D graphics)
–COLLADA (digital asset exchange)
–glFX (run-time effects API for OpenGL)
–OpenGL ES (Embedded 3D graphics)
–…
Computer Graphics 8
OpenGL Libraries
•OpenGL core library
–OpenGL32.dll on Windows
–GL on most unix/linux systems (libGL.a)
•OpenGL Utility Library (GLU)
–Provides functionality in OpenGL core but avoids having to
rewrite code
•Links with window system
–GLX for X window systems
–WGL for Windows
–AGL for Macintosh
Computer Graphics 9
GLUT
•OpenGL Utility Toolkit (GLUT)
–Provides functionality common to all window systems
•Open a window
•Get input from mouse and keyboard
•Menus
•Event-driven
–Code is portable but GLUT lacks the functionality of a good
toolkit for a specific platform
•No slide bars
Computer Graphics 10
Software Organization
application program
OpenGL Motif
widget or similar GLUT
GLX, AGL
or WGL GLU
X, Win32, Mac O/S GL
software and/or hardware
Computer Graphics 11
OpenGL Architecture
Immediate Mode geometry
pipeline
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Display Per Fragment Frame
CPU Rasterization
List Operations Buffer
Texture
Memory
Pixel
Pixel
Operations
Operations
Computer Graphics 12
OpenGL Functions
•Primitives
–Points
–Line Segments
–Polygons
•Attributes
•Transformations
–Viewing
–Modeling
•Control (GLUT / org.lwjgl.opengl )
•Input (GLUT / org.lwjgl.input )
•Query
Computer Graphics 13
OpenGL State
•OpenGL is a state machine
•OpenGL functions are of two types
–Primitive generating
•Can cause output if primitive is visible
•How vertices are processed and appearance of primitive are controlled by
the state
–State changing
•Transformation functions
•Attribute functions
Computer Graphics 14
Lack of Object Orientation
•OpenGL is not object oriented so that there are multiple
functions for a given logical function
–glVertex3f – 3 FLOATS
–glVertex2i – 2 INTEGERS
–glVertex3dv
•a vector (pointer to) 3 DOUBLES (in C/C++
•glVertexPointer(int size, int stride, java.nio.FloatBuffer pointer)
•Underlying storage mode is the same
•Easy to create overloaded functions but issue is efficiency
•The OpenGL API is a C API, but in practice most
implementations (NVidia, AMD/ATI) are written in C++.
Computer Graphics 15
OpenGL function format
function name
dimensions
glVertex3f(x,y,z)
x,y,z are floats
belongs to GL library
glVertex3fv(p)
p is a pointer to an array
Computer Graphics 16
OpenGL defines
•Most constants are defined in the package
org.lwjgl.opengl.GL11
•Examples
–glBegin(GL_POLYGON)
–glClear(GL_COLOR_BUFFER_BIT)
•include files or org.lwjgl.opengl.GL11 package
in LWJGL, also define OpenGL data types:
GLfloat, GLdouble,….
Computer Graphics 17
Coordinate Systems
•The units in glVertex are determined by the application
and are called object or problem coordinates
•The viewing specifications are also in object coordinates and
it is the size of the viewing volume that determines what will
appear in the image
•Internally, OpenGL will convert to camera (eye) coordinates
and later to screen coordinates
•OpenGL also uses some internal representations that usually
are not visible to the application
Computer Graphics 18
OpenGL Camera
•OpenGL places a camera at
the origin in object space
pointing in the negative z
direction
•The default viewing volume
is a box centered at the
origin with a side of
length 2
Computer Graphics 19
Orthographic Viewing
•In the default orthographic view, points are
•projected forward along the z axis onto the
•plane z=0
Computer Graphics 20
Transformations and Viewing
•In OpenGL, projection is carried out by a projection
matrix (transformation)
•There is only one set of transformation functions so
we must set the matrix mode first
glMatrixMode (GL_PROJECTION)
• Transformation functions are incremental so we
start with an identity matrix and alter it with a
projection matrix that gives the view volume
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
Computer Graphics 21
Projection matrix & Modelview matrix
•OpenGL uses two matrices (projection & modelview) to make
life simpler. A single matrix could be used (as two matrices can
be multiplied together to create just one).
•In most scenarios the projection the user/programmer wants is
rarely changed, so the projection matrix is used for this and isn't
changed often
•The modelview matrix is used to transform the position,
orientation, etc of objects in the real world and so is changed
very frequently (when objects move, but more importantly, it is
different for each object).
Computer Graphics 22
GL_PROJECTION
•glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1.0, 1.0);
glTranslate( 100, 100, 100 );
glRotateF( 45, 1, 0, 0 );
•Really means:
GL_PROJECTION_MATRIX =
IDENTITY
* ORTHOGRAPHIC_MATRIX
* TRANSLATION_MATRIX
* ROTATION_MATRIX
Computer Graphics 23
GL_MODELVIEW
•glMatrixMode(GL_MODELVIEW);
•glLoadIdentity();
glTranslate( 100, 100, 100 );
glRotateF( 45, 1, 0, 0 );
•glPushMatrix();
•glTranslate( carx, cary, carz );
// draw car here, by specifying various gl vertices
•glPopMatrix();
•glPushMatrix();
•glTranslate( battleshipx, battleshipy, battleshipz );
// draw battleship here, by specifying various gl vertices
•glPopMatrix();
•car_vertice_matrix = projection_ortho_matrix * projection_translation * car_translation_matrix
•battleship_vertice_matrix = projection_ortho_matrix * projection_translation * battleship_translation_matrix
Computer Graphics 24
Two- and three-dimensional viewing
•In glOrtho(left, right, bottom, top, near,
far) the near and far distances are measured from the
camera
•Two-dimensional vertex commands place all vertices in the
plane z=0
•If the application is in two dimensions, we can use the
function
gluOrtho2D(left, right,bottom,top)
•In two dimensions, the view or clipping volume becomes a
clipping window
Computer Graphics 25
OpenGL Primitives
GL_POINTS GL_POLYGON
GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLES
GL_QUAD_STRIP
GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
Any missing?
Computer Graphics 26
Polygon Issues
•OpenGL will only display polygons correctly that are
–Simple: edges cannot cross
–Convex: All points on line segment between two points in a polygon are also in the
polygon
–Flat: all vertices are in the same plane
•User program can check if above true
–OpenGL will produce output if these conditions are violated but it may not be what is
desired
•Triangles satisfy all conditions
nonsimple polygon
nonconvex polygon
Computer Graphics 27
Attributes
•Attributes are part of the OpenGL state and determine
the appearance of objects
–Color (points, lines, polygons)
–Size and width (points, lines)
–Stipple pattern (lines, polygons)
–Polygon mode
•Display as filled: solid color or stipple pattern
•Display edges
•Display vertices
Computer Graphics 28
RGB color
•Each color component is stored separately in the frame
buffer
•Usually 8 bits per component in buffer
•Note in glColor3f the color values range from 0.0 (none)
to 1.0 (all), whereas in glColor3ub the values range from 0
to 255
Computer Graphics 29
Indexed Color
•Colors are indices into tables of RGB values
•Requires less memory
–indices usually 8 bits
–not as important now
•Memory inexpensive
•Need more colors for shading
Computer Graphics 30
Color and State
•The color as set by glColor becomes part of the state and
will be used until changed
–Colors and other attributes are not part of the object but are
assigned when the object is rendered
•We can create conceptual vertex colors by code such as
glColor
glVertex
glColor
glVertex
Computer Graphics 31
Smooth Color
•Default is smooth shading
–OpenGL interpolates vertex colors across visible polygons
•Alternative is flat shading
–Color of first vertex
determines fill color
•glShadeModel
(GL_SMOOTH)
or GL_FLAT
Computer Graphics 32
Viewports
•Do not have use the entire window for the image:
glViewport(x,y,w,h)
•Values in pixels (screen coordinates)
Computer Graphics 33