Euphoria
raytracer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "base/vec3.h"
4 #include "base/ray.h"
5 #include "base/range.h"
6 #include "core/sphere.h"
7 #include "base/rgb.h"
8 
9 #include <memory>
10 
11 #include <optional>
12 
13 
14 namespace eu::core
15 {
16  struct Image;
17 
18  namespace raytracer
19  {
20  struct Collision;
21 
22 
24  {
27  };
28 
29 
30  struct Material
31  {
32  Material() = default;
33  virtual ~Material() = default;
34 
35  Material(const Material& other) = delete;
36  void operator=(const Material&) = delete;
37  Material(Material&& other) = delete;
38  void operator=(Material&&) = delete;
39 
40  [[nodiscard]] virtual std::optional<ScatterResult>
42  (
43  const UnitRay3f& ray,
44  const Collision& hit,
45  Random* random
46  ) = 0;
47  };
48 
49 
50  struct Collision
51  {
52  float ray_distance;
55  std::shared_ptr<raytracer::Material> material;
56 
57  Collision
58  (
59  float aray_distance,
60  const vec3f& aposition,
61  const unit3f& anormal,
62  std::shared_ptr<raytracer::Material> amaterial
63  );
64  };
65 
66 
67  struct Object
68  {
69  Object() = default;
70  virtual ~Object() = default;
71 
72  Object(const Object&) = delete;
73  Object(Object&&) = delete;
74  void operator=(const Object&) = delete;
75  void operator=(Object&&) = delete;
76 
77  [[nodiscard]] virtual std::optional<Collision>
78  get_collision(const UnitRay3f& ray, const Range<float>& range) const = 0;
79  };
80 
81 
82  std::shared_ptr<Object> create_sphere
83  (
84  const Sphere& sphere,
85  const vec3f& position,
86  std::shared_ptr<Material> material
87  );
88 
89 
90  std::shared_ptr<Material> create_diffuse_material
91  (
92  const Rgb& albedo
93  );
94 
95 
96  std::shared_ptr<Material> create_metal_material
97  (
98  const Rgb& albedo,
99  // 0-1, 0=clear
100  float fuzz
101  );
102 
103 
104  std::shared_ptr<Material> create_dielectric_material
105  (
106  const Rgb& albedo,
107  float refractive_index
108  );
109 
110 
111  struct Scene
112  {
113  std::vector<std::shared_ptr<Object>> objects;
114 
115  [[nodiscard]] std::optional<Collision>
116  get_collision(const UnitRay3f& ray, const Range<float>& range) const;
117  };
118 
119 
120  void
121  raytrace(Image* image, const raytracer::Scene& scene, int number_of_samples);
122  }
123 
124 }
125 
std::shared_ptr< Material > create_metal_material(const Rgb &albedo, float fuzz)
Definition: raytracer.cc:342
std::shared_ptr< Object > create_sphere(const Sphere &sphere, const vec3f &position, std::shared_ptr< raytracer::Material > material)
Definition: raytracer.cc:81
void raytrace(Image *aimage, const raytracer::Scene &scene, int number_of_samples)
Definition: raytracer.cc:466
std::shared_ptr< Material > create_dielectric_material(const Rgb &albedo, float refractive_index)
Definition: raytracer.cc:357
std::shared_ptr< Material > create_diffuse_material(const Rgb &albedo)
Definition: raytracer.cc:329
WEL512 Random Number Generator.
Definition: random.h:21
Definition: rgb.h:62
Collision(float aray_distance, const vec3f &aposition, const unit3f &anormal, std::shared_ptr< raytracer::Material > amaterial)
Definition: raytracer.cc:18
std::shared_ptr< raytracer::Material > material
Definition: raytracer.h:55
virtual std::optional< ScatterResult > get_scattered(const UnitRay3f &ray, const Collision &hit, Random *random)=0
virtual ~Material()=default
Material(const Material &other)=delete
void operator=(const Material &)=delete
Material(Material &&other)=delete
void operator=(Material &&)=delete
Object(const Object &)=delete
Object(Object &&)=delete
void operator=(Object &&)=delete
void operator=(const Object &)=delete
virtual std::optional< Collision > get_collision(const UnitRay3f &ray, const Range< float > &range) const =0
virtual ~Object()=default
std::vector< std::shared_ptr< Object > > objects
Definition: raytracer.h:113
std::optional< Collision > get_collision(const UnitRay3f &ray, const Range< float > &range) const
Definition: raytracer.cc:97
Definition: vec3.h:48