Introduction to RayTracing
Dr. B. Raghu
Professor /CSE
Sri  Ramanujar Engineering College
11/20/2016
Classifying RenderingAlgorithms
One way to classify rendering algorithms isaccording to the type of light interactions theycapture
For example: The OpenGL lighting model captures:
Direct light to surface to eye light transport
Diffuse and rough specular surface reflectance
It actually doesn’t do light to surface transport correctly,because it doesn’t do shadows
We would like way of classifying interactions: lightpaths
2
Dr.B.Raghu, Professor/ CSE
11/20/2016
Classifying Light Paths
Classify light paths according to where they comefrom, where they go to, and what they do along theway
Assume only two types of surface interactions:
Pure diffuse, D
Pure specular, S
Assume all paths of interest:
Start at light source, L
End at the eye, E
Use regular expressions on the letters D, S, and Eto describe light paths
Valid paths are L(D|S)*E
3
Dr.B.Raghu, Professor/ CSE
11/20/2016
Simple Light Path Examples
LE
The light goes straight from the source to theviewer
LDE
The light goes from the light to diffuse surfacethat the viewer can see
LSE
The light is reflected off mirror into the viewer’seyes
L(S|D)E
The light is reflected off either diffuse surfaceor specular surface toward the viewer
Which do OpenGL (approximately) support?
4
Dr.B.Raghu, Professor/ CSE
11/20/2016
cbox_ig
More Complex Light Paths
Find thefollowing:
LE
LDE
LSE
LDDE
LDSE
LSDE
5
Dr.B.Raghu, Professor/ CSE
11/20/2016
cbox_ig
More Complex Light Paths
LE
LDDE
LDE
LSDE
LSE
LDSE
6
Dr.B.Raghu, Professor/ CSE
11/20/2016
The OpenGL Model
The “standard” graphics lighting model captures onlyL(D|S)E
It is missing:
Light taking more than one diffuse bounce: LD*E
Should produce an effect called color bleeding,among other things
Approximated, grossly, by ambient light
Light refracted through curved glass
Consider the refraction as “mirror” bounce: LDSE
Light bouncing off mirror to illuminate diffuse surface:LS+D+E
Many others
Not sufficient for photo-realistic rendering
7
Dr.B.Raghu, Professor/ CSE
11/20/2016
PCKTWTCH byKevin Odhner,POV-Ray
Raytraced Images
pcktwtch
8
Dr.B.Raghu, Professor/ CSE
11/20/2016
kettle
Kettle, MikeMiller, POV-Ray
9
Dr.B.Raghu, Professor/ CSE
11/20/2016
bell_jar
10
Dr.B.Raghu, Professor/ CSE
11/20/2016
Graphics Pipeline Review
Properties of the Graphics Pipeline
Primitives are transformed and projected (not depending on displayresolution)
Primitives are processed one at a time
Forward-mapping from geometrical space to image space
FEEDFORWARD
11
Dr.B.Raghu, Professor/ CSE
11/20/2016
Alternative Approaches: RayCasting
Ray-casting searches along lines of sight, or rays, to determine theprimitive that is visible along it.
Properties of ray-casting:
 Go through all primitives at each pixel
 Image space sample first
 Analytic processing afterwards
FEEDBACKWARD
12
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray Casting Outline
For every pixel shoot ray fromthe eye through the pixel.
 For every object in the scene
Find the point of intersectionwith the ray closest to (and infront of) the eye
Compute normal at point ofintersection
Compute color for pixel based onpoint and normal at intersectionclosest to the eye (e.g. by Phongillumination model).
FEEDBACKWARD
t0 
j0198994[1]
13
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray Casting
Ray Cast ( Point R, Ray D ) {
foreach object in the scene
find minimum t>0 such that R + t D hits object
if ( object hit )
return object
else return background object
 }
14
Dr.B.Raghu, Professor/ CSE
11/20/2016
Raytracing
Cast rays from the eye point the same way as ray casting
Builds the image pixel by pixel, one at a time
Cast additional rays from the hit point to determine the pixelcolor
Shoot rays toward each light. If they hit something, then theobject is shadowed from that light, otherwise use“standard” model for the light
Reflection rays for mirror surfaces, to see what should bereflected in the mirror
Refraction rays to see what can be seen throughtransparent objects
Sum all the contributions to get the pixel color
15
Dr.B.Raghu, Professor/ CSE
11/20/2016
Raytracing
60%
Shadow rays
Reflection ray
refracted ray
16
Dr.B.Raghu, Professor/ CSE
11/20/2016
Recursive Ray Tracing
When reflected or refracted ray hits surface,repeat the whole process from that point
Send out more shadow rays
Send out new reflected ray (if required)
Send out new refracted ray (if required)
Generally, reduce the weight of each additional ray whencomputing the contributions to surface color
Stop when the contribution from ray is too small to noticeor maximum recursion level has been reached
17
Dr.B.Raghu, Professor/ CSE
11/20/2016
Raytracing Implementation
Raytracing breaks down into two tasks:
Constructing the rays to cast
Intersecting rays with geometry
The former problem is simple vector arithmetic
Intersection is essentially root finding (as we willsee)
Any root finding technique can be applied
Intersection calculation can be done in worldcoordinates or model coordinates
18
Dr.B.Raghu, Professor/ CSE
11/20/2016
Constructing Rays
Define rays by an initial point and a direction: x(t)=x0+td
Eye rays: Rays from the eye through a pixel
Construct using the eye location and the pixel’s location on theimage plane. Xeye
Shadow rays: Rays from a point on a surface to the light.
X= point on surface
Reflection rays: Rays from a point on a surface in the reflectiondirection
Construct using laws of reflection. X= surface point
Transmitted rays: Rays from a point on a transparent surfacethrough the surface
Construct using laws of refraction. X= surface point
19
Dr.B.Raghu, Professor/ CSE
11/20/2016
From Pixels to Rays
20
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray-Object Intersections
Aim: Find the parameter value, tiat which the rayfirst meets object i
Write the surface of the object implicitly: f(x)=0
Unit sphere at the origin is xx-1=0
Plane with normal n passing through origin is: nx=0
Put the ray equation in for x
Result is an equation of the form f(t)=0 where we want t
Now it’s just root finding
21
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray-Sphere Intersection
Quadratic in t
solutions: Ray passes through sphere take minimumvalue that is 0
solution: Ray is tangent use it if >0
solutions: Ray does not hit sphere
Numerical stability is very important.  For example, can areflection ray hit the same sphere?
22
Dr.B.Raghu, Professor/ CSE
11/20/2016
Sphere Intersection
A sphere is defined by its center, s, and its radius r. The intersection of a raywith a sphere can be computed as follows:
Sphere
RaySphere
23
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray-Plane Intersections
This is plane going through the origin
What about an arbitrary plane?
To do polygons, intersect with plane then do point-in-polygontest…
24
Dr.B.Raghu, Professor/ CSE
11/20/2016
Point-in-Polygon Testing
Project point and polygon onto 2D plane
Find biggest component of normal vector, and just useother two coordinates
For example, if n=(0.2, 0.4, 0.9), just use x,y coordinates,which is like projecting down onto the x-y plane
Use the clipping algorithms to determine if the pointin the polygon
Cast ray from the point to infinity and count the numberof edges it crosses
Odd number means point is inside
Evaluate against each polygon edge
25
Dr.B.Raghu, Professor/ CSE
11/20/2016
Barycentric Coordinates
barycentric
This can be used for checking if P is
In the triangle. It is also useful when
Computing the texture coordinates and
Other linear interpolations (normal).
P is in the triangle if c1 > 0, c2 >0, and
c1+c2 < 1
26
Dr.B.Raghu, Professor/ CSE
11/20/2016
Intersection in ModelCoordinates
Transform ray into object space! Why?
– Intersections straightforward, even trivial there
How do we transform the ray's origin and direction?
27
Dr.B.Raghu, Professor/ CSE
11/20/2016
Intersection in ModelCoordinates
Transform ray origin into object space
R’ = M-1R
How do we transform the ray's direction?
Same M-1 as in homogenous coordinates
We find intersection point and the normal at thepoint in the model coordinate system. How do wetransform normal back to world space?
Cannot use M
Use (MT)-1
28
Dr.B.Raghu, Professor/ CSE
11/20/2016
Ray Tracing Illumination
Recursive
j0198994[1]
Check for shadowing (intersection with object along ray (P,L))
29
Dr.B.Raghu, Professor/ CSE
11/20/2016
The Ray Tree
R2 
R1 
R3 
L2
L1
L3
N1
N2
N3
T1
T3
Ni surface normal
Ri reflected ray
Li shadow ray
Ti transmitted (refracted) ray
Psuedo-code
Viewpoint
L1
T3
R3 
L3
L2
T1
R1 
R2 
Eye
j0198994[1]
30
Dr.B.Raghu, Professor/ CSE
11/20/2016
Reflection
Reflection angle = view angle
31
Dr.B.Raghu, Professor/ CSE
11/20/2016
Reflection
The maximum depth of the tree affects the handling of refraction
If we send another reflected ray from here, when do we stop? 2solutions (complementary)
Answer 1: Stop at fixed depth.
Answer 2: Accumulate product of reflection coefficients and stop whenthis product is too small.
32
Dr.B.Raghu, Professor/ CSE
11/20/2016
Reflection
33
Dr.B.Raghu, Professor/ CSE
11/20/2016
Refraction
Snell’s Law
Total internal reflection when thesquare root is imaginary
Note that I is the negative ofthe incoming ray
34
Dr.B.Raghu, Professor/ CSE
11/20/2016
Pseudo Code for Ray Tracing
rgb lsou; // intensity of light source
rgb back;// background intensity
rgb ambi;// ambient light intensity
Vector L// vector pointing to light source
Vector N// surface normal
Object objects [n] //list of n objects in scene
float Ks [n]// specular reflectivity factor for each object
float Kr [n]//  refractivity index for each object
float Kd [n]// diffuse reflectivity factor for each object
Ray r;
void raytrace() {
for (each pixel P of projection viewport in raster order) {
  r = ray emanating from viewer through P
int depth = 1; // depth of ray tree consisting of multiple paths
  the pixel color at P = intensity(r, depth)
}
}
35
Dr.B.Raghu, Professor/ CSE
11/20/2016
rgb intensity (Ray r, int depth) {
Ray  flec, frac;
rgb   spec, refr, dull, intensity;
 if (depth >= 5) intensity back;
 else {
find the closest intersection of with all objects in scene
if (no intersection) {
    intensity =back;
else {
    Take closest intersection which is object[j]
    compute normal at the intersection point
    if (Ks[j] >0)   {  // non-zero specular reflectivity
          compute reflection ray flec;
          refl Ks[j]*intensity(flec, depth+1);
    else refl =0;
    if (Kr[j]>0)  // non-zero refractivity
         compute refraction ray frac;
         refr Kr[j]*intensity(frac, depth+1);
    else refr =0;
    check for shadow;
    if (shadow) direct Kd[j]*ambi
    else direct Phong illumination computation;
    intensity direct refl +refr;
    }
  return intensity; }
36
Dr.B.Raghu, Professor/ CSE
11/20/2016
cbox_ray
Ray-traced Cornell box, dueto Henrik Jensen,
http://www.gk.dtu.dk/~hwj
Which pathsare missing?
Raytraced Cornell Box
37
Dr.B.Raghu, Professor/ CSE
11/20/2016
Paths in RayTracing
Ray Tracing
Captures LDS*E paths: Start at the eye, any number of specularbounces before ending at a diffuse surface and going to the light
Raytracing cannot do:
LS*D+E: Light bouncing off a shiny surface like a mirror andilluminating a diffuse surface
LD+E: Light bouncing off one diffuse surface to illuminate others
Basic problem: The raytracer doesn’t know where to send raysout of the diffuse surface to capture the incoming light
Also a problem for rough specular reflection
Fuzzy reflections in rough shiny objects
Need other rendering algorithms that get more paths
38
Dr.B.Raghu, Professor/ CSE
11/20/2016
cbox_ig
A Better Rendered Cornell Box
39
Dr.B.Raghu, Professor/ CSE