Avara3D 0.2.0
C++ API reference
VisualWorld.h
1//
2// VisualWorld.h
3// avara3d
4//
5// Created by Morgan Davis on 11/25/23.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_VISUAL_VISUALWORLD_H
10#define AVARA3D_VISUAL_VISUALWORLD_H
11
12#include <cstdint>
13#include <functional>
14#include <memory>
15#include <optional>
16#include <vector>
17
18#include "a3d/scene/Scene.h"
19#include "a3d/scene/HitTestResult.h"
20#include "a3d/visual/Atmosphere.h"
21#include "a3d/visual/Background.h"
22#include "a3d/visual/Fog.h"
23#include "a3d/visual/Ground.h"
24#include "a3d/visual/Surface.h"
25#include "a3d/visual/material/Material.h"
26
27namespace a3d {
28
29 struct FrameStats;
30
31 class Color;
32 class FrameStatsHistory;
33 class Material;
34 class Mesh;
35 class Node;
36 class PhysicsWorld;
37 class Profiler;
38 class Renderer;
39 class RenderContext;
40 class Scene;
41
54
55 public:
56 // // [Internal Types]
57 //
58 // using Plane = PlaneSurface;
59 // using Sphere = SphereSurface;
60 // using Surface = a3d::Surface;
61
62 // [Public Types]
63
65 struct Capabilities {
66 bool wireframeRendering {false};
67 };
68
70 struct RenderInfo {
71
72 // zero-based successful-render-frame index
73 std::uint64_t frameIndex {0};
74
75 // Runner update responsible for this render attempt
76 std::uint64_t updateIndex {0};
77
78 // monotonic Runner elapsed time for the containing update
79 double updateTime {0.0};
80
81 // monotonic delta for the containing Runner update
82 double updateDeltaTime {0.0};
83
84 // time reached by the most recently completed simulation step
86 0.0};
87
88 // total number of completed simulation steps
89 std::uint64_t simulationStepCount {0};
90 };
91
94 HitTestSearchMode searchMode {HitTestSearchMode::Closest};
95 bool elementBoundsOnly {false};
96 };
97
99 using DidBeginFrameCallback = std::function<void(VisualWorld& visualWorld, const RenderInfo& info)>;
100
101 // [Public Lifecycle Functions]
102
103 VisualWorld() = delete;
104
110 explicit VisualWorld(RenderContext& context);
111
112 VisualWorld(const VisualWorld&) = delete;
113 VisualWorld& operator=(const VisualWorld&) = delete;
114
115 VisualWorld(VisualWorld&&) = delete;
116 VisualWorld& operator=(VisualWorld&&) = delete;
117
118 virtual ~VisualWorld();
119
120 // [Public Member Functions]
121
124
126 const std::optional<Background>& background() const;
127
137 void background(const std::optional<Background>& background);
138
140 const std::optional<Fog>& fog() const;
141
147 void fog(const std::optional<Fog>& fog);
148
150 const std::optional<Atmosphere>& atmosphere() const;
151
161 void atmosphere(const std::optional<Atmosphere>& atmosphere);
162
164 const std::optional<Ground>& ground() const;
165
174 void ground(const std::optional<Ground>& ground);
175
177 const std::optional<a3d::Surface>& surface() const;
178
188 void surface(const std::optional<a3d::Surface>& surface);
189
191 std::weak_ptr<Node>& pointOfView();
192
200 void pointOfView(const std::weak_ptr<Node>& cameraNode);
201
210 math::vec3 projectPoint(const math::vec3& point) const;
211
221
231 std::vector<HitTestResult> hitTest(const math::vec2& point, const HitTestOptions& options) const;
232
234 std::vector<HitTestResult> hitTest(const math::vec2& point) const;
235
238
245 void defaultLightingEnabled(bool enabled);
246
249
252
255
257 Scene* scene() const;
258
259 // [Internal Member Functions]
260
261 void attachedToScene(Scene& scene);
262 void detachedFromScene(Scene& scene);
263
264 bool draw(const Scene& scene,
265 const PhysicsWorld* physicsWorld,
266 const RenderInfo& info,
267 Scene::DebugOptions debugOptions,
268 FrameStats& stats,
269 Profiler& profiler,
270 const FrameStatsHistory& statsHistory);
271
272 std::shared_ptr<Material> backgroundMaterial();
273
274 private:
275 // [Private Member Functions]
276
277 void firstDraw();
278 std::shared_ptr<Node> defaultPOV();
279
280 // [Private Member Variables]
281
282 std::optional<Background> _background;
283 std::shared_ptr<Material> _backgroundMaterial;
284 std::optional<Fog> _fog;
285 std::optional<Atmosphere> _atmosphere;
286 std::optional<Ground> _ground;
287 std::optional<a3d::Surface> _surface;
288 bool _defaultLightingEnabled;
289 std::weak_ptr<Node> _pointOfView;
290 RenderContext* _renderContext;
291 Scene* _scene;
292 DidBeginFrameCallback _didBeginFrameCallback;
293 };
294
295}
296
297#endif // AVARA3D_VISUAL_VISUALWORLD_H
Simulates rigid bodies and performs physics collision queries for a Scene.
Definition: PhysicsWorld.h:39
Abstract rendering destination used by VisualWorld.
Definition: RenderContext.h:34
Owns a scene graph and the worlds used to simulate and render it.
Definition: Scene.h:44
DebugOptions
Scene-wide debug visualization options.
Definition: Scene.h:61
Manages the visual presentation, camera, and visual queries for a Scene.
Definition: VisualWorld.h:53
VisualWorld(RenderContext &context)
Creates a VisualWorld that renders through context.
void pointOfView(const std::weak_ptr< Node > &cameraNode)
Sets the camera Node used as the point of view.
std::vector< HitTestResult > hitTest(const math::vec2 &point, const HitTestOptions &options) const
Hit-tests visible Scene mesh geometry beneath a logical viewport point.
void didBeginFrameCallback(DidBeginFrameCallback function)
Sets the begin-frame callback; an empty callback disables it.
void surface(const std::optional< a3d::Surface > &surface)
Sets or removes the reference Surface used by ground and atmospheric effects.
math::vec3 unprojectPoint(const math::vec3 &point) const
Unprojects a logical viewport point into world coordinates.
std::vector< HitTestResult > hitTest(const math::vec2 &point) const
Hit-tests the closest visible Scene mesh geometry beneath a logical viewport point.
const std::optional< Fog > & fog() const
Returns the optional distance fog configuration.
Scene * scene() const
Returns the Scene this VisualWorld is attached to, or nullptr if unattached.
math::vec3 projectPoint(const math::vec3 &point) const
Projects a world-space point into logical viewport coordinates.
void ground(const std::optional< Ground > &ground)
Sets or disables visual ground rendering.
std::function< void(VisualWorld &visualWorld, const RenderInfo &info)> DidBeginFrameCallback
Callback invoked after renderer frame setup and before Scene rendering begins.
Definition: VisualWorld.h:99
const std::optional< Atmosphere > & atmosphere() const
Returns the optional atmosphere configuration.
void fog(const std::optional< Fog > &fog)
Sets or disables distance fog.
const std::optional< Background > & background() const
Returns the optional Scene background.
const std::optional< Ground > & ground() const
Returns the optional visual ground configuration.
DidBeginFrameCallback didBeginFrameCallback() const
Returns the callback invoked immediately before Scene rendering begins.
void defaultLightingEnabled(bool enabled)
Enables or disables renderer-provided default lighting.
void atmosphere(const std::optional< Atmosphere > &atmosphere)
Sets or disables atmospheric effects.
void background(const std::optional< Background > &background)
Sets or disables the Scene background.
std::weak_ptr< Node > & pointOfView()
Returns the weak reference to the camera Node used as the point of view.
const std::optional< a3d::Surface > & surface() const
Returns the optional reference Surface used by ground and atmospheric effects.
bool defaultLightingEnabled() const
Returns whether renderer-provided default lighting is enabled.
Capabilities capabilities() const
Returns the rendering capabilities currently available to the VisualWorld.
RenderContext * renderContext() const
Returns the non-owning RenderContext associated with this VisualWorld.
Rendering capabilities available through this VisualWorld.
Definition: VisualWorld.h:65
bool wireframeRendering
Whether wireframe rendering is supported.
Definition: VisualWorld.h:66
Options controlling visual hit testing.
Definition: VisualWorld.h:93
bool elementBoundsOnly
Test MeshElement bounds instead of individual triangles.
Definition: VisualWorld.h:95
HitTestSearchMode searchMode
Which matching hits to return.
Definition: VisualWorld.h:94
Timing and progression information associated with a rendered frame.
Definition: VisualWorld.h:70
double updateTime
Runner elapsed time for the containing update, in seconds.
Definition: VisualWorld.h:79
double simulationTime
Time reached by the most recently completed simulation step, in seconds.
Definition: VisualWorld.h:85
std::uint64_t frameIndex
Zero-based index of the successful rendered frame.
Definition: VisualWorld.h:73
std::uint64_t updateIndex
Runner update responsible for this render attempt.
Definition: VisualWorld.h:76
std::uint64_t simulationStepCount
Total number of completed simulation steps.
Definition: VisualWorld.h:89
double updateDeltaTime
Delta time of the containing Runner update, in seconds.
Definition: VisualWorld.h:82