cs123 introduction to computer graphics andries van dam © andries van dam texture mapping...

23
cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

Upload: carlton-bucey

Post on 15-Dec-2015

228 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

Andries van Dam

Texture MappingBeautification of Surfaces

1/23

Page 2: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

Andries van DamAndries van Dam

Texture mapping: Implemented in hardware on every GPU Simplest surface detail hack, dating back to the ‘60s GE

flight simulator and its terrain generator Technique:

“Paste” photograph or bitmap (the texture, for example: a brick pattern, a wood grain pattern, a sky with clouds) on a surface to add detail without adding more polygons

Map texture onto surface, get surface color or alter the object’s surface color

Think of texture map as stretchable contact paper

November 4, 2014

Texture Mapping Overview (1/3)

2/23

Page 3: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

How do we add more detail to a model? Add more detailed geometry; more, smaller triangles:

Pros: Responds realistically to lighting, other surface interaction Cons: Difficult to generate, takes longer to render, takes more memory space

Map a texture to a model: Pros: Can be stored once and reused, easily compressed to reduce size, rendered very quickly, very

intuitive to use, especially useful on far-away objects, terrain, sky,…”billboards” used extensively in games

Cons: Very crude approximation of real life. Texture mapped but otherwise unaltered surfaces still look smooth. Need to perspectivize/filter for real effectiveness

What can you put in a texture map? (or any other kind of map?) Diffuse, ambient, specular, or any kind of color Specular exponents, transparency or reflectivity coefficients Surface normal data (for normal mapping or bump mapping – stay tuned) Projected reflections or shadows

Texture Mapping Overview (2/3: Motivation)

3/23

Page 4: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

A function is a mapping Takes any value in the domain as an input and outputs (“maps it to”) one

unique value in the co-domain. Mappings in “Intersect”: linear transformations with matrices

Map screen space points (input) to camera space rays (output) Map camera space rays into world space rays Map world space rays into un-transformed object space for intersecting Map intersection point normals to world space for lighting

Mapping a texture: Take points on the surface of an object (domain) Return an entry in the texture (co-domain)

Texture Mapping Overview (3/3: Mappings)

4/23

Page 5: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Texture mapping is process of mapping a geometric point in space to a value (color, normal, other…) in a texture map of arbitrary width and height Our goal is to map any arbitrary geometry to a texture map Done in two steps:

Map a point on geometry to a point on unit square (a proxy for texture map) Map unit square point to point on texture

Second mapping much easier, we’ll cover it first – both maps based on proportionality Note 1: this 2D uv coordinate system is unrelated to the camera’s 3D uvw coordinate system! Note 2: In this lecture, show unit square oriented with (0,0) in the bottom corner. However, most images have

(0,0) in upper left. In Ray, use the latter – simple adjustment

Texture Mapping Technique (1/8)

𝑢

𝑣

(0.0, 0.0)

(1.0, 1.0)

Van Gogh

5/23

Page 6: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Mapping a point in unit u, v square to a texture of arbitrary width/height: In general, for any point on unit square, corresponding point on texture of length pixels and

height pixels is

Above: (0.0, 0.0) -> (0, 0); (1.0, 1.0) -> (200, 100); (.7, .45) -> (140, 45) Once have coordinates for texture, just look up color of texture at these coordinates Coordinates not always a discrete (int) point on texture as they are mapped from points in

continuous uv space. May need to average neighboring texture pixels (i.e., filter)

Texture Mapping Technique (2/8)

6/23

(1.0, 1.0)𝑣

(0.0, 0.0)

unit texture square

(200, 100)

(0, 0)texture map

𝑢

Page 7: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Texture mapping polygons texture coordinates are pre-

calculated and specified per vertex

Vertices may have different texture coordinates for different faces

Texture coordinates are linearly interpolated across polygon, as usual

Texture Mapping Technique (3/8)

7/23

Page 8: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Texture mapping in “Ray”: mapping solids Using ray tracing, get an intersection point (x, y, z) in

object space Need to map this point to a point on the (u, v) unit

square, so we can map that to a texture value Three easy cases: planes, cylinders, and spheres Easiest to compute the mapping from (x, y, z)

coordinates in object space to (u, v) Can cause unwanted texture scaling Texture filtering is an option in most graphics

libraries OpenGL allows you to choose filtering method

GL_NEAREST: Picks the nearest pixel in the texture GL_LINEAR: Weighted average of the 4 nearest pixels

Texture Mapping Technique (4/8): Mapping from point on object to (u, v) square

8/23

Page 9: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Texture mapping large quads: How to map a point on a very large quad to a point on the unit square? Tiling: texture is repeated over and over across infinite plane Given coordinates of a point on an arbitrarily large quad that we want to tile

with quads of size , the coordinates on the unit square representing a texture with arbitrary dimensions are:

Texture Mapping Technique (5/8)

(𝑢 ,𝑣 )=( (𝑥% 𝑤 )𝑤

,(𝑦%  h)h )

9/23

y

x(w, h)

(0, 0)v

u(0, 0)(1.0, 1.0)

Infinite plane Unit planeNote: C/C++ do not allow non-integer moduli.In code, w and h must be integers.

Page 10: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

How to texture map cylinders and cones: Given a point P on the surface:

If it’s on one of the caps, map as though the cap is a plane If it’s on the curved surface:

Use position of point around perimeter to determine Use height of point to determine

Mapping v is trivial: [-.5, .5] gets mapped to [0.0, 1.0] just by adding .5

Texture Mapping Technique (6/8)

P x

y

z𝑣=.4

x

𝑢=.85

z

P

http://www.syvum.com/iosundry/teasers/cylind1b.gif

𝑣

𝑢

10/23

Page 11: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Computing coordinate for cones and cylinders: Must map all points on perimeter to [0,

1], going CCW (see arrows) Note where positive first quadrant is! Easiest way is to say , but computing can

be tricky Standard atan function computes a result

for but it’s always between 0 and and it maps two positions on the perimeter to the same value. Example: atan(1, 1) = atan(-1, -1)

atan2(x, y) yields values between and , but isn’t continuous -- see diagram

Texture Mapping Technique (7/8)

11/23

Page 12: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Texture mapping for spheres: Find coordinates for P We compute the same we do for

cylinders and cones: distance around perimeter of circle

At poles, or , there is a singularity. Set to some predefined value. (.5 is good)

is a function of the latitude of P

Texture Mapping Technique (8/8)

𝜙=sin− 1𝑃 𝑦

𝑟−𝜋2≤𝜙 ≤

𝜋2

= radius

𝑣=𝜙𝜋

+12

12/23

uv

ϕ

Page 13: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

We want to create a brick wall with a brick pattern texture A brick pattern is very repetitive, we can use a small texture and “tile” it

across the wall

Texture Mapping Style - Tiling

Without Tiling

With Tiling

Texture

Tiling allows you to scale repetitive textures to make texture elements just the right size.

13/23

Page 14: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

With non-repetitive textures, we have less flexibility Have to fill an arbitrarily large object with a texture of finite size Can’t tile (will be noticeable), have to stretch instead Example, creating a sky backdrop:

Texture Mapping Style - Stretching

TextureApplied with stretching

14/23

Page 15: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Sometimes, reducing objects to primitives for texture mapping doesn’t achieve the right result. Consider a simple house shape as an example If we texture map it using polygons, we get discontinuities at some edges.

Solution: Pretend object is a sphere and texture map using the sphere map

Texture Mapping Complex Geometry (1/5)

15/23

Page 16: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Intuitive approach: Place a bounding sphere around the complex object Find ray’s object space intersection with bounding sphere Convert to coordinates

Don’t actually need to construct a bounding sphere! Once have intersection point with object, just treat it as though it were on a sphere passing

through point. Same results, but different radii.

Texture Mapping Complex Geometry (2/5)

16/23

Page 17: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

When we treat the object intersection point as a point on a sphere passing through the point, our “sphere” won’t always have the same radius

What radius to use? Compute radius as distance from defined or computed center of complex object to the

nearest intersection point. Use that as the radius for the mapping.

Texture Mapping Complex Geometry (3/5)

17/23

roof intersection point = large radius

bottom intersection point = small radius

Page 18: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Results of spherical mapping on house: You can also use cylindrical or planar

mappings when texture mapping complex objects. Additionally, some programs offer “Automatic”

mapping that has a still different way of doing it. Each mapping has drawbacks

For example, what if we want to put a texture on an object like this?

Texture Mapping Complex Geometry (4/5)

18/23

Page 19: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

Andries van DamAndries van Dam

A checkerboard pattern is useful when looking for problems with mappings. The goal is to minimize uneven distortion of

the pattern. Spherical, cylindrical, and automatic have a lot of

distortion. Planar looks good from one view, but the map

overlaps itself and two axes are ignored. This leads to distortion when viewing from

other axes. There is no good solution. To get the right look, the artist will often have to

go in and manually connect and split edges in the maps.

November 4, 2014

Texture Mapping Complex Geometry (5/5)

19/23

Spherical AutomaticCylindrical

Images by Vivian MorgowiczRendered in Maya on the CS125 lighting stage

=

Planar

=

=

Handmade

Page 20: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Say we have a triangle mesh And we’ve defined colors at vertices of triangles How do we define colors of pixels that are on interior or edges of triangle? Interpolating colors in triangle can be done using “scanline” interpolation

of scanline extrema, but is made easier by using Barycentric coordinates define a point on a triangle in terms of its 3 vertices then use this representation to choose appropriate color

Many applications Mesh Colors (painting textures on to arbitrary meshes):

Cem Yuksel, John Keyser, and Donald H. HouseMesh ColorsACM Transactions on Graphics (TOG), Vol. 29, No. 2, 2010

Take CS224

Barycentric Coordinates

?

20/23

Page 21: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Consider interpolating between two values along a line Given two colors and , compute any value along “line” between the two colors by

evaluating:

This equation can be written as:

and are the Barycentric Coordinates of line segment ft The EQN of line is convex linear combination (i.e., a weighted average) of its endpoints

– parameters can be viewed as weights. Recall splines, clipping, color mixing… Barycentric coordinates can be generalized to triangles

Barycentric Coordinates

P (𝑡 )=(1− 𝑡 ) 𝐴1+𝑡 𝐴2

P

𝑃 (𝑡1 , 𝑡2 ,𝑡 3 )=𝑡1 𝐴1+𝑡 2𝐴2+𝑡 3𝐴3 𝑡1+𝑡 2+𝑡3=1𝑡 1 , 𝑡 2 , 𝑡3≥0

21/23

Page 22: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

When intersect a ray with a polyhedral object (not needed for our intersect/ray projects): Return vertex data of triangle intersected Return Barycentric coordinates of intersection point These coordinates can be used to interpolate between vertex

colors, normals, texture coordinates, or other data – convex linear combo of vertex values

But what are weights for an arbitrary point ? Restate as: What weights do we hang on each vertex such that triangle would be perfectly balanced on a pin at point , as center of mass, the barycenter?

Another way of thinking about this is by trianglearea. The weight at should be proportional tothe area of the triangle opposite, i.e., , etc..

Think of area of that triangle decreasing as does the influence of , the further P is from

Applying Barycentric Coordinates (1/2)

http://mathworld.wolfram.com/images/eps-gif/Barycentric_901.gif

22/23

ti is a weight proporti0nal to distance, not just the distance

Page 23: Cs123 INTRODUCTION TO COMPUTER GRAPHICS Andries van Dam © Andries van Dam Texture Mapping Beautification of Surfaces 1/23

cs123 INTRODUCTION TO COMPUTER GRAPHICS

Andries van Dam©

November 4, 2014Andries van DamAndries van Dam

Compute Q as intersection of line through and and line through and

+ R is the ratio of the distance from A1 to P and from P to Q

Divide to normalize the coefficients to add to 1

Useful for ray tracing arbitrary meshes Intersect ray with plane of triangle Calculate coordinates for intersection point If all are in range [0,1] then the point is in the triangle

Applying Barycentric Coordinates (2/2)

http://mathworld.wolfram.com/images/eps-gif/Barycentric_901.gif

23/23