Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. float pi = 3.14159265358979323846;
  2.  
  3. // Raytrace renderer.
  4. static vec4f trace_raytrace(const rtr::scene* scene, const ray3f& ray,
  5.     int bounce, rng_state& rng, const trace_params& params) {
  6.  
  7.   // intersect next point
  8.   auto inter = intersect_scene_bvh(scene, ray);
  9.   if(!inter.hit) {return {eval_environment(scene, ray), 1};}
  10.   // evaluate geometry
  11.   auto& el = inter.element;
  12.   auto& forma = scene -> objects[inter.object];
  13.   auto new_position = transform_point(forma -> frame, eval_position(forma -> shape, el, inter.uv));
  14.   // normal corrections
  15.   auto normal = transform_direction(forma -> frame, eval_normal(forma -> shape, el, inter.uv));
  16.  
  17.   // evaluate material
  18.   auto material = forma -> material;
  19.   auto temp_coord = eval_texcoord(scene -> objects[inter.object] -> shape, el, inter.uv);
  20.   vec2f coord = vec2f{fmod(temp_coord.x, 1), fmod(temp_coord.y, 1)};
  21.   auto color = forma -> material -> color * eval_texture(material -> color_tex, coord);
  22.   auto radiance = forma -> material -> emission;
  23.   auto outgoing = -ray.d;
  24.  
  25.  
  26.   // handle opacity
  27.  
  28.   // accumulate emission
  29.  
  30.   // exit if enough bounces are done
  31.   if (bounce >= params.bounces) {return {radiance, 1};}
  32.  
  33.   // compute indirect illumination
  34.   auto ambient = sample_hemisphere(normal, rand2f(rng));
  35.  
  36.   // transmission -> polished dielectric
  37.   if (material -> transmission) {
  38.     if (rand1f(rng) < mean(fresnel_schlick(vec3f(0.04f), normal, outgoing))) {
  39.       auto incoming = reflect(outgoing, normal);
  40.       radiance += xyz(trace_raytrace(scene, ray3f{new_position, incoming}, ++bounce, rng, params));}
  41.     else {
  42.       auto incoming = -outgoing;
  43.       radiance += color * xyz(trace_raytrace(scene, ray3f{new_position, incoming}, ++bounce, rng, params));
  44.     }
  45.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement