- Forward


Texture Mapping in OpenGL
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Steps in the Initialization Process
Back SMYC Forward
  • Name the texture
  • Bind the texture to its name
  • Specify the parameters to use
  • Specify an application mode (either replace or modulate)
  • Create the texture object
  • Enable texture mapping
Steps in the Rendering Process
Back SMYC Forward
  • Select a texture
  • Specify a mapping from the texture to pixels
Naming a Texture
Back SMYC Forward
  • The Function:
    • void glGenTextures(GLsizei n, GLuint* names)
  • Purpose:
    • Returns n unused "names" (actually integers)
    • The "names" need not be contiguous
Naming a Texture (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: name)
 
Binding a Texture to a Name
Back SMYC Forward
  • The Function:
    • void glBindTexture(GLenum target, GLuint name)
  • The Target:
    • Either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D or GL_TEXTURE_CUBE_MAP
Binding a Texture to a Name (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: bind)
 
Specifying Parameters
Back SMYC Forward
  • The Function:
    • void glTexParameter*(GLenum target, GLenum name, TYPE value)
    • where the target is either GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D or GL_TEXTURE_CUBE_MAP
  • Wrapping:
    • Name: GL_TEXTURE_WRAP_S or GL_TEXTURE_WRAP_T (the \(s\) and \(t\) directions)
    • Value: GL_CLAMP, GL_REPEAT, (GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER, GL_MIRRORED_REPEAT)
  • Border Color:
    • Name: GL_TEXTURE_BORDER_COLOR
    • Value: RGBA
  • Magnification and Minification:
    • Name: GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER
    • Value: GL_NEAREST and GL_LINEAR (and others)
Specifying Parameters (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: parameters)
 
Specifying the Application Mode
Back SMYC Forward
  • The Function:
    • void glTexEnvf(GLenum target, Glenum name, TYPE value)
    • where target is GL_TEXTURE_ENV and name is GL_TEXTURE_ENV_MODE
  • Values:
    • GL_REPLACE, GL_ADD, GL_COMBINE, or GL_BLEND
    • The last three are different ways of combing source and destination pixels
Specifying the Application Mode (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: mode)
 
Creating the Texture Object
Back SMYC Forward
  • The Function:
    • void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint borderWidth, GLenum format, Glenum type const GLvoid* texels)
    • where target is GL_TEXTURE_2D (or one of several other values)
  • Level:
    • 0 when one resolution is supplied
  • Internal Format:
    • 1..4 to indicate which components are included or one of many symbolic constants
  • Format:
    • GL_RGB, GL_RGBA, GL_COLOR_INDEX (or others)
  • Deleting:
    • Use glDeleteTextures()
Creating the Texture Object (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: create)
 
Enabling Texture Mapping
Back SMYC Forward
  • The Function:
    • void glEnable(GLenum capability)
  • Capabilities:
    • GL_TEXTURE_2D, GL_TEXTURE_3D, (or one of several others)
Enabling Texture Mapping (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: enable)
 
Rendering
Back SMYC Forward
  • Selecting a Texture:
    • Use glBindTexture() again
  • Mapping to Texels:
    • glTexCoord*(TYPE coords)
Rendering (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: render)
 
Reading Images
Back SMYC Forward
  • An Observation:
    • This is not OpenGL's responsibility
  • Utilities:
    • GLUT has functions for reading OBJ model files which includes a function glmReadPPM() that reads .ppm files
    • SDL has a function SDL_LoadBMP() for reading .bmp files
    • Many other utilities exist
Reading Images (cont.)
Back SMYC Forward

An Example

openglexamples/textures/image.c (Fragment: image)
 
Using Multiple Textures
Back SMYC Forward
brick brownpaper flagstone
frostedglass oak lake
Using Multiple Textures (cont.)
Back SMYC Forward

Initialization

openglexamples/textures/multiple.c (Fragment: init)
 

Rendering

openglexamples/textures/multiple.c (Fragment: render)
 
Manipulating Textures
Back SMYC Forward
  • Scaling:
    • gluScaleImage()
  • Copying:
    • glCopyTexImage2D()
  • Parts:
    • glTexSubImage2D()
Non-2D Textures
Back SMYC Forward
  • One-Dimensional:
    • Essentially a 2D texture with a height of 1
  • Three-Dimensional:
    • Different grids at different depths
    • Mostly used for medical imaging
Mipmaps
Back SMYC Forward
  • Initializing:
    • Use glTexParameter*() with a GL_TEXTURE_MIN_FILTER of either GL_NEAREST_MIMPAP_NEAREST, GL_NEAREST_MIMPAP_LINEAR, GL_LINEAR_MIMPAP_NEAREST or GL_LINEAR_MIMPAP_LINEAR
  • Example:
    • See the "Red Book"
Texture Mapping onto a Sphere
Back SMYC Forward
  • We Need a Quadric:
    • A quadric (sometimes called a quadric surface) is the locus of zeros of a quadratic polynomial (i.e., letting \(\bs{Q}\) be an \(n \times n\) matrix, \(\bs{r}\) be an \(n\)-dimensional point, and \(s\) be a scalar, the set of \(\bs{p}\) that satisfy \(\bs{p}^T \bs{Q} \bs{p} + \bs{p}^T r + s = 0\)
  • We Need an Appropriate Texture:
    • A cylindrical projection
    • earth-texture
Texture Mapping onto a Sphere (cont.)
Back SMYC Forward

The Display Callback

openglexamples/textures/sphere.c (Fragment: display)
 
Texture Mapping onto other Shapes
Back SMYC Forward
  • We Need the Shape:
    • Teapots are always good!
  • We Need an Appropriate Texture:
    • Something irregular works best
    • abstract-texture
Texture Mapping onto other Shapes (cont.)
Back SMYC Forward

The Display Callback

openglexamples/textures/teapot.c (Fragment: display)
 
There's Always More to Learn
Back -