Euclid
Geometry Processing and Shape Analysis in C++
RayTracer.h
1 
9 #pragma once
10 
11 #include <cmath>
12 #include <limits>
13 #include <vector>
14 
15 #include <embree3/rtcore.h>
16 #include <Euclid/Render/RenderCore.h>
17 
18 namespace Euclid
19 {
25 class RayCamera : public Camera
26 {
27 public:
34  struct Film
35  {
36  float width = 0.0f;
37  float height = 0.0f;
38  };
39 
40 public:
44  RayCamera() = default;
45 
54  RayCamera(const Vec3& position,
55  const Vec3& focus,
56  const Vec3& up,
57  float tnear,
58  float tfar);
59 
60  virtual ~RayCamera() = default;
61 
67  virtual RTCRayHit gen_ray(float s, float t) const = 0;
68 
69 public:
76 };
77 
83 class PerspRayCamera : public RayCamera
84 {
85 public:
89  PerspRayCamera() = default;
90 
105  PerspRayCamera(const Vec3& position,
106  const Vec3& focus,
107  const Vec3& up,
108  float vfov,
109  float aspect,
110  float tnear,
111  float tfar);
112 
128  PerspRayCamera(const Vec3& position,
129  const Vec3& focus,
130  const Vec3& up,
131  float vfov,
132  unsigned width,
133  unsigned height,
134  float tnear,
135  float tfar);
136 
137  ~PerspRayCamera();
138 
142  void set_aspect(float aspect);
143 
147  void set_aspect(unsigned width, unsigned height);
148 
152  void set_fov(float vfov);
153 
159  RTCRayHit gen_ray(float s, float t) const override;
160 };
161 
167 class OrthoRayCamera : public RayCamera
168 {
169 public:
173  OrthoRayCamera() = default;
174 
188  OrthoRayCamera(const Vec3& position,
189  const Vec3& focus,
190  const Vec3& up,
191  float xextent,
192  float yextent,
193  float tnear,
194  float tfar);
195 
196  ~OrthoRayCamera();
197 
201  void set_extent(float xextent, float yextent);
202 
208  RTCRayHit gen_ray(float s, float t) const override;
209 };
210 
216 {
217 public:
222  explicit RayTracer(int threads = 0);
223 
224  ~RayTracer();
225 
242  void attach_geometry_buffers(const std::vector<float>& positions,
243  const std::vector<unsigned>& indices);
244 
264  void attach_color_buffer(const std::vector<float>* colors,
265  bool vertex_color = false);
266 
276  void attach_face_mask_buffer(const std::vector<uint8_t>* mask);
277 
281  void release_buffers();
282 
286  void set_material(const Material& material);
287 
291  void set_background(const Eigen::Ref<const Eigen::Array3f>& color);
292 
296  void set_background(float r, float g, float b);
297 
301  void enable_light(bool on);
302 
315  void render_shaded(std::vector<uint8_t>& pixels,
316  const RayCamera& camera,
317  int width,
318  int height,
319  bool interleaved = true);
320 
335  void render_shaded(std::vector<uint8_t>& pixels,
336  const RayCamera& camera,
337  int width,
338  int height,
339  int samples,
340  bool interleaved = true);
341 
352  void render_depth(std::vector<uint8_t>& pixels,
353  const RayCamera& camera,
354  int width,
355  int height);
356 
367  void render_depth(std::vector<float>& values,
368  const RayCamera& camera,
369  int width,
370  int height);
371 
379  void render_silhouette(std::vector<uint8_t>& pixels,
380  const RayCamera& camera,
381  int width,
382  int height);
383 
401  void render_index(std::vector<uint8_t>& pixels,
402  const RayCamera& camera,
403  int width,
404  int height,
405  bool interleaved = true);
406 
418  void render_index(std::vector<uint32_t>& indices,
419  const RayCamera& camera,
420  int width,
421  int height);
422 
423 private:
427  std::function<Eigen::Array3f(const RTCHit&)> _select_diffuse_color();
428 
432  Eigen::Array3f _diffuse_material(const RTCHit& hit);
433 
437  Eigen::Array3f _diffuse_face_color(const RTCHit& hit);
438 
442  Eigen::Array3f _diffuse_vertex_color(const RTCHit& hit);
443 
444 private:
445  Material _material;
446  Eigen::Array3f _background = Eigen::Array3f::Zero();
447  RTCDevice _device;
448  RTCScene _scene;
449  RTCGeometry _geometry;
450  const std::vector<float>* _colors = nullptr;
451  const std::vector<uint8_t>* _face_mask = nullptr;
452  int _geom_id = -1;
453  bool _vertex_color = false;
454  bool _lighting = true;
455 };
456 
458 } // namespace Euclid
459 
460 #include "src/RayTracer.cpp"
float tnear
The near plane.
Definition: RenderCore.h:100
Film film
The film plane.
Definition: RayTracer.h:75
A RayCamera using orthographic projection.
Definition: RayTracer.h:167
float tfar
The far plane.
Definition: RenderCore.h:105
Definition: AABB.h:6
virtual RTCRayHit gen_ray(float s, float t) const =0
Generate an embree rayhit structure.
A simple Lambertian material model.
Definition: RenderCore.h:154
A basic positionable camera model.
Definition: RenderCore.h:17
A RayCamera using perspective projection.
Definition: RayTracer.h:83
A simple ray tracer.
Definition: RayTracer.h:215
The film plane of a camera.
Definition: RayTracer.h:34
A camera model used for ray tracing.
Definition: RayTracer.h:25
RayCamera()=default
Create a RayCamera.