Lecture 6 Texture mapping
Linear interpolation Texture mapping We already learned a lot: Vectors, basic geometric entities Intersection of objects Matrices, transformations And some shading
Motivation Linear interpolation Texture mapping 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 Linear interpolation Texture mapping 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 Linear interpolation Texture mapping 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 Linear interpolation Texture mapping 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 Linear interpolation Texture mapping Given two vectors a, b, linear interpolation is defined 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 Texture mapping 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 efficiently? What about phong shading? We will learn this in a later lecture
Texture mapping Linear interpolation 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 Linear interpolation 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 Linear interpolation Texture mapping Basic idea of texture mapping: Instead of calculating color, shade, light, etc. for each pixel we just paste images to our objects in order to create the illusion of realism
Different approaches Linear interpolation Texture mapping Different approaches exist, for example 2D vs. 3D: 3D Object 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 2D texture 3D texture
Outline 1 Linear interpolation Texture mapping 2 3D stripe textures Texture arrays Solid noise 3 Basic idea Spherical mapping Triangles 4 Bump mapping Displacement mapping Environment mapping Linear interpolation Texture mapping
Texturing 3D objects 3D stripe textures Texture arrays Solid noise Let s start with 3D mapping, which is a procedural approach, i.e. we use a mathematical procedure to create a 3D texture, i.e. 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, y, z) = c with c R 3 f(x p, y p, z p ) = c p
3D stripe textures 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise Smooth variation between two colors, instead of two distinct 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 3D stripe textures Texture arrays Solid noise 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. Carving vs. array lookup 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).
2D texture arrays 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise 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 floor function that give the highest integer value x.
Nearest neighbor interpolation 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise For smoother effects we may use bilinear interpolation: c(u, v) = (1 u )(1 v )c ij +u (1 v )c (i+1)j +(1 u )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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid 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 randomness by adding noice, e.g. to create the impression of a marble like structure.
Perlin noise 3D stripe textures Texture arrays Solid 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 Problem: bad results and/or 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 3D stripe textures Texture arrays Solid 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 3D stripe textures Texture arrays Solid noise 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 2 x + v 2 y + v 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise
Hashing 3D stripe textures Texture arrays Solid noise
Perlin noise 3D stripe textures Texture arrays Solid 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 3D stripe textures Texture arrays Solid noise 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 3D stripe textures Texture arrays Solid noise Characteristics of hermite interpolation (or why this creates better noise than linear ): Linear interpolation: linear weights, i.e. ω t Hermite interpolation: cubic weights, i.e. ω t 3
Summary 3D stripe textures Texture arrays Solid noise Perlin noise: Virtual 3D array & hashing Scalar product with random unit vector Hermite interpolation
Outline 1 Linear interpolation Texture mapping 2 3D stripe textures Texture arrays Solid noise 3 Basic idea Spherical mapping Triangles 4 Bump mapping Displacement mapping Environment mapping 3D stripe textures Texture arrays Solid noise
Basic idea Spherical mapping Triangles 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 Basic idea Spherical mapping Triangles How do we map a rectangular image onto a sphere?
Spherical texture mapping Basic idea Spherical mapping Triangles Example: use world map and sphere to create a globe
Spherical texture mapping Basic idea Spherical mapping Triangles We have seen the parametric equation of a sphere with radius r and center c: x = x c + r cos φ sin θ y = y c + r sin φ sin θ z = z c + r cos θ Given a point (x, y, z) on the surface of the sphere, we can find θ and φ by θ = arccos z zc r φ = arctan y yc x x c
Spherical texture mapping Basic idea Spherical mapping Triangles For each point (x, y, z) we have θ = arccos z zc r φ = arctan y yc 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 Basic idea Spherical mapping 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 = (xb, y b ), c = (x c, y c ) become a = (u a, v a ), b = (u b, v b ), c = (u c, v c ) (0.8, 0.7) (0.1, 0.9) (0.6, 0.1)
Texturing triangles Basic idea Spherical mapping Triangles Remember that barycentric coordinates are very useful for interpolating over a triangle and related textures ;) p(β, γ) = a + β( b a) + γ( c a) (0.8, 0.7) (0.1, 0.9) (0.6, 0.1) 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.
Texturing triangles Basic idea Spherical mapping Triangles (0.8, 0.7) 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. (0.1, 0.9) (0.6, 0.1) Also, (u, v) coordinates for the vertices may lie outside the range [0, 1] [0, 1].
Texturing triangles Basic idea Spherical mapping 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 Basic idea Spherical mapping Triangles 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 minification: efficiency (esp. when whole texture is mapped onto one pixel!)
MIP-mapping Basic idea Spherical mapping Triangles 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 Linear interpolation Texture mapping 2 3D stripe textures Texture arrays Solid noise 3 Basic idea Spherical mapping Triangles 4 Bump mapping Displacement mapping Environment mapping Basic idea Spherical mapping Triangles
Bump mapping Bump mapping Displacement mapping Environment mapping One of the reasons why we apply texture mapping: Real surfaces are hardly flat but often rough and bumpy. These bumps cause (slightly) different reflections of the light.
Bump mapping Bump mapping Displacement mapping Environment 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 effect of bump mapping is an apparent change of the geometry of the object.
Bump mapping Displacement mapping Environment mapping Bump mapping Major problems with bump mapping: silhouettes and shadows
Displacement mapping Bump mapping Displacement mapping Environment 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 refined using the displacement map, giving an increase in storage requirements.
Displacement mapping Bump mapping Displacement mapping Environment mapping
Environment mapping Bump mapping 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 Bump mapping Displacement mapping Environment mapping... why not use this to make objects appear to reflect 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 reflection vector, and use that to look-up texture values from the cubic texture map.
Environment mapping Bump mapping Displacement mapping Environment mapping
Bump mapping Displacement mapping Environment mapping
And now? Bump mapping Displacement mapping Environment mapping In case you didn t notice: it s halftime :)... so let s sit back and have a break before we continue. Lifted, copyrighted by Pixar/Disney (but you find various versions of it on YouTube)
What s next? Bump mapping Displacement mapping Environment mapping 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 final information about time and room see http://www.cs.uu.nl/education/vak.php?vak=infogr
The midterm exam Bump mapping Displacement mapping Environment mapping What do I have to do? Come in time Bring a pen (no pencil) Bring your student id And know the answers ;) Note: You may not use books, notes, or any electronic equipment (including cell phones!).
The midterm exam Bump mapping Displacement mapping Environment mapping The exam covers lectures 1-5 and tutorials 1-3. If you...... followed the lectures... read the textbook... and actively did the exercises... you should be fine.
Important dates Bump mapping Displacement mapping Environment mapping 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