OpenGL Texture Mapping• Apply a 1D, 2D, or 3D image to geometric... Idea: Use Map Shape• Map shapes correspond to various projections – Planar, Cylindrical, Spherical • First, map square
Trang 1Texture Mapping
Trang 2Texture 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
Trang 3Adding Visual Detail
• Basic idea: use images instead of more
polygons to represent fine scale color variation
3
Trang 5Option: Varieties of projections
[Paul Bourke] 5
Trang 6Option: unfold the surface
[Piponi2000]
Trang 7Option: make an atlas
[Sander2001]
7
Trang 8Option: it’s the artist’s problem
Trang 9OPENGL TEXTURE MAPPING
9
Trang 10OpenGL Texture Mapping
• Apply a 1D, 2D, or 3D image to geometric
Trang 11How to map object to texture?
To each vertex (x, y, z in object coordinates),
must associate 2D texture coordinates (s, t)
So texture fits “nicely” over object
s t
Trang 12Idea: 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
Trang 13Planar mapping
• Like projections, drop z coord (s, t) = (x, y)
• Problems: what happens near z = 0?
13
Trang 14Cylindrical Mapping
• Cylinder: r, θ, z with (s, t) = (θ/(2π), z)
– Note seams when wrapping around (θ = 0 or 2π)
Trang 15Spherical Mapping
• Convert to spherical coordinates: use latitude/long
– Singularities at north and south poles
15
Trang 16Cube Mapping
Trang 17Cube Mapping
17
Trang 18Texture Mapping and the OpenGL Pipeline
• Images and geometry flow through separate
pipelines that join at the rasterizer
– "complex" textures do not affect geometric complexity
geometry pipeline vertices
pixel pipeline image
rasterizer
Trang 20• Assign texture coordinates to vertices
• Specify texture parameters
– Wrapping, filtering
Trang 21Applying 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
Trang 22Texture 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[])
Trang 23Texture Objects (cont.)
• Create texture objects with texture data and state
– 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
Trang 24Specify 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
Trang 25Converting 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
Trang 26Specifying a Texture: Other Methods
• Use frame buffer as source of texture image
– uses current buffer as source image
Trang 27Mapping a Texture
• Based on parametric texture coordinates
• glTexCoord*() specified at each vertex
Trang 28Generating Texture Coordinates
• Automatically generate texture coords
Trang 29Tutorial: Texture
29
Trang 30Texture Application Methods
– How to mix primitive’s color with texture’s color
• blend, modulate or replace texels
Trang 35Perspective Correction Hint
• Texture coordinate and color interpolation
– either linearly in screen space
– or using depth/perspective values (slower)
• Noticeable for polygons “on edge”
Trang 36Is 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
Trang 37Texture Residency
• Working set of textures
– high-performance, usually hardware accelerated
– textures must be in texture objects
– a texture in the working set is resident
– for residency of current texture, check
GL_TEXTURE_RESIDENT state
• If too many textures, not all are resident
– can set priority to have some kicked out first
– establish 0.0 to 1.0 priorities for texture objects
37
Trang 38TEXTURE MAPPING APPLICATIONS
Trang 39Texture Mapping Applications
• Modulation, light maps
Trang 40) (
) (
( (
,
(s t I K I K N L K V R n S I K I K I T
Trang 41Bump Mapping
• Texture = change in surface normal!
41
Trang 42Displacement Mapping
Trang 43Illumination Maps
• Quake introduced illumination maps or light maps to
capture lighting effects in video games
Trang 44Environment Maps
Images from Illumination and Reflection Maps:
Simulated Objects in Simulated and Real Environments
Gene Miller and C Robert Hoffman
Trang 45• Compute on the fly,
e.g Perlin noise
45
Trang 46Procedural Texture Gallery