Ocean
Loading...
Searching...
No Matches
Object.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_RENDERING_OBJECT_H
9#define META_OCEAN_RENDERING_OBJECT_H
10
12
13#include "ocean/base/Lock.h"
15
16#include <set>
17
18namespace Ocean
19{
20
21namespace Rendering
22{
23
24// Forward declaration.
25class Engine;
26// Forward declaration.
27class Object;
28
29/**
30 * Definition of a rendering object reference with an internal reference counter.
31 * @see Object.
32 * @ingroup rendering
33 */
35
36/**
37 * Definition of a vector holding rendering object references.
38 * @ingroup rendering
39 */
40using ObjectRefs = std::vector<ObjectRef>;
41
42/**
43 * Definition of a set holding rendering object references.
44 * @ingroup rendering
45 */
46using ObjectRefSet = std::set<ObjectRef>;
47
48/**
49 * This class is the base class for all rendering objects.
50 * @ingroup rendering
51 */
52class OCEAN_RENDERING_EXPORT Object
53{
54 friend class Ocean::ObjectRef<Object>;
55
56 public:
57
58 /**
59 * Definition of different object type.
60 */
61 enum ObjectType : uint32_t
62 {
63 /// Unknown type.
65 /// Absolute transform type.
67 /// Attribute type.
69 /// AttributeSet type.
71 /// Background type.
73 /// Billboard type.
75 /// BitmapFramebuffer type.
77 /// BlendAttribute type.
79 /// Box type.
81 /// Cone type.
83 /// Cylinder type.
85 /// DepthAttribute type.
87 /// DirectionalLight type.
89 /// Framebuffer type.
91 /// FrameTexture2D type.
93 /// Geometry type.
95 /// Group type.
97 /// IndependentPrimitive type.
99 /// LightSource type.
101 /// Lines type.
103 /// Line strips type.
105 /// LOD type.
107 /// Material type.
109 /// Node type.
111 /// Object type.
113 /// ParallelView type.
115 /// PerspectiveView type.
117 /// PhantomAttribute type.
119 /// PointLight type.
121 /// Points type.
123 /// Primitive type.
125 /// PrimitiveAttribute type.
127 /// QuadStrips type.
129 /// Quads type.
131 /// Renderable type.
133 /// Scene type.
135 /// ShaderProgram type.
137 /// Shape type.
139 /// SkyBackground type.
141 /// Sphere type.
143 /// SpotLight type.
145 /// StereoAttribute type.
147 /// StereoView type.
149 /// StripPrimitive type.
151 /// Switch type.
153 /// Text type.
155 /// Texture type.
157 /// Texture2D type.
159 /// MediaTexture2D type.
161 /// TextureFramebuffer type.
163 /// Textures type.
165 /// Transform type.
167 /// TriangleFans type.
169 /// Triangles type.
171 /// TriangleStrips type.
173 /// UndistortedBackground type.
175 /// VertexSet type.
177 /// View type.
179 /// WindowFramebuffer type.
180 TYPE_WINDOW_FRAMEBUFFER
181 };
182
183 /**
184 * Definition of a map holding object ids to reference counters.
185 */
186 using ObjectIdMap = std::unordered_map<ObjectId, unsigned int>;
187
188 public:
189
190 /**
191 * Returns the unique object id of this object.
192 * @return Unique object id
193 */
194 inline ObjectId id() const;
195
196 /**
197 * Returns the name of this object.
198 * @return Object name
199 */
200 inline std::string name() const;
201
202 /**
203 * Sets or changes the name of this object.
204 * @param name New object name
205 */
206 virtual void setName(const std::string& name);
207
208 /**
209 * Returns the name of the owner engine.
210 * @return Owner engine
211 */
212 virtual const std::string& engineName() const = 0;
213
214 /**
215 * Returns the type of this object.
216 * @return Object type
217 */
218 virtual ObjectType type() const;
219
220 /**
221 * Returns all parent objects.
222 * @return Vector holding all parent objects
223 */
225
226 /**
227 * Returns all parent nodes.
228 * @return Set holding all parent nodes
229 */
231
232 /**
233 * Returns descriptive information about the object as string.
234 * The descriptive information can be used during debugging.
235 * @return The object's descriptive information, if any
236 */
237 virtual std::string descriptiveInformation() const;
238
239 /**
240 * Translates an object type to a readable string.
241 * @param objectType The object type to translate
242 * @return The resulting readable string of the object type
243 */
244 static std::string translateObjectType(const ObjectType objectType);
245
246 protected:
247
248 /**
249 * Creates a new object.
250 */
252
253 /**
254 * Disabled copy constructor.
255 * @param object Object which would be copied
256 */
257 Object(const Object& object) = delete;
258
259 /**
260 * Destructs an object.
261 */
262 virtual ~Object();
263
264 /**
265 * Returns the render engine which is owner of this object.
266 * @return Rendering engine
267 */
268 Engine& engine() const;
269
270 /**
271 * Registers a parent node for this (child) node.
272 * @param parentId Id of the parent node to register
273 */
274 inline void registerParent(const ObjectId parentId);
275
276 /**
277 * Unregisters a parent node for this (child) node.
278 * @param parentId Id of the parent node to unregister
279 */
280 inline void unregisterParent(const ObjectId parentId);
281
282 /**
283 * Registers this object at a child as parent object.
284 * @param child New child object for this object
285 */
287
288 /**
289 * Unregisters this object from a child as parent.
290 * @param child Child object to unregister
291 */
293
294 /**
295 * Disabled copy operator.
296 * @param object Object which would be copied
297 * @return Reference to this object
298 */
299 Object& operator=(const Object& object) = delete;
300
301 protected:
302
303 /// Lock for the object
305
306 private:
307
308 /// Unique object id.
310
311 /// Object name.
312 std::string objectName_;
313
314 /// Object ids of parent objects, with the number of times each parent has registered itself.
316};
317
318inline ObjectId Object::id() const
319{
320 return objectId_;
321}
322
323inline std::string Object::name() const
324{
325 const ScopedLock scopedLock(objectLock_);
326
327 return objectName_;
328}
329
330inline void Object::registerParent(const ObjectId parentId)
331{
332 const ScopedLock scopedLock(objectLock_);
333
334 ++objectParents_[parentId];
335}
336
337inline void Object::unregisterParent(const ObjectId parentId)
338{
339 const ScopedLock scopedLock(objectLock_);
340
341 const ObjectIdMap::iterator iParent = objectParents_.find(parentId);
342 ocean_assert(iParent != objectParents_.cend());
343
344 ocean_assert(iParent->second != 0u);
345
346 if (--iParent->second == 0u)
347 {
348 objectParents_.erase(iParent);
349 }
350}
351
352/**
353 * Write the descriptive information of an object to a stream.
354 * @param stream The stream to which the object's description will be written
355 * @param object The object to be written to the stream
356 * @ingroup rendering
357 */
358inline std::ostream& operator<<(std::ostream& stream, const Object& object)
359{
360 stream << object.descriptiveInformation();
361
362 return stream;
363}
364
365/**
366 * Write the descriptive information of an object to a message object.
367 * @param messageObject The message object to which the object's description will be written
368 * @param object The object to be written to the message object
369 * @tparam tActive True, if the messenger object is active; False, if the messenger object is disabled
370 * @ingroup rendering
371 */
372template <bool tActive>
373inline MessageObject<tActive>& operator<<(MessageObject<tActive>& messageObject, const Object& object)
374{
375 return messageObject << object.descriptiveInformation();
376}
377
378/**
379 * Write the descriptive information of an object to a message object.
380 * @param messageObject The message object to which the object's description will be written
381 * @param object The object to be written to the message object
382 * @tparam tActive True, if the messenger object is active; False, if the messenger object is disabled
383 * @ingroup rendering
384 */
385template <bool tActive>
386inline MessageObject<tActive>& operator<<(MessageObject<tActive>&& messageObject, const Object& object)
387{
388 return messageObject << object.descriptiveInformation();
389}
390
391}
392
393}
394
395#endif // META_OCEAN_RENDERING_OBJECT_H
This class implements a recursive lock object.
Definition Lock.h:31
Messenger object, one object for each message.
Definition Messenger.h:448
This class is the base class for all rendering engines like.
Definition Engine.h:46
This class is the base class for all rendering objects.
Definition Object.h:53
Lock objectLock_
Lock for the object.
Definition Object.h:304
std::string name() const
Returns the name of this object.
Definition Object.h:323
ObjectRefSet parentNodes() const
Returns all parent nodes.
ObjectId objectId_
Unique object id.
Definition Object.h:309
virtual std::string descriptiveInformation() const
Returns descriptive information about the object as string.
std::string objectName_
Object name.
Definition Object.h:312
virtual ~Object()
Destructs an object.
std::unordered_map< ObjectId, unsigned int > ObjectIdMap
Definition of a map holding object ids to reference counters.
Definition Object.h:186
static std::string translateObjectType(const ObjectType objectType)
Translates an object type to a readable string.
Object()
Creates a new object.
virtual void setName(const std::string &name)
Sets or changes the name of this object.
Engine & engine() const
Returns the render engine which is owner of this object.
void unregisterThisObjectAsParent(const ObjectRef &child)
Unregisters this object from a child as parent.
void unregisterParent(const ObjectId parentId)
Unregisters a parent node for this (child) node.
Definition Object.h:337
ObjectRefs parentObjects() const
Returns all parent objects.
ObjectId id() const
Returns the unique object id of this object.
Definition Object.h:318
ObjectType
Definition of different object type.
Definition Object.h:62
@ TYPE_PRIMITIVE_ATTRIBUTE
PrimitiveAttribute type.
Definition Object.h:126
@ TYPE_DIRECTIONAL_LIGHT
DirectionalLight type.
Definition Object.h:88
@ TYPE_ATTRIBUTE
Attribute type.
Definition Object.h:68
@ TYPE_ABSOLUTE_TRANSFORM
Absolute transform type.
Definition Object.h:66
@ TYPE_MATERIAL
Material type.
Definition Object.h:108
@ TYPE_FRAMEBUFFER
Framebuffer type.
Definition Object.h:90
@ TYPE_OBJECT
Object type.
Definition Object.h:112
@ TYPE_BLEND_ATTRIBUTE
BlendAttribute type.
Definition Object.h:78
@ TYPE_BITMAP_FRAMEBUFFER
BitmapFramebuffer type.
Definition Object.h:76
@ TYPE_FRAME_TEXTURE_2D
FrameTexture2D type.
Definition Object.h:92
@ TYPE_SHADER_PROGRAM
ShaderProgram type.
Definition Object.h:136
@ TYPE_UNDISTORTED_BACKGROUND
UndistortedBackground type.
Definition Object.h:174
@ TYPE_SCENE
Scene type.
Definition Object.h:134
@ TYPE_POINTS
Points type.
Definition Object.h:122
@ TYPE_TEXTURES
Textures type.
Definition Object.h:164
@ TYPE_UNKNOWN
Unknown type.
Definition Object.h:64
@ TYPE_LINE_STRIPS
Line strips type.
Definition Object.h:104
@ TYPE_TEXTURE_2D
Texture2D type.
Definition Object.h:158
@ TYPE_LIGHT_SOURCE
LightSource type.
Definition Object.h:100
@ TYPE_SKY_BACKGROUND
SkyBackground type.
Definition Object.h:140
@ TYPE_QUAD_STRIPS
QuadStrips type.
Definition Object.h:128
@ TYPE_POINT_LIGHT
PointLight type.
Definition Object.h:120
@ TYPE_STEREO_ATTRIBUTE
StereoAttribute type.
Definition Object.h:146
@ TYPE_RENDERABLE
Renderable type.
Definition Object.h:132
@ TYPE_PRIMITIVE
Primitive type.
Definition Object.h:124
@ TYPE_DEPTH_ATTRIBUTE
DepthAttribute type.
Definition Object.h:86
@ TYPE_SPHERE
Sphere type.
Definition Object.h:142
@ TYPE_LOD
LOD type.
Definition Object.h:106
@ TYPE_LINES
Lines type.
Definition Object.h:102
@ TYPE_MEDIA_TEXTURE_2D
MediaTexture2D type.
Definition Object.h:160
@ TYPE_PARALLEL_VIEW
ParallelView type.
Definition Object.h:114
@ TYPE_TRIANGLE_FANS
TriangleFans type.
Definition Object.h:168
@ TYPE_STRIP_PRIMITIVE
StripPrimitive type.
Definition Object.h:150
@ TYPE_CYLINDER
Cylinder type.
Definition Object.h:84
@ TYPE_TEXTURE
Texture type.
Definition Object.h:156
@ TYPE_PHANTOM_ATTRIBUTE
PhantomAttribute type.
Definition Object.h:118
@ TYPE_SHAPE
Shape type.
Definition Object.h:138
@ TYPE_GEOMETRY
Geometry type.
Definition Object.h:94
@ TYPE_VIEW
View type.
Definition Object.h:178
@ TYPE_TRIANGLES
Triangles type.
Definition Object.h:170
@ TYPE_SWITCH
Switch type.
Definition Object.h:152
@ TYPE_INDEPENDENT_PRIMITIVE
IndependentPrimitive type.
Definition Object.h:98
@ TYPE_NODE
Node type.
Definition Object.h:110
@ TYPE_BOX
Box type.
Definition Object.h:80
@ TYPE_CONE
Cone type.
Definition Object.h:82
@ TYPE_GROUP
Group type.
Definition Object.h:96
@ TYPE_PERSPECTIVE_VIEW
PerspectiveView type.
Definition Object.h:116
@ TYPE_STEREO_VIEW
StereoView type.
Definition Object.h:148
@ TYPE_TRIANGLE_STRIPS
TriangleStrips type.
Definition Object.h:172
@ TYPE_ATTRIBUTE_SET
AttributeSet type.
Definition Object.h:70
@ TYPE_BACKGROUND
Background type.
Definition Object.h:72
@ TYPE_BILLBOARD
Billboard type.
Definition Object.h:74
@ TYPE_TEXT
Text type.
Definition Object.h:154
@ TYPE_TEXTURE_FRAMEBUFFER
TextureFramebuffer type.
Definition Object.h:162
@ TYPE_SPOT_LIGHT
SpotLight type.
Definition Object.h:144
@ TYPE_QUADS
Quads type.
Definition Object.h:130
@ TYPE_TRANSFORM
Transform type.
Definition Object.h:166
@ TYPE_VERTEX_SET
VertexSet type.
Definition Object.h:176
virtual const std::string & engineName() const =0
Returns the name of the owner engine.
ObjectIdMap objectParents_
Object ids of parent objects, with the number of times each parent has registered itself.
Definition Object.h:315
Object(const Object &object)=delete
Disabled copy constructor.
void registerParent(const ObjectId parentId)
Registers a parent node for this (child) node.
Definition Object.h:330
virtual ObjectType type() const
Returns the type of this object.
void registerThisObjectAsParent(const ObjectRef &child)
Registers this object at a child as parent object.
Object & operator=(const Object &object)=delete
Disabled copy operator.
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
std::vector< ObjectRef > ObjectRefs
Definition of a vector holding rendering object references.
Definition Object.h:40
const ObjectId invalidObjectId
Definition of an invalid object id.
Definition rendering/Rendering.h:65
std::ostream & operator<<(std::ostream &stream, const Object &object)
Write the descriptive information of an object to a stream.
Definition Object.h:358
std::set< ObjectRef > ObjectRefSet
Definition of a set holding rendering object references.
Definition Object.h:46
The namespace covering the entire Ocean framework.
Definition Accessor.h:15