Avara3D 0.2.0
C++ API reference
Scene.h
1//
2// Scene.h
3// avara3d
4//
5// Created by Morgan Davis on 10/21/16.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_SCENE_SCENE_H
10#define AVARA3D_SCENE_SCENE_H
11
12#include <cstdint>
13#include <filesystem>
14#include <functional>
15#include <memory>
16#include <optional>
17#include <string>
18
19#include "a3d/Math.h"
20#include "a3d/input/InputContext.h"
21#include "a3d/util/Bitmask.h"
22
23namespace a3d {
24
25 struct AABB;
26
27 class Color;
28 class Mesh;
29 class Node;
30 class PhysicsWorld;
31 class Profiler;
32 class Renderer;
33 class RenderContext;
34 class VisualWorld;
35
44 class Scene {
45
46 public:
47 // [Public Types]
48
50 enum class ImportOptions : uint16_t {
51 None = 0,
52 ImportMeshes = 1 << 0,
53 ImportMaterials = 1 << 1,
54 ImportLights = 1 << 2,
55 ImportCameras = 1 << 3,
56 ImportAll = UINT16_MAX
57 };
58
59 // TODO: move to VisualWorld?
61 enum class DebugOptions : uint32_t {
62 None = 0,
63
64 ShowStatsOverlay = 1 << 0,
65
66 ShowMeshBounds = 1 << 1,
67 ShowMeshFrames = 1 << 2,
68 ShowMeshWireframes = 1 << 3,
69
70 ShowCameras = 1 << 4,
71 ShowLights = 1 << 5,
72 ShowLightExtents = 1 << 6,
73
74 ShowPhysicsBounds = 1 << 7,
75 ShowPhysicsFrames = 1 << 8,
76 ShowPhysicsWireframes = 1 << 9,
77 ShowPhysicsContactPoints = 1 << 10,
78 ShowPhysicsNormals = 1 << 11,
79
80 ShowPhysicsConstraints = 1 << 12,
81
82 ShowPhysicsConstraintLimits =
83 1 << 13,
84 };
85
87 struct StepInfo {
88
90 std::uint64_t stepIndex {0};
91
93 double startTime {0.0};
94
96 double endTime {0.0};
97
99 double deltaTime {0.0};
100 };
101
103 using WillStepCallback = std::function<void(Scene& scene, const StepInfo& info)>;
104
106 using DidStepCallback = std::function<void(Scene& scene, const StepInfo& info)>;
107
108 // [Public Static Member Functions]
109
119 static std::unique_ptr<Scene> FromFile(const std::filesystem::path& path,
121
122 // [Public Lifecycle Functions]
123
126
128 explicit Scene(const std::string& name);
129
135 Scene(std::unique_ptr<VisualWorld> visualWorld,
136 std::unique_ptr<PhysicsWorld> physicsWorld,
137 std::unique_ptr<InputContext> inputContext);
138
144 Scene(const std::string& name,
145 std::unique_ptr<VisualWorld> visualWorld,
146 std::unique_ptr<PhysicsWorld> physicsWorld,
147 std::unique_ptr<InputContext> inputContext);
148
149 Scene(const Scene&) = delete;
150 Scene& operator=(const Scene&) = delete;
151
152 Scene(Scene&&) = delete;
153 Scene& operator=(Scene&&) = delete;
154
155 ~Scene();
156
157 // [Public Member Functions]
158
160 const std::optional<std::string>& name() const;
161
163 void name(const std::string& name);
164
166 const std::shared_ptr<Node>& rootNode() const;
167
175 void rootNode(const std::shared_ptr<Node>& node);
176
179
181 void visualWorld(std::unique_ptr<VisualWorld> world);
182
185
187 void physicsWorld(std::unique_ptr<PhysicsWorld> world);
188
191
193 void inputContext(std::unique_ptr<InputContext> context);
194
203 AABB aabb(bool vertfit = false) const;
204
213 math::vec3 extent(bool vertfit = false) const;
214
217
220
223
226
229
232
233 // [Internal Member Functions]
234
235 void pollEvents(Profiler& profiler);
236 void updateInput(const InputContext::UpdateInfo& info, Profiler& profiler);
237 void stepSimulation(const StepInfo& info, Profiler& profiler);
238
239 private:
240 // [Private Member Variables]
241
242 std::optional<std::string> _name;
243 std::shared_ptr<Node> _rootNode;
244 std::unique_ptr<VisualWorld> _visualWorld;
245 std::unique_ptr<PhysicsWorld> _physicsWorld;
246 std::unique_ptr<InputContext> _inputContext;
247 DebugOptions _debugOptions;
248 WillStepCallback _willStepCallback;
249 DidStepCallback _didStepCallback;
250 };
251
252 namespace util::bitmask {
253
254 template<>
255 struct enable_ops<Scene::ImportOptions> : std::true_type {};
256
257 template<>
258 struct enable_ops<Scene::DebugOptions> : std::true_type {};
259
260 }
261}
262
263#endif // AVARA3D_SCENE_SCENE_H
Base interface for Scene input state updated once per Runner host update.
Definition: InputContext.h:27
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
InputContext * inputContext() const
Returns the owned InputContext, or nullptr if none is installed.
PhysicsWorld * physicsWorld() const
Returns the owned PhysicsWorld, or nullptr if none is installed.
void willStepCallback(WillStepCallback callback)
Replaces the callback invoked before each simulation step; an empty callback disables it.
VisualWorld * visualWorld() const
Returns the owned VisualWorld, or nullptr if none is installed.
Scene(const std::string &name, std::unique_ptr< VisualWorld > visualWorld, std::unique_ptr< PhysicsWorld > physicsWorld, std::unique_ptr< InputContext > inputContext)
Creates a named Scene and takes ownership of its optional worlds and input context.
std::function< void(Scene &scene, const StepInfo &info)> DidStepCallback
Callback invoked immediately after a Scene simulation step.
Definition: Scene.h:106
DebugOptions debugOptions() const
Returns the enabled Scene debug visualization options.
std::function< void(Scene &scene, const StepInfo &info)> WillStepCallback
Callback invoked immediately before a Scene simulation step.
Definition: Scene.h:103
Scene(std::unique_ptr< VisualWorld > visualWorld, std::unique_ptr< PhysicsWorld > physicsWorld, std::unique_ptr< InputContext > inputContext)
Creates a Scene and takes ownership of its optional worlds and input context.
void physicsWorld(std::unique_ptr< PhysicsWorld > world)
Replaces the owned PhysicsWorld; nullptr removes the current world.
const std::optional< std::string > & name() const
Returns the optional Scene name.
void didStepCallback(DidStepCallback callback)
Replaces the callback invoked after each simulation step; an empty callback disables it.
static std::unique_ptr< Scene > FromFile(const std::filesystem::path &path, ImportOptions options=ImportOptions::ImportAll)
Imports a Scene from a glTF or GLB file.
AABB aabb(bool vertfit=false) const
Returns the world-space axis-aligned bounding box enclosing the Scene hierarchy.
Scene(const std::string &name)
Creates a named Scene with an empty root node and no worlds or input context.
math::vec3 extent(bool vertfit=false) const
Returns the dimensions of the Scene world-space axis-aligned bounding box.
void inputContext(std::unique_ptr< InputContext > context)
Replaces the owned InputContext; nullptr removes the current context.
DidStepCallback didStepCallback() const
Returns the callback invoked after each simulation step.
void visualWorld(std::unique_ptr< VisualWorld > world)
Replaces the owned VisualWorld; nullptr removes the current world.
WillStepCallback willStepCallback() const
Returns the callback invoked before each simulation step.
Scene()
Creates a Scene with an empty root node and no worlds or input context.
ImportOptions
Selects which resource categories are imported by FromFile().
Definition: Scene.h:50
@ ImportAll
Import all supported resource categories.
void name(const std::string &name)
Sets the Scene name.
void rootNode(const std::shared_ptr< Node > &node)
Replaces the root node of the Scene hierarchy.
DebugOptions
Scene-wide debug visualization options.
Definition: Scene.h:61
const std::shared_ptr< Node > & rootNode() const
Returns the root node of the Scene hierarchy.
void debugOptions(DebugOptions options)
Sets the enabled Scene debug visualization options.
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
Timing information for one input update.
Definition: InputContext.h:33
Timing information for one simulation step.
Definition: Scene.h:87
std::uint64_t stepIndex
Zero-based simulation-step index.
Definition: Scene.h:90
double endTime
Simulation time in seconds after this step completes.
Definition: Scene.h:96
double startTime
Simulation time in seconds before this step.
Definition: Scene.h:93
double deltaTime
Amount of simulation time in seconds advanced by this step.
Definition: Scene.h:99