Ray Tracing • Rendering Methods • Rays

Download Report

Transcript Ray Tracing • Rendering Methods • Rays

Ray Tracing
• Rendering
Methods
• Rays
• Rays for screen
images
• Shadows
• Recursion
1
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Visible Surface Raytracing
Object
Coordinates
Raytracing then utilizes this
data structure to build the image.
2
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Raytracing
• 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 1970’s. SIGGRAPH’77
saw many significant developments, including the first
looks at modeling reflection and shadows.
3
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
More 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.
4
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
That Pinhole Camera Model
After eyespace transformation
Projection Plane at z=-d
5
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Raytracing
• 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
– That’s the pixel color
6
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Rays
• 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
t=1.5
7
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
t 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
8
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Where will the viewplane be
and how big is it?
We describe projection with:
Field of view (angle)
Aspect Ratio (x/y)
9
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Viewplane z location
y
-z
Z location is unimportant,
field of view is what matters.
We’ll use z=-1
Could be defined using fov or as a frustum
10
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Viewplane size
y
y (in + direction)
-z
1
y = tan(fov/2)
height = 2y
x = aspect*y
width = 2x
11
(x,y)
(-x,-y)
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
What if I have a frustum?
• glFrustum(left, right, bottom, top, znear, zfar)
(r,t,n)
(l,b,n)
12
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
What 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)
13
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
The View Plane as a Pixel Grid
8 tall
(x,y)
(11,7)
(-x,-y)
(0,0)
14
12 wide
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
What 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
15
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
(x,y)
(11,7)
8 tall
Pixel Centers
(4,5)
(4,5)
(-x,-y)
(0,0)
16
12 wide
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Visible Surface Raytracing
// width – Screen width, height – Screen height
// fov – Field of view in degrees, aspect – Aspect ratio
double ey = tan(fov / 2. * DEGTORAD);
double ehit=2 * ey;
double ex = ey * aspect;
double ewid = 2 * ex;
for(int r=0; r<height; r++)
for(int c=0; c<width; 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
}
17
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
How 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)2=r2
– Substitute ray equation in and solve for t
– Note: Text uses two points on the line
18
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Ray intersection with a sphere
 B  B  4 AC
t1, 2 
2A
2
2
2
A  xd  y d  z d
2
B  2( xd ( xs  x0 )  yd ( y s  y0 )  z d ( z s  z0 ))
C  ( xs  x0 ) 2  ( y s  y0 ) 2  ( z s  z0 ) 2  r 2
Questions: Why two t values?
How do we tell if there is an intersection?
19
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Intersections with a plane
• Plane equation
– Ax+By+Cz+D=0
– (A,B,C) is the plane normal
– We can compute the normal and, given a single point,
compute D
Axs  By s  Czs  D
t
Axd  By d  Czd
Questions: Can t be negative? Zero?
Why is this “more difficult” than a sphere?
20
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Plane Intersection to Polygon Intersection
• Given a plane intersection, the question is:
– Is this point inside the polygon?
21
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Polygon 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?
22
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
But, what projection?
• Project the polygon for the maximum area
–
–
–
–
–
23
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?
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Another 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?
24
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Computing 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).
v2
Surface
Normal
v1
Edge
Normal
25
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Interior 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.
26
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Why 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…
27
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Faster 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
28
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Bounding box intersection test
• Assumes: Upright box
– Sometimes called “generalized box”
• Consider box to have 6 planes
–
–
–
–
29
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?
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Bounding 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  txd
• x = xs + t xd
• y = ys + t yd
• z = zs + t zd
txd  x  xs
x  xs
t
xd
– So, tx1 = (0.5 – 0) / 1 = 0.5
tx2 = (1.3 – 0) / 1 = 1.3
– What’s the condition that indicates we intersected the
box?
30
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Bounding box tests
0.8
0.9
Max tnear = 4.3
Max tnear = 3.5
3.5
Min tfar = 4.0
4.0
y2
4.3
4.6
Min tfar = 4.6
y1
x1
31
6.3
x2
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
7.5
Bounding box tests
0.8
0.9
Max tnear > min tfar
No intersection
Max tnear = 4.3
Max tnear = 3.5
3.5
y2
4.3
Max tnear <= min tfar
Intersection
4.6
Min tfar = 4.6
y1
x1
32
Min tfar = 4.0
4.0
6.3
x2
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
7.5
Algorithm
•
•
•
•
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
33
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Ray Walking Algorithms
• We can also subdivide the space into boxes. Then
we trace the ray through the series of boxes.
34
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Ray Walking Algorithms
• Problem: Not the same as line drawing:
= line draw
35
= extras for ray
walking
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Cleary and Wyvill ray walking
Dx
Dy
dx
dy
36
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Spatial subdivisions
• Uniform divisions
– All boxes are equal sized
– Advantages? Disadvantages?
• Non-uniform divisions
– How?
– Why?
37
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Octtree-based spatial subdivision
38
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Speed-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
39
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
How 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?
40
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
How do we do?
• Shadows?
• Reflections?
41
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Terms 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
42
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Recursion
• Reflections
– Simple compute the color in the reflection direction
• What does this do to the running time?
– Was O(WHP)
43
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Adaptive 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, don’t recurse anymore…
44
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
How do we do?
•
•
•
•
•
45
Shadows?
Penumbra?
Depth of field?
Motion blur?
Antialiasing?
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Stochastic 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
46
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Minimum 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
47
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Distribution Comparisons
Poisson
48
Uniform
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Adaptive sampling
• Determine if number of samples is enough
– If not, increase the number
• Works for uniform and stochastic sampling
49
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics
Ray tracing from the light sources
• In regular ray tracing we don’t 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
50
CSE 872 Dr. Charles B. Owen
Advanced Computer Graphics