SLIDE 1 Photorealism
Steve Ash, Rhodes College 2006
SLIDE 2
T exture Mapping
+ = + =
SLIDE 3
T exture Mapping
+ =
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 P(x, y, z) C(i, j)
SLIDE 4
T exture Mapping
+ =
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 P(x, y, z) C(i, j)
SLIDE 5 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
SLIDE 6 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
SLIDE 7 Cartesian => Polar
Given the (x, y) Cartesian coordinates what are the (r, θ) Polar coordinates
r = sqrt( x2 + y2 ) θ = atan( y / x )
image source: http://en.wikipedia.org/wiki/File:Polar_to_cartesian.svg
SLIDE 8 Spherical Coordinates
A point in 3D space can be described by
(x, y, z) coordinates – Cartesian coordinates (r, θ, φ) coordinates – Spherical coordinates (θ, φ – latitude, longitude)
image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
x z θ ϕ y r z x y N U V
SLIDE 9 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θ
image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
x z θ ϕ y r z x y N U V
SLIDE 10 Spherical => Cartesian
Given the (x, y, z) Cartesian coordinates what are the (r, θ, φ) Spherical coordinates
r = sqrt( x2 + y2 + z2 ) θ = atan ( x / z ) use C function atan2( a, b ) – range is [-π .. π] φ = acos ( y / r ) use C function acos( a ) – range is [0 .. π]
In the context of the ray-tracer the assumption is P(x, y, z) is on a sphere centered at the origin – for a sphere not centered at the origin correct the coordinates of P
image source: http://en.wikipedia.org/wiki/File:Coord_system_SZ_0.svg
x z θ ϕ y r z x y N U V
SLIDE 11
T exture Mapping
+ =
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
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 ]
P(x, y, z) C(i, j)
SLIDE 12 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Suppose i = 2 and j = 5.5 What should we use for C1 ?
P(x, y, z) C(i, j) C1 UL LL UR LR (2, 5) (3, 5) (3, 6) (2, 6)
SLIDE 13 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Suppose i = 2 and j = 5.5 What should we use for C1 ? C1 = 0.5 ∙ image[ UL ] + 0.5 ∙ image[ LL ]
P(x, y, z) C(i, j) C1 UL LL UR LR (2, 5) (3, 5) (3, 6) (2, 6)
SLIDE 14 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Suppose i = 2 and j = 5.7 What should we use for C1 ?
P(x, y, z) C(i, j) C1 UL LL UR LR (2, 5) (3, 5) (3, 6) (2, 6)
SLIDE 15 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Suppose i = 2 and j = 5.7 What should we use for C1 ? C1 = 0.7 ∙ image[ UL ] + 0.3 ∙ image[ LL ] C1 = frac ∙ image[ UL ] + (1-frac) ∙ image[ LL ]
P(x, y, z) C(i, j) C1 UL LL UR LR (2, 5) (3, 5) (3, 6) (2, 6)
SLIDE 16 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Let i0 = i – floor(i) and i1 = ceil(i) – i = 1 - i0
j0 = j – floor(j) and j1 = ceil(j) – j = 1 - j0
Compute C1 = j0 ∙ image[ UL ] + j1 ∙ image[ LL ] C2 = j0 ∙ image[ UR ] + j1 ∙ image[ LR ] C = i0 ∙ C2 + i1 ∙ C1
P(x, y, z) C(i, j) i0 j0 i1 j1 C1 C2 UL LL UR LR
SLIDE 17 T exture Mapping (Interpolation)
+ =
The computed coordinates (i, j) will not be integers, in
- general. To have smooth color change, interpolate the
colors of neighboring pixels.
Let i0 = i – floor(i) and i1 = ceil(i) – i = 1 - i0
j0 = j – floor(j) and j1 = ceil(j) – j = 1 - j0
Compute C1 = j0 ∙ image[ UL ] + j1 ∙ image[ LL ] C2 = j0 ∙ image[ UR ] + j1 ∙ image[ LR ] C = i0 ∙ C2 + i1 ∙ C1
P(x, y, z) C(i, j) i0 j0 i1 j1 C1 C2 UL LL UR LR use % for indices to wrap around the image when looking up neighbors
SLIDE 18
Bump Mapping
SLIDE 19
Bump Mapping
+ = + =
SLIDE 20
Bump + T exture
+ =
SLIDE 21
Bump Mapping
Perturb the normal slightly before returning it – this will cause the illumination equation to compute different rays, and therefore, different shadow patterns C P Norig ray Npert Lorig Lpert
SLIDE 22
Bump Mapping
Perturbing the normal in 3D
calculate vectors U, V that are perpendicular to N (i.e. tangential to sphere)
Calculating U, V, N vectors using Spherical coordinates:
N = (x, y, z) = (sin φ sin θ, cos φ, sin φ cos θ) U = ∂N/ ∂θ = ( cos θ, 0, - sin θ ) V = ∂N/ ∂φ = (cos φ sin θ, -sin φ , cos φ cos θ )
All vectors are already normalized N U V N U
Npert= N + u∙U + v∙V
V u∙U v∙V
SLIDE 23
Bump Mapping
Perturbing the normal in 3D
calculate vectors U, V that are perpendicular to N (i.e. tangential to sphere)
N U V N U
Npert= N + u∙U + v∙V
V 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
SLIDE 24 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]
Create a class image that stores the 2D array of colors stored in a PPM image
method that loads a PPM image (see Canvas class for saving a PPM image)
- perator () , so that image(j, j) returns an interpolated value
method that converts to grayscale – replace each pixel c(r, g, b) with c’(r’, g’, b’) where r’ = g’ = b’ = 0.299∙r + 0.587∙g + 0.114∙b
texture map bump map
+ + =
SLIDE 25
Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
An intersection object stores: t value intersection point p normal at p color at p u, v values at p (in [0..1]) U, V vectors at p
data members: shape pointer image map
SLIDE 26 Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
An intersection object stores: t value intersection point p normal at p color at p u, v values at p (in [0..1]) U, V vectors at p
Sphere::intersect(Ray r, Intersection& i) { same as Assignment 8 }
data members: shape pointer image map
SLIDE 27
Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
An intersection object stores: t value intersection point p normal at p color at p u, v values at p (in [0..1]) U, V vectors at p
data members: shape pointer image map
SLIDE 28 Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
An intersection object stores: t value intersection point p normal at p color at p u, v values at p (in [0..1]) U, V vectors at p
TextureDecor::intersect(Ray r, Intersection& i) {
- 1. call intersect on the shape
- 2. overwrite the color with the
interpolated color from the map }
data members: shape pointer image map
SLIDE 29 Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
An intersection object stores: t value intersection point p normal at p color at p u, v values at p (in [0..1]) U, V vectors at p
BumpDecor::intersect(Ray r, Intersection& i) {
- 1. call intersect on the shape
- 2. overwrite the normal with the
perturbed normal from the map }
data members: shape pointer image map
SLIDE 30 Shapes and Decorators
Shape Sphere T extureDecor BumpDecor getMaterial intersect(Ray r, Intersection i)
void example() { Shape* s = new Sphere(…) Shape* textured = new TextDecor(s); Shape* bumped = new BumpDecor(s); Shape* both = new BumpDecor(textured); }
data members: shape pointer image map
void example() { Shape* p = new Plane(…) Shape* textured = new TextDecor(p); Shape* bumped = new BumpDecor(p); Shape* both = new BumpDecor(textured); }