Texture Mapping
Texture Mapping
• Important topic: nearly all objects textured
– Wood grain, faces, bricks and so on
– Adds visual detail to scenes
• Meant as a fun and practically useful lecture
Polygonal model
With surface texture
2
Adding Visual Detail
• Basic idea: use images instead of more
polygons to represent fine scale color variation
3
Parameterization
+
geometry
=
image
texture map
Q: How do we decide where on the geometry each
color from the image should go?
4
Option: Varieties of projections
5
[Paul Bourke]
Option: unfold the surface
[Piponi2000]
6
Option: make an atlas
charts
atlas
surface
[Sander2001]
7
Option: it’s the artist’s problem
8
OPENGL TEXTURE MAPPING
9
OpenGL Texture Mapping
• Apply a 1D, 2D, or 3D image to geometric
primitives
• Uses of Texturing
–
–
–
–
Simulating materials
Reducing geometric complexity
Image warping
Reflections
10
How to map object to texture?
y
z
x
geometry
t
screen
image
s
To each vertex (x, y, z in object coordinates),
must associate 2D texture coordinates (s, t)
So texture fits “nicely” over object
11
Idea: Use Map Shape
• Map shapes correspond to various projections
– Planar, Cylindrical, Spherical
• First, map (square) texture to basic map shape
• Then, map basic map shape to object
– Or vice versa: Object to map shape, map shape to
square
• Usually, this is straightforward
– Maps from square to cylinder, plane, sphere well
defined
– Maps from object to these are simply spherical,
cylindrical, cartesian coordinate systems
12
Planar mapping
• Like projections, drop z coord (s, t) = (x, y)
• Problems: what happens near z = 0?
13
Cylindrical Mapping
• Cylinder: r, θ, z with (s, t) = (θ/(2π), z)
– Note seams when wrapping around (θ = 0 or 2π)
14
Spherical Mapping
• Convert to spherical coordinates: use latitude/long.
– Singularities at north and south poles
15
Cube Mapping
16
Cube Mapping
17
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
18
Texture Example
• The texture (below) is
a 256 x 256 image
that has been
mapped to a
rectangular polygon
which is viewed in
perspective
19
Applying Textures I
Three steps
• Specify texture
– Read or generate image
– Assign to texture
• Assign texture coordinates to vertices
• Specify texture parameters
– Wrapping, filtering
20
Applying Textures II
•
•
•
•
•
•
•
•
Specify textures in texture objects
Set texture filter
Set texture function
Set texture wrap mode
Set optional perspective correction hint
Bind texture object
Enable texturing
Supply texture coordinates for vertex
– Coordinates can also be generated
21
Texture Objects
• Like display lists for texture images
– One image per texture object
– May be shared by several graphics contexts
• Generate texture names
glGenTextures(n, texIds[]);
– Returns n currently unused names for texture objects
in the array texIds.
• Delete texture objects
glDeleteTextures(n, texIds[])
22
Texture Objects (cont.)
• Create texture objects with texture data and state
glBindTexture(target, texId)
– for the first time, a new texture object is created and
assigned that name.
– When binding to a previously created texture object,
that texture object becomes active.
– When binding to a texId value of zero, OpenGL stops
using texture objects and returns to the default texture.
• Bind textures before using
glBindTexture(target, id);
23
Specify Texture Image
• Define a texture image from an array of texels in
CPU memory
glTexImage2D( target, level, components,
w, h, border, format, type,
texels[] );
– dimensions of image must be powers of 2
• Texel colors are processed by pixel pipeline
– pixel scales, biases and lookups can be done
24
Converting A Texture Image
• If dimensions of image are not power of 2
gluScaleImage(format, w_in, h_in,
type_in, data_in[], w_out, h_out,
type_out, data_out[]);
– *_in is for source image
– *_out is for destination image
• Image interpolated and filtered during scaling
25