Photorealism Steve Ash, Rhodes College 2006
T exture Mapping = + = +
T exture Mapping C(i, j) P(x, y, z) = + Given the intersection point P between the sphere and ray R , map the Cartesian coordinates ( x, y, z ) of P to ( u, v ) coordinates in the image to pick a color
T exture Mapping C(i, j) P(x, y, z) = + Given the intersection point P between the sphere and ray R , map the Cartesian coordinates ( x, y, z ) of P to ( u, v ) coordinates in the image to pick a color
Polar Coordinates A point in the plane can be described by ( x, y ) coordinates – Cartesian coordinates ( r, θ ) coordinates – Polar coordinates image source: http://en.wikipedia.org/wiki/File:Polar_to_cartesian.svg
Polar => Cartesian Given the ( r, θ ) polar coordinates what are the ( x, y ) Cartesian coordinates x = r cosθ y = r sinθ image source: http://en.wikipedia.org/wiki/File:Polar_to_cartesian.svg
Cartesian => Polar Given the ( x, y ) Cartesian coordinates what are the ( r, θ ) Polar coordinates r = sqrt( x 2 + y 2 ) θ = atan( y / x ) image source: http://en.wikipedia.org/wiki/File:Polar_to_cartesian.svg
Spherical Coordinates A point in 3D space can be described by ( x, y, z ) coordinates – Cartesian coordinates ( r, θ, φ ) coordinates – Spherical coordinates (θ, φ – latitude, longitude) y N U r V y z x x z image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
Spherical => Cartesian Given the ( r, θ, φ ) spherical coordinates what are the ( x, y, z ) Cartesian coordinates x = r sin φ sinθ (based on the second diagram) y = r cos φ z = r sin φ cosθ y N U r V y z x x z image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
Spherical => Cartesian Given the ( x, y, z ) Cartesian coordinates what are the ( r, θ, φ ) Spherical coordinates r = sqrt ( x 2 + y 2 + z 2 ) θ = atan ( x / z ) use C function atan2 ( a, b ) – range is [-π .. π] φ = acos ( y / r ) use C function acos ( a ) – range is [0 .. π] These formulas are for P ( x, y, z ) on sphere centered at origin For sphere not centered at origin correct coordinates of P y N U r V y z x x z image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
T exture Mapping C(i, j) P(x, y, z) = + Given the intersection point P between the sphere and ray R , map the Cartesian coordinates ( x, y, z ) of P to ( u, v ) coordinates in the image to pick a color for sphere correct (x, y, z) as if sphere is centered at origin convert the Cartesian (x, y, z) to Spherical (r, θ, φ) keep only (θ, φ) range of θ is [ -π .. π ] and of φ is [0 .. π] compute u = f(θ) and v = g(φ) so that range of u and v is [0..1] compute i = u ∙ imageWidth j = v ∙ imageHeight set color of P to color at image [ i ][ j ]
T exture Mapping C(i, j) P(x, y, z) = + Given the intersection point P between the sphere and ray R , map the Cartesian coordinates ( x, y, z ) of P to ( u, v ) coordinates in the image to pick a color in general correct (x, y, z) as if shape is centered at origin convert the Cartesian (x, y, z) to Parametric form (α, β, γ) keep any two (α, β) range of α is [ α 0 .. α 1 ] and of β is [ β 0 .. β 1 ] compute u = f(α) and v = g(β) so that range of u and v is [0..1] compute i = u ∙ imageWidth j = v ∙ imageHeight set color of P to color at image [ i ][ j ]
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + The computed coordinates ( i, j ) will not be integers, in general. To have smooth color change, interpolate the colors of neighboring pixels. (2, 6) (3, 6) UL UR Suppose i = 2 and j = 5.5 C 1 What should we use for C 1 ? LR LL (3, 5) (2, 5)
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + The computed coordinates ( i, j ) will not be integers, in general. To have smooth color change, interpolate the colors of neighboring pixels. (2, 6) (3, 6) UL UR Suppose i = 2 and j = 5.5 C 1 What should we use for C 1 ? LR LL (3, 5) (2, 5) C 1 = 0.5 ∙ image[ UL ] + 0.5 ∙ image[ LL ]
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + The computed coordinates ( i, j ) will not be integers, in general. To have smooth color change, interpolate the colors of neighboring pixels. (2, 6) (3, 6) UL UR Suppose i = 2 and j = 5.7 C 1 What should we use for C 1 ? LR LL (3, 5) (2, 5)
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + The computed coordinates ( i, j ) will not be integers, in general. To have smooth color change, interpolate the colors of neighboring pixels. (2, 6) (3, 6) UL UR Suppose i = 2 and j = 5.7 C 1 What should we use for C 1 ? LR LL (3, 5) (2, 5) C 1 = 0.7 ∙ image[ UL ] + 0.3 ∙ image[ LL ] C 1 = frac ∙ image[ UL ] + (1-frac) ∙ image[ LL ]
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + The computed coordinates ( i, j ) will not be integers, in general. To have smooth color change, interpolate the colors of neighboring pixels. UL UR Let i 0 = i – floor(i) and i 1 = ceil(i) – i = 1 - i 0 j 1 C 1 C 2 j 0 = j – floor(j) and j 1 = ceil(j) – j = 1 - j 0 i 0 i 1 j 0 LR LL Compute C 1 = j 0 ∙ image[ UL ] + j 1 ∙ image[ LL ] C 2 = j 0 ∙ image[ UR ] + j 1 ∙ image[ LR ] C = i 0 ∙ C2 + i 1 ∙ C1
T exture Mapping (Interpolation) C(i, j) P(x, y, z) = + use % for indices to wrap around The computed coordinates ( i, j ) will not be integers, in the image general. To have smooth color change, interpolate the when looking up neighbors colors of neighboring pixels. UL UR Let i 0 = i – floor(i) and i 1 = ceil(i) – i = 1 - i 0 j 1 C 1 C 2 j 0 = j – floor(j) and j 1 = ceil(j) – j = 1 - j 0 i 0 i 1 j 0 LR LL Compute C 1 = j 0 ∙ image[ UL ] + j 1 ∙ image[ LL ] C 2 = j 0 ∙ image[ UR ] + j 1 ∙ image[ LR ] C = i 0 ∙ C2 + i 1 ∙ C1
Bump Mapping
Bump Mapping = + = +
Bump + T exture + =
Bump Mapping Perturb the normal slightly before returning it – this will cause the illumination equation to compute different rays, and therefore, different shadow patterns L pert L orig N orig N pert ray P C
Bump Mapping Perturbing the normal in 3D calculate vectors U, V that are perpendicular to N (i.e. tangential to sphere) N N N pert V ∙ v V V U ∙ u U U ∙ ∙ N pert = N + u U + v V Calculating U, V, N vectors using Spherical coordinates: Easier to find in Cartesian since N = (x, y, z) = (sin φ sin θ, cos φ, sin φ cos θ) V perp. to N and y -axis U = ∂N/ ∂θ = ( cos θ, 0, - sin θ ) V = ∂N/ ∂φ = (cos φ sin θ, -sin φ , cos φ cos θ ) Find V=(vx, 0, vz) perp. to N All vectors are already normalized Find U perp. to both N and V
Bump Mapping Perturbing the normal in 3D calculate vectors U, V that are perpendicular to N (i.e. tangential to sphere) N N N pert V ∙ v V V U ∙ u U U ∙ ∙ N pert = N + u U + v V The range of u, v is [-1..1] and they are calculated from the bump map using interpolation, i.e. a similar principle as in texture mapping For smoother approximation: u = (image( i + 1, j ) – image( i - 1, j )) / 2 horizontal difference v = (image( i, j + 1 ) – image( i, j - 1 )) / 2 vertical difference
PPM Image Format P6 PPM type identifier 475 238 width height 255 max value per color channel r, g, b ÙÙ×ÎÖÖÙÙ×ÙÙ×ÎÖÖ... triples of rgb values (one byte per channel), rgbrgbrgbrgbrgb... range is [0..255] -- convert to [0..1] <-row0-><-row1->.. Create a class Image that stores in 2D array the color intensities in a PPM image method that loads a PPM image (see Canvas class for saving a PPM image) operator () , so that image(j, j) returns an interpolated value method that converts to grayscale – replace each color(r,g,b) with intensity(r,g,b) where intensity(r, g, b) = 0.299 ∙ r + 0.587 ∙ g + 0.114 ∙ b + + = texture map bump map
Cylinder T exture Mapping Find the intersection point P(x, y, z) and correct for center at origin Convert to cylindrical coordinates P’(r, θ, y) x = R * sin θ y = y z = R * cos θ Find y and θ (with atan2 only; y is known already) y y is in range [-H/2..+H/2] θ is in range [-π..π] P(x,y,z) H Compute u and v R θ x u = θ converted to range [0..1] v = y converted to range [0..1] z Compute i and j i = u ∙ imageWidth may use 1-v to flip image upside down j = v ∙ imageWidth
Cylinder Bump Mapping Find the intersection point P(x, y, z) Find the unit normal N and vectors U and V so that N,U,V are mutually perpendicular Compute u and v as difference of neighboring color intensities Use u and v to perturb the normal as in the case for spheres y U and N are easy to find V is perpendicular to them U V but same as sphere works N so use that for consistency P(x,y,z) x z
Cone T exture Mapping Find the intersection point P(x, y, z) and correct for top at origin Convert to parametric form x = |y| / H * Rx * sin θ y = y z = |y| / H * Rz * cos θ Find | y| and θ (with atan only; y is known already) y | y| is in the range [0..H] θ is in the range [-π..π] Compute u and v x u = θ converted to the range [0..1] P(x,y,z) H v = |y| converted to the range [0..1] z Compute i and j θ i = u ∙ imageWidth j = v ∙ imageWidth
Recommend
More recommend