Open GL
Open GL
to OpenGL Programming
Dave Shreiner
Ed Angel
Vicki Shreiner
What You’ll See Today
Dave Shreiner
OpenGL and GLUT
Overview
What is OpenGL & what can it do for
me?
OpenGL in windowing systems
Why GLUT
A GLUT program template
5
What Is OpenGL?
6
OpenGL Architecture
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Texture
Memory
Pixel
Operations
7
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
Rendering depends on state
• colors, materials, light sources, etc.
8
Related APIs
9
OpenGL and Related APIs
application program
OpenGL Motif
widget or similar GLUT
GLX, AGL
or WGL GLU
10
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
GLUT Basics
Application Structure
• Configure and open window
• Initialize OpenGL state
• Register input callback functions
• render
• resize
• input: keyboard, mouse, etc.
• Enter event processing loop
12
Sample Program
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHTING );
glEnable( GL_DEPTH_TEST );
}
14
GLUT Callback Functions
Do
Do all
all of
of your
your drawing
drawing here
here
glutDisplayFunc(
glutDisplayFunc( display
display );
);
void
void display(
display( void
void ))
{{
glClear(
glClear( GL_COLOR_BUFFER_BIT
GL_COLOR_BUFFER_BIT );
);
glBegin(
glBegin( GL_TRIANGLE_STRIP
GL_TRIANGLE_STRIP );
);
glVertex3fv(
glVertex3fv( v[0]
v[0] );
);
glVertex3fv(
glVertex3fv( v[1]
v[1] );
);
glVertex3fv(
glVertex3fv( v[2]
v[2] );
);
glVertex3fv(
glVertex3fv( v[3]
v[3] );
);
glEnd();
glEnd();
glutSwapBuffers();
glutSwapBuffers();
}}
16
Idle Callbacks
17
User Input Callbacks
Process
Process user
user input
input
glutKeyboardFunc(
glutKeyboardFunc( keyboard
keyboard );
);
void
void keyboard(
keyboard( unsigned
unsigned char
char key,
key, int
int x,
x, int
int yy ))
{{
switch(
switch( key
key )) {{
case
case ‘q’
‘q’ :: case
case ‘Q’
‘Q’ ::
exit(
exit( EXIT_SUCCESS
EXIT_SUCCESS ););
break;
break;
case
case ‘r’
‘r’ :: case
case ‘R’
‘R’ ::
rotate
rotate == GL_TRUE;
GL_TRUE;
glutPostRedisplay();
glutPostRedisplay();
break;
break;
}}
}}
18
Elementary Rendering
Vicki Shreiner
Elementary Rendering
Geometric
Geometric Primitives
Primitives
Managing
Managing OpenGL
OpenGL State
State
OpenGL
OpenGL Buffers
Buffers
20
OpenGL Geometric
Primitives
All
All geometric
geometric primitives
primitives are
are specified
specified
by
by vertices
vertices
GL_LINES
GL_POINTS GL_POLYGON
GL_LINE_STRIP GL_LINE_LOOP
GL_TRIANGLES
GL_TRIANGLE_STRIP GL_QUADS GL_QUAD_STRIP
GL_TRIANGLE_FAN
21
Simple Example
void
void drawRhombus(
drawRhombus( GLfloat
GLfloat color[]
color[] ))
{{
glBegin(
glBegin( GL_QUADS
GL_QUADS );
);
glColor3fv(
glColor3fv( color
color );
);
glVertex2f(
glVertex2f( 0.0,
0.0, 0.0
0.0 );
);
glVertex2f(
glVertex2f( 1.0,
1.0, 0.0
0.0 );
);
glVertex2f(
glVertex2f( 1.5,
1.5, 1.118
1.118 );
);
glVertex2f(
glVertex2f( 0.5,
0.5, 1.118
1.118 );
);
glEnd();
glEnd();
}}
22
OpenGL Command Formats
glVertex3fv( v )
23
Specifying Geometric
Primitives
Primitives are specified using
glBegin( primType );
glEnd();
• primType determines how vertices are
combined
GLfloat red, green, blue;
Glfloat coords[3];
glBegin( primType );
for ( i = 0; i < nVerts; ++i ) {
glColor3f( red, green, blue );
glVertex3fv( coords );
}
glEnd();
24
OpenGL Color Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Models
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
RGBA
RGBA or
or Color
Color Index
Index
color index mode
Red Green Blue
0
1 1 Display
2
4 2
8 3
16
24 123 219 74
25
26
RGBA mode
25
Shapes Tutorial
26
Controlling Rendering
Appearance
From
From
Wireframe
Wireframe
to
to Texture
Texture
Mapped
Mapped
27
OpenGL’s State Machine
All
All rendering
rendering attributes
attributes are
are
encapsulated
encapsulated in in the
the OpenGL
OpenGL State
State
• rendering
rendering styles
styles
• shading
shading
• lighting
lighting
• texture
texture mapping
mapping
28
Manipulating OpenGL
State
Appearance
Appearance is
is controlled
controlled by
by current
current
state
state
for
for each
each (( primitive
primitive to
to render
render )) {
{
update
update OpenGL
OpenGL state
state
render
render primitive
primitive
}
}
Manipulating
Manipulating vertex
vertex attributes
attributes is
is most
most
common
common wayway to
to manipulate
manipulate state
state
glColor*()
glColor*() // glIndex*()
glIndex*()
glNormal*()
glNormal*()
glTexCoord*()
glTexCoord*()
29
Controlling current state
Setting
Setting State
State
glPointSize(
glPointSize( size
size );
);
glLineStipple(
glLineStipple( repeat
repeat,, pattern
pattern );
);
glShadeModel(
glShadeModel( GL
GL__SMOOTH
SMOOTH );
);
Enabling
Enabling Features
Features
glEnable(
glEnable( GL
GL__LIGHTING
LIGHTING );
);
glDisable(
glDisable( GL_TEXTURE_2D
GL_TEXTURE_2D );
);
30
Transformations
Ed Angel
Transformations in
OpenGL
Modeling
Viewing
• orient camera
• projection
Animation
Map to screen
32
Camera Analogy
camera
tripod model
33
Camera Analogy and
Transformations
Projection transformations
• adjust the lens of the camera
Viewing transformations
• tripod–define position and orientation of the viewing
volume in the world
Modeling transformations
• moving the model
Viewport transformations
• enlarge or reduce the physical photograph
34
Coordinate Systems and
Transformations
Steps in Forming an Image
• specify geometry (world coordinates)
• specify camera (camera coordinates)
• project (window coordinates)
• map to viewport (screen coordinates)
Each step uses transformations
Every transformation is equivalent to a
change in coordinate systems (frames)
35
Affine Transformations
36
Homogeneous Coordinates
x
y
v
z
w
• w is usually 1.0
• all operations are matrix multiplications
• directions (directed line segments) can be represented with
w = 0.0
37
3D Transformations
m2 m6 m10 m14
m
3 m 7 m 11 m 15
38
Specifying
Transformations
Programmer has two styles of
specifying transformations
• specify matrices (glLoadMatrix,
glMultMatrix)
• specify operation (glRotate, glOrtho)
glOrtho
Programmer does not have to
remember the exact matrices
• check appendix of Red Book (Programming
Guide)
39
Programming
Transformations
Prior to rendering, view, locate, and
orient:
• eye/camera position
• 3D geometry
Manage the matrices
• including matrix stack
Combine (composite) transformations
40
Transformation Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Pipeline
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
Specify
Specify Current
Current Matrix
Matrix Stack
Stack
glMatrixMode(
glMatrixMode( GL_MODELVIEW
GL_MODELVIEW or
or GL_PROJECTION
GL_PROJECTION ))
Other
Other Matrix
Matrix or
or Stack
Stack Operations
Operations
glLoadIdentity()
glLoadIdentity() glPushMatrix()
glPushMatrix()
glPopMatrix()
glPopMatrix()
Viewport
Viewport
•• usually
usually same
same as
as window
window size
size
•• viewport
viewport aspect
aspect ratio
ratio should
should be
be same
same as
as projection
projection
transformation
transformation or
or resulting
resulting image
image may
may be
be distorted
distorted
glViewport(
glViewport( x,
x, y,
y, width,
width, height
height ))
42
Projection Transformation
Shape
Shape of
of viewing
viewing frustum
frustum
Perspective
Perspective projection
projection
gluPerspective(
gluPerspective( fovy,
fovy, aspect,
aspect, zNear,
zNear, zFar
zFar ))
glFrustum(( left,
glFrustum left, right,
right, bottom,
bottom, top,
top, zNear, zFar )
zNear, zFar )
Orthographic
Orthographic parallel
parallel projection
projection
glOrtho(
glOrtho( left,
left, right,
right, bottom,
bottom, top,
top, zNear,
zNear, zFar
zFar ))
gluOrtho2D(
gluOrtho2D( left,
left, right,
right, bottom,
bottom, top
top ))
••calls glOrtho with
calls glOrtho with zz values
values near
near zero
zero
43
Applying Projection
Transformations
Typical
Typical use
use (orthographic
(orthographic projection)
projection)
glMatrixMode(
glMatrixMode( GL_PROJECTION
GL_PROJECTION );
);
glLoadIdentity();
glLoadIdentity();
glOrtho(
glOrtho( left,
left, right,
right, bottom,
bottom, top,
top, zNear,
zNear, zFar
zFar );
);
44
Viewing Transformations
45
Projection Tutorial
46
Modeling Transformations
Move object
glTranslate{fd}( x, y, z )
x y z
Rotate object around arbitrary axis
glRotate{fd}( angle, x, y, z )
• angle is in degrees
Dilate (stretch or shrink) or mirror
object
glScale{fd}( x, y, z )
47
Transformation Tutorial
48
Connection: Viewing and
Modeling
Moving camera is equivalent to
moving every object in the world
towards a stationary camera
Viewing transformations are
equivalent to several modeling
transformations
gluLookAt() has its own command
can make your own polar view or pilot view
49
Projection is left handed
Projection transformations
(gluPerspective, glOrtho) are left
handed
• think of zNear and zFar as distance from
view point
Everything else is right handed, y
y
including the z+
vertexes to be
left handed right handed
rendered x
x
z+
50
Common Transformation
Usage
3
3 examples
examples of resize() routine
of resize() routine
• restate
restate projection
projection &
& viewing
viewing transformations
transformations
Usually
Usually called
called when
when window
window resized
resized
Registered
Registered as
as callback
callback for
for
glutReshapeFunc
glutReshapeFunc()
()
51
resize(): Perspective &
LookAt
void
void resize(
resize( int
int w,
w, int
int hh ))
{{
glViewport(
glViewport( 0,
0, 0,
0, (GLsizei)
(GLsizei) w, w, (GLsizei)
(GLsizei) hh );
);
glMatrixMode(
glMatrixMode( GL_PROJECTION
GL_PROJECTION ); );
glLoadIdentity();
glLoadIdentity();
gluPerspective(
gluPerspective( 65.0,
65.0, (GLdouble)
(GLdouble) ww // h,
h,
1.0,
1.0, 100.0
100.0 ););
glMatrixMode(
glMatrixMode( GL_MODELVIEW
GL_MODELVIEW ); );
glLoadIdentity();
glLoadIdentity();
gluLookAt(
gluLookAt( 0.0,
0.0, 0.0,
0.0, 5.0,
5.0,
0.0,
0.0, 0.0,
0.0, 0.0,
0.0,
0.0,
0.0, 1.0,
1.0, 0.0
0.0 );
);
}}
52
resize(): Perspective &
Translate
Same
Same effect
effect as
as previous
previous LookAt
LookAt
void
void resize(
resize( int
int w,
w, int
int hh ))
{{
glViewport(
glViewport( 0,
0, 0,
0, (GLsizei)
(GLsizei) w, w, (GLsizei)
(GLsizei) hh );
);
glMatrixMode(
glMatrixMode( GL_PROJECTION
GL_PROJECTION ); );
glLoadIdentity();
glLoadIdentity();
gluPerspective(
gluPerspective( 65.0,
65.0, (GLdouble)
(GLdouble) w/h,
w/h,
1.0,
1.0, 100.0
100.0 ););
glMatrixMode(
glMatrixMode( GL_MODELVIEW
GL_MODELVIEW ); );
glLoadIdentity();
glLoadIdentity();
glTranslatef(
glTranslatef( 0.0,
0.0, 0.0,
0.0, -5.0
-5.0 );
);
}}
53
resize(): Ortho (part 1)
void
void resize(
resize( int
int width,
width, int
int height
height ))
{{
GLdouble
GLdouble aspect
aspect == (GLdouble)
(GLdouble) width
width // height;
height;
GLdouble
GLdouble left
left == -2.5,
-2.5, right
right == 2.5;
2.5;
GLdouble
GLdouble bottom
bottom == -2.5,
-2.5, top
top == 2.5;
2.5;
glViewport(
glViewport( 0,
0, 0,
0, (GLsizei)
(GLsizei) w,w, (GLsizei)
(GLsizei) hh );
);
glMatrixMode(
glMatrixMode( GL_PROJECTION
GL_PROJECTION ););
glLoadIdentity();
glLoadIdentity();
…
… continued
continued …
…
54
resize(): Ortho (part 2)
55
Compositing Modeling
Transformations
Problem 1: hierarchical objects
• one position depends upon a previous position
• robot arm or hand; sub-assemblies
Solution 1: moving local coordinate
system
• modeling transformations move coordinate system
• post-multiply column-major matrices
• OpenGL post-multiplies matrices
56
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
Solution 2: fixed coordinate system
• modeling transformations move objects around fixed
coordinate system
• pre-multiply column-major matrices
• OpenGL post-multiplies matrices
• must reverse order of operations to achieve desired
effect
57
Additional Clipping Planes
58
Reversing Coordinate
Projection
Screen space back to world space
glGetIntegerv( GL_VIEWPORT, GLint viewport[4] )
glGetDoublev( GL_MODELVIEW_MATRIX, GLdouble mvmatrix[16] )
glGetDoublev( GL_PROJECTION_MATRIX,
GLdouble projmatrix[16] )
gluUnProject( GLdouble winx, winy, winz,
mvmatrix[16], projmatrix[16],
GLint viewport[4],
GLdouble *objx, *objy, *objz )
Vicki Shreiner
Animation and Depth
Buffering
Discuss double buffering and
animation
Discuss hidden surface removal
using the depth buffer
61
Double Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Buffering
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
1 1
2 2
4 4
Front 8 8 Back
16 16
Buffer Buffer
Display
62
Animation Using Double
Buffering
Request
Request a
a double
double buffered
buffered color
color buffer
buffer
glutInitDisplayMode(
glutInitDisplayMode( GLUT_RGB
GLUT_RGB || GLUT_DOUBLE
GLUT_DOUBLE );
);
Clear
Clear color
color buffer
buffer
glClear(
glClear( GL_COLOR_BUFFER_BIT
GL_COLOR_BUFFER_BIT );
);
Render
Render scene
scene
Request
Request swap
swap of
of front
front and
and back
back buffers
buffers
glutSwapBuffers();
glutSwapBuffers();
Repeat
Repeat steps
steps 2
2 -- 4
4 for
for animation
animation
63
Depth Buffering and
Hidden Surface Removal
1 1
2 2
4 4
Color 8 8 Depth
16 16
Buffer Buffer
Display
64
Depth Buffering Using
OpenGL
Request
Request a
a depth
depth buffer
buffer
glutInitDisplayMode(
glutInitDisplayMode( GLUT_RGB
GLUT_RGB ||
GLUT_DOUBLE
GLUT_DOUBLE || GLUT_DEPTH
GLUT_DEPTH );
);
Enable
Enable depth
depth buffering
buffering
glEnable(
glEnable( GL_DEPTH_TEST
GL_DEPTH_TEST );
);
Clear
Clear color
color and
and depth
depth buffers
buffers
glClear(
glClear( GL_COLOR_BUFFER_BIT
GL_COLOR_BUFFER_BIT ||
GL_DEPTH_BUFFER_BIT
GL_DEPTH_BUFFER_BIT );
);
Render
Render scene
scene
Swap
Swap color
color buffers
buffers
65
An Updated Program
Template
void
void main(
main( int
int argc,
argc, char**
char** argv
argv ))
{{
glutInit(
glutInit( &argc,
&argc, argv
argv );
);
glutInitDisplayMode(
glutInitDisplayMode( GLUT_RGB
GLUT_RGB ||
GLUT_DOUBLE
GLUT_DOUBLE || GLUT_DEPTH
GLUT_DEPTH ););
glutCreateWindow(
glutCreateWindow( “Tetrahedron”
“Tetrahedron” ););
init();
init();
glutIdleFunc(
glutIdleFunc( idle
idle );
);
glutDisplayFunc(
glutDisplayFunc( display
display );
);
glutMainLoop();
glutMainLoop();
}}
66
An Updated Program
Template (cont.)
void
void init(
init( void
void ))
{{
glClearColor(
glClearColor( 0.0,
0.0, 0.0,
0.0, 1.0,
1.0, 1.0
1.0 );
);
}}
void
void idle(
idle( void
void ))
{{
glutPostRedisplay();
glutPostRedisplay();
}}
67
An Updated Program
Template (cont.)
void
void drawScene(
drawScene( void
void ))
{{
GLfloat
GLfloat vertices[]
vertices[] == {{ …… };
};
GLfloat
GLfloat colors[]
colors[] == {{ …… };
};
glClear(
glClear( GL_COLOR_BUFFER_BIT
GL_COLOR_BUFFER_BIT ||
GL_DEPTH_BUFFER_BIT
GL_DEPTH_BUFFER_BIT ); );
glBegin(
glBegin( GL_TRIANGLE_STRIP
GL_TRIANGLE_STRIP ); );
/*
/* calls
calls to
to glColor*()
glColor*() andand glVertex*()
glVertex*() */
*/
glEnd();
glEnd();
glutSwapBuffers();
glutSwapBuffers();
}}
68
Lighting
Vicki Shreiner
Lighting Principles
71
Surface Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Normals
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
72
Material Properties
74
Light Sources (cont.)
Light
Light color
color properties
properties
• GL_AMBIENT
GL_AMBIENT
• GL_DIFFUSE
GL_DIFFUSE
• GL_SPECULAR
GL_SPECULAR
75
Types of Lights
76
Turning on the Lights
77
Light Material Tutorial
78
Controlling a Light’s
Position
Modelview matrix affects a light’s
position
• Different effects based on when position is
specified
• eye coordinates
• world coordinates
• model coordinates
• Push and pop matrices to uniquely control a
light’s position
79
Light Position Tutorial
80
Advanced Lighting
Features
Spotlights
• localize lighting affects
•GL_SPOT_DIRECTION
•GL_SPOT_CUTOFF
•GL_SPOT_EXPONENT
81
Advanced Lighting
Features
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
82
Light Model Properties
83
Tips for Better Lighting
Dave Shreiner
Imaging and Raster
Primitives
Describe OpenGL’s raster primitives:
bitmaps and image rectangles
Demonstrate how to get OpenGL to
read and render pixel rectangles
86
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
OpenGL doesn’t understand image
formats
87
Per
Poly. Per
Poly. Vertex
Pixel Pipeline
Vertex
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
Texture glCopyTex*Image();
Memory
glReadPixels(), glCopyPixels()
Positioning Image
Primitives
glRasterPos3f( x, y, z )
• raster position transformed like geometry
• discarded if raster position
is outside of viewport
• may need to fine tune
viewport for desired
results
Raster Position
89
Rendering Bitmaps
height
• advance raster position by
xmove ymove after yorig
rendering width
xorig
xmove
90
Rendering Fonts using
Bitmaps
OpenGL uses bitmaps for font
rendering
• each character is stored in a display list
containing a bitmap
• window system specific routines to access
system fonts
•glXUseXFont()
•wglUseFontBitmaps()
91
Rendering Images
92
Reading Pixels
glPixelZoom(
glPixelZoom( x,
x, yy ))
• expand,
expand, shrink
shrink or
or reflect
reflect pixels
pixels
around
around current
current raster
raster position
position
• fractional
fractional zoom
zoom supported
supported
Raster glPixelZoom(1.0, -1.0);
Position
94
Storage and Transfer
Modes
Storage modes control accessing
memory
• byte alignment in host memory
• extracting a subimage
Transfer modes allow modify pixel
values
• scale and bias pixel component values
• replace colors using pixel maps
95
Texture Mapping
Ed Angel
Texture Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Mapping
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
Apply
Apply a a 1D,
1D, 2D,
2D, or
or 3D3D image
image to
to
geometric
geometric
primitives
primitives
Uses
Uses of of Texturing
Texturing
• simulating
simulating materials
materials
• reducing
reducing geometric
geometric complexity
complexity
• image
image warping
warping
• reflections
reflections 97
Texture Mapping
z x
geometry screen
t
image
s 98
Texture Mapping and the
OpenGL Pipeline
Images and geometry flow through
separate pipelines that join at the
rasterizer
• “complex” textures do not affect geometric
complexity
vertices geometry pipeline
rasterizer
image pixel pipeline
99
Texture Example
100
Applying Textures I
Three steps
specify texture
• read or generate image
• assign to texture
• enable texturing
assign texture coordinates to vertices
specify texture parameters
• wrapping, filtering
101
Applying Textures II
•• specify
specify textures
textures inin texture
texture objects
objects
•• set
set texture
texture filter
filter
•• set
set texture
texture function
function
•• set
set texture
texture wrap
wrap mode
mode
•• set
set optional
optional perspective
perspective correction
correction hint
hint
•• bind
bind texture
texture object
object
•• enable
enable texturing
texturing
•• supply
supply texture
texture coordinates
coordinates for
for vertex
vertex
•• coordinates
coordinates can
can also
also be
be generated
generated
102
Texture Objects
103
Texture Objects (cont.)
104
Specify Texture Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Image
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
107
Mapping a Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Texture
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
c (0.4, 0.2)
b
B C
0, 0 1, 0 s (0.8, 0.4)
108
Generating Texture
Coordinates
Automatically generate texture coords
glTexGen{ifd}[v]()
specify a plane
• generate texture coordinates based upon distance
Ax By Cz
from plane D 0
generation modes
• GL_OBJECT_LINEAR
• GL_EYE_LINEAR
• GL_SPHERE_MAP
109
Tutorial: Texture
110
Texture Application
Methods
Filter Modes
• minification or magnification
• special mipmap minification filters
Wrap Modes
• clamping or repeating
Texture Functions
• how to mix primitive’s color with texture’s color
• blend, modulate or replace texels
111
Filter Modes
Example:
glTexParameteri( target, type, mode );
112
Mipmapped Textures
113
Wrapping Mode
Example:
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP )
glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT )
s
GL_REPEAT GL_CLAMP
texture wrapping wrapping
114
Texture Functions
116
Is There Room for a
Texture?
Query largest dimension of texture image
• typically largest square texture
• doesn’t consider internal format size
glGetIntegerv( GL_MAX_TEXTURE_SIZE, &size )
Texture proxy
• will memory accommodate requested texture size?
• no image specified; placeholder
• if texture won’t fit, texture state variables set to 0
• doesn’t know about other textures
• only considers whether this one texture will fit all of memory
117
Texture Residency
118
Advanced OpenGL Topics
Dave Shreiner
Advanced OpenGL Topics
121
Immediate Mode versus
Display Lists
Immediate Mode
Per Vertex
Polynomial Operations &
Evaluator Primitive
Assembly
Display Listed
Texture
Memory
Pixel
Operations
122
Per
Poly. Per
Poly. Vertex
Display Lists
Vertex
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
125
Advanced Primitives
Vertex
Vertex Arrays
Arrays
Bernstein
Bernstein Polynomial
Polynomial Evaluators
Evaluators
• basis
basis for
for GLU
GLU NURBS
NURBS
••NURBS
NURBS (Non-Uniform
(Non-Uniform Rational
Rational B-Splines)
B-Splines)
GLU
GLU Quadric
Quadric Objects
Objects
• sphere
sphere
• cylinder
cylinder (or
(or cone)
cone)
• disk
disk (circle)
(circle)
126
Vertex Poly.
Poly.
Per
Per
Vertex
Vertex
Frag FB
CPU DL Raster
Arrays
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
Pass
Pass arrays
arrays of
of vertices,
vertices, colors,
colors, etc.
etc. toto
Color Vertex
OpenGL
OpenGL inin a
a large
large chunk
chunk data data
glVertexPointer(
glVertexPointer( 3,
3, GL_FLOAT,
GL_FLOAT, 0, coords ))
0, coords
glColorPointer
glColorPointer(( 4,
4, GL_FLOAT,
GL_FLOAT, 0, colors ))
0, colors
glEnableClientState
glEnableClientState(( GL_VERTEX_ARRAY
GL_VERTEX_ARRAY ))
glEnableClientState
glEnableClientState(( GL_COLOR_ARRAY
GL_COLOR_ARRAY ))
glDrawArrays(
glDrawArrays( GL_TRIANGLE_STRIP,
GL_TRIANGLE_STRIP, 0, numVerts );
0, numVerts );
All
All active
active arrays
arrays are
are used
used in
in rendering
rendering
127
Why use Display Lists or
Vertex Arrays?
May provide better performance than
immediate mode rendering
Display lists can be shared between
multiple OpenGL context
• reduce memory usage for multi-context
applications
Vertex arrays may format data for
better memory access
128
Alpha: the 4th Color
Component
Measure of Opacity
• simulate translucent objects
• glass, water, etc.
• composite images
• antialiasing
• ignored if blending is not enabled
glEnable( GL_BLEND )
129
Per
Poly. Per
Poly. Vertex
Blending
Vertex
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
130
Multi-pass Rendering
131
Antialiasing
135
Full Scene Antialiasing :
Jittering the view
Each time we move the viewer, the
image shifts
• Different aliasing artifacts in each image
• Averaging images using accumulation
buffer averages out
these artifacts
136
Depth of Focus : Keeping a
Plane in Focus
Jitter the viewer to keep one plane
unchanged Back Plane
Focal Plane
Front Plane
138
Fog Tutorial
139
Feedback Mode
140
Selection Mode
142
Picking
143
Picking Template
glutMouseFunc( pickMe );
144
Picking Template (cont.)
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluPickMatrix( (GLdouble) x, (GLdouble)
(myViewport[3]-y), 5.0, 5.0, myViewport );
/* gluPerspective or glOrtho or other projection */
glPushName( 1 );
/* draw something */
glLoadName( 2 );
/* draw something else … continue … */
145
Picking Template (cont.)
glMatrixMode( GL_PROJECTION );
glPopMatrix();
hits = glRenderMode( GL_RENDER );
/* process nameBuffer */
}
146
Picking Ideas
147
Getting to the Framebuffer
Fragment
Scissor
Scissor Alpha
Alpha Stencil
Stencil
Test
Test Test
Test Test
Test
Framebuffer
Depth
Depth Logical
Logical
Blending
Blending Dithering
Dithering
Test
Test Operations
Operations
148
Scissor Box
149
Per
Poly. Per
Poly. Vertex
Alpha Test
Vertex
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
150
Per
Poly. Per
Poly. Vertex
Stencil Buffer
Vertex
CPU DL Raster Frag FB
CPU DL Raster Frag FB
Texture
Texture
Pixel
Pixel
151
Controlling Stencil Buffer
152
Creating a Mask
glInitDisplayMode( …|GLUT_STENCIL|… );
glEnable( GL_STENCIL_TEST );
glClearStencil( 0x0 );
153
Using Stencil Mask
154
Dithering
glEnable( GL_DITHER )
Dither colors for better looking
results
• Used to simulate more available colors
155
Logical Operations on
Pixels
Combine pixels using bitwise logical
operations
glLogicOp( mode )
• Common modes
•GL_XOR
•GL_AND
156
Advanced Imaging
Imaging Subset
• Only available if GL_ARB_imaging defined
• Color matrix
• Convolutions
• Color tables
• Histogram
• MinMax
• Advanced Blending
157
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 Paul’s 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!
159
Books
161