Texture Mapping
+
=
ObjectTexture
Texture Mapped Object
Texture Mapping Basics
Basic Concepts
Texture (2D Texture): A two-dimensional array of data
Texel : An individual data element in a texture
Texture Space : A normalized coordinate space in which the texture is specified.
s
t
0 1
1
(s, t): Texture Coordinates0.0 s 1.0, 0.0 t 1.0
Surface-Texture Transformation
Transform from object space (x, y, z) to texture space (s, t)
Parametric equation P = P(u, v). Usually transform from parametric space (u, v) to texture space (s, t)
Example: Sphere
x
y
z
P = (x, y, z)
r
sin
sincos
coscos
rz
ry
rxParametric equation:
5.05.0 ,20
Surface-Texture Transformation
5.0
2
t
s
Curved surface
Polygon surface
Specify texture coordinates for each vertex. Then linearly interpolate texture coordinates across projected polygon.
x
y
1
2
3
4 5p(x, y)
),(),(),( 2221
111
21
244 ts
yy
yyts
yy
yyts
),(),(),( 2223
333
23
255 ts
yy
yyts
yy
yyts
Scan line y
),(),(),( 5545
444
45
5 tsxx
xxts
xx
xxts pp
Texture Mapping Procedure
s
t
xd
yd
Pixel Area
Object Surface
Device Space Object Space Texture Space
STM1MVPM
Projected Pixel Area
Pixel Center
ModelViewProjection MMMM MVP
Surface-texture transformationSTM
Texture Wrapping Methods
Methods to deal with texture coordinates outside [0, 1] range.
Texture
Polygon surface with texture coordinates for four corners
(0, 0) (3, 0)
(3, 3)(0, 3)
Repeat Mirrored Repeat
n s < n 1s s n
n s < n 1if (n is even number) s s nif (n is odd number) s n 1 s
Clamp
if (s < 0) s 0if (s > 1) s 1
Border Clamp
if (s < 0) {s 0; Use border color;}if (s > 1) {s 1; Use border color;}
Border Color:
One-Dimensional and Three-Dimensional Texture
One-Dimensional Texture (Linear Texture)
One-dimensional array of data.Texture coordinate s.
Three-Dimensional Texture (Volume Texture, Solid Texture)
Three-dimensional array of data.Texture coordinates (s, t, r).
s0 1
s t
r
Texture Mapping Examples
Texture Filtering
Texture Filtering: Determine screen pixel color from texel color values using projected pixel area in texture space.
Nearest Filtering
Linear Filtering
Mipmap Nearest Filtering
Mipmap Linear Filtering
Anisotropic Filtering
Texture Minification and Magnification
s
t Projected Pixel Area
Minification: Projected pixel area in texture space is larger than a single texel.
s
t Projected Pixel Area
Magnification: Projected pixel area in texture space is smaller than a single texel.
Nearest Filtering (Point Sampling)
Select the texel whose center lies nearest to the projected pixel center. Use the color of this texel as pixel color.
s
t Projected Pixel Center
Nearest Texel Center
Nearest filtering can result in severe aliasing effects and blocky images.
Linear Filtering (Bilinear Filtering)
Select 22 array of texels that lie nearest to the projected pixel center. The pixel color is linearly interpolated from these texels.
s
t Projected Pixel Center
Linear filtering produces better and smooth texture mapped images.
(s, t)
(s0, t0) (s0+s, t0)
(s0+s, t0+t)(s0, t0+t)Texture array mn
nt
ms
1,
1
),(),(),( 000
000
0 tsss
ssts
s
sssts
CCC
),(),(),( 000
000
0 ttsss
sstts
s
ssstts
CCC
),(),(),( 00
00 tts
t
ttts
t
tttts
CCC
Mipmap
Mipmap (multum in parvo, many things in a small place): A series of prefiltered texture images with decreasing resolution.
Level 0 (8 8) Level 1 (4 4) Level 2 (2 2) Level 3 (1 1)
Using a single texture, when object surface moves further away from the viewer, texture mapping may result in artifacts such as shimmering and flashing.
Mipmap is only used for texture minification. Higher mipmap level should be chosen for longer distance between object surface and the viewer.
Mipmap Nearest Filtering
Choose the nearest mipmap level.
Mipmap Linear Filtering
Choose the two nearest mipmap levels. Linearly interpolate between texels sampled from these two levels.
Trilinear Filtering: Use mipmap linear filtering for sampling between mipmap levels and use bilinear filtering for sampling within each mipmap level.
Without Mipmap With Mipmap
Anisotropic Filtering
Texture anisotropy: The projected pixel area in texture space has elongated shape if the object surface forms a steep angle with respect to view plane.
xd
yd
Pixel Area
s
t
Object Surface
Projected Pixel Area
View Plane
xd
yd
Pixel Area
s
t
Object Surface Projected
Pixel Area
Anisotropic Filtering: Use multiple samples in projected pixel area. Different sample patterns are used for different projected pixel area shape.
View Plane
Trilinear Filtering Anisotropic
OpenGL Texture Mapping
OpenGL Texture Mapping Procedures:
1. Create Texture Object.
2. Specify Texture Image for Texture Object.
3. Specify Texture Parameters (Texture Wrapping and Filtering Modes) for Texture Object.
4. Specify Texture Environment (The way in which texel color is combined with fragment color).
5. Enable Texture Mapping and Bind Texture Object.
6. Render Primitives. Specify Texture Coordinates for Each Vertex.
Texture Objects
A texture object stores texture image data and texture parameters. Each texture object is identified by a positive integer index number.
Procedures to use texture objects:
1. Generate texture object names.
2. Initially bind texture objects and set texture image data and parameters.
3. Bind and rebind texture objects for rendering.
4. Delete texture objects when they are no longer needed.
Generate Texture Object Names
void glGenTextures(GLsizei n, GLuint *textureNames)
Return n currently unused integer index names for texture objects and store them in array textureNames.
GLboolean glIsTexture(GLuint textureName)
Return GL_TRUE if textureName is currently used as the name for a texture object. Otherwise return GL_FALSE.
Delete Texture Objects
void glDeleteTextures(GLsizei n, const GLuint *textureNames)
Delete n texture objects whose names are stored in array textureNames. Their data is released and their names are freed.
Bind Texture Objects
void glBindTexture(GLenum target, GLuint textureName)When using textureName for the first time, a new texture object is created. When binding to a previously created texture object, that texture object becomes active.target: Texture targettextureName: Texture object name
target Meaning
GL_TEXTURE_1D One-dimensional Texture
GL_TEXTURE_2D Two-dimensional Texture
GL_TEXTURE_3D Three-dimensional Texture
GL_TEXTURE_CUBE_MAP Cube Environment Map
Use glEnable(target) to enable texture mapping.Use glDisable(target) to disable texture mapping.
Specify Texture Image
• Texture image can be specified from system memory or frame buffer.
• Texture image of a texture object can be replaced entirely or partly after it is initially specified.
• Texture image can be 1D, 2D or 3D. 3D texture support was added in OpenGL 1.2.
• Cube environment map texture consists of six 2D square texture images. Cube map support was added in OpenGL 1.3.
Specify or Replace Texture Image from System MemoryglTexImage*(), glTexSubImage*()
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
Specify or Replace Texture Image from Frame BufferglCopyTexImage*(), glCopyTexSubImage*()
SystemMemory
FrameBuffer
PixelStorageModes
Pixel TransferOperations(Pixel Map)
Unpack Pack
Rasterization(Pixel Zoom)
FragmentOperations
TextureMemory
Two-Dimensional Texture
void glTexImage2D(GLenum target, GLint level, GLint internalFormat,GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *texels)
target: 2D texture image target.level: Mipmap level. Base level is 0.internalFormat: Internal format for texel data. Indicates how texture im
age is stored internally in texture memory.width: Width of the texture image.height: Height of the texture image.border: Width of the texture image border. Must be 0 or 1.format: Texel data format of source image.type: Texel data type of source image.texels: System memory that stores source image data.
Specify a 2D texture image from system memory. The image is set for texture object currently bound to target.
target Meaning
GL_TEXTURE_2D 2D texture image
GL_TEXTURE_CUBE_MAP_POSITIVE_X Cube map +x face
GL_TEXTURE_CUBE_MAP_NEGATIVE_X Cube map x face
GL_TEXTURE_CUBE_MAP_POSITIVE_Y Cube map +y face
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y Cube map y face
GL_TEXTURE_CUBE_MAP_POSITIVE_Z Cube map +z face
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z Cube map z face
width and height must be (2n + 2border), where n is an integer.
format and type have the same meaning as in glReadPixels() and glDrawPixels(), except that format can not be GL_STENCIL_INDEX.
2D Texture Image Target
Base Internal Format Texel data components
GL_RGB (R, G, B, 1)
GL_RGBA (R, G, B, A)
GL_ALPHA (0, 0, 0, A)
GL_LUMINANCE (L, L, L, 1)
GL_LUMINANCE_ALPHA (L, L, L, A)
GL_INTENSITY (I, I, I, I)
GL_DEPTH_COMPONENT Depth Value
Internal Format
R = Red, G = Green, B = Blue, A = AlphaL = Luminance, I = Intensity
Base Internal Format Internal FormatGL_RGB GL_RGB, GL_R3_G3_B2, GL_RGB4, GL_RGB5, GL_RGB8,
GL_RGB10, GL_RGB12, GL_RGB16
GL_RGBA GL_RGBA, GL_RGBA2, GL_RGBA4, GL_RGB5_A1, GL_RGBA8, GL_RGB10_A2, GL_RGBA12, GL_RGBA16
GL_ALPHA GL_ALPHA, GL_ALPHA4, GL_ALPHA8, GL_ALPHA12, GL_ALPHA16
GL_LUMINANCE GL_LUMINANCE, GL_LUMINANCE4, GL_LUMINANCE8, GL_LUMINANCE12, GL_LUMINANCE16
GL_LUMINANCE_ALPHA GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE6_ALPHA2, GL_LUMINANCE8_ALPHA8, GL_LUMINANCE12_ALPHA4, GL_LUMINANCE12_ALPHA12, GL_LUMINANCE16_ALPHA16
GL_INTENSITY GL_INTENSITY, GL_INTENSITY4, GL_INTENSITY8, GL_INTENSITY12, GL_INTENSITY16
GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32
Use glGetIntegerv(GL_MAX_TEXTURE_SIZE, …) to obtain maximum supported texture size without border.Use glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, …) to obtain maximum supported cube map texture size without border.
To use an image with width or height that is not power of two (without border) as texture image, it must be scaled first.
int gluScaleImage(GLenum format, GLint widthin, GLint heightin, GLenum typein, const void *datain, GLint widthout, GLint heightout, GLenum typeout, const void *dataout)
Scale an image. Return 0 if succeeds, otherwise return a GLU error code.format: Pixel data formatwidthin, heightin: Width and height of input imagetypein: Pixel data type of input imagedatain: Input image datawidthout, heightout: Width and height of output imagetypeout: Pixel data type of output imagedataout: Output image data
void glCopyTexImage2D(GLenum target, GLint level, GLint internalFormat,
GLint x, GLint y, GLsizei width, GLsizei height, GLint border)
Specify a 2D texture image from frame buffer.target: 2D texture image target.level: Mipmap level. Base level is 0.internalFormat: Internal format for texel data.x, y: Lower left corner of the source image in frame buffer.width: Width of the texture image.height: Height of the texture image.border: Width of the texture image border. Must be 0 or 1.
target, level, internalFormat, width, height and border have the same meaning as in glTexImage2D().
void glTexSubImage2D(GLenum target, GLint level, GLint xoff, GLint yoff, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *texels)
xoff, yoff: Texel offsets of the lower left corner of the subimage with respect to the lower left corner of the entire texture image.
width: Width of the texture subimage.height: Height of the texture subimage.
Replace a subimage of a 2D texture with image data from system memory.
Other parameters have the same meaning as in glTexImage2D().
void glCopyTexSubImage2D(GLenum target, GLint level, GLint xoff, GLint yoff, GLint x, GLint y, GLsizei width, GLsizei height)
Replace a subimage of a 2D texture with image data from frame buffer.x, y: Lower left corner of the source image rectangle in frame buffer.Other parameters have the same meaning as in glTexSubImage2D().
Sub-Image
Texture Imageyoff
xoff
width
height
Automatic Mipmap Generation
int gluBuild2DMipmaps(GLenum target, GLint internalFormat,GLsizei width, GLsizei height,GLenum format, GLenum type, const GLvoid *texels)
Construct a series of mipmaps down to resolution of 1 1, then call glTexImage2D() to load texture images on all mipmap levels. Also automatically scale the original image so that its width and height become power of two. All parameters have the same meaning as in glTexImage2D().Return 0 if succeeds, otherwise return a GLU error code.
One-Dimensional Texture
glTexImage1D(), glTexSubImage1D()
glCopyTexImage1D(), glCopyTexSubImage1D()
gluBuild1DMipmaps()
Three-Dimensional Texture
glTexImage3D(), glTexSubImage3D()
glCopyTexImage3D(), glCopyTexSubImage3D()
gluBuild3DMipmaps()
Specify Texture Parameters
void glTexParameteri(GLenum target, GLenum pname, GLint param)void glTexParameterf(GLenum target, GLenum pname, GLfloat param)void glTexParameteriv(GLenum target, GLenum pname, GLint *param)void glTexParameterfv(GLenum target, GLenum pname, GLfloat *param)Specify texture parameters for texture object that is currently bound to target.target: GL_TEXTURE_1D
GL_TEXTURE_2DGL_TEXTURE_3DGL_TEXTURE_CUBE_MAP
pname: Texture parameter nameparam: Texture parameter value
Texture parameter name Values
GL_TEXTURE_BORDER_COLOR (R, G, B, A)
GL_TEXTURE_MAG_FILTER GL_NEARESTGL_LINEAR
GL_TEXTURE_MIN_FILTER GL_NEARESTGL_LINEARGL_NEAREST_MIPMAP_NEARESTGL_NEAREST_MIPMAP_LINEARGL_LINEAR_MIPMAP_NEARESTGL_LINEAR_MIPMAP_LINEAR
GL_TEXTURE_WRAP_SGL_TEXTURE_WRAP_TGL_TEXTURE_WRAP_R
GL_REPEATGL_MIRRORED_REPEATGL_CLAMPGL_CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE was added in OpenGL 1.2.GL_MIRRORED_REPEAT was added in OpenGL 1.4.
Constant Texture Border Color
pname = GL_TEXTURE_BORDER_COLOR
Constant texture border color is used as border color when the texture image does not have borders.
Texture Magnification Filter
pname = GL_TEXTURE_MAG_FILTER
param Meaning
GL_NEAREST Nearest filtering
GL_LINEAR Linear filtering
Texture Minification Filter
pname = GL_TEXTURE_MIN_FILTER
param Meaning
GL_NEAREST Nearest filtering (Mipmap is not used)
GL_LINEAR Linear filtering (Mipmap is not used)
GL_NEAREST_MIPMAP_NEAREST Nearest filtering within each mipmapNearest filtering between mipmaps
GL_NEAREST_MIPMAP_LINEAR Nearest filtering within each mipmapLinear filtering between mipmaps
GL_LINEAR_MIPMAP_NEAREST Linear filtering within each mipmapNearest filtering between mipmaps
GL_LINEAR_MIPMAP_LINEAR Linear filtering within each mipmapLinear filtering between mipmaps
Texture Wrapping Modes
pname Meaning
GL_TEXTURE_WRAP_S Texture wrapping mode for s coordinate
GL_TEXTURE_WRAP_T Texture wrapping mode for t coordinate
GL_TEXTURE_WRAP_R Texture wrapping mode for r coordinate
param Meaning
GL_REPEAT Integer part of texture coordinate is ignored.Border color is always ignored. For linear filtering, 22 texel array wraps to the opposite edge of the texture.
GL_MIRRORED_REPEAT Integer part of texture coordinate is ignored. Fractional part is reversed at every integer boundary. The rest is the same as GL_REPEAT.
GL_CLAMP Texture coordinate is clamped to [0, 1]. Edge color is used for nearest filtering. Border color is used in 22 texel array for linear filtering.
GL_CLAMP_TO_EDGE Texture coordinate is clamped to [0, 1]. Border color is always ignored. Texels at or near the edge are used.
Specify Texture Environment
void glTexEnvi(GLenum envtarget, GLenum pname, GLint param)void glTexEnvf(GLenum envtarget, GLenum pname, GLfloat param)void glTexEnviv(GLenum envtarget, GLenum pname, GLint *param)void glTexEnvfv(GLenum envtarget, GLenum pname, GLfloat *param)
Specify texture environment. Texture environment determines how texture color is combined with incoming fragment color.envtarget: Must be GL_TEXTURE_ENV
pname param
GL_TEXTURE_ENV_MODE GL_REPLACE GL_MODULATE GL_DECALGL_BLEND
GL_TEXTURE_ENV_COLOR Constant environment color (Rc, Gc, Bc, Ac)
Base Internal Format Texture source color (Rs, Gs, Bs, As)
GL_RGB (R, G, B, 1)
GL_RGBA (R, G, B, A)
GL_ALPHA (0, 0, 0, A)
GL_LUMINANCE (L, L, L, 1)
GL_LUMINANCE_ALPHA (L, L, L, A)
GL_INTENSITY (I, I, I, I)
• Texture source color (Rs, Gs, Bs, As)
• Incoming fragment color (Rf, Gf, Bf, Af)
• Constant environment color (Rc, Gc, Bc, Ac)
• Final combined color (R, G, B, A)
Replace Mode
Base Internal Format GL_REPLACE
GL_ALPHA R = Rf, G = Gf , B = Bf
A = As
GL_RGBGL_LUMINANCE
R = Rs, G = Gs , B = Bs
A = Af
GL_RGBAGL_LUMINANCE_ALPHAGL_INTENSITY
R = Rs, G = Gs , B = Bs
A = As
The texture source color components replace corresponding fragment color components.
Modulation Mode (default mode)
Base Internal Format GL_MODULATE
GL_ALPHA R = Rf, G = Gf , B = Bf A = AfAs
GL_RGBGL_LUMINANCE
R = RfRs, G = GfGs , B = BfBs
A = Af
GL_RGBAGL_LUMINANCE_ALPHAGL_INTENSITY
R = RfRs, G = GfGs , B = BfBs
A = AfAs
The texture source color components multiply corresponding fragment color components.
Decal Mode
Base Internal Format GL_DECAL
GL_RGB R = Rs, G = Gs , B = Bs
A = Af
GL_RGBA R = Rf(1 As) + RsAs
G = Gf(1 As) + GsAs
R = Bf(1 As) + BsAs
A = Af
GL_ALPHAGL_LUMINANCEGL_LUMINANCE_ALPHAGL_INTENSITY
undefined
For GL_RGB format, The texture source color replace fragment color.For GL_RGBA format, The texture source color is linearly blended with fragment color, using source alpha as blending weight.
Blending Mode
Base Internal Format GL_BLEND
GL_ALPHA R = Rf, G = Gf , B = Bf A = AfAs
GL_RGBGL_LUMINANCE
R = Rf(1 Rs) + RcRs
G = Gf(1 Gs) + GcGs
B = Bf(1 Bs) + BcBs
A = Af
GL_RGBAGL_LUMINANCE_ALPHAGL_INTENSITY
R = Rf(1 Rs) + RcRs
G = Gf(1 Gs) + GcGs
B = Bf(1 Bs) + BcBs
A = AfAs
The fragment color is linearly blended with constant environment color, using texture source color as blending weight.
Specify Texture Coordinates
void glTexCoord2{sifd}(TYPE s, TYPE t)
Specify current texture coordinates. Vertices defined using subsequent glVertex*() commands are assigned current texture coordinates.This command must be used between glBegin( ) and glEnd( ).
void glTexCoord3{sifd}(TYPE s, TYPE t , TYPE r)
void glTexCoord{1234}{sifd}v(TYPE * texcoords)
void glTexCoord4{sifd}(TYPE s, TYPE t , TYPE r , TYPE q)
void glTexCoord1{sifd}(TYPE s)
Texture coordinates
Meaning Applicable texture target
(s) One-dimensional GL_TEXTURE_1D
(s, t) Two-dimensional GL_TEXTURE_2D
(s, t, r) Three-dimensional GL_TEXTURE_3DGL_TEXTURE_CUBE_MAP(s, t, r, q) Homogeneous
Texture Matrix
• Texture matrix is a 44 transformation matrix that is used to transform texture coordinates.
• Texture matrix stack has two entries.
• Use glMatrixMode(GL_TEXTURE) to set current matrix to texture matrix.
• All standard matrix manipulation commands can be applied to texture matrix.
• The default texture matrix is an identity matrix.
Homogeneous Texture Coordinates
q
r
t
s
q
r
t
s
Tq
q
q
M
qrr
qtt
qss
q
q
q
Texture coordinates Homogeneous coordinates
(s) (s, 0, 0, 1)
(s, t) (s, t, 0, 1)
(s, t, r) (s, t, r, 1)
(s, t, r, q) (s, t, r, q)
[MT] is the texture matrix. (s', t', r') are the transformed texture coordinates.Homogenous texture coordinates also undergo perspective division.
Automatic Texture Coordinate Generation
void glTexGen{ifd}(GLenum coord, GLenum pname, TYPE param)void glTexGen{ifd}v(GLenum coord, GLenum pname, TYPE *param)Specify functions for automatic texture coordinate generation.coord: Specify which texture coordinate to use automatic generation.
Valid values are GL_S, GL_T, GL_R, GL_Q
pname param
GL_TEXTURE_GEN_MODE GL_OBJECT_LINEARGL_EYE_LINEARGL_SPHERE_MAPGL_REFLECTION_MAPGL_NORMAL_MAP
GL_OBJECT_PLANE Plane equation coefficients (A, B, C, D)
GL_EYE_PLANE Plane equation coefficients (A, B, C, D)
To enable automatic texture coordinate generation for s-coordinate, use glEnable(GL_TEXTURE_GEN_S).To enable automatic texture coordinate generation for t-coordinate, use glEnable(GL_TEXTURE_GEN_T).To enable automatic texture coordinate generation for r-coordinate, use glEnable(GL_TEXTURE_GEN_R).To enable automatic texture coordinate generation for q-coordinate, use glEnable(GL_TEXTURE_GEN_Q).
Linear Generation Mode (GL_OBJECT_LINEAR, GL_EYE_LINEAR)
GL_OBJECT_LINEAR
Texture coordinate = Ax + By + Cz + Dh(A, B, C, D) are plane equation coefficients specified using GL_OBJECT_PLANE parameter.(x, y, z, h) are vertex coordinates in object space.
GL_EYE_LINEAR
Texture coordinate = A'xe + B'ye + C'ze + D'he
[A' B' C' D']=[A B C D][M]1
(A, B, C, D) are plane equation coefficients specified using GL_EYE_PLANE parameter. [M] is modeview matrix when (A, B, C, D) was specified. (xe, ye, ze, he) are vertex coordinates in eye space.
Sphere Map (GL_SPHERE_MAP)
1. Calculate reflection vector R in eye space.2. Use R to calculate texture coordinates (s, t) for sampling in a sphere
environment map.
Cube Map (GL_REFLECTION_MAP, GL_NORMAL_MAP)
GL_REFLECTION_MAP
1. Calculate reflection vector R in eye space.2. Use R directly as texture coordinates (s, t, r) for sampling in a cube
environment map.
GL_NORMAL_MAP
1. Transform normal vector N from object space to eye space.2. Use transformed N as texture coordinates (s, t, r) for sampling in a
cube environment map.