Texturing CS 6965 Fall 2011 90 80 70 60 50 40 FPS (prog2) - - PowerPoint PPT Presentation

texturing
SMART_READER_LITE
LIVE PREVIEW

Texturing CS 6965 Fall 2011 90 80 70 60 50 40 FPS (prog2) - - PowerPoint PPT Presentation

Texturing CS 6965 Fall 2011 90 80 70 60 50 40 FPS (prog2) 30 20 10 0 Erik's Danny's Program 2 CS 6965 Fall 2011 2 Texture mapping Most real objects do not have uniform color Texture: color and other material


slide-1
SLIDE 1

Texturing

CS 6965 Fall 2011

slide-2
SLIDE 2

Fall 2011 CS 6965

Erik's Danny's 10 20 30 40 50 60 70 80 90 FPS (prog2)

Program 2

2

slide-3
SLIDE 3

Fall 2011 CS 6965

Texture mapping

  • Most real objects do not have

uniform color

  • Texture: color and other material

properties as a function of space

3

slide-4
SLIDE 4

Fall 2011 CS 6965

Texture mapping topics

  • Image textures
  • Texture coordinates
  • Linear, cylindrical, spherical mappings
  • Barycentric coordinates for triangles
  • Software architecture
  • Procedural textures
  • Simple: checkerboards, tiles, etc.
  • Fractal noise based: marble, granite,

wood, etc.

  • Bump mapping

Ken Perlin, NYU

4

slide-5
SLIDE 5

Fall 2011 CS 6965

Lambertian Shading

Compute hit position (P   = O   + tV   ) Call primitive to get normal (N  ) (normalized) costheta = −N  ⋅V   if (costheta < 0) normal =-normal Color light = scene.ambient*Ka foreach light source get CL and L   dist= L   ,Ln   = L   L   cosphi = N  ⋅ Ln   if(cosphi > 0) if(!intersect with 0 < t < dist) light += CL *(Kd *cosphi) result=light*surface color

5

slide-6
SLIDE 6

Fall 2011 CS 6965

Simple image texture

Use hit position to determine image location P   : hit position C   : lower left corner of image U  : image X axis in world space V   : image Y axis in world space x, y: normalized image coordinates (0-1) P   = C   + xU   + yV   if U  ⋅V   = 0 : x = P   − C  

( )⋅U

  U   y = P   − C  

( )⋅V

  V  

6

World

P C x y U V Image

slide-7
SLIDE 7

Fall 2011 CS 6965

Simple image texture

x = P   − C  

( )⋅U

  U   y = P   − C  

( )⋅V

  V   ix = (int) x xres −1

( )

( ), fx = ix − x xres −1

( )

iy = (int) y yres −1

( )

( ), fy = iy − y yres −1

( )

Bilinear interpolation of image(ix,iy) - image(ix+1,iy+1) using fx,fy as interpolation factors Use interpolated color in lambertian shading

7

World

P C x y U V Image

slide-8
SLIDE 8

Fall 2011 CS 6965

Miscellaneous details

  • What happens for points outside of the texture?
  • Tile texture (mod function)
  • Clamp (stretch out last value)
  • Use an “outside” color
  • What if U/V are not orthogonal?
  • Solve a 2x2 matrix to get x/y
  • Other interpolations:
  • Nearest neighbor
  • Higher order interpolation
  • Filter reconstruction (what filter width?)

8

slide-9
SLIDE 9

Fall 2011 CS 6965

Texturing process

  • This example illustrated two orthogonal issues of texturing for

ray tracing:

  • Mapping hit position to image space (coordinate

transformation), example: linear

  • Mapping texture coordinate to color or other attributes,

example: linear interpolation of an image

9

slide-10
SLIDE 10

Fall 2011 CS 6965

Texture coordinates

  • Textures are not always flat
  • Textures live in a space called

“uvw”

  • Texture coordinate mapping:

xyz to uvw

  • Texture space may not be

linear

NASA Blue marble project

10

slide-11
SLIDE 11

Fall 2011 CS 6965

Cylinder projection

x = cos θ

( )

y = sin θ

( )

inverse mapping: R = x2 + y2 θ = atan2(y,x) atan2 :robust atan(y/x) for all quadrants

11

R θ Z

slide-12
SLIDE 12

Fall 2011 CS 6965

Cylinder projection

x = cosθ y = sinθ inverse mapping: R = x2 + y2 θ = atan2(y,x) atan2 :robust atan(y/x) for all quadrants u = θ 2π ;if(u<1)u+=1, v = z,w = R

12

R θ Z

slide-13
SLIDE 13

Fall 2011 CS 6965

Sphere mapping

  • Lots of ways to map a plane (image)

to a sphere

  • Remember grade school geography

http://www.colorado.edu/geography/gcraft/notes/mapproj/mapproj_f.html

13

slide-14
SLIDE 14

Fall 2011 CS 6965

Polar projection

Use longitude/latitude angles: x = cosθ sinφ y = sinθ sinφ z = cosφ inverse mapping: R = x2 + y2 + z2 φ = cos−1 z R θ = atan2(y,x) atan2 :robust atan(y/x) for all quadrants u = θ 2π ;if(u<0)u+=1, v = 1− θ π ,w = R

14

φ θ R

slide-15
SLIDE 15

Fall 2011 CS 6965

Arbitrary position/orientation

Z   : vector to north pole X  : vector to seam Y   = Z   × X  ʹ″ x = P   − C  

( )⋅ X

 ʹ″ y = P   − C  

( )⋅Y

  ʹ″ z = P   − C  

( )⋅ Z

  ʹ″ x = cosθ sinφ ʹ″ y = sinθ sinφ ʹ″ z = cosφ inverse mapping similar

15

φ θ R

Z X

slide-16
SLIDE 16

Fall 2011 CS 6965

Linear projection

x y z ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ = Ux Vx Wx Uy Vy Wy Uz Vz Wz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ u v w ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ + Tx Ty Tz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ inverse mapping: u v w ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ = Ux Vx Wx Uy Vy Wy Uz Vz Wz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥

−1 x − Tx

y − Ty z − Tz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥

16

V U W

T

slide-17
SLIDE 17

Fall 2011 CS 6965

Linear special case

x y z ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ = u v w ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ + Tx Ty Tz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ inverse mapping: u v w ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥ = x − Tx y − Ty z − Tz ⎡ ⎣ ⎢ ⎢ ⎢ ⎤ ⎦ ⎥ ⎥ ⎥

17

V U W

T

slide-18
SLIDE 18

Fall 2011 CS 6965

0 ≤ b1,b2,b3 ≤ 1 b1 + b2 + b3 = 1 P   = b1P

1

 + b2P

2

  + b3P

3

  = b1P

1

 + b2P

2

  + 1− b1 − b2

( )P

3

  u = b1 v = b2 w = 0

Barycentric coordinates

18

P

2

  P

1

 P

3

  P   b2 b3 b1

slide-19
SLIDE 19

Fall 2011 CS 6965

Specified texture coordinates

0 ≤ b1,b2,b3 ≤ 1 b1 + b2 + b3 = 1 P   = b1P

1

 + b2P

2

  + b3P

3

  = b1P

1

 + b2P

2

  + 1− b1 − b2

( )P

3

  u = b1u1 + b2u2 + 1− b1 − b2

( )u3

v = b1v1 + b2v2 + 1− b1 − b2

( )v3

w = b1w1 + b2w2 + 1− b1 − b2

( )w3

19

P

2

  P

1

 P

3

  P   b2 b3 b1

slide-20
SLIDE 20

Fall 2011 CS 6965

Other coordinate transforms

  • Any of those spherical projections
  • Could transform u/v on the disc or ring (similar to cylindrical)
  • Conical projections
  • Many more

20

slide-21
SLIDE 21

Fall 2011 CS 6965

Implementation choices

  • Where do these coordinate transforms go?
  • Object? Might want linear or cylindrical mapping on a sphere
  • Material? Material doesn’t know about coordinates
  • Somewhere else?

21

slide-22
SLIDE 22

Fall 2011 CS 6965

Implementation tradeoffs

  • Object: different sphere subclasses for different UV mappings
  • Object: multiple inheritance
  • Material: similar tradeoffs
  • Somewhere else: redundant data?
  • Somewhere else: multiple inheritance

22

slide-23
SLIDE 23

Fall 2011 CS 6965

What Steve does

23

  • Add to primitive base class:

TexCoordMapper* tcmapper; void setTexCoordMapper(TexCoordMapper*); TexCoordMapper* getTexCoordMapper();

  • Sphere is multiply inherited and default tcmapper

is “this” pointer

  • Objects with no inherent uv space default to

linear tcmapper (xyz == uvw)

  • Default space can be overridden in scene
slide-24
SLIDE 24

Fall 2011 CS 6965

Compute hit position (P   = O   + tV   ) Call primitive to get tcmapper Call tcmapper to get UVW coordinates (T   ) Interpolate surface color from image using T   scaled to image resolution Call primitive to get normal (N  ) (normalized) costheta = −N  ⋅V   if (costheta < 0) normal =-normal Color light = scene.ambient*Ka foreach light source get CL and L   dist= L   ,Ln   = L   L   cosphi = N  ⋅ Ln   if(cosphi > 0) if(!intersect with 0 < t < dist) light += CL *(Kd *cosphi) result=light*surface color

24

Image Textured Lambertian Shading

slide-25
SLIDE 25

Fall 2011 CS 6965

Checkerboard texture

i1 = int

( ) u * scale ( )

i2 = int

( ) v * scale ( )

cell = (i1+ i2)%2 if (cell = 0) use color1 else use color2

25

0 1 2 3 4 5 6 7

u*scale v*scale

7 6 5 4 3 2 1

slide-26
SLIDE 26

Fall 2011 CS 6965

3D Checkerboard texture

i1 = int

( ) u * scale ( )

i2 = int

( ) v * scale ( )

i3 = int

( ) w * scale ( )

cell = (i1+ i2 + i3)%2 if (cell = 0) use color1 else use color2

http://www.chez.com/jrlivenais/vdesprit/tut_index/check_sphere/check_sphere_eng.htm

26

0 1 2 3 4 5 6 7

u*scale v*scale

7 6 5 4 3 2 1

slide-27
SLIDE 27

Fall 2011 CS 6965

Checkboard comparisons

http://courses.dce.harvard.edu/~cscie234/projects/Slocum/checker-spheres-4x4-jitt-1.gif http://cgg.ms.mff.cuni.cz/~pepca/lectures/textures/sample/checker.jpg

3D checker planar mapping 2 D (or 3D) checker spherical mapping

27

slide-28
SLIDE 28

Fall 2011 CS 6965

Floor tile texture

s = u * scale − int

( ) u * scale ( )

t = v * scale − int

( ) v * scale ( )

if (s < grout _width || t < grout _width) color = grout color else color = tile color

28

Grout width

0 s 1

slide-29
SLIDE 29

Fall 2011 CS 6965

Brick texture

ti = (int)(v *vscale) // get brick row ʹ″ u = u *uscale − (ti%2)*0.5 // shift column for odd rows s = ʹ″ u − int

( ) ʹ″

u // brick column fraction t = v *vscale − ti // brick row fraction if (s < mortar _width || t < mortar _width) color = mortar _color else color = brick _color

29

slide-30
SLIDE 30

Fall 2011 CS 6965

More flexible implementation

30

  • Instead of choosing between two (or

more) colors, choose between two materials if(cell == 0) matl1->shade(…); else matl2->shade(…);

slide-31
SLIDE 31

Fall 2011 CS 6965

Other simple procedural textures

  • Hexagonal mapping
  • Cutout shapes
  • Use your imagination!

31

slide-32
SLIDE 32

Fall 2011 CS 6965

Solid textures

  • Many objects are created out of solid materials
  • Texture is three-dimensional
  • Surface texture is cutaway of the 3D texture space
  • Examples:
  • Wood
  • Marble
  • Granite

32

slide-33
SLIDE 33

Fall 2011 CS 6965

Perlin noise

  • A pseudorandom method for making

natural textures

  • Appeared in Siggraph 1985 (preview in

1984)

  • Ken Perlin won an academy award

(technical achievement award) in 1997

  • More info: http://noisemachine.com
  • Improved in 2002:
  • http://mrl.nyu.edu/~perlin/noise

33

slide-34
SLIDE 34

Fall 2011 CS 6965

Perlin noise

  • Need a function that:
  • Looks random
  • Has fixed frequency content
  • Is coherent (value changes smoothly from
  • ne point to another)
  • Solution: smooth random values on a regular

grid

Incoherent Coherent

http://www.robo-murito.net/code/perlin-noise-math-faq.html 34

slide-35
SLIDE 35

Fall 2011 CS 6965

Value noise function

  • Idea: place random values on a grid lattice
  • Interpolate between them (spline)
  • Spline is expensive in 3D

35

slide-36
SLIDE 36

Fall 2011 CS 6965

Perlin noise (gradient noise)

  • Idea: place random gradients on a grid lattice
  • Weighted interpolation of gradient values

0 ≤ x ≤ 1 g0 = G0x,g1 = G1 1− x

( )

noise(x) = lerp(g0,g1,ease(x)) Note: noise(0) = 0,noise(1) = 0

36

slide-37
SLIDE 37

Fall 2011 CS 6965

Noise

  • Properties of good noise functions
  • Repeatable
  • Bandlimited
  • Stays within known limits
  • Isotropic

37

slide-38
SLIDE 38

Fall 2011 CS 6965

Value Noise

Take a continuous random process. (white noise)

38

slide-39
SLIDE 39

Fall 2011 CS 6965

Value Noise

Sample it.

39

slide-40
SLIDE 40

Fall 2011 CS 6965

Value Noise

Convolve the samples with a reconstruction filter.

40

slide-41
SLIDE 41

Fall 2011 CS 6965

Value Noise

This produces basic value noise.

41

slide-42
SLIDE 42

Fall 2011 CS 6965

Value Noise

This produces basic value noise.

42

slide-43
SLIDE 43

Fall 2011 CS 6965

Perlin’s Insight

x = x x = = Value noise just multiplies the kernel with a simple constant.

43

slide-44
SLIDE 44

Fall 2011 CS 6965

Perlin’s Insight

x = x x = = Suppose instead that we multiplied the kernel with a gradient?

44

slide-45
SLIDE 45

Fall 2011 CS 6965

Perlin Noise

Instead of random intensities at each point, use random unit vectors.

45

slide-46
SLIDE 46

Fall 2011 CS 6965

Perlin Noise

The result is Perlin noise.

46

slide-47
SLIDE 47

Fall 2011 CS 6965

Perlin Noise

The result is Perlin noise.

47

slide-48
SLIDE 48

Fall 2011 CS 6965

Classic Implementation

// P[n] = permuation table: numbers 0 through n-1, shuffled // G[n] = gradients: random unit vectors float noise(point S) { I = floor(S) F = S - I ease = 3F2 - 2F3 for each corner, C, of the containing cube: hash = P[Cx + P[Cy + P[Cz mod n] mod n] mod n] dot = G[hash] . (S-C) return trilinear(dot, ease) }

48

slide-49
SLIDE 49

Fall 2011 CS 6965

“Improved” Perlin

  • Higher order ease function: 6F5 - 15F4 + 10F3
  • Evenly distribute gradients: use just 12 vectors to center of

cube edges

49

slide-50
SLIDE 50

Fall 2011 CS 6965

2D perlin noise

  • Sum over each corner: weighted gradient dot vector to point

http://www.robo-murito.net/code/perlin-noise-math-faq.html 50

slide-51
SLIDE 51

Fall 2011 CS 6965

First derivative == 0 at t=0 and t=1

Ease curve

  • Get rid of discontinuities at grid boundaries
  • Interpolate with hermite polynomial

51

Linear

2t 3 − 3t 2

slide-52
SLIDE 52

Fall 2011 CS 6965

Faster way

  • Randomly choose a gradient vector based on a hash function:
  • Precompute table of permutations P[n]
  • Precompute table of gradients G[n]
  • G = G[ ( i + P[ (j + P[k]) mod n ] ) mod n ]

52

slide-53
SLIDE 53

Fall 2011 CS 6965

2002 implementation

  • Randomness comes from permutation and random gradients
  • Random gradients are not necessary
  • Use gradients to edges of a cube:
  • [0 1 1] [0 1 -1] [0 -1 1] [0 -1 -1]
  • [1 0 1] [1 0 -1] [-1 0 1] [-1 0 -1]
  • [1 1 0] [1 -1 0] [-1 1 0] [-1 -1 0]
  • Use better ease function:

6t 5 −15t 4 +10t 3

53

slide-54
SLIDE 54

Fall 2011 CS 6965

Comparison

  • 2002 implementation is a little faster and looks better

Original Improved

54

slide-55
SLIDE 55

Fall 2011 CS 6965

Easy way

  • Perlin’s 2002 reference implementation (Java)
  • http://mrl.nyu.edu/~perlin/noise/

55

slide-56
SLIDE 56

Fall 2011 CS 6965

Improved Noise

  • Better gradient noise
  • Software implementation
  • http://www.cs.utah.edu/~aek/research/noise.pdf
  • Hardware-accelerated gradient noise for graphics
  • VLSI implementation
  • 4-stage pipeline at 1 GHz
  • www.cs.utah.edu/~aek/research/hwnoise.pdf

56

slide-57
SLIDE 57

Fall 2011 CS 6965

Fractal noise

  • Perlin noise is (nearly) fixed frequency
  • Defined by the grid
  • Fractal noise:

turbulence x

( ) = noise x ( ) + 1

2 noise 2x

( ) + 1

4 noise 4x

( ) + ...

More generally: turbulence x

( ) =

ainoise 2i

( )

bi

i=0

  • ctaves

a: lacunarity b: gain or persistence

57

slide-58
SLIDE 58

Fall 2011 CS 6965

Fractal noise

Sum

http://astronomy.swin.edu.au/~pbourke/texture/perlin/ 58

1x 2x 4x 8x

slide-59
SLIDE 59

Fall 2011 CS 6965

Using noise

  • Creativity is in using functions of noise or turbulence

Noise Turbulence Turbulence Using |noise| sin(x+turb(x))

59

slide-60
SLIDE 60

Fall 2011 CS 6965

Marble

  • Swirled veins of

molten rock

sin(40x)*0.5+0.5

60

slide-61
SLIDE 61

Fall 2011 CS 6965

  • Swirled veins of

molten rock

sin(40x+noise(4x))*0.5+0.5

Marble

61

slide-62
SLIDE 62

Fall 2011 CS 6965

  • Swirled veins of

molten rock

sin(40x+5noise(4x))*0.5+0.5

Marble

62

slide-63
SLIDE 63

Fall 2011 CS 6965

  • Swirled veins of

molten rock

sin(40x+10noise(4x))*0.5+0.5

Marble

63

slide-64
SLIDE 64

Fall 2011 CS 6965

  • Swirled veins of

molten rock

sin(40x+16noise(4P)+8noise(8P))*0.5+0.5 =sin(40x+16turbulence(2, 4P , 2, 0.5))*0.5+0.5

Marble

64

slide-65
SLIDE 65

Fall 2011 CS 6965

  • Swirled veins of

molten rock

=sin(40x+16turbulence(3, 4P , 2, 0.5))*0.5+0.5

Marble

65

slide-66
SLIDE 66

Fall 2011 CS 6965

  • Swirled veins of

molten rock

=sin(40x+16turbulence(4, 4P , 2, 0.5))*0.5+0.5

Marble

66

slide-67
SLIDE 67

Fall 2011 CS 6965

  • Swirled veins of

molten rock

=sin(40x+16turbulence(6, 4P , 2, 0.5))*0.5+0.5

Marble

67

slide-68
SLIDE 68

Fall 2011 CS 6965

  • Swirled veins of

molten rock

=1-(sin(40x+16turbulence(6, 4P , 2, 0.5))*0.5+0.5)^6

Marble

68

slide-69
SLIDE 69

Fall 2011 CS 6965

Advanced marble

  • Use |noise| instead of noise
  • Makes sharper features
  • Use different colors
  • Interpolate between two colors
  • General spline function

69

slide-70
SLIDE 70

Fall 2011 CS 6965

Wood texture

  • Trees are concentric cylinders of growth

cos(radius)

70

slide-71
SLIDE 71

Fall 2011 CS 6965

Wood cuts: Quarter-sawn Flat-sawn Rift-sawn Rotary sliced

Hardwood.org

71

slide-72
SLIDE 72

Fall 2011 CS 6965 http://www.wood-veneers.com/

Rift-sawn Plain-sawn

72

slide-73
SLIDE 73

Fall 2011 CS 6965

Walnut Curly maple Maple

http://www.wood-veneers.com/

73

slide-74
SLIDE 74

Fall 2011 CS 6965

Wood texture

  • Dark rings are a little further apart

cos(radius)^8

74

slide-75
SLIDE 75

Fall 2011 CS 6965

Wood texture

  • Rings aren’t perfect

cos(R+noise(P))^8

75

slide-76
SLIDE 76

Fall 2011 CS 6965

Wood texture

  • Large imperfections make the wood “curly”

76

slide-77
SLIDE 77

Fall 2011 CS 6965

Other improvements

  • Modulate center based on noise
  • Model finer grain structure of wood grain
  • Model rays

77

slide-78
SLIDE 78

Fall 2011 CS 6965

Advanced wood

  • Render grain
  • Better transitions
  • Shader from

Advanced Renderman has 11 parameters

Advanced Renderman

78

slide-79
SLIDE 79

Fall 2011 CS 6965

Wooden bunny

  • Marble base and

simple wood texture on bunny

79

slide-80
SLIDE 80

Fall 2011 CS 6965

Other noise tricks

  • Embed noise in higher dimension and animate

(2D ->3D, 3D->4D)

  • Fire burning
  • Clouds evolving

noisemachine.com

80

slide-81
SLIDE 81

Fall 2011 CS 6965

Other procedural textures

  • Granite
  • Stucco
  • Dented/Eroded
  • Flames
  • Clouds
  • Many more - use your imagination!

noisemachine.com

81

slide-82
SLIDE 82

Fall 2011 CS 6965

Combinations of shaders

  • Combine shaders to get even better results

Advanced Renderman

82

slide-83
SLIDE 83

Fall 2011 CS 6965

Recipe for success

  • Add method to Primitive based class to compute texture

coordinates:

  • void computeUVW(Vector& uvw, const RenderContext&

context const Ray& ray, const HitRecord& hit) const;

  • Default implementation sets uvw as hit position:
  • uvw = (ray.origin + ray.direction*t)

83

slide-84
SLIDE 84

Fall 2011 CS 6965

Polar Coordinates

  • Used for left two spheres (with maps)
  • Create subclass of Sphere (SpherePolar)
  • Override computeUVW method to compute polar coordinates
  • In constructor:

Z   = poleAxis    (4th argument) X  = primeMeridianAxis    (5th argument) Y   = Z   × X  normalize X  , Y   , and Z  

84

slide-85
SLIDE 85

Fall 2011 CS 6965

Polar coordinates

P   = O   + tV   ʹ″ P   = P   − C   x = ʹ″ P   ⋅ X  ,y = ʹ″ P   ⋅Y   ,z = ʹ″ P   ⋅ Z   R = x2 + y2 + z2 θ = atan2 y,x

( )

u = θ 2π if (u < 0)u+ = 1 φ = arccos z R ⎛ ⎝ ⎜ ⎞ ⎠ ⎟ v = 1− φ π uvw = Vector(u,v,R)

85

slide-86
SLIDE 86

Fall 2011 CS 6965

Checkerboard texture

Checkerboard a subclass of Material In constructor: Given: C   (center of checkerboard),V

1

 ,V2   (checker axis directions) V

1

  should be vector to first checker vertex We want to switch checker vertices at integers 1,2,... ∴V

1

 ⋅ ʹ″ V

1

 = 1 ʹ″ V

1

 = V

1

 V

1

 2 , ʹ″ V2   = V2   V2  

2

86

slide-87
SLIDE 87

Fall 2011 CS 6965

Checkerboard texture

Call computeUVW on hit primitive: uvw    i1 = floor uvw    ⋅ ʹ″ V

1



( )

i2 = floor uvw    ⋅ ʹ″ V2  

( )

which_ matl = (i1+ i2)%2 if (which_ matl == 0) matl0->shade(....) else matl1->shade(...)

87

slide-88
SLIDE 88

Fall 2011 CS 6965

Checkerboard

  • ptimization
  • Put materials in a two-element array, indexed by which_matl
  • Beware (-1)%2 = -1
  • One solution: make a three-element array, indexed by

which_matl+1

  • matls[which_matl+1]->shade(…)

88

slide-89
SLIDE 89

Fall 2011 CS 6965

Phong Marble Material

Compute uvw    using hit primitive T   = uvw    * scale* fscale value = cos u * scale + tscale*turbulenceAbs(octaves,T   ,lacunarity,gain)

( )

value = value*0.5 + 0.5 color = c1(1− value) + c2value Use color in phong lighting calculations Parameters: scale = spatial magnification fscale = frequency magnfication of turbulence (smaller: coarser structure) tscale = distortion due to marble (0: pure sinusoid, large: more veins)

  • ctaves = number of frequencies in fractal turbulence

lacunarity = frequency multiplication gain = decay of higher frequencies in fractal turbulence

89

slide-90
SLIDE 90

Fall 2011 CS 6965

Phong Image Material

Compute uvw    using hit primitive x = u * xres y = v * yres ix = Floor(x),iy = Floor(y) fx = x − ix, fy = y − iy Interpolate color from image(ix,iy) using fx,fy Use color in phong shading calculations

90

slide-91
SLIDE 91

Fall 2011 CS 6965

Image wrapping

Need to interpolate at prime meridian: Interpolation: image(x,y)(1-fx)(1-fy) +image((x+1)%xres, y)(fx)(1-fy) +image(x,(y+1)%yres)(1-fx)(fy) +image((x+1)%xres,(y+1)%yres)(fx)(fy) This still isn't quite right, but it works fine for this assignment You probably won't notice the difference unless you use a smaller image

91

slide-92
SLIDE 92

Fall 2011 CS 6965

Floor

92

  • Note that Floor(x) != (int)x
  • Wrong for x<0
  • Integer cast actually undefined for

x<0

  • Most machines round toward 0

(not what we want)

Correct Floor implementation: inline int Floor(float d) { if(d<0){ int i = -static_cast<int>(-d); if(i == d) return i; else return i-1; } else { return static_cast<int>(d); } }