Title: Computer Graphics 2 Mini Module 2
1Computer Graphics 2Mini Module 2
- Henrik R. Nagel
- hrn_at_cvmt.dk
2Outline
- Texture Mapping
- Rendering Polygons Efficiently
- Display lists
- Vertex arrays
- Rendering Curves and surfaces
- Quadrics
- Evaluators
- Polynomial curves and surfaces
- The OpenGL Rendering Pipeline
3Texture Mapping
4Texture Mapping
- Apply a 1D, 2D, or 3D image to geometric
primitives - Uses of Texturing
- simulating materials
- reducing geometric complexity
- reflections
5Texture Mapping
screen
geometry
image
6Texture Mapping and the OpenGL Pipeline
- Images and geometry flow through separate
pipelines that join at the rasterizer - complex textures do not affect geometric
complexity
7Applying Textures
- Specify a texture image
- read or generate image
- assign to texture
- enable texturing
- Specify texture parameters
- wrapping, filtering
- Assign texture coordinates to vertices
81 Generating a Texture Identifier
- Generate texture names
- glGenTextures( n, texIds )
- Example
- static GLuint texName
- glGenTextures(1, texname)
91 Binding Textures
- Create texture objects with texture data and
state - glBindTexture( target, id )
- Bind textures before using
- glBindTexture( target, id )
- Example
- glBindTexture( GL_TEXTURE_2D, texName )
-
101 Specifying Textures
- Define a texture image from an array in CPU
memory - glTexImage2D( target, level, internalFormat,
width, height, border, format, type, texels ) - dimensions of image must be powers of 2
- Example
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width,
height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image )
111 Specifying LOD
- Mipmap allows for prefiltered texture maps of
decreasing resolutions - Lessens interpolation errors for smaller textured
objects - Declare mipmap level during texture definition
- glTexImageD( GL_TEXTURE_D, level, )
- GLU mipmap builder routines
- gluBuildDMipmaps( )
122 Texture Application Methods
- Filter Modes
- minification or magnification
- special mipmap minification filters
- Wrap Modes
- clamping or repeating
- Texture Functions
- how to mix primitives color with textures color
- blend, modulate or replace texels
132 Filter Modes
Example glTexParameteri( target, type, mode )
142 Wrapping Mode
- Example
- glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_CLAMP ) - glTexParameteri( GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_REPEAT )
152 Texture Functions
- Controls how texture is applied
- glTexEnvfiv( GL_TEXTURE_ENV, prop, param )
- GL_TEXTURE_ENV_MODE modes
- GL_MODULATE
- GL_BLEND
- GL_REPLACE
- Set blend color with GL_TEXTURE_ENV_COLOR
163 Mapping Texture
- Based on parametric texture coordinates
- glTexCoord() specified at each vertex
Texture Space
Object Space
t
1, 1
(s, t) (0.2, 0.8)
0, 1
A
a
(0.4, 0.2)
c
b
B
C
(0.8, 0.4)
s
0, 0
1, 0
17Rendering Polygons Efficiently
18Immediate Mode
- glBegin(GL_TRIANGLES)
- glVertex3f()
- glVertex3f()
- glVertex3f()
- glEnd()
- Easy, flexible, good match for early hardware
- Slow on current hardware
- Bus-limited, API-limited
- Hard to parallelize with strict ordering semantics
19Display Lists
- Fast, but limited
- Immutable
- Requires driver to allocate memory to hold data
- Allows large amount of driver optimization
- Can sometimes be cached in fast memory
- Typically very fast
20Immediate Mode versus Display Listed Rendering
- Immediate Mode Graphics
- Primitives are sent to pipeline and display right
away - No memory of graphical entities
- Display Listed Graphics
- Primitives placed in display lists
- Display lists kept on graphics server
- Can be redisplayed with different state
21Display Lists
- Creating a display list
- GLuint id
- void init( void )
-
- id glGenLists( 1 )
- glNewList( id, GL_COMPILE )
- / other OpenGL routines /
- glEndList()
-
- Call a created list
- void display( void )
-
- glCallList( id )
22Display 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
23Display Lists and Hierarchy
- Consider model of a car
- Create display list for chassis
- Create display list for wheel
- glNewList( CAR, GL_COMPILE )
- glCallList( CHASSIS )
- glTranslatef( )
- glCallList( WHEEL )
- glTranslatef( )
- glCallList( WHEEL )
-
- glEndList()
24Vertex Arrays
- Best of both worlds
- Data can be changed as often as you like
- Data can be interleaved or in separate arrays
- Can use straight lists or indices
- Reduces number of API calls vs. immediate mode
- Little room for driver optimization, since data
referenced by pointers can change at any time
25Vertex Arrays
- List of vertices in an array
- Separate
- Interleaved
- Can render from subarrays
- Compiled vertex arrays
- Indexed vertex arrays
- Bandwidth
- Vertex cache
26Vertex Arrays
- Pass arrays of vertices, colors, etc. to OpenGL
in a large chunk - glVertexPointer( 3, GL_FLOAT, 0, coords )
- glColorPointer( 4, GL_FLOAT, 0, colors )
- glEnableClientState( GL_VERTEX_ARRAY )
- glEnableClientState( GL_COLOR_ARRAY )
- glDrawArrays( GL_TRIANGLE_STRIP, 0, numVerts )
- All active arrays are used in rendering
27Compiled Vertex Arrays
- Solve part of the problem
- Allow user to lock portions of vertex array
- In turn, gives driver more optimization
opportunities - Shared vertices can be detected, allowing driver
to eliminate superfluous operations - Locked data can be copied to higher bandwidth
memory for more efficient transfer to the GPU - Still requires transferring data twice
28Why 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
29RenderingCurves and Surfaces
30What Does OpenGL Support?
- Quadrics
- GLU and GLUT contain polynomial approximations of
quadrics - Evaluators a general mechanism for working with
the Bernstein polynomials - Can use any degree polynomials
- Can use in 1-4 dimensions
- Automatic generation of normals and texture
coordinates
31Quadrics
- Quadrics are in both the GLU and GLUT libraries
- Both use polygonal approximations where the
application specifies the resolution - Sphere lines of longitude and lattitude
- GLU disks, cylinders, spheres
- Can apply transformations to scale, orient, and
position - GLUT Platonic solids, torus, Utah teapot, cone
32GLUT Objects
Each has a wire and a solid form
glutWireCone()
glutWireTorus()
glutWireTeapot()
33GLUT Platonic Solids
glutWireTetrahedron()
glutWireDodecahedron()
glutWireOctahedron()
glutWireIcosahedron()
34Quadric Objects in GLU
- GLU can automatically generate normals and
texture coordinates - Quadrics are objects that include properties such
as how we would like the object to be rendered
disk
partial disk
sphere
35Defining a Cylinder
- GLUquadricOBJ p
- P gluNewQuadric() /set up object /
- gluQuadricDrawStyle(GLU_LINE)/render style/
- gluCylinder(p, BASE_RADIUS, TOP_RADIUS,
- BASE_HEIGHT, sections, slices)
36One-Dimensional Evaluators
- Evaluate a Bernstein polynomial of any degree at
a set of specified values - Can evaluate a variety of variables
- Points along a 2, 3 or 4 dimensional curve
- Colors
- Normals
- Texture Coordinates
- We can set up multiple evaluators that are all
evaluated for the same value
37Setting Up an Evaluator
what we want to evaluate
max and min of u
glMap1f(type,u_min,u_max,stride, order,
pointer_to_array)
separation between data points
1degree of polynomial
pointer to control data
Each type must be enabled by glEnable(type)
38Example
Consider an evaluator for a cubic Bezier curve
over (0,1)
point data .. /3d data
/ glMap1f(GL_MAP_VERTEX_3,0.0,1.0,3,4,data)
cubic
data are 3D vertices
data are arranged as x,y,z,x,y,z three floats
between data points in array
glEnable(GL_MAP_VERTEX_3)
39Evaluating
- The function glEvalCoord1f(u) causes all enabled
evaluators to be evaluated for the specified u - Can replace glVertex, glNormal, glTexCoord
- The values of u need not be equally spaced
40Example
- Consider the previous evaluator that was set up
- for a cubic Bezier over (0,1)
- Suppose that we want to approximate the curve
- with a 100 point polyline
glBegin(GL_LINE_STRIP) for(i0 ilt100 i)
glEvalCoord1f( (float) i/100.0) glEnd()
41Equally Spaced Points
- Rather than use a loop, we can set up an equally
spaced mesh (grid) and then evaluate it with one
function call
glMapGrid(100, 0.0, 1.0)
sets up 100 equally-spaced points on (0,1)
glEvalMesh1(GL_LINE, 0, 99)
renders lines between adjacent evaluated points
from point 0 to point 99
42Bezier Surfaces
- Similar procedure to 1D but use 2D evaluators in
u and v - Set up with
- glMap2f(type, u_min, umax, u_stride, u_order,
v_min, v_max, v_stride, v_order, pointer_to_data) - Evaluate with glEvalCoord2f(u,v)
43Example
bicubic over (0,1) x (0,1)
point data44 glMap2f(GL_MAP_VERTEX_3,
0.0, 1.0, 3, 4, 0.0, 1.0, 12, 4, data)
Note that in v direction data points are
separated by 12 floats since array data is stored
by rows
44Rendering with Lines
must draw in both directions
for(j0jlt100j) glBegin(GL_LINE_STRIP)
for(i0ilt100i) glEvalCoord2f((float)
i/100.0, (float) j/100.0) glEnd() glBegin(GL_L
INE_STRIP) for(i0ilt100i)
glEvalCoord2f((float) j/100.0, (float) i/100.0)
glEnd()
45Rendering with Quadrilaterals
We can form a quad mesh and render with lines
for(j0 jlt99 j) glBegin(GL_QUAD_STRIP)
for(i0 ilt100 i) glEvalCoord2f
((float) i/100.0, (float) j/100.0)
glEvalCoord2f ((float)(i1)/100.0,
(float)j/100.0) glEnd()
46Uniform Meshes
- We can form a 2D mesh (grid) in a similar manner
to 1D for uniform spacing - glMapGrid2(u_num, u_min, u_max, v_num, v_min,
v_max) - Can evaluate as before with lines or if want
filled polygons - glEvalMesh2( GL_FILL, u_start, u_num, v_start,
v_num)
47Rendering with Lighting
- If we use filled polygons, we have to shade or we
will see solid color uniform rendering - Can specify lights and materials but we need
normals - Let OpenGL find them
- glEnable(GL_AUTO_NORMAL)
48OpenGL Architecture