Академический Документы
Профессиональный Документы
Культура Документы
OpenGL Programming
Dave Shreiner
Ed Angel
Vicki Shreiner
22
55
What Is OpenGL?
66
OpenGL Architecture
Polynomial
Evaluator
CPU
Display
List
Per Vertex
Operations &
Primitive
Assembly
Rasterization
Texture
Memory
Pixel
Operations
77
Per Fragment
Operations
Frame
Buffer
OpenGL as a Renderer
Geometric primitives
points, lines and polygons
Image Primitives
images and bitmaps
separate pipeline for images and geometry
linked through texture mapping
Related APIs
GLX, AGL
or WGL
GLUT
GLU
GL
11
00
Preliminaries
Headers Files
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
Libraries
Enumerated Types
OpenGL defines numerous types for
compatibility
GLfloat, GLint, GLenum, etc.
11
11
GLUT Basics
Application Structure
Configure and open window
Initialize OpenGL state
Register input callback functions
render
resize
input: keyboard, mouse, etc.
Sample Program
void main( int argc, char** argv )
{
int mode = GLUT_RGB|GLUT_DOUBLE;
glutInitDisplayMode( mode );
glutCreateWindow( argv[0] );
init();
glutDisplayFunc( display );
glutReshapeFunc( resize );
glutKeyboardFunc( key );
glutIdleFunc( idle );
glutMainLoop();
}
11
33
OpenGL Initialization
Rendering Callback
Idle Callbacks
11
77
11
88
Elementary Rendering
Vicki Shreiner
Elementary Rendering
Geometric Primitives
Managing OpenGL State
OpenGL Buffers
22
00
GL_POINTS
GL_LINE_LOOP
GL_POLYGON
GL_TRIANGLES
GL_QUADS
GL_QUAD_STRIP
22 GL_TRIANGLE_STRIP
11
GL_TRIANGLE_FAN
Simple Example
void drawRhombus( GLfloat color[] )
{
glBegin( GL_QUADS );
glColor3fv( color );
glVertex2f( 0.0, 0.0 );
glVertex2f( 1.0, 0.0 );
glVertex2f( 1.5, 1.118 );
glVertex2f( 0.5, 1.118 );
glEnd();
}
22
22
Number of
components
2 - (x,y)
3 - (x,y,z)
4 - (x,y,z,w)
22
33
Data Type
b
ub
s
us
i
ui
f
d
byte
unsigned byte
short
unsigned short
int
unsigned int
float
double
Vector
omit v for
scalar form
glVertex2f( x, y )
22
44
OpenGL Color
Models
Poly.
CPU
Per
Vertex
Raster
DL
Frag
Texture
Pixel
ww
16
ww
Blue
0
1
2
3
24
25
26
Display
123
www
219
74
www
22
55
RGBA mode
FB
Shapes Tutorial
22
66
Controlling Rendering
Appearance
22
77
22
88
Setting State
glPointSize( size );
glLineStipple( repeat, pattern );
glShadeModel( GL_SMOOTH );
Enabling Features
glEnable( GL_LIGHTING );
glDisable( GL_TEXTURE_2D );
33
00
Transformations
Ed Angel
Transformations in OpenGL
Modeling
Viewing
orient camera
projection
Animation
Map to screen
33
22
Camera Analogy
tripod
33
33
model
Projection transformations
Modeling transformations
Affine Transformations
Homogeneous Coordinates
each vertex is a column vector
x
y
v
z
w
w is usually 1.0
all operations are matrix multiplications
directions (directed line segments) can be
33
77
3D Transformations
A vertex is transformed by 4 x 4 matrices
33
88
M
m2 m6 m10 m14
m
m
m
m
7
11
15
3
Specifying Transformations
Programming Transformations
Transformation
Pipeline
v
e
r
t
e
x
Raster
DL
Frag
FB
Texture
Pixel
normalized
device
clip
window
Modelview
Matrix
Projection
Matrix
Perspective
Division
Modelview
Projection
Modelview
l
l
l
44
11
CPU
eye
object
Per
Vertex
Poly.
Viewport
Transform
material color
shade model (flat)
polygon rendering mode
polygon culling
clipping
Matrix Operations
Specify Current Matrix Stack
glMatrixMode( GL_MODELVIEW or GL_PROJECTION )
Viewport
44
22
Projection Transformation
Applying Projection
Transformations
44
44
Viewing Transformations
44
55
tripod
Projection Tutorial
44
66
Modeling Transformations
Move object
glTranslate{fd}( x, y, z )
angle is in degrees
44
77
Transformation Tutorial
44
88
44
99
z+
left handed
right handed
x
55
00
z+
55
11
55
44
55
55
Compositing Modeling
Transformations
Compositing Modeling
Transformations
Problem 2: objects move relative to absolute
world origin
my object rotates around the wrong origin
make it spin around its center or something else
effect
55
88
GLdouble* coeff )
Reversing Coordinate
Projection
66
11
Double
Buffering
CPU
Raster
DL
FB
Pixel
16
16
Display
66
22
Frag
Texture
Front
Buffer
Per
Vertex
Poly.
Back
Buffer
scene
Request swap of front and back buffers
glutSwapBuffers();
Color
Buffer
16
16
Display
66
44
Depth
Buffer
Render
scene
Swap color buffers
66
55
Lighting
Lighting Principles
Lighting contributors
Surface material properties
Light properties
Lighting model properties
77
11
Surface
Normals
Poly.
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
Material Properties
Base color
GL_SPECULAR
Highlight Color
GL_AMBIENT
Low-light Color
GL_EMISSION
Glow Color
GL_SHININESS
Surface Smoothness
Light Properties
glLightfv( light, property, value );
light specifies which light
properties
colors
position and type
attenuation
77
44
77
55
Types of Lights
77
66
y
y
w
77
77
77
88
eye coordinates
world coordinates
model coordinates
88
00
Spotlights
localize lighting affects
GL_SPOT_DIRECTION
GL_SPOT_CUTOFF
GL_SPOT_EXPONENT
88
11
Light attenuation
decrease light intensity with distance
GL_CONSTANT_ATTENUATION
GL_LINEAR_ATTENUATION
GL_QUADRATIC_ATTENUATION
1
fi
k c kl d k q d 2
88
22
GL_LIGHT_MODEL_COLOR_CONTROL
88
66
Pixel-based primitives
Bitmaps
2D array of bit masks for pixels
update pixel color based on current color
Images
2D array of pixel color information
complete color information for each pixel
Poly.
Pixel Pipeline
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
Pixel
Storage
Modes
Pixel-Transfer
Operations
(and Pixel Map)
Rasterization
(including
Pixel Zoom)
TextureM
emory
Per Fragment
Operations
glCopyTex*Image();
glReadPixels(), glCopyPixels()
8
8
Frame
Buffer
Raster Position
88
99
Rendering Bitmaps
glBitmap( width, height, xorig, yorig, xmove,
ymove, bitmap )
width
xorig
xmove
99
00
glXUseXFont()
wglUseFontBitmaps()
99
11
Rendering Images
glDrawPixels( width, height, format, type,
pixels )
99
22
Reading Pixels
glReadPixels( x, y, width, height, format,
type, pixels )
Pixel Zoom
glPixelZoom( x, y )
99
44
glPixelZoom(1.0, -1.0);
99
55
Texture Mapping
Ed Angel
Texture
Mapping
Poly.
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
99
77
primitives
Uses of Texturing
simulating materials
reducing geometric complexity
image warping
reflections
Texture Mapping
geometry
image
99
88
screen
Texture
Mapping and the
I
m
OpenGL
Pipeline
a
g
e
s
a
n
d
vertices
geometry pipeline
rasterizer
99
99
g
e
image
pixel pipeline
Texture Example
1
010
Applying Textures I
Three steps
specify
texture
specify
texture parameters
wrapping, filtering
1
010
Applying Textures II
1
010
Texture Objects
Like display lists for texture images
1
010
1
010
Specify Texture
Image
Poly.
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
pipeline
pixel scales, biases and lookups can be
done
Specifying a Texture:
Other Methods
Use frame buffer as source of texture image
glCopyTexImage2D(...)
glCopyTexImage1D(...)
1
010
Mapping a
Texture
Poly.
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
Texture Space
1, 1
1
010
b
0, 0
Object Space
(0.4, 0.2)
B
1, 0 s
C
(0.8, 0.4)
specify a plane
generate texture coordinates based upon
distance from
Ax plane
By Cz D 0
generation modes
GL_OBJECT_LINEAR
GL_EYE_LINEAR
1
GL_SPHERE_MAP
010
Tutorial: Texture
1
111
Filter Modes
minification or magnification
special mipmap minification filters
Wrap Modes
clamping or repeating
Texture Functions
how to mix primitives color with textures color
1
111
Filter Modes
Example:
glTexParameteri( target, type, mode );
Texture
Polygon
Magnification
1
111
Texture
Polygon
Minification
Mipmapped Textures
Mipmap allows for prefiltered texture maps of
decreasing resolutions
Lessens interpolation errors for smaller
textured objects
Declare mipmap level during texture definition
glTexImage*D( GL_TEXTURE_*D, level, )
1
111
Wrapping Mode
Example:
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP )
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT )
t
s
texture
1
111
GL_REPEAT
wrapping
GL_CLAMP
wrapping
Texture Functions
GL_TEXTURE_ENV_MODE modes
GL_MODULATE
GL_BLEND
GL_REPLACE
GL_TEXTURE_ENV_COLOR
1
111
GL_DONT_CARE
GL_NICEST
GL_FASTEST
Texture proxy
Texture Residency
Working set of textures
1
212
Polynomial
Evaluator
CPU
Display
List
Per Vertex
Operations &
Primitive
Assembly
Rasterization
Display Listed
1
212
Pixel
Operations
Texture
Memor
y
Per Fragment
Operations
Frame
Buffer
Poly.
Display Lists
CPU
Per
Vertex
Raster
DL
Texture
Pixel
1
212
Frag
FB
Display Lists
Not all OpenGL routines can be stored in
display lists
State changes persist, even after a display list
is finished
Display lists can call other display lists
Display lists are not editable, but you can fake
it
make a list (A) which calls other lists (B, C, and D)
delete and replace B, C, and D, as needed
1
212
glEndList();
1
212
Advanced Primitives
Vertex Arrays
Bernstein Polynomial Evaluators
basis for GLU NURBS
NURBS (Non-Uniform Rational B-Splines)
Vertex
Arrays
Poly.
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
Color Vertex
data
data
Measure of Opacity
simulate translucent objects
glass, water, etc.
composite images
antialiasing
ignored if blending is not enabled
glEnable( GL_BLEND )
1
212
Poly.
Blending
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
Cr src C f dst C p
Blending
Equation
Fragment
(src)
1
313
Framebuffer
Pixel
(dst)
Blended
Pixel
Multi-pass Rendering
1
313
Antialiasing
Accumulation Buffer
operations
within the accumulation buffer: GL_ADD,
GL_MULT
from read buffer: GL_ACCUM, GL_LOAD
transfer back to write buffer: GL_RETURN
Accumulation Buffer
Applications
1
313
Compositing
Full Scene Antialiasing
Depth of Field
Filtering
Motion Blur
1
313
Back Plane
Focal Plane
Front Plane
eye pos1
1
313
eye pos2
Fog
glFog( property, value )
Depth Cueing
Specify a range for a linear fog ramp
GL_FOG_LINEAR
Environmental effects
Simulate more realistic fog
GL_FOG_EXP
GL_FOG_EXP2
1
313
Fog Tutorial
1
313
Feedback Mode
Selection Mode
Picking
Picking Template
glutMouseFunc( pickMe );
glPushName( 1 );
/*
draw something */
glLoadName( 2 );
/*
draw something else continue
1
414
*/
*/
1
414
Picking Ideas
1
414
buffer)
Depth
Test
1
414
Scissor
Test
Blending
Alpha
Test
Dithering
Stencil
Test
Logical
Operations
Framebuffer
Fragment
Scissor Box
1
414
Poly.
Alpha Test
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
1
515
Poly.
Stencil Buffer
CPU
Per
Vertex
Raster
DL
Frag
FB
Texture
Pixel
1
515
Creating a Mask
glInitDisplayMode( |GLUT_STENCIL| );
glEnable( GL_STENCIL_TEST );
glClearStencil( 0x1 );
glStencilFunc( GL_ALWAYS, 0x1, 0x1 );
glStencilOp( GL_REPLACE, GL_REPLACE,
GL_REPLACE );
draw mask
1
515
1
515
Dithering
glEnable( GL_DITHER )
1
515
1
515
Advanced Imaging
Imaging Subset
Only available if GL_ARB_imaging defined
Color matrix
Convolutions
Color tables
Histogram
MinMax
Advanced Blending
1
515
Summary / Q & A
Dave Shreiner
Ed Angel
Vicki Shreiner
On-Line Resources
http://www.opengl.org
start here; up to date specification and lots of sample code
news:comp.graphics.api.opengl
http://www.sgi.com/software/opengl
http://www.mesa3d.org/
Brian Pauls Mesa 3D
http://www.cs.utah.edu/~narobins/opengl.html
very special thanks to Nate Robins for the OpenGL Tutors
source code for tutors available here!
1
515
Books
1
616