Texture
Texture
Texture
• Instead of having each surface as a solid colour, we can attach “texture” to the
surface.
✫ ✪
Texture 1 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Digital Images
• For colour images, each pixel is typically specified by a vector of (R, G, B) values
in [0, 255] (24-bit images).
• Images may be stored in different formats (e.g. GIF, TIFF, PNG, PDF, JPEG).
• Most formats perform some data compression, some are lossy (e.g. JPEG).
• OpenGL: need external libraries to load image data.
✫ ✪
Texture 2 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Texture Mapping
✫ ✪
Texture 3 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Texture Mapping
✫ ✪
Texture 4 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
2D Texture Mapping
• A texture map is a set of functions that map coordinates on the surface to texture
coordinates:
(s, t) = (s(x, y, z, w), t(x, y, z, w))
• Conceptually: for each fragment on the object, the texture map tells us which texel
should be used.
✫ ✪
Texture 5 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
2D Texture Mapping
• One particular issue: it may be that (s, t) is “in between” texels. Which texel should
we use?
✫ ✪
Texture 6 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
2D Texture Mapping
✫ ✪
Texture 7 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Mipmapping
• Depending on the distance of the object to the viewer, the size of a fragment may be
much larger or smaller than a texel.
✫ ✪
Texture 9 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• The colour returned can be used to determine the colour of the fragment (possibly
together with lighting information).
✫ ✪
Texture 10 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
3D Texture Mapping
• Sometimes attaching 2D textures to each surface can look unrealistic (e.g. edge
between surfaces)
• Instead, we can define 3D textures for entire object (e.g. wood grain, stone, etc.).
• There will be three texture coordinates (s, t, r).
• The effect will be similar to “carving” an object out of a textured material.
✫ ✪
Texture 11 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Environment/Reflection Map
• How do we render a scene when there is a highly reflective surface (e.g. mirror)?
• We cannot render the mirror without knowing the rest of the scene.
• Texture mapping can be used to make a good approximation.
✫ ✪
Texture 12 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Environment/Reflection Map
• Looking at a mirror: the angle of reflection is known given the viewer position.
• We can first pretend to render the scene without the mirror.
• Place the camera at the centre of the mirror, looking towards the normal vector of
the mirror.
Cube Mapping
Cube Mapping
✫ ✪
Texture 15 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• Care needs to be taken with the size of the box vs. size of objects, and how the
environment changes when the camera moves.
✫ ✪
Texture 16 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• The reflected vector can then be used to sample a cube map to see the reflection.
• Refraction: light bends (e.g. looking into a pool of water).
• We can apply the same principle once we computed the refracted direction (use
physics).
• Reflection of other objects: pretend the surface is a camera and render the scene
into a texture buffer, then use texture mapping.
✫ ✪
Texture 17 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Bump Mapping
• Sometimes we want a surface to appear non-smooth with little “bumps”. e.g. the
peel of an orange is not flat.
✫ ✪
Texture 18 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Bump Mapping
• The perturbations to the normal vectors can be stored as a texture called a normal
map.
• The texture normally stores RGB values in [0, 1]. Need to rescale normal vectors
(x, y, z) in [−1, 1]
• The normal map is used as the normal vectors before lighting calculations in
fragment shader.
✫ ✪
Texture 19 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
Bump Mapping
• Normal maps assume surface is in xy -plane with normal vector pointing in positive
z direction.
• Define a new coordinate system called tangent space (local coordinate so surface is
in xy -plane and normal vector point in positive z direction.
• Compute transformation matrix from tangent to world space, and transform normal
vector to world space (fragment shader), or
✫ ✪
Texture 20 – 21 Howard Cheng
✬ ✩
CPSC 3710 Computer Graphics University of Lethbridge
• For more realistic rendering of depth of a flat surface based on texture, we have to
adjust the sampled texture as well.
• This is because the texture coordinate we see may be slightly shifted based on
height of object.