Nori  24
scene.cpp
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Nori is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include <nori/scene.h>
20 #include <nori/bitmap.h>
21 #include <nori/integrator.h>
22 #include <nori/sampler.h>
23 #include <nori/camera.h>
24 #include <nori/emitter.h>
25 
26 NORI_NAMESPACE_BEGIN
27 
29  m_bvh = new BVH();
30 }
31 
33  delete m_bvh;
34  delete m_sampler;
35  delete m_camera;
36  delete m_integrator;
37  for(auto e : m_emitters)
38  delete e;
39  m_emitters.clear();
40 }
41 
43  m_bvh->build();
44 
45  if (!m_integrator)
46  throw NoriException("No integrator was specified!");
47  if (!m_camera)
48  throw NoriException("No camera was specified!");
49 
50  if (!m_sampler) {
51  /* Create a default (independent) sampler */
52  m_sampler = static_cast<Sampler*>(
54  m_sampler->activate();
55  }
56 
57  cout << endl;
58  cout << "Configuration: " << toString() << endl;
59  cout << endl;
60 }
61 
63  switch (obj->getClassType()) {
64  case EMesh: {
65  Shape *mesh = static_cast<Shape *>(obj);
66  m_bvh->addShape(mesh);
67  m_shapes.push_back(mesh);
68  if(mesh->isEmitter())
69  m_emitters.push_back(mesh->getEmitter());
70  }
71  break;
72 
73  case EEmitter:
74  m_emitters.push_back(static_cast<Emitter *>(obj));
75  break;
76 
77  case ESampler:
78  if (m_sampler)
79  throw NoriException("There can only be one sampler per scene!");
80  m_sampler = static_cast<Sampler *>(obj);
81  break;
82 
83  case ECamera:
84  if (m_camera)
85  throw NoriException("There can only be one camera per scene!");
86  m_camera = static_cast<Camera *>(obj);
87  break;
88 
89  case EIntegrator:
90  if (m_integrator)
91  throw NoriException("There can only be one integrator per scene!");
92  m_integrator = static_cast<Integrator *>(obj);
93  break;
94 
95  default:
96  throw NoriException("Scene::addChild(<%s>) is not supported!",
97  classTypeName(obj->getClassType()));
98  }
99 }
100 
101 std::string Scene::toString() const {
102  std::string shapes;
103  for (size_t i=0; i<m_shapes.size(); ++i) {
104  shapes += std::string(" ") + indent(m_shapes[i]->toString(), 2);
105  if (i + 1 < m_shapes.size())
106  shapes += ",";
107  shapes += "\n";
108  }
109 
110  std::string lights;
111  for (size_t i=0; i<m_emitters.size(); ++i) {
112  lights += std::string(" ") + indent(m_emitters[i]->toString(), 2);
113  if (i + 1 < m_emitters.size())
114  lights += ",";
115  lights += "\n";
116  }
117 
118  return tfm::format(
119  "Scene[\n"
120  " integrator = %s,\n"
121  " sampler = %s\n"
122  " camera = %s,\n"
123  " shapes = {\n"
124  " %s }\n"
125  " emitters = {\n"
126  " %s }\n"
127  "]",
128  indent(m_integrator->toString()),
129  indent(m_sampler->toString()),
130  indent(m_camera->toString()),
131  indent(shapes, 2),
132  indent(lights,2)
133  );
134 }
135 
136 NORI_REGISTER_CLASS(Scene, "scene");
137 NORI_NAMESPACE_END
Bounding Volume Hierarchy for fast ray intersection queries.
Definition: bvh.h:43
void build()
Build the BVH.
Definition: bvh.cpp:329
void addShape(Shape *shape)
Register a shape for inclusion in the BVH.
Definition: bvh.cpp:308
Generic camera interface.
Definition: camera.h:35
Superclass of all emitters.
Definition: emitter.h:64
Abstract integrator (i.e. a rendering technique)
Definition: integrator.h:35
Simple exception class, which stores a human-readable error description.
Definition: common.h:148
static NoriObject * createInstance(const std::string &name, const PropertyList &propList)
Construct an instance from the class of the given name.
Definition: object.h:158
Base class of all objects.
Definition: object.h:32
virtual void activate()
Perform some action associated with the object.
Definition: object.h:107
virtual std::string toString() const =0
Return a brief string summary of the instance (for debugging purposes)
static std::string classTypeName(EClassType type)
Turn a class type into a human-readable string.
Definition: object.h:51
virtual EClassType getClassType() const =0
Return the type of object (i.e. Mesh/BSDF/etc.) provided by this instance.
This is an associative container used to supply the constructors of NoriObject subclasses with parame...
Definition: proplist.h:32
Abstract sample generator.
Definition: sampler.h:63
Main scene data structure.
Definition: scene.h:34
virtual void activate() override
Inherited from NoriObject::activate()
Definition: scene.cpp:42
virtual ~Scene()
Release all memory.
Definition: scene.cpp:32
virtual std::string toString() const override
Return a string summary of the scene (for debugging purposes)
Definition: scene.cpp:101
Scene(const PropertyList &)
Construct a new scene object.
Definition: scene.cpp:28
virtual void addChild(NoriObject *obj) override
Add a child object to the scene (meshes, integrators etc.)
Definition: scene.cpp:62
Superclass of all shapes.
Definition: shape.h:97
bool isEmitter() const
Is this mesh an area emitter?
Definition: shape.h:111
Emitter * getEmitter()
Return a pointer to an attached area emitter instance.
Definition: shape.h:114