# CS计算机代考程序代写 cache 15-462 Computer Graphics

15-462 Computer Graphics
15-462 Computer Graphics Lecture 10
Texture Mapping
Texture Mapping
February 13, 2003
M. Ian Graham
Carnegie Mellon University

n About 1 week until Assignment 3 is due Assignment 2 handback, comments Questions on Assignment 3?
02/13/2003 15-462 Graphics I 2

Itinerary
Itinerary
Introduction to Texture Mapping Aliasing and How to Fight It Texture Mapping in OpenGL Applications of Texture Mapping
02/13/2003 15-462 Graphics I 3

Motivation for Texture Mapping
Motivation for Texture Mapping Phong illumination model coupled with a
single color across a broad surface n Produces boring objects
n Very limited
Options to make things interesting:
n No simple surfaces—use many tiny polygons
Expensive! Too much geometry.
n Apply textures across the polygons
02/13/2003
Less geometry, and the image looks almost as good!
15-462 Graphics I 4

Definitions
Definitions Texture—the appearance and feel of a
surface
Texture—an image used to define the
characteristics of a surface
Texture—a multidimensional image which
is mapped to a multidimensional space.
02/13/2003 15-462 Graphics I 5

Texture mapping sample
Texture mapping sample
02/13/2003 15-462 Graphics I 6

Basic Concept
Basic Concept “Slap an image on a model.”
How do we map a two-dimensional image to a surface in three dimensions?
Texture coordinates
n 2D coordinate (s,t) which maps to a location
on the image (typically s and t are over [0,1]) Assign a texture coordinate to each vertex
n Coordinates are determined by some function
which maps a texture location to a vertex on
the model in three dimensions
02/13/2003 15-462 Graphics I 7

Basic Concept
Basic Concept
Once a point on the surface of the model has been mapped to a value in the texture, change its RGB value (or something else!) accordingly
This is called parametric texture mapping A single point in the texture is called a
texel
02/13/2003 15-462 Graphics I 8

Something else?
Something else?
The first known use of texture in graphics was the modulation of surface color values, (diffuse coefficients) by Catmull in 1974.
A texture does not have to indicate color! Bump mapping was developed in 1978 by
Blinn
Transparency maps in 1985 by Gardner
02/13/2003 15-462 Graphics I 9

What is a texture map?
What is a texture map? Practical: “A way to slap an image on a
model.”
Better: “A mapping from any function onto
a surface in three dimensions.”
Most general: “The mapping of any image
into multidimensional space.”
02/13/2003 15-462 Graphics I 10

(u, v)
Texture Space (2D)
(x, y)
Screen Space (2D)
Screen Space (3D) (z buffer)
projection
Overview
Overview
parameterization
Object Space (3D) World Space (3D)
02/13/2003
15-462 Graphics I
11

Visual Overview
Visual Overview
02/13/2003 15-462 Graphics I 12

Hardware Notes
Hardware Notes
Texture-mapping is supported in all modern graphics hardware since the introduction of the Voodoo 3Dfx—it’s therefore cheap and easy
Though the mapping is conceptualized in the order texture -> object -> screen, it is determined in reverse order in hardware, during scan conversion (“To which texel does this pixel map?”)
02/13/2003 15-462 Graphics I 13

Linear Texture Mapping
Linear Texture Mapping
Do a direct mapping of a block of texture to a surface patch
u = umin + v = vmin +
s − smin (umax − umin smax − smin
t − tmin (vmax − vmin ) tmax − tmin
)
02/13/2003
15-462 Graphics I
14

Cube Mapping
Cube Mapping
“Unwrap” cube and map texture over the cube
02/13/2003 15-462 Graphics I 15

Cylinder Mapping
Cylinder Mapping
Wrap texture along outside of cylinder, not top and bottom
n This stops texture from being distorted
x = r⋅cos(2πu) y = r⋅sin(2πu)
z=v/h
02/13/2003 15-462 Graphics I 16

Two-part Mapping
Two-part Mapping
To simplify the problem of mapping from an image to an arbitrary model, use an object we already have a map for as an intermediary!
Texture -> Intermediate object -> Final model
Common intermediate objects: n Cylinder
n Cube
n Sphere
02/13/2003 15-462 Graphics I 17

Intermediate Object to Model
Intermediate Object to Model
This step can be done in many ways n Normal from intermediate surface
n Normal from object surface
n Use center of object
02/13/2003 15-462 Graphics I 18

Still tough!
Still tough!
Mapping onto complicated objects is difficult
n Even simple objects can be hard—spheres always distort the texture
02/13/2003 15-462 Graphics I 19

Itinerary
Itinerary
Introduction to Texture Mapping Aliasing and How to Fight It Texture Mapping in OpenGL Applications of Texture Mapping
02/13/2003 15-462 Graphics I 20

What is aliasing?
What is aliasing? Interested in mapping from screen to texture
coordinates
02/13/2003 15-462 Graphics I 21

What is aliasing?
What is aliasing?
An on-screen pixel does not always map neatly to a texel. Particularly severe problems in regular textures
02/13/2003 15-462 Graphics I 22

Example
Example
02/13/2003 15-462 Graphics I 23

The Beginnings of a Solution
The Beginnings of a Solution
Pre-calculate how the texture should look at various distances, then use the appropriate texture at each distance. This is called mipmapping.
02/13/2003 15-462 Graphics I 24

The Beginnings of a Solution
The Beginnings of a Solution Each mipmap (each image below) represents a
level of depth (LOD).
Powers of 2 make things much easier.
02/13/2003 15-462 Graphics I 25

The Beginnings of a Solution
The Beginnings of a Solution
Problem: Clear divisions between different levels of depth!
Mipmapping alone is unsatisfactory.
02/13/2003
15-462 Graphics I 26

Another Component: Filtering
Another Component: Filtering Take the average of multiple texels to
obtain the final RGB value
Typically used along with mipmapping
Bilinear filtering
n Average the four surrounding texels
n Cheap, and eliminates some aliasing, but does not help with visible LOD divisions
(demonstration movies)
(demonstration movies)
02/13/2003 15-462 Graphics I 27

Another Component: Filtering
Another Component: Filtering
Trilinear filtering
n Interpolate between two LODs
n Final RGB value is between the result of a bilinear filter at one LOD and a second bilinear filter at the next LOD
n Eliminates “seams” between LODs
n At least twice as expensive as bilinear filtering
02/13/2003 15-462 Graphics I 28

Another Component: Filtering
Another Component: Filtering
Anisotropic filtering
n Basic filtering methods assume that a pixel on-screen maps to a square (isotropic) region of the texture
n For surfaces tilted away from the viewer, this is not the case!
Image courtesy of nVidia
02/13/2003 15-462 Graphics I 29 Figure 5. Anisotropic footprints are very common.

Another Component: Filtering
Another Component: Filtering
Anisotropic filtering
n A pixel may map to a rectangular or trapezoidal section of texels—shape filters accordingly and use either bilinear or trilinear filtering
n Complicated, but produces very nice results
02/13/2003 15-462 Graphics I 30

Bilinear Filtering
Bilinear Filtering
02/13/2003 15-462 Graphics I ID Software 31

Trilinear Filtering
Trilinear Filtering
02/13/2003 15-462 Graphics I ID Software 32

Anisotropic Filtering
Anisotropic Filtering
02/13/2003 15-462 Graphics I ID Software 33

Side-by-Side Comparison
Side-by-Side Comparison
02/13/2003 15-462 Graphics I
34
nVidia

Itinerary
Itinerary
Introduction to Texture Mapping Aliasing and How to Fight It Texture Mapping in OpenGL Applications of Texture Mapping
02/13/2003 15-462 Graphics I 35

glTexImage2D
glTexImage2D glTexImage2D(GL_TEXTURE_2D, level, components, width, height,
border, format, type, tarray) GL_TEXTURE_2D
n Specify that it is a 2D texture Level
n Used for specifying levels of detail for mipmapping (more on this later) Components
n Generally is 0 which means GL_RGB
n Represents components and resolution of components Width, Height
n The size of the texture must be powers of 2 Border
Format, Type
n Specify what the data is (GL_RGB, GL_RGBA, …)
n Specify data type (GL_UNSIGNED_BYTE, GL_BYTE, …)
02/13/2003 15-462 Graphics I 36

glTexCoord2f
glTexCoord2f glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, 3, texture->nx, texture->ny, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pix);
glBegin(GL_POLYGON); glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 0.0, 1.0); glTexCoord2f(1.0, -1.0);
glVertex3f(1.0, 0.0, -1.0); glTexCoord2f(-1.0, -1.0);
glVertex3f(-1.0, 0.0, -1.0); glTexCoord2f(-1.0, 1.0);
glVertex3f(-1.0, 0.0, 1.0); glEnd();
02/13/2003
15-462 Graphics I 37

Other Texture Parameters
Other Texture Parameters
glTexParameterf()
n Use this function to set how textures repeat
glTexParameterf(GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameterf(GL_TEXTURE_WRAP_S, GL_CLAMP) n Which spot on texture to pick
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
02/13/2003 15-462 Graphics I 38

Mipmapping in OpenGL
Mipmapping in OpenGL
gluBuild2DMipmaps(GL_TEXTURE_2D, components, width, height, format, type, data)
This will generate all the mipmaps using gluScaleImage
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST) n This will tell GL to use the mipmaps for the texture
02/13/2003 15-462 Graphics I 39

Mipmapping in OpenGL
Mipmapping in OpenGL If you design the mipmaps yourself
n glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, LOD1)
n glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, LOD2)
02/13/2003 15-462 Graphics I 40

Other Texturing Issues
Other Texturing Issues
glTexEnvi(GL_TEX_ENV, GL_TEX_ENV_MODE, GL_MODULATE) n Will balance between shade color and texture color
glTexEnvi(GL_TEX_ENV, GL_TEX_ENV_MODE, GL_DECAL) n Will replace shade color with texture color
glHint(GL_PERSPECTIVE_CORRECTION, GL_NICEST) n OpenGL does linear interpolation of textures
Works fine for orthographic projections
n Allows for OpenGL to correct textures for perspective projection
There is a performance hit Texture objects
n Maintain texture in memory so that it will not have to be loaded constantly
02/13/2003
15-462 Graphics I 41

OpenGL texturing code
OpenGL texturing code
This code assumes that it’s an RGB texture map
02/13/2003 15-462 Graphics I 42

Itinerary
Itinerary
Introduction to Texture Mapping Aliasing and How to Fight It Texture Mapping in OpenGL Applications of Texture Mapping
02/13/2003 15-462 Graphics I 43

Non-2D Texture Mapping
Non-2D Texture Mapping The domain of a texture mapping function
may be any number of dimensions
n 1D might be used to represent rock strata
n 2D is used most often
n 3D can be used to represent interesting physical phenomena
n Animated textures are a cheap extra dimension—further dimensions are somewhat harder to conceptualize
02/13/2003 15-462 Graphics I 44

3D Texture Mapping
3D Texture Mapping
Almost the same as 2D texture mapping
n Texture is a “block” which objects fit into
n Texture coordinates are 3D coordinates which equal some value inside the texture block
02/13/2003 15-462 Graphics I 45

RGB values or… RGB values or…
Textures do not have to represent color values.
Using texture information to modify other aspects of a model can yield much more realistic results
02/13/2003 15-462 Graphics I 46

RGB values or… RGB values or…
Specularity (patches of shininess) Transparency (patches of clearness) Normal vector changes (bump maps) Reflected light (environment maps) Shadows
Changes in surface height (displacement maps)
02/13/2003 15-462 Graphics I 47

Bump Mapping
Bump Mapping
How do you make a surface look rough?
n Option1:modelthesurfacewithmanysmallpolygons
Fakes small displacements above or below the true surface
The surface doesn’t actually change, but shading makes it look like
there are irregularities!
A texture stores information about the “fake” height of the surface For the math behind it all look at Angel 7.8
02/13/2003
15-462 Graphics I 48

Bump Mapping
Bump Mapping
We can perturb the normal vector without having
to make any actual change to the shape. This illusion can be seen through—how?
02/13/2003
49
Original model (5M)
Simplified (500)
15-462 Graphics I
Simple model with bump map

Environment Mapping
Environment Mapping
Allows for world to be reflected on an object without modeling the physics
Map the world surrounding an object onto a cube
Project that cube onto the object
n Bounce a ray from the viewer off the object (at point P)
n Intersect the ray with the environment map (the cube), at point E
n Get the environment map’s color at E and illuminate P as if there were a light source at position E
n Produces an image of the environment reflected on shiny surfaces 02/13/2003 15-462 Graphics I 50

Light Mapping
Light Mapping
Quake uses light maps in addition to texture maps. Texture maps are used to add detail to surfaces, and light maps are used to store pre-computed illumination. The two are multiplied together at run-time, and cached for efficiency.
02/13/2003
Light Map
15-462 Graphics I
51

Summary
Summary
Introduction to Texture Mapping Aliasing and How to Fight It Texture Mapping in OpenGL Applications of Texture Mapping
02/13/2003 15-462 Graphics I 52

Acknowledgements/Resources
Acknowledgements/Resources Frank Pfenning and Shayan Sharkar
(last year’s instance of this course) Paul Heckbert
n http://www.cs.cmu.edu/~ph
UNC (filter demonstration movies)
n http://www.cs.unc.edu/~sud/courses/comp235 /a6/
02/13/2003 15-462 Graphics I 53