1
2//
3// This source file is part of appleseed.
4// Visit http://appleseedhq.net/ for additional information and resources.
5//
6// This software is released under the MIT license.
7//
8// Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9// Copyright (c) 2014-2017 Francois Beaune, The appleseedhq Organization
10//
11// Permission is hereby granted, free of charge, to any person obtaining a copy
12// of this software and associated documentation files (the "Software"), to deal
13// in the Software without restriction, including without limitation the rights
14// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15// copies of the Software, and to permit persons to whom the Software is
16// furnished to do so, subject to the following conditions:
17//
18// The above copyright notice and this permission notice shall be included in
19// all copies or substantial portions of the Software.
20//
21// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27// THE SOFTWARE.
28//
29
30#ifndef APPLESEED_RENDERER_MODELING_SCENE_OBJECTINSTANCE_H
31#define APPLESEED_RENDERER_MODELING_SCENE_OBJECTINSTANCE_H
32
33// appleseed.renderer headers.
34#include "renderer/global/globaltypes.h"
35#include "renderer/modeling/entity/entity.h"
36#include "renderer/modeling/scene/containers.h"
37
38// appleseed.foundation headers.
39#include "foundation/math/transform.h"
40#include "foundation/platform/compiler.h"
41#include "foundation/platform/types.h"
42#include "foundation/utility/api/apiarray.h"
43#include "foundation/utility/autoreleaseptr.h"
44#include "foundation/utility/uid.h"
45
46// appleseed.main headers.
47#include "main/dllsymbol.h"
48
49// Standard headers.
50#include <cassert>
51#include <cstddef>
52
53// Forward declarations.
54namespace foundation { class DictionaryArray; }
55namespace foundation { class IAbortSwitch; }
56namespace foundation { class StringDictionary; }
57namespace renderer { class Assembly; }
58namespace renderer { class Material; }
59namespace renderer { class Object; }
60namespace renderer { class OnFrameBeginRecorder; }
61namespace renderer { class ParamArray; }
62namespace renderer { class Project; }
63
64namespace renderer
65{
66
67//
68// An array of materials.
69//
70
71APPLESEED_DECLARE_APIARRAY(MaterialArray, const Material*);
72
73// Return true if at least one material in the array emits light.
74bool has_emitting_materials(const MaterialArray& materials);
75
76// Return true if at least one material in the array has an alpha map set.
77bool uses_alpha_mapping(const MaterialArray& materials);
78
79
80//
81// An instance of an object.
82//
83
84class APPLESEED_DLLSYMBOL ObjectInstance
85 : public Entity
86{
87 public:
88 // Return the unique ID of this class of entities.
89 static foundation::UniqueID get_class_uid();
90
91 // Delete this instance.
92 virtual void release() APPLESEED_OVERRIDE;
93
94 // Compute and return the unique signature of this instance.
95 virtual foundation::uint64 compute_signature() const;
96
97 // Return the name of the instantiated object.
98 const char* get_object_name() const;
99
100 // Return the transform of this instance.
101 const foundation::Transformd& get_transform() const;
102
103 // Return true if the transform of this instance swaps handedness.
104 bool transform_swaps_handedness() const;
105
106 // Return the visibility flags of this instance.
107 foundation::uint32 get_vis_flags() const;
108
109 // Return the medium priority of this instance.
110 foundation::uint8 get_medium_priority() const;
111
112 enum RayBiasMethod
113 {
114 RayBiasMethodNone, // no ray bias for this object instance
115 RayBiasMethodNormal, // shift the ray's origin along the surface's geometric normal
116 RayBiasMethodIncomingDirection, // shift the ray's origin along the incoming ray's direction
117 RayBiasMethodOutgoingDirection // shift the ray's origin along the outgoing ray's direction
118 };
119
120 // Return the ray bias settings. The bias distance is expressed in world space.
121 RayBiasMethod get_ray_bias_method() const;
122 double get_ray_bias_distance() const;
123
124 // Find the object bound to this instance.
125 Object* find_object() const;
126
127 // Compute the parent space bounding box of the instance.
128 GAABB3 compute_parent_bbox() const;
129
130 // Sides of this object instance's surface.
131 enum Side
132 {
133 FrontSide = 1 << 0,
134 BackSide = 1 << 1,
135 BothSides = FrontSide | BackSide
136 };
137
138 // Clear all material assignments.
139 void clear_front_materials();
140 void clear_back_materials();
141
142 // Assign a material to a given slot.
143 void assign_material(
144 const char* slot,
145 const Side side,
146 const char* name);
147
148 // Unassign a material from a given slot.
149 void unassign_material(
150 const char* slot,
151 const Side side);
152
153 // Return the slot-to-material mappings of this instance.
154 foundation::StringDictionary& get_front_material_mappings() const;
155 foundation::StringDictionary& get_back_material_mappings() const;
156
157 // Get the name of the material bound to a given primitive of this instance.
158 const char* get_material_name(const size_t pa_index, const Side side) const;
159
160 // Object binding.
161 void unbind_object();
162 void bind_object(const ObjectContainer& objects);
163 void check_object() const;
164
165 // Material binding.
166 void unbind_materials();
167 void bind_materials(const MaterialContainer& materials);
168 void check_materials() const;
169
170 // Return the object bound to this instance.
171 Object& get_object() const;
172
173 // Return the materials bound to this instance.
174 const MaterialArray& get_front_materials() const;
175 const MaterialArray& get_back_materials() const;
176
177 // Return true if at least one of the material referenced by this instance has an alpha map set.
178 bool uses_alpha_mapping() const;
179
180 // This method is called once before rendering each frame.
181 // Returns true on success, false otherwise.
182 virtual bool on_frame_begin(
183 const Project& project,
184 const BaseGroup* parent,
185 OnFrameBeginRecorder& recorder,
186 foundation::IAbortSwitch* abort_switch = 0) APPLESEED_OVERRIDE;
187
188 private:
189 friend class ObjectInstanceFactory;
190
191 struct Impl;
192 Impl* impl;
193
194 foundation::uint32 m_vis_flags;
195 foundation::uint8 m_medium_priority;
196 RayBiasMethod m_ray_bias_method;
197 double m_ray_bias_distance;
198 bool m_transform_swaps_handedness;
199
200 Object* m_object;
201 MaterialArray m_front_materials;
202 MaterialArray m_back_materials;
203
204 // Constructor.
205 ObjectInstance(
206 const char* name,
207 const ParamArray& params,
208 const char* object_name,
209 const foundation::Transformd& transform,
210 const foundation::StringDictionary& front_material_mappings,
211 const foundation::StringDictionary& back_material_mappings);
212
213 // Destructor.
214 ~ObjectInstance();
215};
216
217
218//
219// Object instance factory.
220//
221
222class APPLESEED_DLLSYMBOL ObjectInstanceFactory
223{
224 public:
225 // Return a set of input metadata for object instance entities.
226 static foundation::DictionaryArray get_input_metadata();
227
228 // Create a new object instance.
229 static foundation::auto_release_ptr<ObjectInstance> create(
230 const char* name,
231 const ParamArray& params,
232 const char* object_name,
233 const foundation::Transformd& transform,
234 const foundation::StringDictionary& front_material_mappings,
235 const foundation::StringDictionary& back_material_mappings = foundation::StringDictionary());
236};
237
238
239//
240// ObjectInstance class implementation.
241//
242
243inline bool ObjectInstance::transform_swaps_handedness() const
244{
245 return m_transform_swaps_handedness;
246}
247
248inline foundation::uint32 ObjectInstance::get_vis_flags() const
249{
250 return m_vis_flags;
251}
252
253inline foundation::uint8 ObjectInstance::get_medium_priority() const
254{
255 return m_medium_priority;
256}
257
258inline ObjectInstance::RayBiasMethod ObjectInstance::get_ray_bias_method() const
259{
260 return m_ray_bias_method;
261}
262
263inline double ObjectInstance::get_ray_bias_distance() const
264{
265 return m_ray_bias_distance;
266}
267
268inline Object& ObjectInstance::get_object() const
269{
270 assert(m_object);
271 return *m_object;
272}
273
274inline const MaterialArray& ObjectInstance::get_front_materials() const
275{
276 return m_front_materials;
277}
278
279inline const MaterialArray& ObjectInstance::get_back_materials() const
280{
281 return m_back_materials;
282}
283
284} // namespace renderer
285
286#endif // !APPLESEED_RENDERER_MODELING_SCENE_OBJECTINSTANCE_H
287