OpenGL Texture mapping glBindTexture GLenum target GLuint textureName Creates new by omahafunk

VIEWS: 1,554 PAGES: 12

									                 OpenGL Texture mapping                                                                          Texture Mapping
                                                                                  •   Paste the image on the surface of the 3D model.
•   glBindTexture(GLenum target, GLuint textureName) - Creates new texture        •   Aims: Increase the realistic effect of the 3D Objects.
    objects and binds them to the name given. Target is GL_TEXTURE_1D/2D          •   Example:
    - which is the type of the texture. This function declares which texture is
    the current one.
•   glTexParameter{if}(GLenum target, GLenum pname, TYPE param) - Sets
    various parameters of the texture:
     – GL_TEXTURE_WRAP_S/T - determines if the texture will be drawn
        once - GL_CLAMP or it will be repeated GL_REPEAT (S,T - two
        directions).
     – GL_TEXTURE_MAG/MIN_FILTER - determines the filter that will be
        used to magnify/minimize the texture. Will there be constant -
        GL_NEAREST or linear - GL_LINEAR interpolation.




                                                                                                                                               1
                     Texture mapping in OpenGL                                                            Texture mapping in OpenGL
                                                                            You may
In Init(),                                                                  check their                                                            (0,0)    (1,0)
 In Init(),
     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
                                                                            meaning           GLubyte rgb = {255, 255, 255, 0, 0, 0,

     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);           yourself!!!                    0, 0, 0, 255, 255, 255};
                                                                                              int teximageWidth = 2, teximageHeight = 2;
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
                                                                                          In display(),                                            (0,1)    (1,1)
                                                                        Load texture
      glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);                            glEnable(GL_TEXTURE_2D);

                                                                        into texture
                                                                                              glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth,
                                                                                              teximageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb);
                                                                           memory             glBegin(GL_POLYGON);
 In display(),
In display(),                                                                                    glTexCoord2f(0.0, 0.0); glVertex3fv(vex0);
     glEnable(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_2D);                                                                     glTexCoord2f(1.0, 0.0); glVertex3fv(vex1);         vex0   vex1
     glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth, teximageHeight, 0, GL_RGB,
    glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth, teximageHeight, 0, GL_RGB,                  glTexCoord2f(1.0, 1.0); glVertex3fv(vex2);
     GL_UNSIGNED_BYTE, rgb);
    GL_UNSIGNED_BYTE, rgb);                                                                      glTexCoord2f(0.0, 1.0); glVertex3fv(vex3);
     /* assign texture map to objects */
    /* assign texture map to objects */
     glDisable (GL_TEXTURE_2D);                 teximageWidth - texture image width           glEnd();
    glDisable (GL_TEXTURE_2D);                                                                glDisable(GL_TEXTURE_2D);
                                           teximageHeight - texture image height
                                           rgb - GLubyte array of RGB values                                                                        vex3   vex2




                                                                                                                                                                    2
                      Loading Bitmap Texture                                          Loading Bitmap Texture
                                                                     GLubyte *teximage;
 • bitmap.c & bitmap.h provide routines to read bmp files, and you
                                                                     int teximageWidth, teximageHeight;
   can use:
                                                                     void Init(){
GLubyte* TextureLoadBitmap(char* filename, int *w, int *h);            ...
                                                                       teximage = TextureLoadBitmap(“test.bmp”,
                                                                            &teximageWidth, &teximageHeight);
     – filename specify the bmp file to read
                                                                     }
     – After executed, w & h will contain the image width & height
        while the raw RGB byte is returned                           void display() {
                                                                       ...
 • Then you can load the texture bytes to texture memory               glEnable(GL_TEXTURE);
   by:                                                                 glTexImage2D(GL_TEXTURE_2D, 0, 3,
                                                                         teximageWidth, teximageHeight, 0,
                                                                         GL_RGB, GL_UNSIGNED_BYTE, teximage);
     glTexImage2D(GL_TEXTURE_2D, 0, 3,                                 /* assign texture to object */
         teximageWidth, teximageHeight, 0,                             glDisable(GL_TEXTURE);
         GL_RGB, GL_UNSIGNED_BYTE, teximage);                        }




                                                                                                                  3
                        Steps in Texture mapping                                              Loading Multiple Texture Maps
             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);       Step 1:
In init(),   glTexParameteri(GL_TEXTURE_2D,               Indicate how the      GLubyte *teximage1, *teximage2;
                     GL_TEXTURE_WRAP_S,GL_REPEAT);        texture is applied    int w1, h1, w2, h2;
             glTexEnvf(GL_TEXTURE_ENV,
             GL_TEXTURE_ENV_MODE, GL_MODULATE);                                 void Init(){
                                                                                  ...
                                                          Step 2: Enable          teximage1 = TextureLoadBitmap(“image1.bmp”, &w1, &h1);
                                                          texture mapping         teximage2 = TextureLoadBitmap(“image2.bmp”, &w2, &h2);
                                                                                }
     Step 3:              glEnable(GL_TEXTURE_2D);
     Specify the                                                                void display() {
                          glTexImage2D(GL_TEXTURE_2D, 0, 3, teximageWidth,
                                                                                  ...
     texture                     teximageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE,
                                                                                  glTexImage2D(GL_TEXTURE_2D,   0, 3, w1, h1, 0,
                          rgb);
                                                                                    GL_RGB, GL_UNSIGNED_BYTE,   teximage1);
                          glBegin(GL_POLYGON);
                                                                                  gluSphere(sphere1, 0.5, 30,   30);
Step 4: Draw the                  glTexCoord2f(0.0, 0.0); glVertex3fv(vex0);
                                                                                  ...
scene, supply texture             glTexCoord2f(1.0, 0.0); glVertex3fv(vex1);
                                                                                  glTexImage2D(GL_TEXTURE_2D,   0, 3, w2, h2, 0,
& geometric                       glTexCoord2f(1.0, 1.0); glVertex3fv(vex2);
                                                                                    GL_RGB, GL_UNSIGNED_BYTE,   teximage2);
                                  glTexCoord2f(0.0, 1.0); glVertex3fv(vex3);
coordinates               glEnd();
                                                                                  gluSphere(sphere2, 1.5, 30,   30);
                                                                                }
                          glDisable(GL_TEXTURE_2D);         In display(),




                                                                                                                                           4
                Loading Bitmap Texture                                                        Wrapping Mode
                                                                       • Example:
• Be careful of the Texture Dimension !                                  glTexParameteri( GL_TEXTURE_2D,
   – Must be 2n ,where n is any integer                                       GL_TEXTURE_WRAP_S, GL_CLAMP )
                                                                         glTexParameteri( GL_TEXTURE_2D,
   – gluScaleImage to scale the image like                                    GL_TEXTURE_WRAP_T, GL_REPEAT )
      • OpenGL will do it for you when you texture map on an object.
                                       m
                                   2
                                                                            t


              n
            2                                                                             s
                                                                                                 GL_REPEAT     GL_CLAMP
                                                                                texture
                                                                                                  wrapping      wrapping




                                                                                                                           5
                   Texture Functions                                 Is There Room for a Texture?

• Need to controls how texture is applied               • Query largest dimension of texture image
• Mode of operation is called Modulation                   – typically largest square texture
       glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param )      – doesn’t consider internal format size

• GL_TEXTURE_ENV_MODE modes                                glGetIntegerv( GL_MAX_TEXTURE_SIZE, &size )
   – GL_MODULATE                                        • Texture proxy
   – GL_BLEND                                              – will memory accommodate requested texture size?
   – GL_REPLACE                                            – no image specified; placeholder
• Set blend color with GL_TEXTURE_ENV_COLOR                – 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




                                                                                                                                  6
                       Texture Residency                                                      Enhancing the Realism

• Working set of textures                                                   • We can further improve realism in rendered images using a
   –   high-performance, usually hardware accelerated                         series of hacks:
   –   textures must be in texture objects                                     – Texture Mapping: modulate the colour of a surface according to a
   –   a texture in the working set is resident                                  function or an image mapped to the surface of the object.
   –   for residency of current texture, check GL_TEXTURE_RESIDENT stat e      – Bump Mapping: modulate the normal to the object according to a
                                                                                 function or an image. This is excellent at producing bumpy or crinkled
• If too many textures, not all are resident                                     surfaces without an increase in the model complexity.
   – can set priority to have some kicked out first                            – Displacement Mapping: modulate the heightof a surface along the
   – establish 0.0 to 1.0 priorities for texture objects                         normal according to a function or an image. This method actually creates
                                                                                 new geometry and is thus the most expensive of the 3 methods.
                                                                               – Environment Mapping: modulate the colour of a surface according to
                                                                                 the normal to the surface. Good for getting cheap reflection effects.




                                                                                                                                                            7
                     Parameter Space
                                                                                          Texture Mapping
• For these methods we require a mapping from world co-
  ordinates to the parameter space of the object.
                                                                     • Once we’ve determine the parameter co-ordinates of an
• The idea is that we therefore allow a mapping from a rectalinear     intersection point on a surface, we use these to index into an
  co-ordinate system (i.e. an image) back onto the surface of the      image and assign the colour to the object’s diffuse colour at that
  object.                                                              point:                                    Image
                                 canonical parameter space
               θ

                     h           u
                                                                                                         v
                                       v


                              u = fu (θ ) θ = fθ ( x, y , z )                                                       u
                              v = fv (h ) h = f h ( x, y, z )
                                                                                       World Space
                                                                                                              Parameter Space




                                                                                                                                            8
                              Distortion                                                        Other Common Mappings
• A big problem is the distortion introduced by some mappings
   – in particular the sphere mapping is bad at the poles
• To reduce this we often use intermediate mappings.
   – This is also necessary for objects with no obvious parameterisat ions:




                                                                              shrink-wrapping                           planar mapping




                                                                                                                                         9
                                                                                                        Bump Mapping
                         Bump Mapping                                         • Rather than modulate the colour we can modulate the direction
                                                                                of the normal to give the impression of an uneven surface.
•   Simulates the appearance of small surface details by changing the            – the geometry itself is not altered so the silhouette edge will remain the
                                                                                   same as before:
•   surface normals before shading.
•   The normal changes are specified as a 2-D bump map.
                                                                                                                r r r
•   No need to represent “the bumps” geometrically.                                                             n′ = n + d
•   Silhouettes remain unchanged.
•    No occlusions take place despite the illusion of surface displacement.
•   Simulates the appearance of smallsurface details by changing the
    surface details by changing the surface normal before shading.
                                                                                   r ∂f ∂f
•   The normal changes are specified as a 2-D bump map bump map.                   n=    ×             f(u,v) is the parametric form of the surface. We will add a
                                                                                      ∂ u ∂v           small displance to this surface in the direction of the normal:
                                                                                                                                        r
                                                                                                     ′(u , v ) = f (u , v ) + d (u , v) r
                                                                                                                                        n
                                                                                                   f
                                                                                                                                        n




                                                                                                                                                                         10
                      Bump Mapping                                 Environment Mapping

• The normal to this new surface is given by:
                        r ∂f ′ ∂f ′
                        n′ =    ×
                             ∂u ∂v
• If we assume that the distance distance d is small compared to
  the spatial extent of the surface we end up with an expression
  for the new normal:
                           r ∂f        r ∂f
              r r ∂d n × ∂ v ∂ d n × ∂u             r
              n′ = n +        r +        r         d
                       ∂u n         ∂v n




                                                                                         11
12

								
To top