Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //cpp file
- #include "raytracers/ray-tracer-v5.h"
- using namespace imaging;
- using namespace math;
- using namespace raytracer;
- TraceResult raytracer::raytracers::_private_::RayTracerV5::trace(const Scene& scene, const Ray& ray) const
- {
- return trace(scene, ray, 1);
- }
- TraceResult raytracer::raytracers::_private_::RayTracerV5::trace(const Scene& scene, const Ray& ray, double weight) const {
- if (weight > 0.01)
- {
- Hit hit;
- // Ask the scene for the first positive hit, i.e. the closest hit in front of the eye
- // If there's a hit, find_first_positive_hit returns true and updates the hit object with information about the hit
- if (scene.root->find_first_positive_hit(ray, &hit))
- {
- //base color is black
- Color result = colors::black();
- // There's been a hit
- // Fill in TraceResult object with information about the trace
- Material hit_material = hit.material;
- // color of the material on a given point
- MaterialProperties properties = hit.material->at(hit.local_position);
- result += computeAmbient(properties);
- // process_lights and add color to result
- result += process_lights(scene, properties, hit, ray);
- // compute reflection and add color to result
- result += compute_reflection(scene, properties, hit, ray, weight);
- // The hit object contains the group id, just copy it (group ids are important for edge detection)
- unsigned group_id = hit.group_id;
- // The t-value indicates where the ray/scene intersection took place.
- // You can use ray.at(t) to find the xyz-coordinates in space.
- double t = hit.t;
- // Group all this data into a TraceResult object.
- return TraceResult(result, group_id, ray, t);
- }
- else
- {
- return TraceResult::no_hit(ray);
- }
- }
- else
- {
- return TraceResult::no_hit(ray);
- }
- }
- Color raytracer::raytracers::_private_::RayTracerV5::compute_reflection(const Scene& scene, const MaterialProperties& properties, const Hit& hit, const math::Ray& ray, double weight) const {
- Color result = colors::black();
- if ( properties.reflectivity > 0 ) {
- auto reflected_direction = ray.direction.reflect_by(hit.normal);
- auto reflected_origin = hit.position + 0.001 * reflected_direction;
- Ray newRay = Ray(reflected_origin, reflected_direction);
- Color reflected_color = trace(scene, newRay, weight*properties.reflectivity).color;
- result += properties.reflectivity * reflected_color;
- }
- return result;
- }
- raytracer::RayTracer raytracer::raytracers::v5()
- {
- return raytracer::RayTracer(std::make_shared<raytracer::raytracers::_private_::RayTracerV5>());
- }
- //header file
- #pragma once
- #include "raytracers/ray-tracer-v4.h"
- #include <memory>
- namespace raytracer
- {
- namespace raytracers
- {
- namespace _private_
- {
- class RayTracerV5 : public RayTracerV4
- {
- public:
- TraceResult trace(const Scene&, const math::Ray&) const override;
- TraceResult trace(const Scene&, const math::Ray&, double) const;
- imaging::Color compute_reflection(const Scene&, const MaterialProperties&, const Hit&, const math::Ray&, double) const;
- };
- }
- /// <summary>
- /// Creates simplest ray tracer.
- /// </summary>
- RayTracer v5();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement