Sie sind auf Seite 1von 65

Graphics 2011/2012, 4th quarter

Lecture 6

Texture mapping

### Introduction

Vectors, basic geometric entities Intersection of objects Matrices, transformations And some shading

### Motivation

For example, we can

. .

.

use vectors to represent points

use 3 points to represent triangles

use matrix multiplication to transform them

use our (simple) shading model to put color on them ### Motivation

For example, we can

. .

.

use vectors to represent points

use 3 points to represent triangles

use matrix multiplication to transform them

use our (simple) shading model to put color on them ### Motivation

For example, we can

. .

.

use vectors to represent points

use 3 points to represent triangles

use matrix multiplication to transform them

use our (simple) shading model to put color on them ### Motivation

For example, we can

. .

.

use vectors to represent points

use 3 points to represent triangles

use matrix multiplication to transform them

use our (simple) shading model to put color on them

Q: But how do we get the colors in between two vertices? ### Linear interpolation

Given two vectors a,

b, linear

interpolation is deﬁned as

p (t) = (1 t) a + t b

with a (scalar) parameter 0 t 1.

Note:

If a, b are scalars and t = 1/2

this is usually refered to as

average ;)

 If a, b are color values (r, g, b), this gives us a smooth transition from a to b ### Linear interpolation to color triangles

With this we can linearly

interpolate color

• 1 between two vertices

• 2 between two edges

Q: How to do this eﬃciently?

We will learn this in a later

lecture ### Texture mapping

Adding lots of detail to our models to realistically depict skin,

grass, bark, stone, etc., would increase rendering times

dramatically, even for hardware-supported projective methods. ### Texture mapping

Adding lots of detail to our models to realistically depict skin,

grass, bark, stone, etc., would increase rendering times

dramatically, even for hardware-supported projective methods. ### Basic idea

Basic idea of texture mapping:

shade, light, etc. for each pixel

we just paste images to our

objects in order to create the

illusion of realism  ### Diﬀerent approaches

Diﬀerent approaches exist,

for example 2D vs. 3D:

2D mapping (aka image textures):

paste an image onto the object

3D mapping (aka solid or volume

textures): create a 3D texture

and ”carve” the object

3D Object 2D texture ←→ 3D texture

### Outline

• 1 Introduction

Linear interpolation

Texture mapping

• 2 3D texture mapping

3D stripe textures

Texture arrays

Solid noise

• 3 2D texture mapping

Basic idea

Spherical mapping

Triangles

• 4 Other forms of texture mapping

Bump mapping

Displacement mapping

Environment mapping

### Texturing 3D objects

we use a mathematical procedure to create a 3D texture, i.e.

f(x, y, z) = c with c R 3

Then we use the coordinates of

each point in our 3D model to

calculate the appropriate color

value using that procedure, i.e.

f(x p , y p , z p ) = c p ### 3D stripe textures

A simple example:

stripes along the X-axis

stripe( x p , y p , z p ) { if ( sin x p > 0 ) return color0; else return color1;

}

}

Note: any alternating function

will do it (sin is slow)  ### 3D stripe textures

A simple example:

stripes along the X-axis

stripe( x p , y p , z p ) { if ( sin x p > 0 ) return color0; else return color1;

}

}

Note: any alternating function

will do it (sin is slow)  ### 3D stripe textures

Stripes along the Z-axis:

stripe( x p , y p , z p ) { if ( sin z p > 0) return color0; else return color1;

}

}  ### 3D stripe textures

And what happens here?

stripe( x p , y p , z p ) {

if ( sin x p > 0 & sin z p > 0) return color0; else return color1;

}

}

This looks almost like a checkerboard, and should come in handy when working on practical assignment 1.2 ### 3D stripe textures

Stripes with controllable

width:

stripe( point p, real width )

 { if ( sin(π x p /width) > 0 ) return color0; else return color1; } }

Try this at home :)

Note that we do not multiply

but divide by width!

### 3D stripe textures

Smooth variation between two

ones:

stripe( point p, real width )

 { t = (1 + sin(π x p /width))/ 2 return (1 - t) c 0 + t c 1 }

Try this at home :)

Note: if that doesn’t look

familiar, check the slides on

linear interpolation again ;) ### Texture arrays

Again: this is often called solid or volumetric texturing.

It is called procedural because

we compute the color values

for a point p R 3 with a

procedure.

Alternatively, we can do an

array lookup in a 3D array

(using all three coordinates of

p for indexing),

or in a 2D array (using only

two coordinates of p).

Carving vs. array lookup  ### 2D texture arrays We’ll call the two dimensions to be mapped u and v,

and assume an n x × n y image as texture.

Then every (u, v) needs to be mapped to a color in the image,

i.e. we need a mapping from pixels to texels.

### 2D texture arrays A standard way is to remove the integer portion of u and v,

so that (u, v) lies in the unit square.

### 2D texture arrays The pixel (i, j) in the n x × n y image for (u, v) is found by

i = un x and j = vn y

x is the ﬂoor function that give the highest integer value x.

### Nearest neighbor interpolation

This is a version of nearest-neighbor interpolation, because we take

the color of the nearest neighbor:

c(u, v) = c i,j with i = un x and j = vn y  ### Bilinear interpolation

For smoother eﬀects we may use bilinear interpolation:

c(u, v) =

(1u )(1v )c ij +u (1v )c (i+1)j +(1u )v c i(j+1) +u v c (i+1)(j+1) where u = un x un x

and v = vn y vn y

Notice: all weights are between 0

and 1 and add up to 1, i.e.

(1 u )(1 v ) + u (1 v ) +

(1 u )v + u v = 1

### Trilinear interpolation

Using 2D arrays with bilinear interpolation is easily extended to

using 3D arrays with trilinear interpolation: c(u, v, w)
=
(1 − u )(1
− v )(1 − w )c ijk
+u (1 − v )(1 − w )c (i+1)jk
+
.
.
.

### Using random noise

So far: rather simple textures

(e.g. stripes).

We can create much more

complex (and realistic)

textures, e.g. resembling

wooden structures.

Or we can create some

e.g. to create the impression

of a marble like structure.  ### Perlin noise

Goal: create texture with random appearance, but not too random

(e.g., marble patterns, mottled textures as on birds’ eggs)

1st idea: random color at each point

Problem: too much noise, similar to

“white noise” on TV

2nd idea: smoothing of white noise

computationally too expensive

3rd idea: create lattice with random

numbers & interpolate between them Problem: lattice becomes too obvious

Perlin noise makes lattice less obvious by

using three “tricks”

. .

.

### Perlin noise

Perlin noise is based on the following ideas:

Use a 1D array of random unit vectors and hashing to create a virtual 3D
array of random vectors;
Compute the inner product of
(u, v, w)-vectors with the random
vectors
Use Hermite interpolation to get rid of
visible artifacts

### Random unit vectors

Random unit vectors are obtained as follows:

 v x = 2ξ − 1 v y = 2ξ − 1 v z = 2ξ − 1

where ξ, ξ , and ξ are

random numbers in [0, 1].

Notice that 1 v i 1, so we get vectors in the unit cube.

If (v x + v y + v

2

2

2

z

) < 1, we

normalize the vector and keep it; otherwise not. Why?

Perlin reports that an array with 256 such random unit vectors works well with his technique. ### Hashing

We use this 1D array of random unitvectors to create a

(pseudo-)random 3D array of random unitvectors, using the

following hashing function:

Γ ijk = G(φ(i + φ(j + φ(k))))

where G is our array of n random vectors, and φ(i) = P[i

mod n]

where P is an array of length n containing a permutation of the

integers 0 through n 1.

### Hashing ### Hashing ### Perlin noise

Perlin noise is based on the following ideas:

Use a 1D array of random unit

vectors

and hashing to create a virtual

3D array of random vectors;

Compute the inner product of

(u, v, w)-vectors with the

random vectors

Use Hermite interpolation to get

rid of visible artifacts ### Hermite interpolation

With our random vectors and hashing function in place, the noise

value n(x, y, z) for a point (x, y, z) is computed as:

n(x, y, z) =

 x +1 y +1 z +1 i= x j= y k= z

ijk (x i, y j, z k)

 where Ω ijk (u, v, w) = ω(u)ω(v)ω(w)(Γ ijk · (u, v, w)) and ω(t) = 2|t| 3 − 3|t| 2 + 1 if |t| < 1 0 otherwise

### Hermite interpolation

Characteristics of hermite interpolation

(or “why this creates better noise than linear”): Linear interpolation:

linear weights, i.e. Hermite interpolation:

cubic weights, i.e.

### Summary Perlin noise:

Virtual 3D array & hashing

Scalar product with random unit vector

Hermite interpolation

### Outline

• 1 Introduction

Linear interpolation

Texture mapping

• 2 3D texture mapping

3D stripe textures

Texture arrays

Solid noise

• 3 2D texture mapping

Basic idea

Spherical mapping

Triangles

• 4 Other forms of texture mapping

Bump mapping

Displacement mapping

Environment mapping

### 2D texture mapping

Now let’s look at 2D mapping,

which maps an image onto an

object (cf. wrapping up a gift)

Instead of a procedural, we use

a lookup-table approach here,

i.e. for each point in our 3D

model, we look up the

appropriate color value in the

image.

How do we do this? Again,

let’s look at some simple

examples.  ### Spherical texture mapping

How do we map a rectangular image onto a sphere?  ### Spherical texture mapping

Example: use world map and sphere to create a globe ### Spherical texture mapping

We have seen the parametric equation of a sphere with radius r

and center c:

x

y

z

=

=

=

x c

+

r cos φ sin θ

y

c

+

r sin φ sin θ

z c +

r cos θ

Given a point (x, y, z) on the surface of the sphere, we can ﬁnd θ

and φ by

θ = arccos zz c

r

φ = arctan yy c

xx c

### Spherical texture mapping

For each point (x, y, z) we have

 θ = arccos z−z c r φ = arctan y−y c x−x c

Since both u and v must range from [0, 1], and

(θ, φ) [0, π] × [π, π], we must convert:

u

v

 = φ mod 2π 2π π−θ =

π

### Texturing triangles

Mapping an image onto a triangle is

done by specifying (u, v) coordinates

for the vertices.

So, our triangle vertices

a = (x a , y a ),

b = (x b , y b ),

c = (x c , y c )

become

a = (u a , v a ),

b = (u b , v b ),

c = (u c , v c )  (0.1, 0.9)

(0.6, 0.1)

(0.8, 0.7)

### Texturing triangles

Remember that barycentric

coordinates are very useful for

interpolating over a triangle –

and related textures ;)

p (β, γ) = a + β( b a) + γ( c a)

now becomes

u(β, γ) =

u a + β(u b u a ) + γ(u c u a )

v(β, γ) = v a + β(v b v a ) + γ(v c v a )

We get the texture coordinates by

linearly interpolating the vertex

coordinates over β, γ for

0 β + γ 1.  (0.1, 0.9)

(0.6, 0.1)

(0.8, 0.7)

### Texturing triangles

Again, we can use bilinear

interpolation to avoid artifacts.

Note that the area and shape of the

triangle don’t have to match that of

the mapped triangle.

Also, (u, v) coordinates for the

vertices may lie outside the range

[0, 1] × [0, 1].  (0.1, 0.9)

(0.6, 0.1)

(0.8, 0.7)

### Texturing triangles

Be careful with perspective, because objects further away appear

smaller, so linear interpolation can lead to artifacts: To avoid this, we have to consider the depth of vertices with

respect to the viewer.

Perspective projection is covered in a later lecture.

Fortunately, this is supported by modern hardware and APIs.

### MIP-mapping If viewer is close:
Object gets larger →
Magnify texture
“Perfect” distance:
Not always “perfect” match
(misalignment, etc.)
If viewer is further away:
Object gets smaller → Minify
texture
Problem with miniﬁcation:
eﬃciency (esp. when whole
texture is mapped onto one pixel!)

### MIP-mapping Solutions: MIP maps

Pre-calculated, optimized

collections of images based

on the original texture

Dynamically chosen based on

depth of object (relative to

viewer)

Supported by todays

hardware and APIs

### Outline

• 1 Introduction

Linear interpolation

Texture mapping

• 2 3D texture mapping

3D stripe textures

Texture arrays

Solid noise

• 3 2D texture mapping

Basic idea

Spherical mapping

Triangles

• 4 Other forms of texture mapping

Bump mapping

Displacement mapping

Environment mapping

### Bump mapping

One of the reasons why we apply

texture mapping:

Real surfaces are hardly ﬂat but

often rough and bumpy. These

bumps cause (slightly) diﬀerent

reﬂections of the light.   ### Bump mapping

Instead of mapping an image or noise

onto an object, we can also apply a

bump map, which is a 2D or 3D

array of vectors. These vectors are

added to the normals at the points

for which we do shading calculations. The eﬀect of bump mapping is an

apparent change of the geometry of

the object. ### Bump mapping

Major problems with bump mapping: silhouettes and shadows

### Displacement mapping

To overcome this shortcoming, we

can use a displacement map. This is

also a 2D or 3D array of vectors, but

here the points to be shaded are

actually displaced.

Normally, the objects are reﬁned

using the displacement map, giving

an increase in storage requirements. ### Displacement mapping  ### Environment mapping

Let’s look at image textures again:    If we can map an image of the environment to an object ...

### Environment mapping

...

why not use this to make objects

appear to reﬂect their surroundings

specularly?

Idea: place a cube around the object,

and project the environment of the

object onto the planes of the cube in

a preprocessing stage; this is our

texture map.

During rendering, we compute a

reﬂection vector, and use that to

look-up texture values from the cubic

texture map. ### Environment mapping ### And now?

In case you didn’t notice: it’s halftime :) . . .

so let’s sit back and have a

break before we continue.

(but you ﬁnd various versions of it on YouTube)

### What’s next?

The midterm exam! Time and date:
Friday, 25.5.11
9:00 - 12:00 h
Zaal: EDUC-GAMMA

Note: no responsibility is taken for the correctness of this

information. For ﬁnal information about time and room see

http://www.cs.uu.nl/education/vak.php?vak=INFOGR

### The midterm exam

What do I have to do? Come in time

Bring a pen (no pencil)

Note: You may not use books, notes, or any electronic equipment

(including cell phones!).

### The midterm exam

The exam covers lectures 1-5 and tutorials 1-3. If you ...
followed the lectures
and actively did the
exercises

...

you should be ﬁne.

### Important dates

Today (Tue, 15.5.)

Only one tutorial (room 61)

Thu, 17.5.

No tutorial and lecture (holiday)

Tue, 22.5.

Lecture 7 and “Thursday tutorial”

Thu, 24.5.

No tutorial(?) and lecture

Fri, 25.5.

Midterm exam