This is the ray tracer I developed based on Nori for IFT 6095 Sujet en infographie/Real-time Rendering  course.

I implemented sphere, ray-sphere intersection, directional light, diffuse shading with hard shadows, anti-aliasing with super sampling, Monte Carlo integrator, implicit path tracing with biased path-length termination, explicit path tracing with Russian roulette.

In the future I am planing to continue by adding Phong shading, multiple importance sampling, environmental mapping, and volume rendering.

  1. Picking a Point on the Sphere
  2. Calculating Normal of a Point on the Sphere
  3. Ray-Sphere Intersection
  4. Diffuse Shading
  5. Anti-aliasing
  6. Monte Carlo Estimation
  7. Path Tracing
  8. Some of the Results
  9. References

Picking a Point on the Sphere

For the calculation, Global Illumination Compendium is referenced.

For picking a point on the sphere, the formula below is used

x = Cx + ( 2r * cos(2*PI*u1) * sqrt( u2(1-u2) )
y = Cy + ( 2r * sin(2*PI*u1) * sqrt( u2(1-u2) )
z = Cz + r(1 - 2u2)

where Cx,y,z is the center of sphere, r is radius of the sphere, and u1,2 are random points.



Calculating Normal of a Point on the Sphere

For the calculation, formula from SIGGRAPH tutorial is used.

SN = [(xi - xc)/Sr,   (yi - yc)/Sr,   (zi - zc)/Sr]

Where, SN is the surface normal, Xi, Yi, Zi are given point coordinates, Xc, Yc, Zc are coordinates of the sphere’s center.



Ray-Sphere Intersection

For ray-sphere intersection, the algebraic method told in Ray-Sphere Intersection, SIGGRAPH Tutorial is used.

Algorithm

  1. Calculate A value: A = Xd^2 + Yd^2 + Zd^2 where Xd, Yd, and Zd are direction of the Ray
  2. Calculate B value:  B = 2 * (Xd * (X0 – Xc) + Yd * (Y0 – Yc) + Zd * (Z0 – Zc)) where X0, Y0, Z0 are origin of the ray, and Xc, Yc, Zc are center of the sphere
  3. Calculate C value: C = (X0 – Xc)^2 + (Y0 – Yc)^2 + (Z0 – Zc)^2 – Sr^2 where Sr is radius of the sphere
  4. Calculate discriminant
  5. if(disc < 0.f), No intersection, return false.
  6. Calculate the first root: t0 = ( -b – sqrtDisc ) / (2*a)
  7. if t0 >= 0, there is no need to calculate t1. Because t0 will be the smallest root. Else calculate t1 = ( -b + sqrtDisc ) /  (2*a)


Diffuse Shading

For calculating diffuse shading the formula below is used:

I = IaKa + IpKd(N dot L)

Here, I stands for intensity (color) of the pixel, Ia for ambient
intensity and Ka for ambient coefficient. IaKa together defines
ambient color.

Ip stands for intensity of the light source and Kd for diffuse
reflection coeffient of the object.

(N dot L) defines dot product between surface normal (N) and light
direction vector (L). The result is the cosine value (angle) between
these two vectors.

IpKd(N dot L) stands for diffuse color.

Algorithm

  1. Check for the intersection. If there is no intersection, return background color.
  2. If there is an intersection, create a shadow ray as the origin is the intersection point, and the direction is the light direction.
  3. If the shadow ray hits an object, return ambient color.
  4. If the object is not in the shadow, calculate and return diffuse color of the pixel.


Anti-aliasing

For anti-aliasing, uniform and stochastic super sampling approaches are used.

Uniform super sampling anti-aliasing was implemented with 5 (including the main ray) rays. For each pixel, one ray for each corner of the pixel, and a ray for the center are shot.

For stochastic super sampling, any number of rays are shot to the pixel in a random fashion (with random positions).

After each ray’s result is calculated, all results are summed and divided by the number of rays for calculating final color of the pixel.



Monte Carlo Estimation

Normal ray tracing procedure is followed until an intersection is found. After finding one, if there is no intersection, background color is returned. Light color is returned if the object, which is hit by the ray, is light source. Otherwise, the result is calculated by MC integrator. Shadow rays, and visibility is handled during the MC process.

f(x) was selected as diffuse shading formula which is Li * BRDF * n dot w. Here, Li is intensity of light, BRDF is diffuse color of the object, which is rho/PI, and n dot w is the angle between the surface normal, and light direction.

pdf(x) is selected differently for each type of MC integration.

Spherical MC Integration

Formula, which are used for generating random direction on unit sphere, are taken from PBRT 2nd Edition, CH 13 Monte Carlo Integration I: Basic Concepts page 664.

Direction.z = 1 - 2r1
Direction.x = sqrt(1 - Direction.z^2) * cos(2 * M_PI * r2)
Direction.y = sqrt(1 - Direction.z^2) * sin(2 * M_PI * r2)

r1, and r2 are random values between 0 and 1.

Because, uniform sphere sampling is used, pdf(x) is selected as 1 / 4*PI. Thus, final form of MC integration becomes 1/4*(Li * rho * n dot w).

Hemispherical MC Integration

Formula, which are used for generating random direction on unit hemisphere proportional to solid angle, are taken from Global Illumination Compendium.

Direction.x = cos(2*M_PI*r1) * sqrt(1 - r2^2)
Direction.y = sin(2*M_PI*r1) * sqrt(1 - r2^2)
Direction.z = r2

r1, and r2 are random values between 0 and 1.

Because, hemispherical sampling is used, pdf(x) is selected as 1/2*PI. Thus, final form of MC integration becomes 1/2*(Li * rho * n dot w)

Cosine-Weighted MC Integration

Formulae are taken from IFT 6095 slides to implement unit hemisphere proportional to cosine-weighted solid angle sampling. For direction picking, formula on Techniques: Illumination Globale slide (page 48), and for converting to Cartesian coordinates, formula on Théorie: Illumination Directe slide (page 39) are used.

float theta = acosf( sqrt(r1) );
float phi   = 2 * M_PI * r2;

Vector3f direction = Vector3f( sinf(theta) * cosf(phi), // X
                               sinf(theta) * sinf(phi), // Y
			       cosf(theta)); // Z

r1, and r2 are random values between 0 and 1.

Because, cosine-weighted sampling is used, pdf(x) is selected as ndotW/PI. Thus, final form of MC integration becomes (Li * rho).

Surface Area Sampling

For Spherical Surface Area calculations formula, Global Illumination Compendium is used.

For picking a point on the sphere:

Picking a point on the sphere (selectedPoint):

x = Cx + ( 2r * cos(2*PI*u1) * sqrt( u2(1-u2) )
y = Cy + ( 2r * sin(2*PI*u1) * sqrt( u2(1-u2) )
z = Cz + r(1 - 2u2)

where Cx,y,z is the center of sphere, r is radius of the sphere, and u1,2 are random points.

Getting the direction (in world coordinates):

direction = selectedPoint - firstIntersectionPoint

pdf(x) = 1 / 4*PI*r^2 where r is radius of the sphere (1 / Surface area of sphere)

f(x) = Li * BRDF(P/M_PI) * n dot W * nL dot WL) / PDF * d^2 ] where nL dot wL is the angle between the selected direction, and light source normal, and d^2 is the distance between intersection point, and selected point on the light.

Importance Sampling According to Solid Angle

For coding spherical light importance sampling implementation according to solid anglePBRT 2nd edition book, Section 14.6 Sampling Light Sources [720,722] pages, and PBRT v2 source code are referenced.

Algortihm

  1. Get direction: Needs random values, center of the sphere (light), normal at the intersection point, cosThetamax (needs radius of the sphere (light), and the coordinate system (needs vector between intersection point, and center of the sphere (light) for aligning the cone with the light)
  2. Sample the ray
  3. Calculate visibility
  4. Calculate contrution Li * brdf * cosTheta / pdf(x)


Path Tracing

Implicit path tracing with uniform solid angle sampling and biased path-length termination: Follow normal ray tracing procedure until finding an intersection. If there is no intersection, return background color. If the object is light, directly return light color. Otherwise, call path tracing integrator, and return calculated result. Shadow rays, and checking whether the object in shadow or not will be handle during the MC process.

Algorithm:

  1. Pick a direction from the point by using unit sphere sampling
  2. Calculate contribution at the point P
  3. Shoot the ray
  4. If the ray hits the light, returns Le value of the light. If the ray hits another object, call yourself from the latest point you hit by increasing the path length. If there is no intersection (which means ray goes out of the scene), return background color

Explicit path tracing with surface area sampling, and Russian roulette path termination: Follow normal ray tracing procedure until finding an intersection. If there is no intersection, return background color. If the object is light, directly return light color. Otherwise, call path tracing integrator, and return calculated result. Shadow rays, and checking whether the object in shadow or not will be handle during the MC process.

Algorithm

  1. Calculate light contribution by surface area sampling (Don’t forget to check visibility of the selected point on the sphere)
  2. Check for the path length termination
  3. If there is no reason to terminate, calculate contribution of this point.
  4. Pick a direction by using cosine-weighted sampling for the next point of the path, and shoot the ray
  5. If the ray hits the light, returns color. If the ray hits another object, call yourself from the latest point you hit.


Some of the Results

Diffuse Shading with an OBJ file, and 2 spheres

Diffuse Shading with an OBJ file, and 2 spheres

Diffuse Shading with Spheres

MC Integration with Cosine Weighted Sampling

MC Integration with Cosine Weighted Sampling

Implicit Path Tracer with 0 bounces

Implicit Path Tracer with 0 bounces

Implicit Path Tracer with 3 bounces

Implicit Path Tracer with 3 bounces

Implicit Path Tracer with 25 bounces

Implicit Path Tracer with 25 bounces

Explicit Path Tracer with 50 Samples

Explicit Path Tracer with 50 Samples

Explicit Path Tracer with 5000 Samples

Explicit Path Tracer with 5000 Samples

References