Avara3D 0.2.0
C++ API reference
Node.h
1//
2// Node.h
3// avara3d
4//
5// Created by Morgan Davis on 10/20/16.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_SCENE_NODE_H
10#define AVARA3D_SCENE_NODE_H
11
12#include <climits>
13#include <cstdint>
14#include <memory>
15#include <optional>
16#include <string>
17#include <vector>
18
19#include "a3d/mesh/AABB.h"
20#include "a3d/util/Bitmask.h"
21
22namespace a3d {
23
24 class Camera;
25 class Light;
26 class Mesh;
27 class Renderer;
28 class RenderContext;
29 class RenderItem;
30 class Scene;
31 class PhysicsWorld;
32 class PhysicsBody;
33 class VisualWorld;
34
45 class Node : public std::enable_shared_from_this<Node> {
46
47 public:
48 // [Public Types]
49
50 // TODO: probably move these
52 enum class DebugOptions : uint32_t {
53 None = 0,
54 ShowHighlightBox = 1 << 0,
55 ShowHighlightTint = 1 << 1
56 };
57
58 // [Public Static Member Functions]
59
61 static std::shared_ptr<Node> NamedNode(const std::string& name);
62
64 static std::shared_ptr<Node> MeshNode(const std::shared_ptr<Mesh>& geometry);
65
67 static std::shared_ptr<Node> LightNode(const std::shared_ptr<Light>& light);
68
70 static std::shared_ptr<Node> CameraNode(const std::shared_ptr<Camera>& camera);
71
72 // [Public Lifecycle Functions]
73
76
78 explicit Node(const std::string& name);
79
81 explicit Node(const std::shared_ptr<Mesh>& mesh);
82
84 explicit Node(const std::shared_ptr<Light>& light);
85
87 explicit Node(const std::shared_ptr<Camera>& camera);
88
89 Node(const Node&) = delete;
90 Node& operator=(const Node&) = delete;
91
92 Node(Node&&) = delete;
93 Node& operator=(Node&&) = delete;
94
95 ~Node();
96
97 // [Public Member Functions]
98
100 const std::optional<std::string>& name() const;
101
103 void name(const std::string& name);
104
106 const std::shared_ptr<Light>& light() const;
107
109 void light(const std::shared_ptr<Light>& light);
110
112 const std::shared_ptr<Camera>& camera() const;
113
115 void camera(const std::shared_ptr<Camera>& camera);
116
118 const std::shared_ptr<Mesh>& mesh() const;
119
135 void mesh(const std::shared_ptr<Mesh>& mesh);
136
138 const math::vec3& position() const;
139
142
150
152 void rotation(const math::vec3& axis, float angle);
153
156
158 void eulerAngles(const math::vec3& angles);
159
161 const math::quat& orientation() const;
162
165
167 const math::vec3& scale() const;
168
170 void scale(const math::vec3& scale);
171
174
176 math::vec3 up() const;
177
180
183
186
189
197
200
203
206
209
212
215
218
220 math::vec3 convertFrom(const math::vec3& pos, const Node& from);
221
223 math::vec3 convertTo(const math::vec3& pos, const Node& to);
224
226 math::mat4 convertFrom(const math::mat4& t, const Node& from);
227
229 math::mat4 convertTo(const math::mat4& t, const Node& to);
230
236 void addChild(const std::shared_ptr<Node>& node);
237
239 void addChildren(const std::vector<std::shared_ptr<Node>>& nodes);
240
243
250 std::vector<std::shared_ptr<Node>> children(bool resursive = false) const;
251
259 std::shared_ptr<Node> childNamed(const std::string& name, bool resursive = false) const;
260
263
273 void physicsBody(std::unique_ptr<PhysicsBody> body);
274
276 bool hidden() const;
277
279 void hidden(bool hidden);
280
282 int renderOrder() const;
283
285 void renderOrder(int order);
286
289
292
294 Scene* scene() const;
295
297 std::weak_ptr<Node> parent() const;
298
299 // [Internal Types]
300
301 enum class DirtyMask : uint32_t {
302 None = 0,
303 WorldTransform = 1 << 0,
304 All = UINT_MAX
305 };
306
307 // [Internal Member Functions]
308
309 void attachedToParent(Node& parent);
310 void detachedFromParent(Node& parent);
311
312 void attachedToScene(Scene& scene);
313 void detachedFromScene(Scene& scene);
314
315 void ancestorAttachedToParent(Node& ancestor, Node& parent);
316 void ancestorDetachedFromParent(Node& ancestor, Node& parent);
317
318 void ancestorAttachedToScene(Node& ancestor, Scene& scene);
319 void ancestorDetachedFromScene(Node& ancestor, Scene& scene);
320
321 void visualWorldAttachedToScene(VisualWorld& world, Scene& scene);
322 void visualWorldDetachedFromScene(VisualWorld& world, Scene& scene);
323
324 void physicsWorldAttachedToScene(PhysicsWorld& world, Scene& scene);
325 void physicsWorldDetachedFromScene(PhysicsWorld& world, Scene& scene);
326
327 VisualWorld* visualWorld() const;
328 PhysicsWorld* physicsWorld() const;
329
330 void checkNotifyPhysicsBodyOfReachablePhysicsWorld() const;
331 void checkNotifyPhysicsBodyOfUnreachablePhysicsWorld() const;
332
333 bool containsChild(const std::shared_ptr<Node>& node);
334
335 AABB aabb(bool vertfit = false) const; // recursive world AABB
336 math::vec3 extent(bool vertfit = false) const; // recursive world extent
337
338 void applyPhysicsTransform(const math::mat4& transform);
339
340 void _debugPrint();
341 void _debugPrintRec(Node& node, unsigned level);
342
343 private:
344 // [Private Member Functions]
345
346 void getAABBRec(AABB& aabb);
347
348 void setTransformComponents(const math::mat4& transform);
349 void syncPhysicsTransforms();
350
351 std::vector<std::shared_ptr<Node>> children(const Node& root) const;
352 void childrenRec(const std::shared_ptr<Node>& node, std::vector<std::shared_ptr<Node>>& children) const;
353
354 DirtyMask dirtyMask() const;
355 void dirtyMask(DirtyMask mask);
356
357 // [Private Member Variables]
358
359 std::optional<std::string> _name;
360 std::shared_ptr<Light> _light;
361 std::shared_ptr<Camera> _camera;
362 std::shared_ptr<Mesh> _mesh;
363 std::vector<std::shared_ptr<Node>> _children;
364 math::vec3 _position;
365 math::quat _orientation;
366 math::vec3 _scale;
367 mutable std::optional<math::vec3> _eulerAngles;
368 std::unique_ptr<PhysicsBody> _physicsBody;
369 bool _hidden;
370 int _renderOrder;
371 DebugOptions _debugOptions;
372 Scene* _scene;
373 std::weak_ptr<Node> _parent;
374 DirtyMask _dirtyMask;
375 };
376
377 namespace util::bitmask {
378
379 template<>
380 struct enable_ops<Node::DebugOptions> : std::true_type {};
381
382 template<>
383 struct enable_ops<Node::DirtyMask> : std::true_type {};
384
385 }
386}
387
388#endif // AVARA3D_SCENE_NODE_H
A transformable object in a Scene hierarchy.
Definition: Node.h:45
const math::vec3 & scale() const
Returns the node scale relative to its parent.
math::vec3 worldRight() const
Returns the node's local +X axis expressed in world coordinates.
const std::shared_ptr< Mesh > & mesh() const
Returns the mesh attached to this node, or nullptr if none is attached.
math::vec3 convertFrom(const math::vec3 &pos, const Node &from)
Converts pos from from local coordinates into this node's local coordinates.
math::vec3 worldUp() const
Returns the node's local +Y axis expressed in world coordinates.
void position(const math::vec3 &position)
Sets the node position in parent coordinates.
math::vec3 worldPosition() const
Returns the node position in world coordinates.
Node(const std::shared_ptr< Mesh > &mesh)
Creates a node containing mesh and an identity transform.
const std::shared_ptr< Camera > & camera() const
Returns the camera attached to this node, or nullptr if none is attached.
void scale(const math::vec3 &scale)
Sets the node scale relative to its parent.
void physicsBody(std::unique_ptr< PhysicsBody > body)
Replaces the physics body owned by this node; nullptr removes it.
DebugOptions debugOptions() const
Returns the enabled node debug visualization options.
math::vec3 worldScale() const
Returns the effective node scale in world coordinates.
void eulerAngles(const math::vec3 &angles)
Sets the node Euler angles as pitch, yaw, and roll in radians.
math::vec3 convertTo(const math::vec3 &pos, const Node &to)
Converts pos from this node's local coordinates into to local coordinates.
math::mat4 convertFrom(const math::mat4 &t, const Node &from)
Converts t from from local coordinates into this node's local coordinates.
static std::shared_ptr< Node > CameraNode(const std::shared_ptr< Camera > &camera)
Creates a new node containing camera.
bool hidden() const
Returns true when this node is hidden from rendering.
void renderOrder(int order)
Sets the node render-order value.
void hidden(bool hidden)
Sets whether this node is hidden from rendering.
math::mat4 worldTransform() const
Returns the transform from this node's local coordinates to world coordinates.
math::vec3 right() const
Returns the node's local +X axis expressed in parent coordinates.
std::shared_ptr< Node > childNamed(const std::string &name, bool resursive=false) const
Finds a child with name.
Node(const std::shared_ptr< Camera > &camera)
Creates a node containing camera and an identity transform.
const std::shared_ptr< Light > & light() const
Returns the light attached to this node, or nullptr if none is attached.
math::mat4 convertTo(const math::mat4 &t, const Node &to)
Converts t from this node's local coordinates into to local coordinates.
Node()
Creates an empty node with an identity transform.
Node(const std::shared_ptr< Light > &light)
Creates a node containing light and an identity transform.
math::vec3 worldEulerAngles() const
Returns the node world Euler angles as pitch, yaw, and roll in radians.
static std::shared_ptr< Node > LightNode(const std::shared_ptr< Light > &light)
Creates a new node containing light.
void removeFromParent()
Detaches this node from its current parent, if it has one.
std::weak_ptr< Node > parent() const
Returns the current parent, or an empty weak pointer for a root or detached node.
void name(const std::string &name)
Sets the node name.
DebugOptions
Debug visualization options associated with a node.
Definition: Node.h:52
const std::optional< std::string > & name() const
Returns the optional node name.
static std::shared_ptr< Node > NamedNode(const std::string &name)
Creates a new node with name.
static std::shared_ptr< Node > MeshNode(const std::shared_ptr< Mesh > &geometry)
Creates a new node containing geometry.
math::vec3 up() const
Returns the node's local +Y axis expressed in parent coordinates.
const math::vec3 & position() const
Returns the node position in parent coordinates.
int renderOrder() const
Returns the node render-order value.
math::vec3 eulerAngles() const
Returns the node Euler angles in parent coordinates as pitch, yaw, and roll in radians.
const math::quat & orientation() const
Returns the node orientation in parent coordinates as a quaternion.
math::vec4 worldRotation() const
Returns the node world rotation.
void mesh(const std::shared_ptr< Mesh > &mesh)
Replaces the mesh attached to this node.
Node(const std::string &name)
Creates a node with name and an identity transform.
math::vec3 forward() const
Returns the node's local -Z axis expressed in parent coordinates.
void debugOptions(DebugOptions options)
Sets the enabled node debug visualization options.
void camera(const std::shared_ptr< Camera > &camera)
Replaces the camera attached to this node; nullptr removes it.
std::vector< std::shared_ptr< Node > > children(bool resursive=false) const
Returns this node's children.
void transform(const math::mat4 &transform)
Sets the node transform from local coordinates to parent coordinates.
void addChildren(const std::vector< std::shared_ptr< Node > > &nodes)
Adds each node in nodes as a child.
math::vec3 worldForward() const
Returns the node's local -Z axis expressed in world coordinates.
void light(const std::shared_ptr< Light > &light)
Replaces the light attached to this node; nullptr removes it.
math::quat worldOrientation() const
Returns the node orientation in world coordinates.
void orientation(const math::quat &orientation)
Sets the node orientation in parent coordinates.
math::mat4 transform() const
Returns the node transform from local coordinates to parent coordinates.
Scene * scene() const
Returns the Scene containing this node, or nullptr if the node is not attached to a Scene.
math::vec4 rotation() const
Returns the node rotation in parent coordinates.
PhysicsBody * physicsBody() const
Returns the physics body owned by this node, or nullptr if none is attached.
void rotation(const math::vec3 &axis, float angle)
Sets the node rotation from axis and angle in radians.
void addChild(const std::shared_ptr< Node > &node)
Adds node as a child of this node.
Rigid-body simulation state attached to a Node.
Definition: PhysicsBody.h:39
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39
Owns a scene graph and the worlds used to simulate and render it.
Definition: Scene.h:44
Manages the visual presentation, camera, and visual queries for a Scene.
Definition: VisualWorld.h:53
Axis-aligned bounding box defined by minimum and maximum corners.
Definition: AABB.h:17