cs559: computer graphics lecture 27: texture mapping li zhang spring 2008

46
CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Upload: emerald-hoover

Post on 23-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

CS559: Computer Graphics

Lecture 27: Texture MappingLi Zhang

Spring 2008

Page 2: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Today• Continue on texture maping

• Reading– Shirley: Ch 11.2, 11.3, 11.4– Redbook: Ch 9– (optional) Moller and Haines: Real-Time Rendering, 3e,

Ch 6• Linux:

/p/course/cs559-lizhang/public/readings/6_texture.pdf• Windows: P:\course\cs559-lizhang\public\readings\

6_texture.pdf

Page 3: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Texture Mapping

Many slides from Ravi Ramamoorthi, Columbia Univ, Greg Humphreys, UVA and Rosalee Wolfe, DePaul tutorial teaching texture mapping visually

Page 4: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Adding Visual Detail

• Basic idea: use images instead of more polygons to represent fine scale color variation

Page 5: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Parameterization

geometrygeometry

++ ==

imageimage texture maptexture map

• Q: How do we decide where on the geometryeach color from the image should go?

Page 6: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Option: Varieties of mappings

[Paul Bourke][Paul Bourke]

Page 7: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

How to map object to texture?• To each vertex (x,y,z in object coordinates), must

associate 2D texture coordinates (s,t)• So texture fits “nicely” over object

Page 8: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

How to map object to texture?• To each vertex (x,y,z in object coordinates), must

associate 2D texture coordinates (s,t)• So texture fits “nicely” over object

Page 9: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Outline• Types of mappings• Interpolating texture coordinates • Broader use of textures

Page 10: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Planar mapping• Like projections, drop z coord (u,v) = (x/W,y/H)• Problems: what happens near silhouettes?

Page 11: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Cylindrical Mapping• Cylinder: r, θ, z with (u,v) = (θ/(2π),z)

– Note seams when wrapping around (θ = 0 or 2π)

Page 12: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Basic procedure• First, map (square) texture to basic map shape• Then, map basic map shape to object

– Or vice versa: Object to map shape, map shape to square

• Usually, this is straightforward– Maps from square to cylinder, plane, …– Maps from object to these are simply coordinate

transform

Page 13: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Spherical Mapping• Convert to spherical coordinates: use

latitude/long.– Singularities at north and south poles

Page 14: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Cube Mapping

Page 15: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Cube Mapping

Page 16: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Slides from Leonard Mcmillan

Page 17: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Outline• Types of projections• Interpolating texture coordinates • Broader use of textures

Page 18: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

1st idea: Gouraud interp. of texcoords

1I

2I

3I

sI

Using barycentric Coordinates

Page 19: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

1st idea: Gouraud interp. of texcoords

Scan line

1I

2I

3I

1y

2y

3y

syaI bI

1 2 2 1

1 2

( ) ( )s sa

I y y I y yI

y y

31

1331 )()(

yy

yyIyyII ssb

ab

asbsbas xx

xxIxxII

)()(

sI

Page 20: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Artifacts• McMillan’s demo of this is at

http://graphics.lcs.mit.edu/classes/6.837/F98/Lecture21/Slide05.html

• Another example http://graphics.lcs.mit.edu/classes/6.837/F98/Lecture21/Slide06.html

• What artifacts do you see?• Why?• Hint: problem is in interpolating parameters

Page 21: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Interpolating Parameters• The problem turns out to be fundamental to

interpolating parameters in screen-space– Uniform steps in screen space uniform steps in world space

Texture image

Page 22: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Slides from Jingyi Yu

Linear Interpolation in Screen Space

Compare linear interpolation in screen space

Without loss of generality, let’s assume that the viewport is located 1 unit away from the center of projection. That is

0100

0100

0010

0001

Page 23: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Linear Interpolation in 3-Space

to interpolation in 3-space:

Slides from Jingyi Yu

Page 24: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

How to make them MeshStill need to scan convert in screen space... so we need a mapping from t

values to s values. We know that the all points on the 3-space edge project onto our screen-space line. Thus we can set up the following equality:

and solve for s in terms of t giving:

Unfortunately, at this point in the pipeline (after projection) we no longer have z1 and z2 lingering around (Why? Efficiency, don’t need to compute 1/z all the time). However, we do have w1 = 1/z1 and w2 = 1/z2.

Slides from Jingyi Yu

Page 25: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Interpolating ParametersWe can now use this expression for s to interpolate arbitrary parameters, such as texture indices (u, v), over our 3-space triangle. This is accomplished by substituting our solution for s given t into the parameter interpolation.

Therefore, if we premultiply all parameters that we wish to interpolate in 3-space by their corresponding w value and add a new plane equation to interpolate the w values themselves, we can interpolate the numerators and denominator in screen-space. We then need to perform a divide a each step to get to map the screen-space interpolants to their corresponding 3-space values. This is a simple modification to the triangle rasterizer that we developed in class.

Slides from Jingyi Yu

Page 26: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

1st idea: Gouraud interp. of texcoords

Scan line

1I

2I

3I

1y

2y

3y

syaI bI

1 2 2 1

1 2

( ) ( )s sa

I y y I y yI

y y

31

1331 )()(

yy

yyIyyII ssb

ab

asbsbas xx

xxIxxII

)()(

sI

Replace I to uw, vw, and w, then compute (uw/w, and vw/w)

Page 27: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Perspective-Correct Interpolation• In short…

– Rather than interpolating u and v directly, interpolate u/z and v/z

• These do interpolate correctly in screen space• Also need to interpolate z and multiply per-pixel

– Problem: we don’t keep z anymore– Solution: we do keep w 1/z– So…interpolate uw and vw and w, and compute

u = uw/w and v = vw/w for each pixel• This unfortunately involves a divide per pixel

• http://graphics.lcs.mit.edu/classes/6.837/F98/Lecture21/Slide14.html

Page 28: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Texture Mapping

Linear interpolationof texture coordinates

Correct interpolationwith perspective divide

Hill Figure 8.42

http://graphics.lcs.mit.edu/classes/6.837/F98/Lecture21/Slide14.html

Page 29: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Why don’t we notice? Traditional screen-space Gourand shading is wrong. However, you usually will not notice because the transition in colors is very smooth (And we don't know what the right color should be anyway, all we care about is a pretty picture). There are some cases where the errors in Gourand shading become obvious.

• When do we notice? – When switching between different

levels-of-detail representations

– At "T" joints.

Page 30: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Dealing with Incorrect InterpolationYou can reduce the perceived artifacts of non-perspective correct interpolation by subdividing the texture-mapped triangles into smaller triangles (why does this work?). But, fundamentally the screen-space interpolation of projected parameters is inherently flawed.

http://groups.csail.mit.edu/graphics/classes/6.837/F98/Lecture21/Slide15.html

Page 31: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Texture Map Filtering

• Naive texture mapping aliases badly • Look familiar?

int uval = (int) (u * W);int vval = (int) (v * H);int pix = texture.getPixel(uval, vval);

• Actually, each pixel maps to a region in texture– |PIX| < |TEX|

• Easy: interpolate (bilinear) between texel values – |PIX| > |TEX|

• Hard: average the contribution from multiple texels– |PIX| ~ |TEX|

• Still need interpolation!

Page 32: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Mip Maps• Keep textures prefiltered at multiple resolutions

– For each pixel, linearly interpolate between two closest levels (e.g., trilinear filtering)

– Fast, easy for hardware

• Why “Mip” maps?

Page 33: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

MIP-map Example

• No filtering:

• MIP-map texturing:

AAAAAAAGH

Where are my glasses?

Page 34: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008
Page 35: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Outline• Types of projections• Interpolating texture coordinates • Broader use of textures

Page 36: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Texture Mapping Applications• Modulation, light maps• Bump mapping• Displacement mapping• Illumination or Environment Mapping• Procedural texturing• And many more

Page 37: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Modulation textures

Map texture values to scale factorW

ood

text

ure

Page 38: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Bump Mapping• Texture = change in surface normal!

Sphere w/ diffuse texture Swirly bump mapSphere w/ diffuse textureand swirly bump map

Page 39: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Displacement Mapping

Page 40: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Illumination Maps

• Quake introduced illumination maps or light maps to capture lighting effects in video games

Texture map:

Texture map+ light map:

Light map

Page 41: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Cube map

Page 42: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Sphere map

Page 43: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Shaddow map

Page 44: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Environment Maps

Images from Illumination and Reflection Maps: Simulated Objects in Simulated and Real EnvironmentsGene Miller and C. Robert HoffmanSIGGRAPH 1984 “Advanced Computer Graphics Animation” Course Notes

Terminator

Page 45: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Arc-length parameterization

Freeformx

y

0 1t

)(

)()(

2

1

tf

tft

y

xf

0 Ls

)(

)()(

2

1

sg

sgs

y

xg

Arc-length parameterization

dtdt

tdfs

t

0

)(

Page 46: CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008

Interpolating Parameters• Perspective foreshortening is not getting applied

to our interpolated parameters– Parameters should be compressed with distance– Linearly interpolating them in screen-space doesn’t

do this

• Correct Mapping Results– http://graphics.lcs.mit.edu/classes/6.837/F98/

Lecture21/Slide14.html