Texture Mapping - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Texture Mapping

Description:

Idea: associate each (x, y, z) with a point (s, t) through a function ... Sometimes, several texels map to the same point on the surface (minification) ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 18
Provided by: mm197
Category:

less

Transcript and Presenter's Notes

Title: Texture Mapping


1
Texture Mapping
  • OpenGL Part III

2
Texture Maps
  • An array of pixels (an image)
  • Coordinates (s, t)
  • Sometimes called texels (texture elements)
  • Geometric Object
  • Has coordinates (x, y, z) for each point on
    surface
  • Idea associate each (x, y, z) with a point (s,
    t) through a function
  • Typically define texture coordinate for each
    vertex

3
Building Texture Maps
  • Define an image (either procedural or from image)
  • Define texture coordinates when defining geometry
  • Define parameters for how the texture should be
    applied

4
1. Defining an Image
  • GLubyte myimage64643
  • void initImage()
  • int c
  • for (int i 0 i lt 64 i)
  • for (int j 0 j lt 64 j)
  • c ((((i0x8)0)((j0x8))0))255
  • myimageij0 (GLubyte) c
  • myimageij1 (GLubyte) c
  • myimageij2 (GLubyte) c

5
1. Defining an Image
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0,
    GL_RGB, GL_UNSIGNED_BYTE, myimage)
  • GL_TEXTURE_2D its a 2D image
  • 0 level of texture map (mipmapping)
  • GL_RGB internal image format (GL_RGBA)
  • 64 rows in the texture (must be 2x)
  • 64 cols in the texture (must be 2x)
  • 0 border width
  • GL_RGB type of texels
  • GL_UNSIGNED_BYTE format of texels
  • myimage pointer to array of image data

6
2. Defining Texture Coordinates
  • glBegin (GL_QUADS)
  • glTexCoord2f (0.0, 0.0)
  • glVertex3f(-1.0f, -1.0f, 0.0f)
  • glTexCoord2f (0.0, 1.0)
  • glVertex3f(-1.0f, 1.0f, 0.0f)
  • glTexCoord2f (1.0, 1.0)
  • glVertex3f(1.0f, 1.0f, 0.0f)
  • glTexCoord2f(1.0, 0.0f)
  • glVertex3f(1.0f, -1.0f, 0.0f)
  • glEnd()

7
Examples
Note Normals and texture coordinates are already
defined in glutSolidTeapot()
8
3. Texture Parameters
  • Usually, texture coordinates range from 0.0 to
    1.0
  • Two modes
  • Sometimes, we have to mod to get a texture
    coordinate (i.e. 1.1 becomes 0.1). This is
    GL_REPEAT
  • Sometimes, we are strict, and round up or down
    (-0.4 become 0, 1.7 becomes 1). This is GL_CLAMP

9
Examples
Note I changed the texture coordinates to go
from 0.0 to 2.0 on purpose
10
3. Texture Parameters
  • Sometimes, several texels map to the same point
    on the surface (minification)
  • Sometimes, one texel maps to several points on
    the objects surface (magnification)
  • Can specify to use the NEAREST or we can use
    bilinear filtering (blurring)

11
Note on Texture Parameters
Passing GL_LINEAR
Passing GL_NEAREST
12
3. Texture Parameters(only do this once)
  • 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_FILT
    ER, GL_NEAREST)
  • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILT
    ER, GL_NEAREST)

Note we could use GL_CLAMP for our wrapping
parameter. This cuts off anything above 1.0 or
0.0
13
glHint
  • We have the option to request either speed or a
    better-looking image
  • // Could be GL_FASTEST
  • glHint(GL_PERSPECTIVE_CORRECTION_HINT,
    GL_NICEST)

14
Texture Objects
  • By default, there is a current texture on the
    graphics card
  • What if there are two textures?
  • Constantly updating the texture (slow)
  • Must reload the texture each time!
  • Instead, create a texture object
  • Store a texture (and parameters) on the card
  • Each texture is then assigned a unique ID

15
Typical Example
  • glGenTextures (5, myid)
  • // Start binding a texture, and store on
    graphics card
  • glBindTexture (GL_TEXTURE_2D, myid)
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64,
    0, GL_RGB, GL_UNSIGNED_BYTE, myimage)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_S, GL_CLAMP)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_T, GL_CLAMP)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  • // ----------------------------All the way down
    to here-------------------------------------------
  • // Get a second number
  • glGenTextures (5, yourid)
  • glBindTexture (GL_TEXTURE_2D, yourid)
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64,
    0, GL_RGB, GL_UNSIGNED_BYTE, yourimage)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_S, GL_CLAMP)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_T, GL_CLAMP)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  • glTexParameteri(GL_TEXTURE_2D,
    GL_TEXTURE_MIN_FILTER, GL_NEAREST)

16
  • void display()
  • glClear (GL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_BIT)
  • glEnable(GL_TEXTURE_2D)
  • glMatrixMode(GL_MODELVIEW)
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslatef(-1.0, 0.0, -5.0)
  • glPushMatrix()
  • glRotatef (deg, 0.0, 1.0, 0.0)
  • glBindTexture(GL_TEXTURE_2D, myid)
  • glutSolidTeapot (1.0)
  • glPopMatrix()
  • glPushMatrix()
  • glTranslatef(2.0, 0.0, 0.0)
  • glRotatef(deg, 0.0, 1.0, 0.0)
  • glBindTexture(GL_TEXTURE_2D, yourid)
  • glutSolidTeapot (1.0)
  • glPopMatrix()

17
Example
Write a Comment
User Comments (0)
About PowerShow.com