marksurnin/computer-graphics-nyuad

Iterative Ray Tracing algorithm

Closed this issue · 1 comments

This issue is created to address the problem of converting a recursive ray tracing algorithm to an iterative one. GLSL for WebGL does not support recursive function calls.

Definitions

  • n = Ray Tracing depth: 0 for the screen, 1 for the first object hit by ray, 2 for the object hit by reflected ray...
  • c_i = color of object that was hit by ray at depth i
  • r_i = reflectivity of object that was hit by ray at depth i

Recursive Ray Tracing

We have a recursive trace function in JavaScript:

//RayTracerStatic.js line 59

function trace(ray, depth){

  var color = background_color; 
  var h = hit(ray);

  if(h.obj!==null){
    var normal = computeNormal(h.obj, h.point); 
    var material = Materials[h.obj.material_id];
    var viewDir = scale(-1, ray.d);
    color = shade(h.point, normal, viewDir, material);

    if(depth === 0) return color;
  
    var reflected_dir = add(ray.d, scale(-2*dot(ray.d, normal),normal)); 
    var reflected_ray = {e:h.point, d:reflected_dir};
    var reflected_col = trace( reflected_ray, depth-1);
    color = add(color, scale(material.reflectivity, reflected_col));
  }

  return color;
}

Simplifying the problem

Note the role of the last few lines: they recursively calculate the color of the object hit by a reflected ray, multiply reflectivity and add it as a color component of the current object.

    var reflected_col = trace( reflected_ray, depth-1);
    color = add(color, scale(material.reflectivity, reflected_col));

If we simplify this algorithm into a simple mathematical formula, it is the following:

c_0 
= c_1 + r_1 * (c_2 + r_2 * (c_3 + r_3 * (...( c_n ))))
= (c_1 * 1) + (c_2 * r_1) + (c_3 * r_1 * r_2) + ... + (c_n * r_1 * r_2 * ... * r_n-1)

The second expression of c_0 can be evaluated using iteration. We iterate calculating c_i. We keep track of the product of all r_i.

Iterative Ray Tracing

The following is a demonstrative JavaScript code of the above method. The general outline is the same, except that we removed recursion. We are keeping track of the product of all reflectivities. Refer to the comments in the code:

function trace(ray, depth){

  var finalColor = vec3(0,0,0);
  var noHit = true;
  var r = 1;

  for (var i = 0; i < depth; i++) {
    var h = hit(ray);
    if(h.obj!==null){

      noHit = false;

      var normal = computeNormal(h.obj, h.point); 
      var material = Materials[h.obj.material_id];
      var viewDir = scale(-1, ray.d);
      color = shade(h.point, normal, viewDir, material);

      var reflected_dir = add(ray.d, scale(-2*dot(ray.d, normal),normal)); 
      ray = {e:h.point, d:reflected_dir};             // now we consider the reflected ray

      finalColor = add(finalColor, scale(r, color));  // c += r x c_i
      r *= material.reflectivity;                     // r *= r_i
    }
  }

  return noHit ? background_color : finalColor;           // if ray never hit any objects, return bg color
}

To do

Now we have to translate this into GLSL.

Resolved in commit 7d1d84e