Title: Ray%20Tracing
1Ray Tracing
- Rendering Methods
- Rays
- Rays for screen images
- Shadows
- Recursion
2Visible Surface Raytracing
ObjectCoordinates
ModelviewMatrix
Eye coordinates
IntersectionData Structure
Raytracing then utilizes this data structure to
build the image.
3Raytracing
- Invented by Arthur Appel in 1968 at the IBM T.J.
Watson Research Center. - Not considered a major accomplishment since it
was deemed impractical given its enormous
computational requirements. - Languished in obscurity until the late 1970s.
SIGGRAPH77 saw many significant developments,
including the first looks at modeling reflection
and shadows.
4More History
- A true recursive ray tracer was presented for the
first time by Turner Whitted in the classic
article An improved illumination model for
shaded display in the June, 1980 Communications
of the ACM. - Note that the article is more concerned with the
model for illumination than the ray tracing
algorithm.
5That Pinhole Camera Model
After eyespace transformation Projection Plane at
z-d
6Raytracing
- If we shoot a line from the center of projection
through the center of a pixel and off into space - What does it hit first?
- If it hits an object, compute the color for that
point on the object - Thats the pixel color
7Rays
- A Ray is a vector and a point
- Point The starting point of the ray
- Vector The ray direction
- This describes an infinite line starting at the
point and going in the ray direction - Ray t values
- A distance along the ray
t1.5
8t values
- Any point along the ray with origin (xs,ys,zs)
and direction(xd,yd,zd) can be described using t - x xs t xd
- y ys t yd
- z zs t zd
- Input ray
- Output color
9Where will the viewplane beand how big is it?
We describe projection with Field of view
(angle) Aspect Ratio (x/y)
10Viewplane z location
y
-z
Z location is unimportant, field of view is what
matters. Well use z-1 Could be defined using
fov or as a frustum
11Viewplane size
y
y (in direction)
-z
1
(x,y)
y tan(fov/2) height 2y x aspecty width 2x
(-x,-y)
12What if I have a frustum?
- glFrustum(left, right, bottom, top, znear, zfar)
(r,t,n)
(l,b,n)
13What if I have a frustum?
- glFrustum(left, right, bottom, top, znear, zfar)
y bottom/znear height (top
bottom)/znear xleft left/znear width
(right-left)/znear
(r,t,n)
(l,b,n)
14The View Plane as a Pixel Grid
(x,y) (11,7)
8 tall
(-x,-y) (0,0)
12 wide
15What we want
- We want to shoot a ray
- Starting point (0,0,0) (center of projection)
- Direction through the center of a pixel
- What we need is the coordinates of the center of
a pixel
16Pixel Centers
(x,y) (11,7)
(4,5)
8 tall
(4,5)
(-x,-y) (0,0)
12 wide
17Visible Surface Raytracing
// width Screen width, height Screen
height// fov Field of view in degrees, aspect
Aspect ratiodouble ey tan(fov / 2.
DEGTORAD) double ehit2 eydouble ex ey
aspect double ewid 2 exfor(int r0
rltheight r) for(int c0 cltwidth
c) ray.start.Set(0,0,0)
ray.dir.Set(-ex ewid (c 0.5) / width,
-ey ehit (r 0.5) /
height, -1.)
ray.dir.Normalize() // Compute color in
ray direction // Save color as image pixel
18How do we figure out what we will hit?
- Ray intersection tests
- Reduces to solving for t
- x xs t xd
- y ys t yd
- z zs t zd
- Easiest is the sphere
- (x-x0)2(y-y0)2(z-z0)2r2
- Substitute ray equation in and solve for t
- Note Text uses two points on the line
19Ray intersection with a sphere
Questions Why two t values? How do we tell if
there is an intersection?
20Intersections with a plane
- Plane equation
- AxByCzD0
- (A,B,C) is the plane normal
- We can compute the normal and, given a single
point, compute D
Questions Can t be negative? Zero? Why is this
more difficult than a sphere?
21Plane Intersection to Polygon Intersection
- Given a plane intersection, the question is
- Is this point inside the polygon?
22Polygon Interior Tests
- In a 2D projection
- Find all edges that overlap in one dimension
- Determine intersection of a line parallel with an
axis and the overlapping edges - How many intersections are to the left of the
point?
23But, what projection?
- Project the polygon for the maximum area
- What is the largest component of the normal?
- If x Use a Y/Z projection
- If y Use an X/Z projection
- If z Use an X/Y projection
- Why?
24Another Way
- Suppose you know the intersection and know a
normal for each edge that points towards the
inside of the polygon
How do we compute these normals? What is the
characteristic of points inside the polygon?
25Computing the Normals
- Let v1, v2 be two vertices (in counter clockwise
order). Then the edge normal is N x (v2-v1) - Orthogonal to the edge and the polygon surface
normal (N).
Surface Normal
v2
v1
Edge Normal
26Interior Test
- For an edge v1,v2 with edge normal e1, the
intersection p is on the inside side if (p-v1)e1
is not negative.
27Why is ray tracing sometimes considered
impractical?
- Suppose you do a ray for every pixel and test
against very polygon - O(WHP)
- Then if we allow recursion
28Faster Intersection Tests
- Grouping objects and using bounding volumes
- Bounding boxes are easier to test for
intersections. - Your chair object could have a box around each
polygon and a box around the entire object. - We can create hierarchical levels of bounding
boxes pretty easily (cluster tree) - Spheres are a common bounding volume
29Bounding box intersection test
- Assumes Upright box
- Sometimes called generalized box
- Consider box to have 6 planes
- Planes are orthogonal to some axis
- Box with range (0.5, 0.7, -1) to (1.3, 2.2, -4)
- Ray R((0,0,0),(1, 1, -1))
- What is t for intersections with box planes
orthogonal to the X axis?
30Bounding box tests
- Conditions
- Box with range (0.5, 0.7, -1) to (1.3, 2.2, -4)
- Ray R((0,0,0),(1, 1, -1))
- Remember
- x xs t xd
- y ys t yd
- z zs t zd
- So, tx1 (0.5 0) / 1 0.5
- tx2 (1.3 0) / 1 1.3
- Whats the condition that indicates we
intersected the box?
31Bounding box tests
0.8
0.9
Max tnear 4.3
Max tnear 3.5
Min tfar 4.0
4.0
3.5
y2
4.3
4.6
Min tfar 4.6
7.5
6.3
y1
x1
x2
32Bounding box tests
0.8
Max tnear gt min tfarNo intersection
0.9
Max tnear 4.3
Max tnear 3.5
Min tfar 4.0
4.0
3.5
y2
4.3
Max tnear lt min tfarIntersection
4.6
Min tfar 4.6
7.5
6.3
y1
x1
x2
33Algorithm
- Compute tnear and tfar for the three planes
- Let tnear max(tnear1, tnear2, tnear3)
- Let tfar min(tfar1, tfar2, tfar3)
- If tnear tfar we have an intersection
- Note that we can short-circuit this test after
only two planes in some cases
34Ray Walking Algorithms
- We can also subdivide the space into boxes. Then
we trace the ray through the series of boxes.
35Ray Walking Algorithms
- Problem Not the same as line drawing
extras for ray walking
line draw
36Cleary and Wyvill ray walking
Dx
Dy
dx
dy
37Spatial subdivisions
- Uniform divisions
- All boxes are equal sized
- Advantages? Disadvantages?
- Non-uniform divisions
- How?
- Why?
38Octtree-based spatial subdivision
39Speed-up
- Item buffer
- A z-buffer which holds pointers to objects rather
than colors - How can this speed up ray tracing?
- Text calls this First-hit speedup
40How do we compute the color?
- Intersection gives a point on a polygon
- Apply the color model
- How do we get
- View direction?
- Light direction?
- Normal?
41How do we do?
42Terms to know
- Shadow feeler
- Casting a ray in the direction of the light
- If intersection is less than distance to the
light, light is shadowed - Self shadowing
- Roundoff errors can make us intersect ourselves
43Recursion
- Reflections
- Simple compute the color in the reflection
direction - What does this do to the running time?
- Was O(WHP)
44Adaptive Depth Control
- How deep to you recurse?
- A room with mirrors?
- A room with only specular reflection?
- Keep track of how much a ray will contribute to
the final color - Falls below some threshold, dont recurse anymore
45How do we do?
- Shadows?
- Penumbra?
- Depth of field?
- Motion blur?
- Antialiasing?
46Stochastic Ray Tracing
- Problem with ray tracing Point sampling
- We can introduce randomness all over the place
- Jitter for antialiasing
- Light area for penumbra
- Depth of field and motion blur
47Minimum distance Poisson distribution
- Better matches distribution in the eye
- Algorithm
- Generate a random point
- If less than specified distance to nearest point,
discard, otherwise keep
48Distribution Comparisons
Poisson
Uniform
49Adaptive sampling
- Determine if number of samples is enough
- If not, increase the number
- Works for uniform and stochastic sampling
50Ray tracing from the light sources
- In regular ray tracing we dont see
- Lighting reflected through mirrors
- Surfaces lit by reflection from other surfaces
- Large set of rays from each light
- Computes light that hits surfaces
- General solution for diffuse reflection?
- Still requires tracing from the eye as pass 2