raycasting-1
raycasting-1
raycasting-1
Forward
Forward && Backward
Backward Ray
Ray Tracing
Tracing
Ray
Ray Casting
Casting
Ray-Surface
Ray-Surface Intersection
Intersection Testing
Testing
Barycentric
Barycentric Coordinates
Coordinates
11 Apr. 2000
Light is Bouncing Photons
• Light sources send off photons in all directions
– Model these as particles that bounce off objects in the scene
– Each photon has a wavelength and energy (color and intensity)
– When photons bounce, some energy is absorbed, some reflected,
some transmitted
Closest_intersection(ray)
for each surface in scene
calc_intersection(ray, surface)
return the closest point of intersection to viewer
(also return other info about that point, e.g., surface
normal, material properties, etc.)
• Compute Intersections:
– Substitute ray equation for x
– Find roots
– Implicit: f(p + td) = 0
» one equation in one unknown – univariate root finding
– Parametric: p + td - g(u,v) = 0
» three equations in three unknowns (t,u,v) – multivariate root finding
– For univariate polynomials, use closed form soln. otherwise use
numerical root finder
Computer Graphics 15-462 9
The Devil’s in the Details
• Solving these intersection equations can be tough...
– General case: non-linear root finding problem
– Simple surfaces can yield a closed-form solution
– But generally a numerical root-finding method is required
» Expensive to calculate
» Won’t always converge
» When repeated millions of times, errors WILL occur
• The good news:
– Ray tracing is simplified using object-oriented techniques
» Implement one intersection method for each type of surface primitive
» Each surface handles its own intersection
– Some surfaces yield closed form solutions:
» quadrics: spheres, cylinders, cones, elipsoids, etc…)
» polygons
» tori, superquadrics, low-order spline surface patches
At2+Bt+C=0 B = 2(pxdx+pydy+pzdz )
C = px2+py2+pz2 - r2
• Quadratic formula has two roots: t=(-B±sqrt(B2-4C))/2
– which correspond to the two intersection points
– negative discriminant means ray misses sphere
– In other words, project into the plane for which the perpendicular component of the
normal vector n is largest
• Optimization:
– We should optimize the inner loop (ray-triangle intersection testing) as much as
possible
– We can determine which plane to project to, for each triangle, as a preprocess
C
C2
C0
• Define three barycentric coordinates: , ,
C C0 C1 C 2 where 1
C is inside C0C1C 2 , , [0,1]
C1 Area CC1C 2
Area C0C1C 2
Area C0CC 2
C
Area C0C1C 2
C2 Area C0C1C
1
Area C0C1C 2
C0
A B
• in 2-D
– Area(xy-projection(ABC)) = [(bx-ax)(cy-ay) – (cx-ax)(by-ay)]/2
project A,B,C to xy plane, take z component of cross product
– positive if ABC is CCW (counterclockwise)
ax bx cx cy
1
Area ( ABC ) a y by cy
2 ay
1 1 1
bx c x c x a x a x bx
/2
by
by c y c y a y a y by ax bx cx
(bx c y c x by c x a y a x c y c x a y a x c y ) / 2
The short & long formulas above agree.
Short formula better because fewer multiplies. Speed is important!
Can we explain the formulas geometrically?
Computer Graphics 15-462 20
Computing Area of a Triangle - Geometry
Area(ABC) =[(bx-ax)(cy-ay) – (cx-ax)(by-ay)]/2
is a sum of rectangle areas, divided by 2.
(bx-ax)(cy-ay) (cx-ax)(ay-by)
[ ]
cy
? ay
+ /2 = /2 =
b
ax b x cx y
cy
ay
! !
/2 = = =
by
ax b x cx since triangle area = base*height/2
it works!
Computer Graphics 15-462 21
Uses for Barycentric Coordinates
• Point-in-triangle testing!
– point is in triangle iff , , > 0
– note similarity to standard point-in- N1
polygon methods that use tests of form
aix+biy+ci<0 for each edge i
N2
N0