Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. static vec4f trace_naive(const ptr::scene* scene, const ray3f& ray_,
  2.     rng_state& rng, const trace_params& params) {
  3.   // YOUR CODE GOES HERE ------------------------------------------------------
  4.   // initialize
  5.   auto radiance = zero3f;
  6.   auto weight   = vec3f{1, 1, 1};
  7.   auto ray      = ray_;
  8.   auto hit      = false;
  9.  
  10.   // trace  path
  11.   for (auto bounce : range(params.bounces)) {
  12.     // intersect next point
  13.     auto intersection = intersect_scene_bvh(scene, ray);
  14.     if (!intersection.hit) {
  15.       radiance += weight * eval_environment(scene, ray);
  16.       break;
  17.     }
  18.  
  19.     // prepare shading point
  20.     auto outgoing = -ray.d;
  21.     auto object   = scene->objects[intersection.object];
  22.     auto element  = intersection.element;
  23.     auto uv       = intersection.uv;
  24.     auto position = eval_position(object, element, uv);
  25.     auto normal   = eval_shading_normal(object, element, uv, outgoing);
  26.     auto emission = eval_emission(object, element, uv, normal, outgoing);
  27.     auto brdf     = eval_brdf(object, element, uv, normal, outgoing);
  28.  
  29.     // handle opacity
  30.     if (brdf.opacity < 1 && rand1f(rng) >= brdf.opacity) {
  31.       ray = {position + ray.d * 1e-2f, ray.d};
  32.       bounce -= 1;
  33.       continue;
  34.     }
  35.     hit = true;
  36.  
  37.     // accumulate emission
  38.     radiance += weight * eval_emission(emission, normal, outgoing);
  39.  
  40.     vec3f incoming = zero3f; //initialize incoming
  41.     // next direction
  42.     if (!is_delta(brdf)) {  // sample smooth brdfs (fold cos into f)
  43.       incoming = sample_brdfcos(brdf, normal, outgoing, rand1f(rng), rand2f(rng));  // incoming
  44.       weight *= eval_brdfcos(brdf, normal, incoming, outgoing) /
  45.                 sample_brdfcos_pdf(brdf, normal, incoming, outgoing);
  46.     } else {                                        // sample sharp brdfs
  47.       incoming = sample_delta(brdf, normal, outgoing, rand1f(rng));  // incoming
  48.       weight *= eval_delta(brdf, normal, incoming, outgoing) /
  49.                 sample_delta_pdf(brdf, normal, incoming, outgoing);
  50.     }
  51.  
  52.     // check weight
  53.     if (weight == zero3f || !isfinite(weight)) break;
  54.  
  55.     // russian roulette
  56.     if (max(weight) < 1 && bounce > 3) {
  57.       auto rr_prob = max((float)0.05, 1 - max(weight));
  58.       if (rand1f(rng) > rr_prob) break;
  59.       weight *= 1 / rr_prob;
  60.     }
  61.  
  62.     // setup next iteration
  63.     ray = {position, incoming};
  64.   }
  65.  
  66.   return {radiance, hit ? 1.0f : 0.0f};
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement