Arty
scene.h
1 #ifndef SCENE_H
2 #define SCENE_H
3 
4 #include <vector>
5 #include <memory>
6 #include <string>
7 
8 #include "lights.h"
9 #include "materials.h"
10 #include "cameras.h"
11 #include "textures.h"
12 #include "float3.h"
13 #include "float2.h"
14 #include "bvh.h"
15 
16 struct Scene {
17  template <typename T>
18  using unique_vector = std::vector<std::unique_ptr<T>>;
19 
20  // Camera and viewport
21  std::unique_ptr<Camera> camera;
22  size_t width, height;
23 
24  // Shading data
25  unique_vector<Bsdf> bsdfs;
26  unique_vector<Light> lights;
27  unique_vector<Texture> textures;
28  std::vector<Material> materials;
29 
30  // Traversal data
31  Bvh bvh;
32 
33  // Mesh data
34  std::vector<float3> vertices;
35  std::vector<float2> texcoords;
36  std::vector<float3> normals;
37  std::vector<uint32_t> indices;
38 
39  std::vector<float3> face_normals;
40 
43  Hit intersect(const Ray& ray) const {
44  Hit hit;
45  bvh.traverse(ray, hit);
46  return hit;
47  }
48 
50  bool occluded(const Ray& ray) const {
51  Hit hit;
52  bvh.traverse(ray, hit, true);
53  return hit.tri >= 0;
54  }
55 
57  const Material& material(const Hit& hit) const {
58  assert(hit.tri >= 0);
59  return materials[indices[hit.tri * 4 + 3]];
60  }
61 
63  SurfaceParams surface_params(const Ray& ray, const Hit& hit) const {
64  assert(hit.tri >= 0);
65  auto i0 = indices[hit.tri * 4 + 0];
66  auto i1 = indices[hit.tri * 4 + 1];
67  auto i2 = indices[hit.tri * 4 + 2];
68 
69  auto& fn = face_normals[hit.tri];
70  auto n = normalize(lerp(normals[i0], normals[i1], normals[i2], hit.u, hit.v));
71  auto uv = lerp(texcoords[i0], texcoords[i1], texcoords[i2], hit.u, hit.v);
72 
73  // Compute the surface parameters, and make sure the face and per-vertex normal agree
74  SurfaceParams surf;
75  surf.entering = dot(ray.dir, fn) <= 0;
76  surf.face_normal = surf.entering ? fn : -fn;
77  surf.point = ray.org + ray.dir * hit.t;
78  surf.coords = gen_local_coords(dot(ray.dir, n) <= 0 ? n : -n);
79  surf.uv = uv;
80  return surf;
81  }
82 };
83 
85 bool load_scene(const std::string& config, Scene& scene);
86 
87 #endif // SCENE_H
bool occluded(const Ray &ray) const
Returns true if the given ray hits the scene.
Definition: scene.h:50
float3 org
Origin of the ray.
Definition: intersect.h:12
SurfaceParams surface_params(const Ray &ray, const Hit &hit) const
Returns the surface parameters for a hit point.
Definition: scene.h:63
Definition: scene.h:16
float3 dir
Direction of the ray.
Definition: intersect.h:14
LocalCoords coords
Local coordinates at the hit point, w.r.t shading normal.
Definition: materials.h:31
bool entering
True if entering the surface.
Definition: materials.h:27
float3 face_normal
Geometric normal.
Definition: materials.h:30
Bounding Volume Hierarchy.
Definition: bvh.h:12
A material is a combination of a BSDF and an optional light emitter.
Definition: materials.h:38
int32_t tri
Triangle index, or -1 if no intersection was found.
Definition: intersect.h:29
float3 point
Hit point in world coordinates.
Definition: materials.h:28
float t
Time of intersection.
Definition: intersect.h:30
float u
First barycentric coordinate.
Definition: intersect.h:31
float2 uv
Texture coordinates.
Definition: materials.h:29
Ray-triangle hit information.
Definition: intersect.h:28
void traverse(const Ray &ray, Hit &hit, bool any=false) const
Traverses the BVH in order to find the closest intersection, or any intersection if &#39;any&#39; is set...
float v
Second barycentric coordinate.
Definition: intersect.h:32
Ray defined as org + t * dir, with t in [tmin, tmax].
Definition: intersect.h:11
Surface parameters for a given point.
Definition: materials.h:26
Hit intersect(const Ray &ray) const
Definition: scene.h:43
const Material & material(const Hit &hit) const
Returns the material associated with a hit point.
Definition: scene.h:57