foundations of computer graphics (spring 2010) cs 184, lecture 18: shading & texture mapping...

52
Foundations of Computer Foundations of Computer Graphics (Spring 2010) Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping http://inst.eecs.berkeley.edu/~cs184 Many slides from Greg Humphreys, UVA and Rosalee Wolfe, DePaul tutorial teaching texture mapping v Chapter 11 in text book covers some portions

Post on 20-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Foundations of Computer Graphics Foundations of Computer Graphics (Spring 2010)(Spring 2010)

CS 184, Lecture 18: Shading & Texture Mapping

http://inst.eecs.berkeley.edu/~cs184

Many slides from Greg Humphreys, UVA andRosalee Wolfe, DePaul tutorial teaching texture mapping visuallyChapter 11 in text book covers some portions

Page 2: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

To DoTo Do

Work on HW4 milestone

Prepare for final push on HW 4, HW 5

Page 3: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

OutlineOutline

Brief discussion on Phong illumination model and Gouraud, Phong shading

Texture Mapping (bulk of lecture)

These are key components of shading

Page 4: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Phong Illumination ModelPhong Illumination Model

Specular or glossy materials: highlights Polished floors, glossy paint, whiteboards For plastics highlight is color of light source (not object) For metals, highlight depends on surface color

Really, (blurred) reflections of light source

Roughness

Page 5: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Idea of Phong IlluminationIdea of Phong Illumination

Find a simple way to create highlights that are view-dependent and happen at about the right place

Not physically based

Use dot product (cosine) of eye and reflection of light direction about surface normal

Alternatively, dot product of half angle and normal

Raise cosine lobe to some power to control sharpness or roughness

Page 6: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Phong FormulaPhong Formula

-LR

E

( ) pI R E

?R 2( )R L L N N

Page 7: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Alternative: Half-Angle (Blinn-Phong)Alternative: Half-Angle (Blinn-Phong)

In practice, both diffuse and specular components for most materials

HN

( ) pI N H

Page 8: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Flat vs. Gouraud ShadingFlat vs. Gouraud Shading

Flat - Determine that each face has a single normal, and color the entire face a single value, based on that normal.

Gouraud – Determine the color at each vertex, using the normal at that vertex, and interpolate linearly for pixels between vertex locations.

glShadeModel(GL_FLAT) glShadeModel(GL_SMOOTH)

Page 9: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Gouraud Shading – DetailsGouraud Shading – Details

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

1 3 3 1

1 3

( ) ( )s sb

I y y I y yI

y y

( ) ( )a b p b p ap

b a

I x x I x xI

x x

pI

Actual implementation efficient: difference equations while scan converting

Page 10: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Gouraud and ErrorsGouraud and Errors

I1 = 0 because (N dot E) is negative.

I2 = 0 because (N dot L) is negative.

Any interpolation of I1 and I2 will be 0.

I1 = 0 I2 = 0

area of desired highlight

Page 11: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

2 Phongs make a Highlight2 Phongs make a Highlight

Besides the Phong Reflectance model (cosn), there is a Phong Shading model.

Phong Shading: Instead of interpolating the intensities between vertices, interpolate the normals.

The entire lighting calculation is performed for each pixel, based on the interpolated normal. (OpenGL doesn’t do this, but you can with current programmable shaders)

I1 = 0 I2 = 0

Page 12: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Problems with Interpolated ShadingProblems with Interpolated Shading

Silhouettes are still polygonal

Interpolation in screen, not object space: perspective distortion

Not rotation or orientation-independent

How to compute vertex normals for sharply curving surfaces?

But at end of day, polygons is mostly preferred to explicitly representing curved objects like spline patches for rendering

Page 13: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

OutlineOutline

Brief discussion on Phong illumination model and Gouraud, Phong shading

Texture Mapping (bulk of lecture)

These are key components of shading

Page 14: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Texture MappingTexture Mapping

Important topic: nearly all objects textured Wood grain, faces, bricks and so on Adds visual detail to scenes

Meant as a fun and practically useful lecture

Polygonal model With surface texture

Page 15: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Adding Visual DetailAdding Visual Detail

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

Page 16: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

ParameterizationParameterization

geometrygeometry

++ ==

imageimage texture maptexture map

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

Page 17: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Option: Varieties of projectionsOption: Varieties of projections

[Paul Bourke][Paul Bourke]

Page 18: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Option: unfold the surfaceOption: unfold the surface

[Piponi2000]

Page 19: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Option: make an atlasOption: make an atlas

[Sander2001]

charts atlas surface

Page 20: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Option: it’s the artist’s problemOption: it’s the artist’s problem

Page 21: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

OutlineOutline

Types of projections

Interpolating texture coordinates

Broader use of textures

Page 22: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

How to map object to texture?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 23: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Idea: Use Map ShapeIdea: Use Map Shape

Map shapes correspond to various projections Planar, Cylindrical, Spherical

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, sphere well defined Maps from object to these are simply spherical, cylindrical,

cartesian coordinate systems

Page 24: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Planar mappingPlanar mapping

Like projections, drop z coord (s,t) = (x,y)

Problems: what happens near z = 0?

Page 25: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Cylindrical MappingCylindrical Mapping

Cylinder: r, θ, z with (s,t) = (θ/(2π),z) Note seams when wrapping around (θ = 0 or 2π)

Page 26: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Spherical MappingSpherical Mapping

Convert to spherical coordinates: use latitude/long. Singularities at north and south poles

Page 27: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Cube MappingCube Mapping

Page 28: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Cube MappingCube Mapping

Page 29: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

OutlineOutline

Types of projections

Interpolating texture coordinates

Broader use of textures

Page 30: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

11stst idea: Gouraud interp. of texcoords 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

1 3 3 1

1 3

( ) ( )s sa

I y y I y yI

y y

( ) ( )a b p b p aa

b a

I x x I x xI

x x

pI

Actual implementation efficient: difference equations while scan converting

Page 31: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

ArtifactsArtifacts

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?

Why not in standard Gouraud shading?

Hint: problem is in interpolating parameters

Page 32: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Interpolating ParametersInterpolating Parameters

The problem turns out to be fundamental to interpolating parameters in screen-space Uniform steps in screen space uniform steps in world space

Page 33: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Texture MappingTexture Mapping

Linear interpolationof texture coordinates

Correct interpolationwith perspective divide

Hill Figure 8.42

Page 34: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Interpolating ParametersInterpolating 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

Page 35: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Perspective-Correct InterpolationPerspective-Correct Interpolation

Skipping a bit of math to make a long story 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 know z anymore Solution: we do know 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 36: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Texture Map FilteringTexture Map Filtering

Naive texture mapping aliases badly Look familiar?

int uval = (int) (u * denom + 0.5f);int vval = (int) (v * denom + 0.5f);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 37: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Mip MapsMip 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 38: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

MIP-map ExampleMIP-map Example

No filtering:

MIP-map texturing:

AAAAAAAGHMY EYES ARE BURNING

Where are my glasses?

Page 39: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

OutlineOutline

Types of projections

Interpolating texture coordinates

Broader use of textures

Page 40: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Texture Mapping ApplicationsTexture Mapping Applications

Modulation, light maps

Bump mapping

Displacement mapping

Illumination or Environment Mapping

Procedural texturing

And many more

Page 41: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Modulation texturesModulation textures

)))()(((),( SSTTL LLn

SDAAE IKIKISRVKLNKIKItsTI )))()(((),( SSTTL LLn

SDAAE IKIKISRVKLNKIKItsTI

Map texture values to scale factor

Woo

d te

xtur

e

Texturevalue

Page 42: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Bump MappingBump Mapping

Texture = change in surface normal!

Sphere w/ diffuse texture Swirly bump mapSphere w/ diffuse texture

and swirly bump map

Page 43: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Displacement MappingDisplacement Mapping

Page 44: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Illumination MapsIllumination Maps Quake introduced illumination maps or light

maps to capture lighting effects in video gamesTexture map:

Texture map+ light map:

Light map

Page 45: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Environment MapsEnvironment 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

Page 46: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Solid texturesSolid texturesTexture values indexed

by 3D location (x,y,z)

• Expensive storage, or

• Compute on the fly,e.g. Perlin noise

Page 47: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg

Procedural Texture GalleryProcedural Texture Gallery

Page 48: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg
Page 49: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg
Page 50: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg
Page 51: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg
Page 52: Foundations of Computer Graphics (Spring 2010) CS 184, Lecture 18: Shading & Texture Mapping cs184 Many slides from Greg