Texture Mapping
  • Topics:
    • Definition of texture mapping
    • Specifying texture images
    • Controlling filtering
    • Manage texture images
    • Color blending 
    • Texture coordinates
    • Automatic texture coordinate generation

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  What is Texture Mapping
  • So far every geometric object has been drawn as either solid colors or smooth shading of colors (specified per-vertex)
  • Texture mapping is a great hack to allow higher realism in scenes with lower polygon counts
  • Compared to smooth shading, texture mapping is a fairly-complicated processes
    • Design decisions must be made regarding texture application
  • Textures are rectangular arrays of data (texels) applied to geometric data


  •  (notice the texture "distortion" when applied to the polygon)
  • Because texel<->pixel mapping is not necessarily 1-to-1 filtering parameters specify the texture mapping

  •  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Steps in Texture Mapping
 
 

    Four Steps:
  1. Create a texture object and specify a texture for that object
  2. Indicate how the texture is to be applied to each pixel
  3. Enable texture mapping
  4. Draw the scene, supplying both texture and geometric coordinates

  5.  

     
     
     

    Note!  Texture mapping only works in RGBA mode (yea!)
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Create a Texture Object

Image from http://www.multigen.com/products/sensorvision.htm
  • Textures are commonly two-dimensional, but can also be one-dimensional.
  • The texture data itself may consist of 1->4 components
  • Example of 1-D texture:


  •  
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Indicate How the Texture is applied & Enabling Texturing
The final RGBA value is affected by the texture in four possible ways:
  1. Decal - Replace the color with the texture color
  2. Replace - Variant of decal: addresses the stencil buffer
  3. Modulate - The texture scales the fragment color
  4. Constant Blend - A constant color is blended with the texture
 Texturing must be enabled with glEnable(GL_TEXTURE_1D) or glEnable(GL_TEXTURE_2D)
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Drawing the Scene w/Texture and Geometric Coordinates
  • Texture alignment must be specified for the geometry
  • Texture coordinates range from 0.0 -> 1.0 on both axes

  •  
  •  Texures may be specified to repeat, or clamp

  •  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  OpenGL Texture-specific calls
void init(void)

   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel(GL_FLAT);
   glEnable(GL_DEPTH_TEST);

   makeCheckImage();
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   glGenTextures(1, &texName);
   glBindTexture(GL_TEXTURE_2D, texName);

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                   GL_NEAREST);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_NEAREST);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
                checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                checkImage);
}
 
 

     
     
     
     
     
     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  OpenGL Texture-specific calls (cont)

    void display(void)
    {
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glEnable(GL_TEXTURE_2D);
       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
       glBindTexture(GL_TEXTURE_2D, texName);
       glBegin(GL_QUADS);
       glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
       glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
       glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
       glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);

       glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
       glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
       glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
       glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
       glEnd();
       glFlush();
       glDisable(GL_TEXTURE_2D);
    }
     
     

     
     
     
     
     
     
     
     
     
     

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Specifying the Texture
  • glTexImage2D() defines a two-dimensional texture

  •  
void glTexImage2D(GLenum target, GLint level, 
GLint internalFormat,GLsizei width, GLsizei height, 
GLint border,GLenum format, GLenum type, 
const GLvoid *pixels);
     
     
  •  glPixelStorei(GL_UNPACK_ALIGNMENT, 1) is made because the data in the example isn't padded at the end of each row
  • The number of texels for width & height must be powers of two (deal with it!) 
  • Use gluScaleImage() to re-format existing images which are not 2n

  •  

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

  Framebuffer as Texture Source
  • The framebuffer itself can be used as a source for texture data
  • glCopyTexImage2D() will copy pixel data from the framebuffer and use it as a texture

  •  

     
     
     
     
     
     
     

    void glCopyTexImage2D(GLenum target, GLint level,
    GLint internalFormat, GLint x, GLint y, GLsizei width, GLsizei height,GLint border);