Texturing
CS 6965 Fall 2011
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
CS 6965 Fall 2011
Fall 2011 CS 6965
Erik's Danny's 10 20 30 40 50 60 70 80 90 FPS (prog2)
2
Fall 2011 CS 6965
uniform color
properties as a function of space
3
Fall 2011 CS 6965
wood, etc.
4
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
P C x y U V Image
Fall 2011 CS 6965
x = P − C
U y = P − C
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
P C x y U V Image
Fall 2011 CS 6965
8
Fall 2011 CS 6965
ray tracing:
transformation), example: linear
example: linear interpolation of an image
9
Fall 2011 CS 6965
“uvw”
xyz to uvw
linear
10
Fall 2011 CS 6965
x = cos θ
( )
y = sin θ
( )
inverse mapping: R = x2 + y2 θ = atan2(y,x) atan2 :robust atan(y/x) for all quadrants
11
R θ Z
Fall 2011 CS 6965
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
Fall 2011 CS 6965
to a sphere
http://www.colorado.edu/geography/gcraft/notes/mapproj/mapproj_f.html
13
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
1
2
3
1
2
3
18
2
1
3
Fall 2011 CS 6965
1
2
3
1
2
3
19
2
1
3
Fall 2011 CS 6965
20
Fall 2011 CS 6965
21
Fall 2011 CS 6965
22
Fall 2011 CS 6965
23
TexCoordMapper* tcmapper; void setTexCoordMapper(TexCoordMapper*); TexCoordMapper* getTexCoordMapper();
is “this” pointer
linear tcmapper (xyz == uvw)
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
Fall 2011 CS 6965
i1 = int
i2 = int
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
Fall 2011 CS 6965
i1 = int
i2 = int
i3 = int
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
Fall 2011 CS 6965
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
27
Fall 2011 CS 6965
28
Grout width
0 s 1
Fall 2011 CS 6965
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
Fall 2011 CS 6965
30
Fall 2011 CS 6965
31
Fall 2011 CS 6965
32
Fall 2011 CS 6965
natural textures
1984)
(technical achievement award) in 1997
33
Fall 2011 CS 6965
grid
http://www.robo-murito.net/code/perlin-noise-math-faq.html 34
Fall 2011 CS 6965
35
Fall 2011 CS 6965
0 ≤ x ≤ 1 g0 = G0x,g1 = G1 1− x
( )
noise(x) = lerp(g0,g1,ease(x)) Note: noise(0) = 0,noise(1) = 0
36
Fall 2011 CS 6965
37
Fall 2011 CS 6965
38
Fall 2011 CS 6965
39
Fall 2011 CS 6965
40
Fall 2011 CS 6965
41
Fall 2011 CS 6965
42
Fall 2011 CS 6965
43
Fall 2011 CS 6965
44
Fall 2011 CS 6965
45
Fall 2011 CS 6965
46
Fall 2011 CS 6965
47
Fall 2011 CS 6965
// 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
Fall 2011 CS 6965
cube edges
49
Fall 2011 CS 6965
http://www.robo-murito.net/code/perlin-noise-math-faq.html 50
Fall 2011 CS 6965
51
Linear
2t 3 − 3t 2
Fall 2011 CS 6965
52
Fall 2011 CS 6965
53
Fall 2011 CS 6965
54
Fall 2011 CS 6965
55
Fall 2011 CS 6965
56
Fall 2011 CS 6965
turbulence x
( ) = noise x ( ) + 1
2 noise 2x
( ) + 1
4 noise 4x
( ) + ...
More generally: turbulence x
( ) =
ainoise 2i
( )
bi
i=0
a: lacunarity b: gain or persistence
57
Fall 2011 CS 6965
http://astronomy.swin.edu.au/~pbourke/texture/perlin/ 58
1x 2x 4x 8x
Fall 2011 CS 6965
59
Fall 2011 CS 6965
molten rock
60
Fall 2011 CS 6965
molten rock
61
Fall 2011 CS 6965
molten rock
62
Fall 2011 CS 6965
molten rock
63
Fall 2011 CS 6965
molten rock
sin(40x+16noise(4P)+8noise(8P))*0.5+0.5 =sin(40x+16turbulence(2, 4P , 2, 0.5))*0.5+0.5
64
Fall 2011 CS 6965
molten rock
=sin(40x+16turbulence(3, 4P , 2, 0.5))*0.5+0.5
65
Fall 2011 CS 6965
molten rock
=sin(40x+16turbulence(4, 4P , 2, 0.5))*0.5+0.5
66
Fall 2011 CS 6965
molten rock
=sin(40x+16turbulence(6, 4P , 2, 0.5))*0.5+0.5
67
Fall 2011 CS 6965
molten rock
=1-(sin(40x+16turbulence(6, 4P , 2, 0.5))*0.5+0.5)^6
68
Fall 2011 CS 6965
69
Fall 2011 CS 6965
70
Fall 2011 CS 6965
Hardwood.org
71
Fall 2011 CS 6965 http://www.wood-veneers.com/
72
Fall 2011 CS 6965
http://www.wood-veneers.com/
73
Fall 2011 CS 6965
74
Fall 2011 CS 6965
75
Fall 2011 CS 6965
76
Fall 2011 CS 6965
77
Fall 2011 CS 6965
Advanced Renderman has 11 parameters
78
Fall 2011 CS 6965
simple wood texture on bunny
79
Fall 2011 CS 6965
(2D ->3D, 3D->4D)
80
Fall 2011 CS 6965
81
Fall 2011 CS 6965
82
Fall 2011 CS 6965
coordinates:
context const Ray& ray, const HitRecord& hit) const;
83
Fall 2011 CS 6965
Z = poleAxis (4th argument) X = primeMeridianAxis (5th argument) Y = Z × X normalize X , Y , and Z
84
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
which_matl+1
88
Fall 2011 CS 6965
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)
lacunarity = frequency multiplication gain = decay of higher frequencies in fractal turbulence
89
Fall 2011 CS 6965
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
Fall 2011 CS 6965
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
Fall 2011 CS 6965
92
x<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); } }