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
117 static std::unique_ptr<Scene> FromFile(const std::filesystem::path& path,
119
120 // [Public Lifecycle Functions]
121
124
126 explicit Scene(const std::string& name);
127
133 Scene(std::unique_ptr<VisualWorld> visualWorld,
134 std::unique_ptr<PhysicsWorld> physicsWorld,
135 std::unique_ptr<InputContext> inputContext);
136
142 Scene(const std::string& name,
143 std::unique_ptr<VisualWorld> visualWorld,
144 std::unique_ptr<PhysicsWorld> physicsWorld,
145 std::unique_ptr<InputContext> inputContext);
146
147 Scene(const Scene&) = delete;
148 Scene& operator=(const Scene&) = delete;
149
150 Scene(Scene&&) = delete;
151 Scene& operator=(Scene&&) = delete;
152
153 ~Scene();
154
155 // [Public Member Functions]
156
158 const std::optional<std::string>& name() const;
159
161 void name(const std::string& name);
162
164 const std::shared_ptr<Node>& rootNode() const;
165
173 void rootNode(const std::shared_ptr<Node>& node);
174
177
179 void visualWorld(std::unique_ptr<VisualWorld> world);
180
183
185 void physicsWorld(std::unique_ptr<PhysicsWorld> world);
186
189
191 void inputContext(std::unique_ptr<InputContext> context);
192
199 AABB aabb(bool vertfit = false) const;
200
207 math::vec3 extent(bool vertfit = false) const;
208
211
214
217
220
223
226
227 // [Internal Member Functions]
228
229 void pollEvents(Profiler& profiler);
230 void updateInput(const InputContext::UpdateInfo& info, Profiler& profiler);
231 void stepSimulation(const StepInfo& info, Profiler& profiler);
232
233 private:
234 // [Private Member Variables]
235
236 std::optional<std::string> _name;
237 std::shared_ptr<Node> _rootNode;
238 std::unique_ptr<VisualWorld> _visualWorld;
239 std::unique_ptr<PhysicsWorld> _physicsWorld;
240 std::unique_ptr<InputContext> _inputContext;
241 DebugOptions _debugOptions;
242 WillStepCallback _willStepCallback;
243 DidStepCallback _didStepCallback;
244 };
245
246 namespace util::bitmask {
247
248 template<>
249 struct enable_ops<Scene::ImportOptions> : std::true_type {};
250
251 template<>
252 struct enable_ops<Scene::DebugOptions> : std::true_type {};
253
254 }
255}
256
257#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 path.
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