• No results found

TNM046: Datorgrafik Texture mapping

N/A
N/A
Protected

Academic year: 2021

Share "TNM046: Datorgrafik Texture mapping"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(1)

TNM046: Datorgrafik Texture mapping

Sasan Gooran VT 2014

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.

3

Texture Mapping

(2)

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?

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.

6

Linear mapping

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

(3)

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

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)

9

Cylindrical mapping

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

(4)

10

Cylindrical mapping

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

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)

12

Spherical mapping

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

(5)

13

Spherical mapping

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

14

Basic Idea

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.

(6)

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.

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)

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)

(7)

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

20

Map unit square to Texture

Nearest Neighbor Bilinear

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)

(8)

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)

23

Map from point object to unit square Cylinder

x = r cosϕ y = r sinϕ 0 ≤ z ≤ h

"

#

$

%

$

0 ≤ ϕ ≤ 2π

ϕ r

x

y z

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

24

Map from point object to unit square Cylinder

x = r cosϕ y = r sinϕ 0 ≤ z ≤ h

"

#

$

%

$ −π / 2 ≤ ϕ ≤ 3π / 2

ϕ r

x

y z

tanϕ =y

x ϕ = 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 > 0 tan−1(y / x) + π, if x < 0 π / 2, if x = 0 and y > 0

−π / 2, if x = 0 and y < 0

"

#

$

$

%

$

$

(9)

25

Map from point object to unit square Cylinder

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

s =

ϕ

+

π

/ 2 2

π

t =z

h

!

"

##

$

##

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

26

Map from point object to unit square Sphere

x = r sinθ cosϕ y = r sinθ sinϕ z = r cosθ

!

"

#

$

#

ϕ

0 ≤ ϕ ≤ 2π x

y z

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

0 ≤ θ ≤ π

27

Map from point object to unit square Sphere

x = r sinθ cosϕ y = r sinθ sinϕ z = r cosθ

!

"

#

$

#

ϕ x

y z

−π / 2 ≤ ϕ ≤ 3π / 2

θ r 0 ≤ θ ≤ π

tanϕ =y

x ϕ = arctan(x, y) See page 24

cosθ =z

r= z

x2+ y2+ z2 θ = cos−1( z x2+ y2+ z2) Note: θ is between 0 and π, and the result of cos-1 is also between 0 and π.

(10)

28

Map from point object to unit square Sphere

ϕ = arctan(x, y)

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

s =

ϕ

+

π

/ 2 2

π

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( z x2+ y2+ z2

)

For arctan see page 24

Ex: 40 a and e

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.

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.

(11)

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)

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

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

(12)

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) (?, ?)

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 Left face p2

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”.

(13)

37

Texture mapping

38

Texture mapping

39

Texture mapping

References

Related documents

Pick the first point, then the sense of your fingers gives you the other two points in the

•  Apply an illumination model to each vertex to calculate the vertex intensity (Ambient light or/and diffuse light or/and specular reflection). •  Linearly interpolate

•  Apply an illumination model to each vertex to calculate the vertex intensity (Ambient light or/and diffuse light or/and specular reflection). •  Linearly interpolate

Assume that we have 5 triangles, which are ordered randomly according to their distance to the view plane (Left table). We sort the triangles according to their distance

If the distance from C to the upper clip plane (CP) is bigger than R then the upper clip plane does not intersect the bounding sphere, thus, any object triangle doesn’t

Source: Teaching Texture Mapping Visually, Rosalee Wolfe, DePaul

In OpenGL, together with the vertex coordinate you also specify the normal vector and the texture coordinates (for texture mapping). Texture mapping will be

In order to perform different transformations (e.g. scaling, translation, rotation etc.) on an object you transform the object’s vertices.. This is done by multiplying the