ToyPathTracer icon indicating copy to clipboard operation
ToyPathTracer copied to clipboard

GPU Tracer

Open D-K-E opened this issue 4 years ago • 0 comments

Hi Aras, I read about your post about adapting the ray_color function of P. Shirley to GPU.

I thought implementing it in the following manner also fits the underlying equation:

vec3 ray_color(in Ray r, in Scene scene, in vec3 background, int depth) {
  //
  // partly taken from here
  // http://aras-p.info/blog/2018/04/03/Daily-Pathtracer-Part-5-Metal-GPU/
  // by Aras Pranckevičius
  Ray r_in;
  r_in.origin = r.origin;
  r_in.direction = r.direction;
  vec3 result = vec3(0);
  vec3 color = vec3(1);

  while (true) {
    if (depth <= 0) {
      return vec3(0);
    }
    HitRecord rec;
    if (hit(scene, r_in, 0.001, INFINITY, rec)) {
      Ray r_out;
      vec3 atten;
      vec3 emitColor = emit(rec.mat_ptr, rec.u, rec.v, rec.point);
      if (scatter(rec.mat_ptr, r_in, rec, atten, r_out) == true) {
        r_in = r_out;
        result += (color * emitColor);
        color *= atten;
        depth--;
      } else {
        result += (color * emitColor);
        return result;
      }
    } else {
      result += (color * background);
      return result;
    }
  }
}

I thought, I was being smart, but then I met this guy.

D-K-E avatar May 31 '20 13:05 D-K-E