Advertisement
Guest User

raytracer v5

a guest
Jan 18th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. //cpp file
  2. #include "raytracers/ray-tracer-v5.h"
  3.  
  4.  
  5. using namespace imaging;
  6. using namespace math;
  7. using namespace raytracer;
  8.  
  9. TraceResult raytracer::raytracers::_private_::RayTracerV5::trace(const Scene& scene, const Ray& ray) const
  10. {
  11.     return trace(scene, ray, 1);
  12. }
  13.  
  14. TraceResult raytracer::raytracers::_private_::RayTracerV5::trace(const Scene& scene, const Ray& ray, double weight) const {
  15.  
  16.     if (weight > 0.01)
  17.     {
  18.         Hit hit;
  19.         // Ask the scene for the first positive hit, i.e. the closest hit in front of the eye
  20.         // If there's a hit, find_first_positive_hit returns true and updates the hit object with information about the hit
  21.         if (scene.root->find_first_positive_hit(ray, &hit))
  22.         {
  23.             //base color is black
  24.             Color result = colors::black();
  25.             // There's been a hit
  26.             // Fill in TraceResult object with information about the trace
  27.             Material hit_material = hit.material;
  28.             // color of the material on a given point
  29.             MaterialProperties properties = hit.material->at(hit.local_position);
  30.  
  31.             result += computeAmbient(properties);
  32.             // process_lights and add color to result
  33.             result += process_lights(scene, properties, hit, ray);
  34.             // compute reflection and add color to result
  35.             result += compute_reflection(scene, properties, hit, ray, weight);
  36.             // The hit object contains the group id, just copy it (group ids are important for edge detection)
  37.             unsigned group_id = hit.group_id;
  38.  
  39.             // The t-value indicates where the ray/scene intersection took place.
  40.             // You can use ray.at(t) to find the xyz-coordinates in space.
  41.             double t = hit.t;
  42.  
  43.             // Group all this data into a TraceResult object.
  44.             return TraceResult(result, group_id, ray, t);
  45.         }
  46.         else
  47.         {
  48.             return TraceResult::no_hit(ray);
  49.         }
  50.     }
  51.     else
  52.     {
  53.         return TraceResult::no_hit(ray);
  54.     }
  55. }
  56.  
  57. Color raytracer::raytracers::_private_::RayTracerV5::compute_reflection(const Scene& scene, const MaterialProperties& properties, const Hit& hit, const math::Ray& ray, double weight) const {
  58.     Color result = colors::black();
  59.  
  60.     if ( properties.reflectivity > 0 ) {
  61.         auto reflected_direction = ray.direction.reflect_by(hit.normal);
  62.         auto reflected_origin = hit.position + 0.001 * reflected_direction;
  63.  
  64.         Ray newRay = Ray(reflected_origin, reflected_direction);
  65.  
  66.         Color reflected_color = trace(scene, newRay, weight*properties.reflectivity).color;
  67.         result += properties.reflectivity * reflected_color;
  68.     }
  69.     return result;
  70. }
  71.  
  72. raytracer::RayTracer raytracer::raytracers::v5()
  73. {
  74.     return raytracer::RayTracer(std::make_shared<raytracer::raytracers::_private_::RayTracerV5>());
  75. }
  76.  
  77.  
  78. //header file
  79. #pragma once
  80.  
  81. #include "raytracers/ray-tracer-v4.h"
  82. #include <memory>
  83.  
  84.  
  85. namespace raytracer
  86. {
  87.     namespace raytracers
  88.     {
  89.         namespace _private_
  90.         {
  91.             class RayTracerV5 : public RayTracerV4
  92.             {
  93.             public:
  94.                 TraceResult trace(const Scene&, const math::Ray&) const override;
  95.  
  96.                 TraceResult trace(const Scene&, const math::Ray&, double) const;
  97.                  imaging::Color compute_reflection(const Scene&, const MaterialProperties&, const Hit&, const math::Ray&, double) const;
  98.             };
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Creates simplest ray tracer.
  103.         /// </summary>
  104.         RayTracer v5();
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement