Computer Graphics Course: Ray Tracing
Computer Graphics Course: Ray Tracing
Ray Tracing
• For each image pixel, a ray (line segment) is cast from the
viewpoint, crosses the pixel and is directed toward the scene
• For hit points:
– Send (at least) one ray to each
light source and check visibility
(shadow)
– Shade the point using a local
illumination model
Whitted-style Ray Tracing (1)
• For each image pixel, a ray (line segment) is cast from the
viewpoint, crosses the pixel and is directed toward the scene
• The ray hits the objects, is absorbed or deflected in order to
gather what the observer would “see” through each pixel
• This is a recursive algorithm that spawns new rays at each hit
point
Whitted-style Ray Tracing (2)
Why not Trace Rays from the Lights? (1)
• Infinite rays leave a light source but only a small number lands
on the viewport
– Even fewer when a pinhole camera is considered
– Extremely low probability to hit Computationally intractable
Why not Trace Rays from the Lights? (2)
Source: http://indigorenderer.com
More than Direct Illumination
• Material properties:
– Reflectance
– Incandescence
– Gloss
– Permeability a
– Index of refraction n
– …
• Number, size and type of lights
Resulting Color
Spawn Gather
The Basic Ray Tracing Algorithm
Color raytrace( Ray r, int depth, Scene world, vector <Light*> lights )
{ Ray *refl, *tran;
Color color_r, color_t, color_l;
Source: http://hof.povray.org/images/ChristmasBaubles.jpg
Ray Tracing Results (2)
Simple ray-traced scene rendered at 60 frames per second on a modern GPU at 1080p
Source: NVIDIA OptiX SDK
Comments
vec3 origin;
vec3 dir;
int depth;
vec3 p_isect;
vec3 n_isect;
vec3 barycentric;
real t;
real strength;
bool hit;
bool inside;
class primitive *hit_primitive;
void *payload;
};
Ray – Scene Intersection: Primitives
in shadow
lit
Shadow Rays
• Cast “shadow rays” toward each light source during the local
color estimation
• Shadow rays are cheaper:
– Once light is blocked by a primitive, the search stops
– No sorting is required
• Occluding geometry does not necessarily completely block or
allow light through
– Modulate the ray strength according to occluder
transparency
– Stop if strength becomes too low (in shadow)
Shadow Determination (1)
if (strength>0)
col += strength * localShadingModel(r,prim,lights[i]->pos);
}
return col;
}
Shadow Determination (2)
...
// Filter the light as it passes through the scene
for ( j=0; j<world.numObjects(); j++ )
for ( k=0; k<world.getObject(j)->numPrims(); k++ )
{
Primitive *prim = world.getObject(j)->getPrim(k);
if (prim->intersect(r));
strength *= prim->material->getTransparency(r);
wv d tan hv wv / a
r r r r h r r
pUL c d n wv u hv v UL
p c d n
w v u tan
r 2wv r r 2hv r
u u v v
w h
Shooting Rays – Primary Rays (2)
• Starting point:
1 r 1 r
– Either p pUL i u j v (planar near surface)
2 2
r
– Or pstart c n r (Spherical near surface – Can be zero!)
r
• Arbitrary ray point: q q(t ) pstart t r
• Sec. rays can and will intersect with originating surface point
(self intersection)
• Fix:
– Offset the origin along its direction before casting
Reflection Direction
𝐫Ԧ𝑖 + 𝐧cos𝜃𝑖 =
𝐫Ԧ𝑖 − 𝐧(𝐧Ԧ𝐫𝑖 ) ⇒
sin t n1
sin i n2
Refraction Direction (1)
Refraction Direction (2)
• Critical angle:
• When 𝑛1 > 𝑛2 , reflection may occur instead of refraction for
incident angles higher than a certain threshold 𝜃𝑐
• This is called total internal reflection and can be easily
observed underwater, when looking upwards almost parallel
to the surface
Transformed sphere
according to matrix 𝐌. Local CS
Sphere is distorted
𝐌 −1 ∙ray
q'
y
q
Perform easy-to-compute
ray ray-sphere intersection
WCS x
q = 𝐌 ∙ q’
RAY TRACING ACCELERATION TECHNIQUES
Basic Acceleration Concepts
Source: https://graphics.stanford.edu/courses/cs348b-05/lectures/lecture3/raytrace_ii.pdf
Bounding Volumes
a. Axes-aligned bounding
box (AABB)
b. Oriented bounding box
(OBB)
c. BV hierarchy (BVH)
d. Bounding slabs
Bounding Volumes – Pros & Cons
• AABB:
– Easy to implement and initialize
– Fast test, no ray transformations required
– Can leave too much void space degraded pruning performance
• OBB:
– Can be costly to initialize (e.g. PCA algorithm)
– Fast test, ray transformation required
– Ideal for animated hierarchies (no recalculation of extents required)
– Tighter fitting than AABB
• Bounding Slabs:
– Very efficient, even less void space
– More computationally expensive than AABB/OBB
Ray - Scene Graph/BVH Intersection
Spatial Subdivision Acceleration (1)
Source: https://graphics.stanford.edu/courses/cs348b-05/lectures/lecture3/raytrace_ii.pdf
Hierarchical Spatial Subdivision
Source: https://graphics.stanford.edu/courses/cs348b-05/lectures/lecture3/raytrace_ii.pdf
Octree
A, B, C, A, B,
E E E
D, E C, D
A, B, B, B,
D C C
A, A,
B D
Complexity Analysis of a Split (1)
𝑆𝐴(𝐿) 𝑆𝐴(𝑅)
𝑁𝐿 𝑏 𝐶𝑂 + 𝑁𝑅 𝑏 𝐶𝑂
𝑆𝐴(𝐿 ∪ 𝑅) 𝑆𝐴(𝐿 ∪ 𝑅)
Complexity Analysis of a Split (3)
Midpoint Splits
http://www.keithlantz.net/2013/04/kd-tree-construction-using-the-surface-area-heuristic-stack-based-traversal-and-the-hyperplane-separation-theorem/
Surface Area Heuristic (3)
Median Splits
http://www.keithlantz.net/2013/04/kd-tree-construction-using-the-surface-area-heuristic-stack-based-traversal-and-the-hyperplane-separation-theorem/
Surface Area Heuristic (4)
SAH Splits
http://www.keithlantz.net/2013/04/kd-tree-construction-using-the-surface-area-heuristic-stack-based-traversal-and-the-hyperplane-separation-theorem/
INTERSECTION TESTS
Intersection Tests: Ray - Plane
q(u, v, w) wv 0 uv1 vv 2 ,
u v w 1
Intersection Tests: Ray - Triangle (2)
• Requiring intersection point in triangle:
r
p td (1 u v) v0 uv1 vv2
• Georgios Papaioannou
References
[RTI]: Fast, Minimum Storage Ray/Triangle Intersection , Möller &
Trumbore. Journal of Graphics Tools, 1997