Gholamhossein Tavasoli @ ZNU

Gholamhossein Tavasoli @ ZNU
 Texture mapping
 Implemented in hardware on every GPU
 Simplest surface detail hack, dating back to the ‘60s GE flight
simulator and its terrain generator
 Technique:
 “Paste” photograph or bitmap (the texture, for example: a brick
pattern, a wood grain pattern, a sky with clouds) on a surface to
add detail without adding more polygons.
 Map texture onto the surface, get the surface color or alter the
object’s surface color
 Think of texture map as stretchable contact paper
 How do we add more detail to a model?
 Add more detailed geometry; more, smaller triangles:
 Pros: Responds realistically to lighting, other surface interaction
 Cons: Difficult to generate, takes longer to render, takes more memory space
 Map a texture to a model:
 Pros: Can be stored once and reused, easily compressed to reduce size, rendered very quickly,
very intuitive to use, especially useful on far-away objects, terrain, sky,…
 Cons: Very crude approximation of real life. Texture mapped but otherwise unaltered surfaces
still look smooth.
 What can you put in a texture map?
 Diffuse, ambient, specular, or any kind of color
 Specular exponents, transparency or reflectivity coefficients
 Surface normal data (for normal mapping or bump mapping)
 Projected reflections or shadows
 A texture is just a bitmap image
 2D array - texture[height][width][4]
 Pixels of the texture are called texels
 Texture coordinates are in 2D, in the range [0,1]
 A technique to handle variations of reflectance properties of an object surface
by storing the reflectance as a function or a pixel-based image and
“mapping” it onto a surface.
 The image is called a texture map
 The texture map is defied in its own coordinate system, parameterized by (u, v)
 Assign (u, v) coordinates to the vertices of surface triangles
 For each pixel inside a triangle, calculate its texture coordinates using
barycentric interpolation on the triangle vertices’ texture coordinates
 Using the pixel texture coordinates, use bilinear interpolation in the texture
map to find the pixel’s color
 Steps in Texture Mapping
 Create a texture object and specify a texture for that object
 Indicate how the texture is to be applied to each pixel
 Enable texture mapping
 Draw the scene, supplying both texture and geometric coordinates
 Loading image file as texture with Qt
void glBindTexture(GLenum target, GLuint textureName);
 When using the textureName of an unsigned integer other than
zero for the first time, a new texture object is created and
assigned that name
 When binding to a previously created texture object, that texture
object becomes active
 When binding to a textureName value of zero, OpenGL stops
using texture objects and returns to the unnamed default texture.
void glDeleteTextures(GLsizei n, const GLuint *textureNames);
 Deletes n texture objects, named by elements in the array
textureNames.
 You must provide both object coordinates and texture
coordinates for each vertex.
 The texture coordinates determine which texel in the
texture map is assigned to that vertex.
 Texture coordinates are interpolated between vertices
 Textures are rectangular arrays of data
 In exactly the same way that colors are interpolated between two
vertices of shaded polygons and lines
void glTexCoord{1234}{sifd}(TYPE coords);
void glTexCoord{1234}{sifd}v(TYPE *coords);
 Sets the current texture coordinates (u,v, w, q)
 Subsequent calls to glVertex*() result in those vertices being
assigned the current texture coordinates.
 Using glTexCoord2*() allows you to specify u and v; u and v are
set to 0 and 1, respectively
 You need to enable texturing before drawing your scene.
 glEnable(GL_TEXTURE_2D)
 glDisable(GL_TEXTURE_2D)
 Supplying Both Texture and Geometric Coordinates
 For a two-dimensional texture map
 the texture coordinates range from 0.0 to 1.0 in both directions, but the coordinates of the items being
textured can be anything
 (0,0) is conventionally the bottom-left corner
 (1,1) is the top-right corner of the texture image
 Wrapping
 The texture should be sampled when a coordinate outside the range of 0 to 1 is given
 GL_REPEAT
 GL_MIRRORED_REPEAT
 GL_CLAMP_TO_EDGE
 GL_CLAMP_TO_BORDER
 The clamping can be set per coordinate, where the equivalent of (x,y,z) in texture coordinates
is called (s,t,r)
 Texture parameter are changed with the glTexParameter* functions
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);