GithubHelp home page GithubHelp logo

computer-graphics-nyuad's People

Contributors

marksurnin avatar simonseo avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

simonseo

computer-graphics-nyuad's Issues

Iterative Ray Tracing algorithm

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.