Top Banner
Ray Tracing Ray Tracing CptS 548 Advanced Computer Graphics
37

Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Dec 26, 2015

Download

Documents

Peter Park
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Ray TracingRay Tracing

CptS 548

Advanced Computer Graphics

Page 2: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Why Write a Ray Tracer?Why Write a Ray Tracer?

• More elegant than polygon scan conversion

• Testbed for numerous techniques and effects– modeling– rendering– texturing

• Easiest photorealistic renderer to implement

Page 3: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

OverviewOverview

• Vector arithmetic

• Lookat viewing coordinates

• Ray casting

• Shadows

• Reflection

• Refraction

Page 4: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Homogeneous CoordinatesHomogeneous Coordinates

• Point: o = (xo, yo, zo, 1)

• Direction: d= (xd, yd, zd, 0)

• Points translate, directions do not

• Homogeneous coordinate always 0 or 1

• No perspective transformation

• Ray: r = (o,d) = ((xo, yo, zo,1),(xd, yd, zd,0))

• Ray direction d always unit length

o

dr

Page 5: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Dot ProductDot Product

• a · b = dot(a,b) = xa xb + ya yb + za zb

• Cosine of angle between a and bif both unit length

• Used to “cast a shadow” of one unit vector onto another

a

b

(a·b) a

Page 6: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Cross ProductCross Product

• a b = cross(a,b)= (ya zb - za yb, za xb - xa zb, xa yb - ya xb, 0)

• Returns direction perp. to plane of a, b

• Used to set up coordinate systems

ab

c = a b

c a

Page 7: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Eye LUV CoordinatesEye LUV Coordinates

lookat

eye

l

up u

v

l = (lookat - eye)/||(lookat - eye)||v = (l up)/||(l up)||u = v l

Page 8: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Pixels in World CoordinatesPixels in World Coordinates

eye

dl

-av

-u

ll

ll = eye + dl - av - u

aspect ratio a = w/h

focal length d = 1/tan(fovy/2)

fovy

Page 9: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Casting Rays through PixelsCasting Rays through Pixels

for (j = 0; j < VRES; j++) {for (i = 0; i < HRES; i++) {

p = ll + 2av (double)i/HRES + 2u (double)j/VRES;d = (p - eye)/||p - eye||;r = (eye,d);color = TraceRay(r);plot(i,j,color);

}}

Page 10: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

TraceRay AttributesTraceRay Attributes

• Invoked with ray parameter– Better if object database is global– Best if TraceRay is a member function of object

database object (yikes)

• Returns a color vector (R,G,B,) indicates transparency– color = foreground + (1- ) background

Page 11: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Object HierarchyObject HierarchyObject

intersects()TraceRay()

Primitive

normal()

HitObject

thitn

shade()

Composite

Sphere

Polygon

Patch

List

CSG

Page 12: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

TraceRay DefinitionTraceRay Definition

Color TraceRay(Ray r, int depth) {HitObject ho;color c = background;if (intersects(r,ho)) {

c = ho.shade(lights); /* casts a shadow ray */c += ho.ks*TraceRay(ho.reflect(r), depth-1);c += ho.kr*TraceRay(ho.refract(r), depth-1);

}return c;

}

Page 13: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

List DefinitionList Definitionclass List : public Composite {

Object item[100];int n;boolean intersects(Ray r, HitObject &ho) {

min_t = 1.e20;for (i = 0; i < n; i++)

if (item[i].intersects(r,o) && o.t < min_t) {min_t = o.t; ho = o;

}combine_shade(ho);return (min_t < 1.e20);

}}

Page 14: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

shade Definitionshade DefinitionColor shade(Lights lights) {

Color c = ka;for (l = 0; l < lights.n; l++) {

ldir = normalized(lights[l].pos - hit);if (!intersects(ray(hit,ldir))) c += kd*cd*dot(n, ldir) + …

}return(c);

}

Page 15: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

reflect Definitionreflect Definition

r.d bounce

o.n

Ray reflect(Ray r) {Ray bounce;bounce.o = hit;bounce.d = 2.0*dot(n,-r.d)*n + r.d;return(bounce);

}

Page 16: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Calculating RefractionCalculating RefractionSnell’s Law: i sin i = t sin t

Let =i /t = sin t / sin i

t = sin t m - cos t n

m = (cos i n - i) / sin i

= (sin t / sin i) (cos i n - i) - cos t n

cos i n - i

i

n

-n

i

t t

m

= ( cos i - cos t )n - i

itt 222 sin1sin1cos

ininint ))(1(1)( 22

Page 17: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

refract Definitionrefract Definition

Ray refract(Ray r) {Ray t;double ni = dot(n,-r.d);eta = current_index/new_index;t.o = hit;t.d =(eta*ni - sqrt(1.0 - eta*eta*(1-ni*ni)))*n + eta*r.d;return(t);

}

ininint ))(1(1)( 22

Page 18: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Ray-Object IntersectionRay-Object Intersection

• intersects() member function of Object class

• Called with ray and hitobject

• Intersects ray with this object

• Returns intersection data in hitobject– t - parameter, hit - location, n - normal– new - cannot be static because of recursion– always primitive, never composite

Page 19: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Intersection ComputationIntersection Computation

• Parametric ray: r(t) = o + t d– t 0– If ||d|| = 1, t is distance along ray

• Implicit object: f(x) = 0

• Intersection occurs when f(r(t)) = 0– Real function of one real variable– Intersection root finding

Page 20: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Sphere ObjectSphere Object

class Sphere : public Primitive {Point c;double r;boolean intersects(Ray r, HitObject ho);

}

cr

Page 21: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Sphere IntersectionSphere Intersectionf(x)=(x - c)(x - c) - r2

f(r(t)) = (o + t d - c)(o + t d - c) - r2

= dd t2 + 2 (o-c)d t + (o-c)(o-c) - r2

A = dd = 1B = 2 (o-c)dC = (o-c)(o-c) - r2

A

ACBBt

2

42

D = B*B - 4*C;if (D < 0.0) return FALSE;rootD = sqrt(D);t0 = 0.5*(-B - rootD);t1 = 0.5*(-B + rootD);if (t0 >= 0) t = t0, return TRUE;if (t1 >= 0) t = t1, return TRUE;return FALSE;

Page 22: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Other HitObject ValuesOther HitObject Values

hit = r.o + t*r.d

n = hit - c

Page 23: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Ellipsoid IntersectionEllipsoid Intersection

f(x) = 0 f(T-1x)=0

Let T be a 4x4 transformationT distorts a sphere into an ellipsoid

f(T-1r(t))= f(T-1(o + t d))= f(T-1o + t T-1d))

Ellipsoid is implicit surface of f(T-1x)

Ellipsoid::intersects(r,&ho) {Ray Tir = (Ti * o, Ti * d);Sphere::intersects(Tir,ho);

}

o

d

T-1o

T-1d

T

xT-1x

Page 24: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

What about the normal?What about the normal?

f(x) = 0 f(T-1x)=0T

Txx

n is tangent plane [a b c d]x is point [x y z 1]T

Such that matrix product n x = 0

n

(n Q) Tx = 0What is Q?Q = T-1

(n T-1) Tx = n (T-1 T)x = 0

New normal n’ = n T-1 = (T-1)T nT

n’

Page 25: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Intersecting QuadricsIntersecting Quadrics

• Just like sphere: x2 + y2 + z2 - r2

• Cylinder?– x2 + y2 - r2

• Cone?– x2 + y2 - z2

• Variations?– Use the 4x4 transformation trick

Page 26: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Constructive Solid GeometryConstructive Solid Geometry

• Construct shapes from primitives using boolean set operations

• Union: A+B?– A or B

• Intersection: A*B?– A and B

• Difference: A - B?– A and not B

Page 27: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

CSG IntersectionsCSG Intersections

• List of t-values– for left object L– for right object R

• Assume ray originates outside object– t = 0 means ray originates inside object

• Traverse both lists in increasing t order

• Roth Table determine resulting t-values

Page 28: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Accelerating Ray TracingAccelerating Ray Tracing

• Q: Why is ray tracing so slow?

• A: It intersects every ray with every object

• Q: How can we make ray tracing faster?

• A: Coherence– Image coherence - neighboring pixel, same obj.– Spatial coherence - neighboring pt., same obj.– Temporal coherence - next frame, same image

Page 29: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Shadow CachingShadow Caching

• Neighboring points in a shadow are probably shadowed by the same object

• Start shadow ray intersection search with object intersected in last shadow search

Page 30: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Bounding VolumeBounding Volume

Cartman contains 100,000 polygons

Ray-Cartman intersection = 100,000 ray-poly intersections

Even if the ray misses Cartman

Solution:Place a sphere around Cartman

If ray misses sphere then ray misses Cartman

Page 31: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Bounding Volume HierarchiesBounding Volume Hierarchies

Page 32: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

GridsGrids

• 3-D array of lists• Ray intersected with

all objects in a given cell

• Cells visited in Bresenham order

Page 33: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Tagging the ObjectTagging the Object

• Intersection test for entire object

• Not just portion of object in grid cell

• Only intersect object once

• Cache temporary intersection info in object

Page 34: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Other Partitioning StructuresOther Partitioning Structures

• Octree– Ray can parse through large empty areas– Requires less space– Subdivision takes time

• Binary Space Partition (BSP) Tree– Trees more balanced– Partitioning more flexible -> shallower trees– Added ray-plane intersections

Page 35: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Saving the PictureSaving the Picture

• Windows– PrtSc key

• Copies current window

• Paste into any app

• Save as any file type

– Libraries• libtiff, etc.

• Unix– xwd

• saves a window

• as an xwd file

• convert to any format

– xv• grabs a window

• save as any format

– Libraries

Page 36: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Submitting the PictureSubmitting the Picture

• Format– JPEG 100% uncompressed– Don’t want compression artifacts– Might be mistaken for bugs

• Web page– ~you/public_html/cs319– small compressed version– link to full-size uncompressed version

Page 37: Ray Tracing CptS 548 Advanced Computer Graphics. Why Write a Ray Tracer? More elegant than polygon scan conversion Testbed for numerous techniques and.

Submitting the CodeSubmitting the Code

• E-mail [email protected]

• Attach– compressed tar file– zip/gzip archive

• Contents– source code (+headers)– makefile– scene description files