v0.3.0 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
24class Camera;
25class Light;
26class Mesh;
27class Renderer;
28class RenderContext;
29class RenderItem;
30class Scene;
31class PhysicsWorld;
32class PhysicsBody;
33class VisualWorld;
34
45class Node : public std::enable_shared_from_this<Node> {
46
47public:
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<Mesh>& mesh() const;
107
123 void mesh(const std::shared_ptr<Mesh>& mesh);
124
126 const std::shared_ptr<Light>& light() const;
127
129 void light(const std::shared_ptr<Light>& light);
130
132 const std::shared_ptr<Camera>& camera() const;
133
135 void camera(const std::shared_ptr<Camera>& camera);
136
139
149 void physicsBody(std::unique_ptr<PhysicsBody> body);
150
152 const math::vec3& position() const;
153
156
164
166 void rotation(const math::vec3& axis, float angle);
167
170
172 void eulerAngles(const math::vec3& angles);
173
175 const math::quat& orientation() const;
176
179
181 const math::vec3& scale() const;
182
184 void scale(const math::vec3& scale);
185
188
190 math::vec3 up() const;
191
194
197
200
203
211
214
217
220
223
226
229
232
234 math::vec3 convertFrom(const math::vec3& pos, const Node& from);
235
237 math::vec3 convertTo(const math::vec3& pos, const Node& to);
238
240 math::mat4 convertFrom(const math::mat4& t, const Node& from);
241
243 math::mat4 convertTo(const math::mat4& t, const Node& to);
244
259 void addChild(const std::shared_ptr<Node>& node, bool reparent = false);
260
274 void addChildren(const std::vector<std::shared_ptr<Node>>& nodes, bool reparent = false);
275
278
285 std::vector<std::shared_ptr<Node>> children(bool recursive = false) const;
286
294 std::shared_ptr<Node> childNamed(const std::string& name, bool recursive = false) const;
295
297 bool hidden() const;
298
300 void hidden(bool hidden);
301
303 int renderOrder() const;
304
306 void renderOrder(int order);
307
310
313
315 Scene* scene() const;
316
318 std::weak_ptr<Node> parent() const;
319
320 // [Internal Types]
321
322 enum class DirtyMask : uint32_t {
323 None = 0,
324 WorldTransform = 1 << 0,
325 All = UINT_MAX
326 };
327
328 // [Internal Member Functions]
329
330 void attachedToParent(Node& parent);
331 void detachedFromParent(Node& parent);
332
333 void attachedToScene(Scene& scene);
334 void detachedFromScene(Scene& scene);
335
336 void ancestorAttachedToParent(Node& ancestor, Node& parent);
337 void ancestorDetachedFromParent(Node& ancestor, Node& parent);
338
339 void ancestorAttachedToScene(Node& ancestor, Scene& scene);
340 void ancestorDetachedFromScene(Node& ancestor, Scene& scene);
341
342 void visualWorldAttachedToScene(VisualWorld& world, Scene& scene);
343 void visualWorldDetachedFromScene(VisualWorld& world, Scene& scene);
344
345 void physicsWorldAttachedToScene(PhysicsWorld& world, Scene& scene);
346 void physicsWorldDetachedFromScene(PhysicsWorld& world, Scene& scene);
347
348 VisualWorld* visualWorld() const;
349 PhysicsWorld* physicsWorld() const;
350
351 void checkNotifyPhysicsBodyOfReachablePhysicsWorld() const;
352 void checkNotifyPhysicsBodyOfUnreachablePhysicsWorld() const;
353
354 bool containsChild(const std::shared_ptr<Node>& node);
355
356 AABB aabb(bool vertfit = false) const; // recursive world AABB
357 math::vec3 extent(bool vertfit = false) const; // recursive world extent
358
359 void applyPhysicsTransform(const math::mat4& transform);
360
361 void _debugPrint();
362 void _debugPrintRec(Node& node, unsigned level);
363
364private:
365 // [Private Member Functions]
366
367 void getAABBRec(AABB& aabb);
368
369 void setTransformComponents(const math::mat4& transform);
370 void syncPhysicsTransforms();
371
372 std::vector<std::shared_ptr<Node>> children(const Node& root) const;
373 void childrenRec(const std::shared_ptr<Node>& node, std::vector<std::shared_ptr<Node>>& children) const;
374
375 DirtyMask dirtyMask() const;
376 void dirtyMask(DirtyMask mask);
377
378 // [Private Member Variables]
379
380 std::optional<std::string> _name;
381 std::shared_ptr<Light> _light;
382 std::shared_ptr<Camera> _camera;
383 std::shared_ptr<Mesh> _mesh;
384 std::vector<std::shared_ptr<Node>> _children;
385 math::vec3 _position;
386 math::quat _orientation;
387 math::vec3 _scale;
388 mutable std::optional<math::vec3> _eulerAngles;
389 std::unique_ptr<PhysicsBody> _physicsBody;
390 bool _hidden;
391 int _renderOrder;
392 DebugOptions _debugOptions;
393 Scene* _scene;
394 std::weak_ptr<Node> _parent;
395 DirtyMask _dirtyMask;
396};
397
398namespace util::bitmask {
399
400 template<>
401 struct enable_ops<Node::DebugOptions> : std::true_type {};
402
403 template<>
404 struct enable_ops<Node::DirtyMask> : std::true_type {};
405
406} // namespace util::bitmask
407} // namespace a3d
408
409#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 recursive=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
void addChildren(const std::vector< std::shared_ptr< Node > > &nodes, bool reparent=false)
Adds each node in nodes as a child, in order.
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.
void addChild(const std::shared_ptr< Node > &node, bool reparent=false)
Adds node as a child of this node.
void transform(const math::mat4 &transform)
Sets the node transform from local coordinates to parent coordinates.
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.
std::vector< std::shared_ptr< Node > > children(bool recursive=false) const
Returns this node's children.
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.
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