texture mapping n.
Skip this Video
Loading SlideShow in 5 Seconds..
Texture Mapping PowerPoint Presentation
Download Presentation
Texture Mapping

Loading in 2 Seconds...

play fullscreen
1 / 75

Texture Mapping - PowerPoint PPT Presentation

  • Uploaded on

Texture Mapping. LIN Tao. Lecture Objectives. Apply a texture by assigning texture coordinates to a polygon explicitly Modify texture parameters Identify the advantages and disadvantages of the different filters available for textures Create textures with multiple levels of detail (mipmaps)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Texture Mapping' - emmanuel-dalton

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lecture objectives
Lecture Objectives

Apply a texture by assigning texture coordinates to a polygon explicitly

Modify texture parameters

Identify the advantages and disadvantages of the different filters available for textures

Create textures with multiple levels of detail (mipmaps)

Use texture objects to improve performance

Modify the texture environment

Use OpenGL and GLU to generate texture coordinates automatically

the limits of geometric modeling
The Limits of Geometric Modeling

Although graphics cards can render over 10 million polygons per second, that number is insufficient for many phenomena





modeling an orange 1
Modeling an Orange(1)

Consider the problem of modeling an orange (the fruit)

Start with an orange-colored sphere

Too simple

Replace sphere with a more complex shape

Does not capture surface characteristics (small dimples)

Takes too many polygons to model all the dimples

modeling an orange 2
Modeling an Orange (2)

Take a picture of a real orange, scan it, and “paste” onto simple geometric model

This process is known as texture mapping

Still might not be sufficient because resulting surface will be smooth

Need to change local shape

Bump mapping

three types of mapping
Three Types of Mapping

Texture Mapping

Uses images to fill inside of polygons

Environment (reflection mapping)

Uses a picture of the environment for texture maps

Allows simulation of highly specular surfaces

Bump mapping

Emulates altering normal vectors during the rendering process

texture mapping1
Texture Mapping

geometric model

texture mapped

opengl as pipeline architecture
OpenGL as Pipeline Architecture

Per Vertex

Operations &






Per Fragment










where does mapping take place
Where does mapping take place?

Mapping techniques are implemented at the end of the rendering pipeline

Very efficient because few polygons make it past the clipper

is it simple
Is it simple?

Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved

2D image

3D surface

coordinate systems
Coordinate Systems

Parametric coordinates

May be used to model curves and surfaces

Texture coordinates

Used to identify points in the image to be mapped

Object or World Coordinates

Conceptually, where the mapping takes place

Window Coordinates

Where the final image is really produced

what is a texture
What is a Texture?
  • A texture image is a rectangular array of pixel data
    • Usually a 2D array, but can be 1D or 3D
  • A "texture pixel" is called a "texel”
    • Can contain color, luminance and/or alpha information
what information does a texture contain
What Information Does a Texture Contain?

struct texel{

unsigned char lumin;


A one component texture contains Luminance, Intensity, or Alpha data.

Examples: Wood, grass, sand


struct texel{

unsighed char lumin;

unsigned char alpha;


A two component texture contains Luminance and Alpha (transparency) data.

Examples: Trees, clouds


struct texel{

unsighed char b;//blue

unsigned char g;//green

unsigned char r;//red


A three component texture contains Red, Green, and Blue values.

Examples: Fabrics, bricks


struct texel{

unsigned char a;//alpha

unsigned char b;//blue

unsigned char g;//green

unsigned char r;//red


A four component texture contains Red, Green, Blue, and Alpha values.

Examples: Objects

what is texture mapping
What is Texture Mapping?

//left top



//left bottom



//right top



//right bottom



Applying textures (images) to polygons.

mapping a texture
Based on parametric texture coordinates

glTexCoord*() specified at each vertex

Mapping a Texture

Texture Space

Object Space


1, 1

(s, t) = (0.2, 0.8)

0, 1




(0.4, 0.2)




(0.8, 0.4)


0, 0

1, 0


The basic texture mapping steps are:

记住: 纹理坐标必须在RGBA模式下才可以使用,在颜色索引模式下使用纹理


  • Specify the texture.
    • glTexImage ;gluScaleImage
  • Indicate how the texture is to be applied to each pixel.
    • glTexEnv ;glTexParameter
  • Enable texture mapping.
    • glEnable
  • Draw the scene, providing geometric and texture coordinates.
    • glTexCoord
define image as a texture
Define Image as a Texture

glTexImage2D( target, level, components, w, h, border, format, type, texels );

target: type of texture, e.g. GL_TEXTURE_2D

level: used for mipmapping (discussed later)

components: elements per texel

w, h: width and height of texels in pixels

border: used for smoothing (discussed later)

format and type: describe texels

texels: pointer to texel array

glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels);

specifying a 2d texture
Specifying a 2D Texture
  • void glTexImage2D ( GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)
    • target- target texture (it must be GL_TEXTURE_2D for OpenGL release 1.0)
    • level - resolution level (it is used for specifying multiple levels-of-detail)
    • internalformat- internal storage format of the texture (it must be 1, 2, 3, 4, or one of a number of symbolic constants;

Specifying a 2D Texture

width - width of the texture image (it must be 2m+2*(border) for some integer m)

height - height of the texture image (it must be 2m+2*(border) for some integer m)

border - the width of the border (either 0 or 1)

format - format of the pixel data (GL_RGBA)

type - data type of the pixel data (GL_UNSIGNED_BYTE)

pixels - pointer to the image data in memory

what if the texture is the wrong size
What if the Texture is the Wrong Size?

glTexImage2D() requires a texture whose pixel dimensions are powers of two

If necessary, use the OpenGL utility routine gluScaleImage() to scale the image

Pass the scaled image to glTexImage2D()

converting a texture image
Converting A Texture Image

OpenGL requires texture dimensions to be powers of 2

If dimensions of image are not powers of 2

gluScaleImage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out );

data_inis source image

data_outis for destination image

Image interpolated and filtered during scaling

scaling texture images
Scaling Texture Images
  • GLint gluScaleImage( GLenum format, GLsizei widthin, GLsizei heightin, GLenum typein, const void *datain, GLsizei widthout, GLsizei heightout, GLenum typeout, void *dataout )
    • format specifies the format of the pixel data
    • widthin, heightin specify the dimensions of the source image to be scaled
    • datain specifies a pointer to the source image
    • typeinand typeout specify the data type for datain and dataout, respectively
texture coordinates
Texture Coordinates
  • Texture coordinates are part of the data that is associated with each vertex.
  • A 2D texture is treated as a 1x1 square whose texture coordinates go from 0.0 to 1.0 in each dimension.
    • Texture coordinates are homogeneous (s, t, r, q)
    • r is unused and q is 1.0

Texture coordinates are assigned to each vertex of a polygon.

    • Assigned explicitly or generated automatically
  • Texture coordinates are interpolated as a polygon is filled.
    • Filters control how the interpolation is performed
setting texture coordinates explicitly
Setting Texture Coordinates Explicitly
  • void glTexCoord2fv( const GLfloat *v )
    • v Specifies a pointer to an array of two, which in turn specify the s, t, r, and q texture coordinates
typical code
Typical Code


glColor3f(r0, g0, b0); //if no shading used

glNormal3f(u0, v0, w0); // if shading used

glTexCoord2f(s0, t0);

glVertex3f(x0, y0, z0);

glColor3f(r1, g1, b1);

glNormal3f(u1, v1, w1);

glTexCoord2f(s1, t1);

glVertex3f(x1, y1, z1);




Note that we can use vertex arrays to increase efficiency

texture parameters
Texture Parameters

OpenGL has a variety of parameters that determine how texture is applied

Wrapping parameters determine what happens if s and t are outside the (0,1) range

Filter modes allow us to use area averaging instead of point samples

Mipmapping allows us to use textures at multiple resolutions

Environment parameters determine how texture mapping interacts with shading

wrapping mode
Wrapping Mode

Clamping: if s,t > 1 use 1, if s,t <0 use 0

Wrapping: use s,t modulo 1










wrap clamp


两种方式可以处理:GL_REPEAT and gl_CLAMP







例如,纹理坐标(1.3, -0.4),采样?


magnification and minification
Magnification and Minification
  • One texel rarely corresponds to one pixel on the final screen image.
  • Magnification and Minification Filter
    • Texture information must be magnified or minified
magnification and minification1
Magnification and Minification







More than one texel can cover a pixel (minification) or

more than one pixel can cover a texel (magnification)

Can use point sampling (nearest texel) or linear filtering

( 2 x 2 filter) to obtain texture values

filter modes p254
Filter Modes P254

Modes determined by

glTexParameteri( target, type, mode )





Note that linear filtering requires a border of an

extra texel for filtering at edges (border = 1)

mipmapped textures
Mipmapped Textures (后面详细讨论)

Mipmapping allows for prefiltered texture maps of decreasing resolutions

Lessens interpolation errors for smaller textured objects

Declare mipmap level during texture definition

glTexImage2D( GL_TEXTURE_*D, level,… )

GLU mipmap builder routines will build all the textures from a given image

gluBuild*DMipmaps( … )

specifying filters
Specifying Filters
  • void glTexParameterf( GLenum target, GLenum pname, const GLfloat param )
    • target specifies the type of the target texture
    • pname specifies which filter to set

param specifies whether speed or image quality is more important

    • GL_NEAREST to choose the texel nearest to the texture coordinate computed for the pixel.
    • GL_LINEAR to use the weighted average of the four texels nearest to the texture coordinate computed for the pixel.
how filters work 1
How Filters Work(1)
  • Magnification
    • Pixels map to less than one texel.
      • If GL_NEAREST is specified, then texel 10 is applied to the pixel.
      • If GL_LINEAR is specified, the weighted average of texels 9, 10, 13 and 14 is applied to the pixel.

How Filters Work(2)

  • Minification
    • Pixels map to more than one texel.
      • If GL_NEAREST is specified, then texel 17 is applied to the pixel.
      • If GL_LINEAR is specified, the weighted average of texels 12, 13, 17 and 18 is applied to the pixel.
enabling texture mapping
Enabling Texture Mapping(注意!)
  • void glEnable( GLenum mode )
    • Set mode to GL_TEXTURE_1D if one-dimensional texturing is performed using glTexImage1D()
    • Set mode to GL_TEXTURE_2D if two-dimensional texturing is performed using glTexImage2D()
    • Set mode to GL_TEXTURE_3D_EXT if three-dimensional texturing is performed using glTexImage3DEXT()
texture c

This program reads in a single image from a .rgb file, and uses it as a texture. It applies the image to a polygon using explicit texture coordinates. The polygon is then made to "swim" across the window.

The name of the file containing the image can be specified on the command line. If no file is specified, the program attempts to open the file fish.rgba in the current directory.

After reading in the image, the program initializes the texture parameters and loads the texture in initTexture(). Before loading the texture, initTexture() determines whether the image is a power of two in each dimension. If not, it uses the utility routine gluScaleImage() to scale the image. Next it sets both the minification and magnification filter to perform nearest neighbor interpolation. Lastly, it calls glTexImage2D() to load the image into texture memory, and enables 2D texturing.

It then applies the texture to a rectangular polygon using explicit texture coordinates.

The function drawScene() simply uses glTexCoord2fv(), to explicitly assign the texture coordinates to the polygon.

texture object
纹理对象(Texture Object)的提出

在默认运算模式下,当前纹理是OpenGL状态的一部分,在执行glTexImage()时,当前纹理从系统内存转移到texture memory ,如果只有一个单独纹理图,上述移动需要一次。



texture objects
Texture Objects 纹理对象




How to Use Texture Object

  • Generate texture names.
    • glGenTextures()
  • Initially bind (create) texture objects to texture data, including the image arrays and texture properties.
    • glBindTexture()
  • Bind and rebind texture objects, making their data currently available for rendering textured models.
    • glBindTexture()
void glgentextures glsize n gluint texnames
void glGenTextures (GLsize n, GLuint *texnames)

Returns n currently unused names for texture objects in the array textureNames. The names returned in textureNames do not have to be a contiguous set of integers.

The names in textureNames are marked as used, but they acquire texture state and dimensionality (1D or 2D) only when they are first bound.

Zero is a reserved texture name and is never returned as a texture name by glGenTextures().

void glbindtexture glenum target gluint texname
void glBindTexture (GLenum target, GLuint texname)
  • glBindTexture() does three things
    • When using textureName of an unsigned integer other than zero 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 textureName value of zero, OpenGL stops using texture objects and returns to the unnamed default texture.

Other related functions

  • void glPrioritizeTextures(GLsize n, const GLuint *texnames, const GLclampf *priorities)
    • Set texture residence priority
  • void glDeleteTextures ( sizei n, uint *textures )
    • Delete texture objects when you are finished with them
tex ture1 c


multiple levels of detail p247
Multiple Levels of Detail P247
  • Objects with textures can be viewed at different distances from the viewpoint
  • You can specify a series of pre-filtered texture maps of decreasing resolution called mipmaps
    • OpenGL automatically determines which texture map to use based on the object's size (in pixels)
As the viewpoint changes, the texture must increase or decrease in size with the size of the object. For example, in texture.c, as the image moves farther away, another sized texture might be more appropriate, rather than trying to filter a large texture down.

If only one resolution of the image is specified, OpenGL has to filter the texture up or down to the appropriate size. A mipmap is an ordered set of pixel arrays representing the same image at progressively lower resolutions.

Using mipmaps allows you to reduce the amount of details on smaller sized objects.

You want to design the mipmaps in such a way that the transition from one to the next is smooth, so that you do not get shimmering and flashing effects

creating mipmaps automatically
Creating Mipmaps Automatically


You can use the OpenGL Utility routine gluBuild2DMipmaps() to build and load mipmaps automatically.

GLint gluBuild2DMipmaps( GLenum target, GLint internalformat, GLsizei width, GLint height, GLenum format, GLenum type, void *data )


GLint gluBuild2DMipmaps((GLenum target, GLint internalformat, GLsizei width, GLint height, GLenum format, GLenum type, void *data )

  • target specifies the target texture
  • internalformat internal storage format of the texture
  • width, height specify the dimensions of the texture
  • format specifies the format of the pixel data
  • type specifies the data type for data
  • data specifies a pointer to the image data in memory

void glTexParameterf( target, GL_TEXTURE_MIN_FILTER, param )





void gltexenv glenum target glenum pname const glfloat param
void glTexEnv( GLenum target, GLenum pname, const GLfloatparam)
  • Specifies how the texture colors interact with the original polygon colors.
    • targetis GL_TEXTURE_ENV
      • If pname is GL_TEXTURE_ENV_MODE, param specifies a texture function
        • 表示纹理值如何与被处理的片段颜色值混合
      • If pname is GL_TEXTURE_ENV_COLOR, param is a pointer to the RGBA values used with the GL_BLEND texture function
        • Default is ( 0, 0, 0, 0 )
texture environment and transparency
Texture Environment and Transparency
  • Modulate
    • Cv = Ct * C f
    • Av = At * Af
      • Use a white polygon, so the resulting color equals the color of the texture
      • The resulting alpha value will be zero where the texture has a zero alpha
      • Use alpha test to remove fragments with alpha close to zero


    • Cv = Ct
    • Av = At
      • Polygon color does not matter
      • Both the color and alpha values will be replaced by the texture values


Choose an appropriate texture environment (GL_MODULATE or GL_REPLACE)

Enable alpha testing to remove alpha values that are close to zero glAlphaFunc( GL_GREATER, 0.1 ); glEnable( GL_ALPHA_TEST );

Set the blending function and enable blending to create smooth edges glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND);a

automatic texture coordinate generations
Automatic Texture Coordinate Generations
  • OpenGL can automatically generate three types of texture coordinates:
    • Object Linear: the texture image remains fixed to an object
    • Eye Linear: a moving object appears to swim through a texture
    • Sphere Map: used for environment mapping
generating texture coordinates
Generating Texture Coordinates
  • There are three steps to having OpenGL generate texture coordinates automatically
  • First, specify the texture coordinate generation functions for S and T:


Second, enable texture coordinate generation: glEnable( GL_TEXTURE_GEN_S ); glEnable( GL_TEXTURE_GEN_T );



Third, generate the texture coordinates. For object or eye linear, they are based on a reference plane: static GLfloat sgenparams[] = { 2, 0, 0, 0 }; static GLfloat tgenparams[] = { 0, 1, 0, 0 };glTexGenfv( GL_S, GL_OBJECT_PLANE, sgenparams );glTexGenfv( GL_T, GL_OBJECT_PLANE, tgenparams );

mapping textures smoothly onto 3d objects
Mapping Textures Smoothly onto 3D Objects

Automatic texture coordinate generation has limits, since it is mapping relative to a plane

You need to provide explicit texture coordinates to improve the mapping

3D modeling and animation software generally create these for you

GLU, the OpenGL Utility library, can map a texture smoothly onto its quadric objectsGLUquadricObj* gluNewQuadric( void )

a textured quadric
A Textured Quadric
  • void gluQuadricTexture( GLUquadric* quad, GLboolean texture )
    • quad specifies the quadrics object (created with gluNewQuadric())
    • texture is a flag indicating if texture coordinates should be generated (GL_TRUE, GL_FALSE)


  • Example:
  • GLUquadricObj *quadObj;
  • quadObj = gluNewQuadric(); gluQuadricDrawStyle( quadObj, GLU_FILL );
  • gluQuadricTexture( quadObj, GL_TRUE );
  • /* ... in the draw routine */
  • gluSphere( quadObj, 0.75, 32, 32 );