tnm046: datorgrafik texture mapping - linköping universitysasgo26/tnm046/lectures/lecture_5.pdf5...

39
TNM046: Datorgrafik Texture mapping Sasan Gooran VT 2014

Upload: others

Post on 23-Jan-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

TNM046: Datorgrafik

Texture mapping

Sasan Gooran VT 2014

Page 2: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

2

Texture Mapping The process of mapping an image onto an object (/triangle) in order to increase the detail of the rendering. We can get fine details without needing to render millions of tiny triangles. The image that gets mapped onto the object is called a texture map and is usually a regular color image.

Page 3: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

3

Texture Mapping

Page 4: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

4

The problem

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

For each pixel on an object, the question is: where to look in the texture map to find the color?

Page 5: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

5

Planar/Linear mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Take the (x, y, z) values from the object and drop one of the components (here z). This gives us a 2D coordinate (planar), which can be used to look up the color in the texture map.

Page 6: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

6

Linear mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Page 7: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

7

Linear mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

The color remains constant when x changes

The color remains constant when y changes

Page 8: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

8

Cylindrical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

The (x, y, z) values are converted to cylindrical coordinates (r, φ, h). For the texture mapping only φ and h are used (φ is converted to an x and h to a y-coordinate)

Page 9: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

9

Cylindrical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Page 10: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

10

Cylindrical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Page 11: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

11

Spherical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

The (x, y, z) values are converted to spherical coordinates (r, θ, φ). For the texture mapping only θ and φ are used (θ is converted to an x and φ to a y-coordinate)

Page 12: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

12

Spherical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Page 13: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

13

Spherical mapping

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul University

Page 14: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

14

Basic Idea

Page 15: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

15

Basic Idea

Source: http://blog.tartiflop.com/2008/11/first-steps-in-away3d-part-3-texture-mapping/

First: Map a point on the object to a point on a unit square (usually denoted by st or uv), second: map the unit square to the texture.

Page 16: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

16

Basic Idea 1- For each coordinate (x, y, z) find its corresponding coordinate (s, t) in the unit square. 2- For each (s, t) coordinate in the unit square find the corresponding coordinate in the texture map. Let’s start with number 2, which means how to map unit square to Texture map. After that, we will see how to map (x, y, z) to the unit square (s, t). Note: The coordinate in the unit map (s, t) are denoted by (u, v) in some literature.

Page 17: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

17

Map unit square to Texture

Texture map

(0,0)

(128,128)

s

t

(0,0)

(1,1)

Unit texture plane

Above example: (0,0)à (0,0), (1,1)à (128,128), (0.25,0.5)à (32,64)

In general: For any point (s, t) on unit plane, corresponding point in texture plane is: (s*texture width, t*texture height)

Once we have the coordinates, we just need to look up the color of the texture at that location.

T(m, n)

Page 18: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

18

Map unit square to Texture Above example: (0.2, 0.3)à (25.6, 38.4), how do we look up the color of the texture at this location? The simplest way: (nearest neighbor interpolation), just look up the color at the closest possible neighbor, in this example (26, 38)

Page 19: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

19

Map unit square to Texture Above example: (0.2, 0.3)à (25.6, 38.4), how do we look up the color of the texture at this location? Or use bilinear interpolation,

T(i, j) T(i+1, j)

T(i, j+1) T(i+1, j+1)

(a, b) Δx

Δy

T (a,b) = (1−Δx)(1−Δy)T (i, j)+Δx(1−Δy)T (i+1, j)+ (1−Δx)ΔyT (i, j +1)+ΔxΔyT (i+1, j +1)

For the above example:

T (25.6,38.4) = 0.4 ⋅0.6 ⋅T (25,38)+ 0.6 ⋅0.6 ⋅T (26,38)+0.4 ⋅0.4 ⋅T (25,39)+ 0.6 ⋅0.4 ⋅T (26,39)

Texture map

a

b

Close up

Page 20: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

20

Map unit square to Texture

Nearest Neighbor Bilinear

Page 21: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

21

Map from point object to unit square Plane

Given (x, y, z), you can drop for example z component (or x or y, depending on which plane you are using) The plane can also be arbitrary, in this case you just need to do some transformations (for example rotation)

Then: (x, y, z) à (x, y) à (s, t) = (x/w, y/h)

Page 22: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

22

Map from point object to unit square Plane

What if you get a texture coordinate outside [0, 1]? Tiling •  Loop around and start at the other side (wrapping) •  Reflect the image (mirroring) •  Repeat the edge pixels (clamping) •  Default to some other color (bordering)

Page 23: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

23

Map from point object to unit square Cylinder

x = rcosϕy = rsinϕ0 ≤ z ≤ h

"

#$

%$

0 ≤ϕ ≤ 2π

x

y

z

OR −π / 2 ≤ϕ ≤ 3π / 2

Page 24: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

24

Map from point object to unit square Cylinder

x = rcosϕy = rsinϕ0 ≤ z ≤ h

"

#$

%$

−π / 2 ≤ϕ ≤ 3π / 2

x

y

z

tanϕ = yx

ϕ = arctan(x, y)

Note: φ is between –π/2 and 3π/2, but the result of tan-1 is between – π/2 and π/2.

ϕ = arctan(x, y) =

tan−1(y / x), if x > 0tan−1(y / x)+π, if x < 0π / 2, if x = 0 and y > 0−π / 2, if x = 0 and y < 0

"

#

$$

%

$$

Page 25: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

25

Map from point object to unit square Cylinder

ϕ = arctan(x, y)Given (x, y, z) on the object, you find:

s = ϕ +π / 22π

t = zh

!

"##

$##

Note: φ is between –π/2 and 3π/2, and z between 0 and h. The unit square coordinates (s, t) are between 0 and 1, therefore:

Ex: 39 a and e

Page 26: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

26

Map from point object to unit square Sphere

x = rsinθ cosϕy = rsinθ sinϕz = rcosθ

!

"#

$#

ϕ

0 ≤ϕ ≤ 2π

x

y

z

OR −π / 2 ≤ϕ ≤ 3π / 2

0 ≤θ ≤ π

Page 27: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

27

Map from point object to unit square Sphere

x = rsinθ cosϕy = rsinθ sinϕz = rcosθ

!

"#

$#

ϕ

x

y

z

−π / 2 ≤ϕ ≤ 3π / 2

rθ0 ≤θ ≤ π

tanϕ = yx

ϕ = arctan(x, y)See page 24

cosθ = zr=

zx2 + y2 + z2

θ = cos−1( zx2 + y2 + z2

)

Note: θ is between 0 and π, and the result of cos-1 is also between 0 and π.

Page 28: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

28

Map from point object to unit square Sphere

ϕ = arctan(x, y)

Given (x, y, z) on the object, you find:

s = ϕ +π / 22π

t = θπ

!

"##

$##

Note: φ is between –π/2 and 3π/2, and θ between 0 and π. The unit square coordinates (s, t) are between 0 and 1, therefore:

and θ = cos−1( zx2 + y2 + z2

)For arctan see page 24

Ex: 40 a and e

Page 29: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

29

Mapping triangles to Texture

Interpolate linearly across triangles. For each (x, y, z) on the triangle find a (s, t) coordinate by interpolation and the corresponding location in the texture map (as explained above) and look up its color.

(xA, yA,, zA)

(xB, yB, zB)

(xC, yC, zC)

(xp , yp, zP)

Pre-calculate texture coordinates (or (s, t) coordinates) for each vertex and store them.

Page 30: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

30

Texture mapping, OpenGL and Lab

x

y

z

p0

p1

p2

p3

Vertex table: Nr coordinate normal texture (s, t) 0: (0,0,0) (0, 0, -1) (s, t) 1: (0,0,0) (0, -1, 0) (s, t) 2: (0,0,0) (-1, 0, 0) (s, t) 3: (1,0,0) (0, 0, -1) (s, t) 4: (1,0,0) (0, -1, 0) (s, t) 5: (1,0,0) (1,1,1)/sqrt(3) (s, t)

As we saw previously, for each vertex we define its coordinates and the normal vectors associated with the vertex. In OpenGL we also add the (s, t) texture coordinates to the vertex list.

Page 31: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

31

Texture mapping, OpenGL and Lab

x

y

z

p0

p1

p2

p3

Vertex table: Nr coordinate normal texture (s, t) 0: (0,0,0) (0, 0, -1) (s, t) 1: (0,0,0) (0, -1, 0) (s, t) 2: (0,0,0) (-1, 0, 0) (s, t) 3: (1,0,0) (0, 0, -1) (s, t) 4: (1,0,0) (0, -1, 0) (s, t) 5: (1,0,0) (1,1,1)/sqrt(3) (s, t)

Observe that both s and t are between 0 and 1 and can be calculated according to one of the three approaches that have been described (plane, cylinder or sphere)

Page 32: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

32

Texture mapping, OpenGL and Lab Lets have a look at a simple example, the unit box.

x

y

p1 p0

p2 p3 Front face

z

y

p0 p4

p5 p2 Left face

y

x

z

p0 p1

p2 p3 p5

p4

P0: (0, 0, 1)

P1: (1, 0, 1)

P2: (0, 1, 1)

P3: (1, 1, 1)

P4: (0, 0, 0)

P5: (0, 1, 0)

P6: (1, 0, 0) P7: (1, 1, 0)

p7

Page 33: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

33

Texture mapping, OpenGL and Lab Assume that the left figure shows the front face of the box, consisting of two triangles. Assume also that we want the whole texture to be mapped to the front face of the box.

s

t

(0,0)

(1,1)

Unit texture plane

This means for the front face, p0 has to be associated with (s, t)=(0, 0). p1 has to be associated with (s, t)=(1, 0). p2 has to be associated with (s, t)=(0, 1) and finally p3 has to be associated with (s, t)=(1, 1).

x

y

p1 p0

p2 p3 Front face

Page 34: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

34

Texture mapping, OpenGL and Lab

Here we just show the vertex table for p0. In the box we can see that p0 is associated with three faces of the box (front, left and bottom). Therefore there are three normal vectors and (s, t) coordinates for p0. So far we just know the (s, t) coordinates for the front face. Vertex table for p0: Nr coordinate normal texture (s, t) Front face: 0 (0,0,1) (0, 0, 1) (0, 0) Left face: 1 (0,0,1) (-1, 0, 0) (?, ?) Bottom face: 2 (0,0,1) (0, -1, 0) (?, ?)

Page 35: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

35

Texture mapping, OpenGL and Lab Lets now have a look at the left face of the box. Assume that we want the whole texture to be mapped to the left face of the box as well (meaning the front face and the left face get exactly the same texture map).

s

t

(0,0)

(1,1)

Unit texture plane

This means for the left face, p4 has to be associated with (s, t)=(0, 0). p0 has to be associated with (s, t)=(1, 0). p5 has to be associated with (s, t)=(0, 1) and finally p2 has to be associated with (s, t)=(1, 1).

z

y

p0 p4

p5 p2 Left face

Page 36: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

36

Texture mapping, OpenGL and Lab

Here we just show the vertex table for p0. In the box we can see that p0 is associated with three faces of the box (front, left and bottom). Now we also know the (s, t) coordinates for the left face. Vertex table for p0: Nr coordinate normal texture (s, t) Front face: 0 (0,0,1) (0, 0, 1) (0, 0) Left face: 1 (0,0,1) (-1, 0, 0) (1, 0) Bottom face: 2 (0,0,1) (0, -1, 0) (?, ?) The rest will be filled during “lektion 3”.

Page 37: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

37

Texture mapping

Page 38: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

38

Texture mapping

Page 39: TNM046: Datorgrafik Texture mapping - Linköping Universitysasgo26/TNM046/Lectures/Lecture_5.pdf5 Planar/Linear mapping Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

39

Texture mapping