# 10. Texture mapping Brock University
COSC 3P98 Computer Graphics
Instructor: Brian J. Ross

## Texture mapping

• using lighting with flat or blended shading renders objects with smooth but constant or blended colour
• most objects have texture: minute details on surfaces
• a number of ways to do this in graphics:
• a) surface-detail polygons
• add more polygons of different colours and shapes to give surface detail
• not realistic for fine detail: too many polygons!
• b) texture maps: define a texture image, and then "wrap" it around object
• c) procedural textures: compute the colour of each point on the surface of the object
• d) bump maps: define a function which varies the geometry of points on the surface when lighting is being computed
• lighting effects will yield bumpy surface
• i) surface normal manipulation: normals altered according to bump map, but geometry stays the same
• ii) surface deformation: actual surface geom is altered

## Texture maps

• texture maps: somewhat like a pixmap, except:
• (i) there isn't a 1:1 correspondence between map entries and pixels;
• (ii) it is applied to objects in a variety of ways
• texture maps may be drawn, scanned, photographed, generated mathematically (fractals), sampled from a video image...
• typical maps: leathery skin, granite, bricks, spaceships...
• texture map resides in its own 2D coordinate space ("texture space", often (u,v) coords)
• simply means you can talk about texture coords without confusing them with your model coordinate space
• texel: one texture map element
• combined with lighting equations, texture mapping can yield impressive photorealism ## Mapping 2D textures

• different geometric mappings (texture projections) possible

• simple one: planar
• map texture to a plane
• position the plane with respect to the object, such that it covers the entire object; for example, position it on (x, y) plane.
• For each surface point P (x,y,z) on model, use it's (x,y) to access colour at texture map position (x, y) --> use that colour at P
• Effect: the texture runs straight through the entire object from one side to the other.
• interpolate fractional coordinates on the texture map
• Can scale the texture onto object by shrinking or expanding its pixmap size.

• cylindrical projection: a texture cylinder encloses the scene
• first need to map your rectangular bitmap to this cylinder
• any point on cylinder can be computed by:
• (r cos A, r sin A, hz)
• 0 < A < 2*pi
• 0 < z < 1
• then texmap coordinates mapped to cylinder via: (u, v) = (A / (2*pi), z)
• 0 <= u, v <= 1
• This maps texture to a cylinder. If you have a non-cylindrical object, you can enclose it within the cylinder.
Then you can use various strategies to associate surface points with the cylinder. • parametric mapping: the model has a coordinate space defined on it
• coordinate system generated when model initialized
• deformed along with model surface during model editing
• texture coordinates will map around model surface directly; there is a 1 to 1 mapping between texture and all points on the model surface
• permits precise interactive painting on model (eg. Bodypaint 3D: screendump.)

## OpenGL: Texture maps

• OpenGL has a powerful texture mapping utility that has a number of user-specifiable parameters
• Very complex and comprehensive! We'll only touch on its basic capabilities.
• Scheme:
• Specify the texture.
• 2d or 1d image; resolution
• Indicate how texture is to be applied to each pixel.
• decals, blending with lighting, ...
• Enable texture mapping.
• Draw the scene, supplying both texture and geometric coordinates.
• mapping texture to polygons; how to treat boundaries (wrapping,...)

## OpenGL: specifying texture

glTexImage2D(GL_TEXTURE_2D, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels)

• level - resolution; if 1 resolution, set to 0 (recommended)
• components - number of components, from 1 to 4
• determines which channels blend with material colours
• width, height - # texels wide, high in image table
• Note that size must be a power of 2 (64, 128, ...)
• border - width of border (usually 0)
• format, type - information on image format
• pixels - texture image
• image formats: a number of predefined image formats are recognized (see documentation)
• the glPixelStorei command permits setting formatting conventions for drawing and reading image bitmaps in OpenGL

## OpenGL: more texture control commands

glTexEnv{if}{v}(GL_TEXTURE_ENV, GLenum pname, TYPE param);

• defines how textures are applied; e.g.
• if param=GL_REPLACE, then texture's colour is used for polygon's colour
• param=GL_DECAL, then texture's colour is blended with polygon using ALPHA channel
• param= GL_MODULATE, then texture multiplied with other colour
• etc.

glTexParameter{if}{v}(GL_TEXTURE_2D, GLenum pname, TYPE param);

• controls if texture repeats, clamps (last texel used wraps across remainder of poly), how texture magnify or minimize, etc

## OpenGL: assigning texture coordinates

• (a) manually: use glTexCoord2*(vertices)
• for each vertex of polygon, you also supply position it maps to in texture table
• then texture is linearly interpolated through the polygon when its drawn
• eg.
```glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 3.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(3.0, 3.0); glVertex3f(2.41, 1.0, -1.41);
glTexCoord2f(3.0, 0.0); glVertex3f(2.41, -1.0, -1.41);
glEnd();```
• (b) Automatic texture coordinate generation: glTexGen( );
• Can be used to create FX such as mirror reflection.

## Procedural textures

• 2D textures
• advantage: literally any picture that can be scanned or painted can be used as a texture
• disadvantage: difficult to wrap 2d textures around 3D objects
• stretch marks
• seams
• repeated patterns ("tiling")

• procedural textures: (algorithmic textures, 3D textures)
• texture is computed by a function/program
• T(x,y,z) is a function over point coord (x,y,z), and returns a colour to render that point
• Parameters can also include surface characteristics such as height, slope,...
• think of T(x,y,z) as an infinite function in 3D
• each renderable point of an object intersects the T(x,y,z) volume; where it intersects it determines its colour/texture
• if you translate the 3D texture along with the object, the object texture will remain static; otherwise, it will transform when it moves wrt coordinate space

• advantages of 3D textures:
• continuous in (x,y,z) space; means that texture features wrap around minute folds in objects
• no seams, stretch marks
• eg. wood grains will naturally encompass object --> solid texturing
• infinite variations
• can obtain very realistic, natural textures: wood, marble, terrain, ...
• can be animated for special effects: animate the texture equation parameters
• often work hand-in-hand with procedural modeling

• disadvantages of 3D textures:
• not necessarily easy to create a mathematical model of a desired effect
• can be computationally expensive

## 3D texture mapping

• In most cases, we need to rotate, translate, scale a texture along with the object; otherwise, the texture will move and mutate
• Note: unlocking texture with object may be an interesting special effect

• Example 1: RGB
• we can translate an object's (x,y,z) coordinates into RGB space
• trans(x,y,z) --> x',y',z' (0 <= x, y, z <= 255)
• then treat these coords as RGB data
• Use modulo arithmetic if coordinates go beyond 0-255.
• Note: Easy effect to apply to assignment 3!

• Example 2: woodgrain
• woodgrain effects effectively modeled using embedded cylinder equations
• eqn of cylinder: r1 = (u^2 + v^2)^0.5
• perturb (deviate) wood growth
• r2 = r1 + 2*sin(a*A)
• twist axis of cylinders:
• r2 = r1 + 2*sin(a*A + v/b)
• where u, v, w are parametric (local) coordinates; a, b are constants
• A = arctan(u/w)
• then use mod function to simulate embedded cylinders, and map r2 size to a colour (eg. dark, light)

eg. Code from Watt, Program 7.1, p. 248 :

```wood_grain(u,v,w:real; var r,g,b:real);
var radius, angle: real;
grain:integer;

radius := sqrt(u*u+v*v);
if w=0 then angle := pi / 2
else angle := arctan(u,w);
/* arctan evaluates arctan(u,w), but uses ingo to return
a value betwen 0 and 2*pi */
radius := radius + 2*sin(20*angle+v/150);
grain := round(radius) mod 60;
if grain < 40 then
r := R_LIGHT; g := G_LIGHT; b := B_LIGHT;
else
r := R_DARK; g := G_DARK; b := B_DARK;
end;``` ## 3D texture mapping

• example 3: noise
• use a finite lattice of random numbers; numbers denote colours (shades)
• mod (x,y,z) coordinate into the lattice, and interpolate its colour based on lattice values
• if you use a spline interpolation, realistic stone effects result
• can also add turbulence (good for marble)
• turbulence(x) = sum {i=0 to k} (noise((2^i) x) / 2^i
• as i increases, its contribution from noise increases, and hence it is scaled by 2^i
• this has fractal properties: the turbulence has the same general shape no matter how precisely you draw it
• of course, you also apply lighting effects on top of this (and other example 3d textures)
• Famous example of using noise to emulate natural textures (minerals, clouds,...): Perlin noise by Ken Perlin (Academy Award winner!)
(Dr Perlin's home page @ NYU).
• Here's Eric Sneek's implementation result: Picture  Some examples of turbulence (via Strata StudioPro 1.75 Macintosh)

## Complex 3D textures: Daz3D Bryce

• Current version is Bryce 7 Pro (\$12 US)
• Bryce's realistic (and not-so-realistic) materials make use of:
• lighting effects
• 2D texture mapping
• 3D algorithmic textures
• mixing of multiple textures
• other procedural effects (fuzziness, alpha-channels,...)
• 3D textures use model geometry within equations
• slope or altitude of a mountain can effect textures (snow, rock stratification)
• shows how modeling and texturing work together
• Impressive cloud FX.

Example Bryce images: One Two. Three. Four.

## Complex editor features (Bryce 1)

• an interactive GUI interface that manipulates 3D texture equation parameters • Step 1: Noise: basic source of texture
• type: various algorithmic generation schemes (random, fractal, ...)
• dimensions: 1D, 2D, 3D, ...
• frequency: density along # dimensions assigned above
• orientation: direction of noise
• octaves: complexity parameter - a cyclic measurement
• modulation: means for combining octaves
• Step 2: Filter: post-processing of noise signal
• clipping, smoothing, ...
• usually, parameter a controls intensity of filter, b adjusts height
• Step 3: Output type
• value (alpha channel - transparency), colour, normal (bump map)
• or a combination of the three
• Step 4: can assign 3 components using steps 1 thru 3, and combine them
• various parameters for blending them together

Complex texture editor (Bryce 2)

## References

• Advanced Animation and Rendering Techniques, A. Watt and M. Watt, Addison-Wesley 1992, ISBN 0-201-54412-1.
• OpenGL Programming Guide, OpenGL ARB, Addison-Wesley 1993, ISBN 0-201-63274-8. (chapter 9)
• The KPT Bryce Book, S. Kitchens, Addison-Wesley 1995, ISBN 0-201-48355-6.
• Real World Bryce 2: the art of digital landscape, S. Kitchens, 1997, ISBN 0-201-694190

Back to COSC 3P98 index

COSC 3P98 Computer Graphics
Brock University
Dept of Computer Science
Copyright © 2016 Brian J. Ross (Except noted figures).
http://www.cosc.brocku.ca/Offerings/3P98/course/lectures/texture/
Last updated: March 26, 2019