An Interactive Introduction to OpenGL Programming by yurtgc548

VIEWS: 4 PAGES: 38

									   Texture Mapping

Creating more realistic and
      exciting images
Texture                 Poly.
                                 Per
                                Vertex



Mapping
                  CPU    DL               Raster   Frag   FB
                                Texture
                        Pixel




Apply a 1D, 2D, or 3D image to geometric
   primitives
Uses of Texturing
  • simulating materials
  • reducing geometric complexity
  • image warping
  • reflections
                  2
Texture Mapping


           y


   z           x
                       geometry         screen



       t
                                image


                   s        3
Texture Mapping and the
OpenGL Pipeline

Images and geometry flow through separate
  pipelines that join at the rasterizer
  • “complex” textures do not affect geometric
    complexity

     vertices    geometry pipeline
                                     rasterizer
      image        pixel pipeline

                       4
Texture Mapping

• Adds extra information from an array to
 graphical objects at the last stage of
 drawing




                   5
A Texture Map Is…

• An array of values stored in a particular
  memory area in the graphics system
• The dimension of the texture map is the
  dimension of the array
• The values in the texture map are used in
  the final computation of the colors of each
  pixel


                    6
Texture Mapping Needs…

• A graphics object that has “texture
  coordinates” that associate a vertex with
  texture values
• An array of values (usually color, but it can
  have other meanings)
• An association of each vertex with the
  texture so it can get a value from the array


                    7
Texture Coordinates..

• Texture coordinates are 1D, 2D, or 3D
  vectors with values in [0,1]
• Each vertex in you model may have not
  only geometric coordinates but also
  texture coordinates
• The values in texture coordinates refer
  proportionately to indices in the texture
  array

                    8
Creating Texture Maps

• From an image: digital photo or scan




                   9
Creating Texture Maps

• Synthetically, from a program that writes
 values to a file




                    10
Format for Texture Files

• Any image file (.bmp, .gif, .jpg..)could be
  used to hold a texture map
• The only question is reading the file and
  writing the contents to memory
• If you use sophisticated files, you may
  need to use tools to read the files



                    11
Texture Example

The texture (below) is a
 256 x 256 image that has been
 mapped to a rectangular
 polygon which is viewed in
 perspective




                  12
Applying Textures I

Three steps
   specify   texture
     • read or generate image
     • assign to texture
     • enable texturing
   assign   texture coordinates to vertices
   specify   texture parameters
     • wrapping, filtering
                           13
Applying 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

                              14
Texture 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 );




                         15
Texture Objects (cont.)

Create texture objects with texture data and
  state
  glBindTexture( target, id );
Bind textures before using
  glBindTexture( target, id );




                     16
Specify Texture             Poly.
                                     Per
                                    Vertex



Image
                      CPU    DL               Raster   Frag   FB
                                    Texture
                            Pixel




 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
                      17
Converting 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


                     18
Mapping a                                 Poly.
                                                   Per
                                                  Vertex



Texture
                                   CPU     DL                Raster   Frag   FB
                                                  Texture
                                          Pixel




Based on parametric texture coordinates
glTexCoord*() specified at each vertex
            Texture Space                             Object Space
     t                      1, 1                            (s, t) = (0.2, 0.8)
  0, 1                                                                 A
            a


                      c                  (0.4, 0.2)
                b
                                                  B                         C
     0, 0                 1, 0 s                                      (0.8, 0.4)

                                   19
Generating Texture Coordinates

Automatically generate texture coords
                    glTexGen{ifd}[v]()
specify a plane
  • generate texture coordinates based upon distance from
    plane   Ax  By  Cz  D  0
generation modes
  • GL_OBJECT_LINEAR
  • GL_EYE_LINEAR
  • GL_SPHERE_MAP
                          20
Tutorial: Texture




                21
Some Details

• Texture coordinates that lie outside of
 [0,1] -- wrapping or clamping




                   22
Texture Application Methods

Filter Modes
  • minification or magnification
  • special mipmap minification filters
Wrap Modes
  • clamping or repeating
Texture Functions
  • how to mix primitive’s color with texture’s color
     • blend, modulate or replace texels

                            23
Filter Modes

 Example:
   glTexParameteri( target, type, mode );




  Texture           Polygon        Texture            Polygon
       Magnification                         Minification


                              24
Sampling

A pixel maps to a non-rectangular region
Usually only perform map on centre of pixel
Still have a problem of over-sampling (same
  texel maps to several pixels) or under-
  sampling the image (pixels only sparsely
  sample the texels)



                   25
Filtering

Nearest neighbor        Bilinear




Pick pixel with         Weighted average based
closest centre           on distance to pixel
                         centre

                   26
Filtering

Bilinear filtering solves (partially) the
  oversampling problem since it provides
  smooth shading between pixels
                scanline




                   27
Wrapping Mode

 Example:
   glTexParameteri( GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_S, GL_CLAMP )
   glTexParameteri( GL_TEXTURE_2D,
        GL_TEXTURE_WRAP_T, GL_REPEAT )

      t


                    s
                        GL_REPEAT   GL_CLAMP
          texture        wrapping    wrapping
                            28
Mipmapped Textures

Mipmap allows for prefiltered texture maps of decreasing
  resolutions
Lessens interpolation errors for smaller textured objects
Declare mipmap level during texture definition
   glTexImage*D( GL_TEXTURE_*D, level, … )

GLU mipmap builder routines
   gluBuild*DMipmaps( … )

OpenGL 1.2 introduces advanced LOD controls



                         29
Mip-Mapping

When undersampling we use mippmapping
Resample image at lower resolution
Create a “pyramid” of textures.
Interpolate texture between two adjacent
  layers




                  30
Texture Pyramid




              31
Texture Functions

Controls how texture is applied
glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param )
GL_TEXTURE_ENV_MODE modes
  • GL_MODULATE
  • GL_BLEND
  • GL_REPLACE
Set blend color with GL_TEXTURE_ENV_COLOR

                   32
Environment Mapping

• Environment maps can be made with the
  sphere map capability
• Texture map is made with a spherical filter




                   33
Environment Mapping


                   Don’t modulate diffuse color
                   modulate color based on
                   normal
                   Effectively creates a reflective
                   object
                   The environment map is a
                   surrounding texture




              34
Bump Maps

Use a map to perturb the normal for
 specular lighting




                  35
Perspective Correction Hint

Texture coordinate and color interpolation
  • either linearly in screen space
  • or using depth/perspective values (slower)
Noticeable for polygons “on edge”
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, hint )
  where hint is one of
     • GL_DONT_CARE
     • GL_NICEST
     • GL_FASTEST

                           36
Is 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


                                   37
Texture 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

                                 38

								
To top