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 properties as a function of space CS 6965 Fall 2011 3
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. • Ken Perlin, NYU Bump mapping CS 6965 Fall 2011 4
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 C L and L = L dist= L , L n L cosphi = N ⋅ L n if( cosphi > 0) if(!intersect with 0 < t < dist ) light += C L *( Kd * cosphi ) result=light*surface color CS 6965 Fall 2011 5
Simple image texture Use hit position to determine image location P P : hit position y C : lower left corner of image x C U : image X axis in world space Image V : image Y axis in world space x, y: normalized image coordinates (0-1) P = C + xU + yV V if U ⋅ V = 0 : ( ) ⋅ U P − C x = U ( ) ⋅ V P − C y = U V World CS 6965 Fall 2011 6
Simple image texture ( ) ⋅ U P − C P y x = U x ( ) ⋅ V C P − C Image y = V ( ) , fx = ix − x xres − 1 ( ) ( ) ix = (int) x xres − 1 ( ) , fy = iy − y yres − 1 ( ) ( ) iy = (int) y yres − 1 V Bilinear interpolation of image(ix,iy) - image(ix+1,iy+1) using fx,fy as interpolation factors Use interpolated color in lambertian shading U World CS 6965 Fall 2011 7
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?) CS 6965 Fall 2011 8
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 CS 6965 Fall 2011 9
Texture coordinates • Textures are not always flat • Textures live in a space called “uvw” • Texture coordinate mapping: xyz to uvw • NASA Blue marble project Texture space may not be linear CS 6965 Fall 2011 10
Cylinder projection ( ) x = cos θ R θ ( ) y = sin θ Z inverse mapping: x 2 + y 2 R = θ = atan2( y , x ) atan2 :robust atan(y/x) for all quadrants CS 6965 Fall 2011 11
Cylinder projection x = cos θ R y = sin θ θ inverse mapping: Z x 2 + y 2 R = θ = atan2( y , x ) atan2 :robust atan(y/x) for all quadrants u = θ 2 π ;if(u<1)u+=1, v = z , w = R CS 6965 Fall 2011 12
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 CS 6965 Fall 2011 13
Polar projection Use longitude/latitude angles: φ x = cos θ sin φ y = sin θ sin φ R z = cos φ θ inverse mapping: x 2 + y 2 + z 2 R = φ = 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 CS 6965 Fall 2011 14
Arbitrary position/orientation Z : vector to north pole Z X : vector to seam Y = Z × X φ ( ) ⋅ X x = P − C ʹ″ ( ) ⋅ Y R y = P − C ʹ″ ( ) ⋅ Z z = P − C ʹ″ θ X x = cos θ sin φ ʹ″ y = sin θ sin φ ʹ″ z = cos φ ʹ″ inverse mapping similar CS 6965 Fall 2011 15
Linear projection U x U x V x W x u T x ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ V y U y V y W y v T y = + ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ T ⎢ ⎥ ⎢ ⎥ ⎢ z ⎥ U z V z W z ⎢ w ⎥ T z ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ W inverse mapping: − 1 x − T x u U x V x W x ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ v U y V y W y y − T y = ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ w U z V z W z z − T z ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ CS 6965 Fall 2011 16
Linear special case U x u T x ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ y v T y = + V ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ z ⎥ ⎢ w ⎥ T z T ⎣ ⎦ ⎣ ⎦ ⎣ ⎦ W inverse mapping: u x − T x ⎡ ⎤ ⎡ ⎤ ⎢ ⎥ ⎢ ⎥ v y − T y = ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ w z − T z ⎣ ⎦ ⎣ ⎦ CS 6965 Fall 2011 17
Barycentric coordinates 0 ≤ b 1 , b 2 , b 3 ≤ 1 P 1 b 1 + b 2 + b 3 = 1 b 2 P = b 1 P + b 2 P + b 3 P 1 2 3 b 3 ( ) P = b 1 P + b 2 P + 1 − b 1 − b 2 P 1 2 3 b 1 u = b 1 P P 2 3 v = b 2 w = 0 CS 6965 Fall 2011 18
Specified texture coordinates 0 ≤ b 1 , b 2 , b 3 ≤ 1 P 1 b 1 + b 2 + b 3 = 1 P = b 1 P + b 2 P + b 3 P b 2 1 2 3 ( ) P b 3 = b 1 P + b 2 P + 1 − b 1 − b 2 P 1 2 3 b 1 ( ) u 3 u = b 1 u 1 + b 2 u 2 + 1 − b 1 − b 2 P P 2 3 ( ) v 3 v = b 1 v 1 + b 2 v 2 + 1 − b 1 − b 2 ( ) w 3 w = b 1 w 1 + b 2 w 2 + 1 − b 1 − b 2 CS 6965 Fall 2011 19
Other coordinate transforms • Any of those spherical projections • Could transform u/v on the disc or ring (similar to cylindrical) • Conical projections • Many more CS 6965 Fall 2011 20
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? CS 6965 Fall 2011 21
Implementation tradeoffs • Object: different sphere subclasses for different UV mappings • Object: multiple inheritance • Material: similar tradeoffs • Somewhere else: redundant data? • Somewhere else: multiple inheritance CS 6965 Fall 2011 22
What Steve does • 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 CS 6965 Fall 2011 23
Compute hit position ( P = O + tV ) Image Call primitive to get tcmapper Call tcmapper to get UVW coordinates (T ) Textured Interpolate surface color from image using T scaled to image resolution Lambertian Call primitive to get normal (N ) (normalized) Shading costheta = − N ⋅ V if ( costheta < 0) normal =-normal Color light = scene.ambient*Ka foreach light source get C L and L = L dist= L , L n L cosphi = N ⋅ L n if( cosphi > 0) if(!intersect with 0 < t < dist ) light += C L *( Kd * cosphi ) result=light*surface color CS 6965 Fall 2011 24
Checkerboard texture 7 6 5 4 v*scale ( ) u * scale ( ) i 1 = int 3 2 ( ) v * scale ( ) i 2 = int 1 0 0 1 2 3 4 5 6 7 cell = ( i 1 + i 2)%2 u*scale if ( cell = 0) use color1 else use color2 CS 6965 Fall 2011 25
3D Checkerboard 7 texture 6 5 4 v*scale 3 ( ) u * scale ( ) i 1 = int 2 ( ) v * scale ( ) 1 i 2 = int 0 ( ) w * scale ( ) i 3 = int 0 1 2 3 4 5 6 7 u*scale cell = ( i 1 + i 2 + i 3)%2 if ( cell = 0) use color1 else use color2 http://www.chez.com/jrlivenais/vdesprit/tut_index/check_sphere/check_sphere_eng.htm CS 6965 Fall 2011 26
Checkboard comparisons 3D checker 2 D (or 3D) checker planar mapping spherical mapping 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 CS 6965 Fall 2011 27
Recommend
More recommend