ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
system.hpp
Go to the documentation of this file.
1
10
16
17#ifndef TOYMAKERENGINE_SPATIALQUERYSYSTEM_H
18#define TOYMAKERENGINE_SPATIALQUERYSYSTEM_H
19
20#include <memory>
21
22#include "octree.hpp"
23#include "../scene_system.hpp"
24#include "../core/ecs_world.hpp"
25#include "../model.hpp"
26#include "../light.hpp"
27
28namespace ToyMaker {
29
39 class SpatialQuerySystem: public System<SpatialQuerySystem, std::tuple<Transform, ObjectBounds>, std::tuple<AxisAlignedBounds>> {
40 public:
41 explicit SpatialQuerySystem(std::weak_ptr<ECSWorld> world):
42 System<SpatialQuerySystem, std::tuple<Transform, ObjectBounds>, std::tuple<AxisAlignedBounds>>{ world }
43 {}
44
51 static std::string getSystemTypeName() { return "SpatialQuerySystem"; }
52
57 class StaticModelBoundsComputeSystem: public System<StaticModelBoundsComputeSystem, std::tuple<std::shared_ptr<StaticModel>, Transform, ObjectBounds>, std::tuple<>> {
58 public:
59
60 explicit StaticModelBoundsComputeSystem(std::weak_ptr<ECSWorld> world):
62 {}
63
69 static std::string getSystemTypeName() { return "SpatialQuerySystem::StaticModelBoundsComputeSystem"; }
70
71 private:
72
78 void onEntityEnabled(EntityID entityID) override;
79
85 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
86
92 void recomputeObjectBounds(EntityID entityID);
93 };
94
103 class LightBoundsComputeSystem: public System<LightBoundsComputeSystem, std::tuple<LightEmissionData, ObjectBounds>, std::tuple<>> {
104
105 public:
106
107 explicit LightBoundsComputeSystem(std::weak_ptr<ECSWorld> world):
109 {}
110
116 static std::string getSystemTypeName() { return "SpatialQuerySystem::LightBoundsComputeSystem"; }
117
118 private:
119
125 void onEntityEnabled(EntityID entityID) override;
126
132 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
133
139 void recomputeObjectBounds(EntityID entityID);
140 };
141
150 std::vector<std::pair<EntityID, AxisAlignedBounds>> findEntitiesOverlappingCoarse(const AxisAlignedBounds& searchBounds, InteractionLayerMask interactionMask=std::numeric_limits<InteractionLayerMask>::max()) const;
151
160 std::vector<std::pair<EntityID, AxisAlignedBounds>> findEntitiesOverlappingCoarse(const Ray& ray, InteractionLayerMask interactionMask=std::numeric_limits<InteractionLayerMask>::max()) const;
161
170 std::vector<std::shared_ptr<SceneNodeCore>> findNodesOverlappingCoarse(const Ray& searchRay, InteractionLayerMask interactionMask=std::numeric_limits<InteractionLayerMask>::max());
171
180 std::vector<std::shared_ptr<SceneNodeCore>> findNodesOverlappingCoarse(const AxisAlignedBounds& searchBounds, InteractionLayerMask interactionMask=std::numeric_limits<InteractionLayerMask>::max());
181
187 EntityID entity,
189 const ObjectBounds::TrueVolume& volume,
190 const glm::vec3& positionOffset,
191 const glm::quat& orientationOffset
192 );
193
200 inline void updateBounds(
201 const std::shared_ptr<SceneNodeCore>& sceneNode,
203 const ObjectBounds::TrueVolume& volume,
204 const glm::vec3& positionOffset,
205 const glm::quat& orientationOffset
206 ) {
207 assert(sceneNode && "Scene node pointer cannot be nullptr");
208 updateBoundingVolume(sceneNode->getEntityID(),
209 volumeType, volume,
210 positionOffset, orientationOffset
211 );
212 };
213
219 EntityID entity
220 );
221
222 private:
231 void updateBounds(EntityID entity, bool forceUpdateFromTransform=false);
232
243 void updateTransform(EntityID entity);
244
250 void rebuildOctree();
251
256 void onSimulationActivated() override;
257
265 void onSimulationPostStep(uint32_t timestepMillis) override;
266
273 void onPostTransformUpdate(uint32_t timestepMillis) override;
274
281 void onEntityEnabled(EntityID entityID) override;
282
288 void onEntityDisabled(EntityID entityID) override;
289
296 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
297
303 std::unique_ptr<Octree> mOctree { nullptr };
304
310 std::set<EntityID> mUpdateQueueAABB {};
311
317 std::set<EntityID> mUpdateQueueTransform {};
318
324 };
325
326 // Prevent enabling and disabling of spatial query related systems, leave their management entirely to
327 // the scene system
328 template <>
329 inline void SceneNodeCore::setEnabled<SpatialQuerySystem>(bool) {/* pass */}
330 template <>
332 template <>
334
335 template <>
336 inline void SceneNodeCore::updateComponent<AxisAlignedBounds>(const AxisAlignedBounds& axisAlignedBoxBounds) {
337 (void)axisAlignedBoxBounds; // prevent unused parameter warnings
338 assert(false && "Cannot update a scene node's WorldBounds component");
339 }
340 template<>
341 inline void SceneNodeCore::updateComponent<ObjectBounds>(const ObjectBounds& objectBounds) {
342 (void)objectBounds; // prevent unused parameter warnings
343 assert(false && "Cannot update a scene node's ObjectBounds component");
344 }
345 template <>
347 assert(false && "Cannot remove a scene node's AABBTreeNode component");
348 }
349 template <>
351 assert(false && "Cannot remove a scene node's ObjectBounds component");
352 }
353}
354
355#endif
An object containing a coarse simplified representation, AABB, of spatially queryable objects.
Definition types.hpp:1322
void updateComponent(const TComponent &component)
Updates the value of a component of this node (to what it should be at the start of the next simulati...
Definition scene_system.hpp:2029
void setEnabled(bool state)
Sets whether or not a given system should be able to influence this scene object.
Definition scene_system.hpp:2053
void removeComponent()
Removes a component present on this node.
Definition scene_system.hpp:2043
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Updates a light entity's bounds when its position or emissive properties were updated.
Definition system.cpp:158
void onEntityEnabled(EntityID entityID) override
Updates a light entity's bounds when it has just been made active.
Definition system.cpp:150
void recomputeObjectBounds(EntityID entityID)
The method responsible for actually recomputing the light entity's object bounds.
Definition system.cpp:174
static std::string getSystemTypeName()
Gets the system type string associated with this class.
Definition system.hpp:116
void onEntityEnabled(EntityID entityID) override
Computes object bounds for an entity as soon as its enabled for this system.
Definition system.cpp:47
static std::string getSystemTypeName()
The system type string for this class.
Definition system.hpp:69
void recomputeObjectBounds(EntityID entityID)
The method responsible for actually recomputing an entity's object bounds.
Definition system.cpp:71
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Computes object bounds for an entity when it has moved or been scaled.
Definition system.cpp:55
std::set< EntityID > mUpdateQueueTransform
A list of entities whose transforms should be recomputed at the next simulation step.
Definition system.hpp:317
void onSimulationPostStep(uint32_t timestepMillis) override
Initializes the tree if necessary, and recomputes octree position for entities whose ObjectBounds wer...
Definition system.cpp:269
static std::string getSystemTypeName()
Gets the system type string associated with this class.
Definition system.hpp:51
std::vector< std::pair< EntityID, AxisAlignedBounds > > findEntitiesOverlappingCoarse(const AxisAlignedBounds &searchBounds, InteractionLayerMask interactionMask=std::numeric_limits< InteractionLayerMask >::max()) const
Returns a list of entities (by their IDs) whose AABBs overlap a search AABB.
Definition system.cpp:33
void rebuildOctree()
Triggers the destruction of any presently existing octree and replaces it with a new one.
Definition system.cpp:222
void onSimulationActivated() override
Marks this system as requiring initialization on the nearest update.
Definition system.cpp:265
void onEntityEnabled(EntityID entityID) override
Marks this entity as requiring an octree update on the nearest simulation step.
Definition system.cpp:248
void updateBoundingVolume(EntityID entity, ObjectBounds::TrueVolumeType volumeType, const ObjectBounds::TrueVolume &volume, const glm::vec3 &positionOffset, const glm::quat &orientationOffset)
Manually updates the volume component of an entity.
Definition system.cpp:301
bool mRequiresInitialization
Whether a fresh Octree should be built for this system as soon as possible.
Definition system.hpp:323
void onPostTransformUpdate(uint32_t timestepMillis) override
Uses latest transforms to update object bounds if required.
Definition system.cpp:284
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Marks this entity as requiring an octree update on the nearest simulation step.
Definition system.cpp:257
std::vector< std::shared_ptr< SceneNodeCore > > findNodesOverlappingCoarse(const Ray &searchRay, InteractionLayerMask interactionMask=std::numeric_limits< InteractionLayerMask >::max())
Gets a list of nodes (by their base class pointers) whose AABBs intersect a search ray.
Definition system.cpp:7
void updateTransform(EntityID entity)
Updates the world transform of an entity based on its objects bounds.
Definition system.cpp:205
void updateBounds(const std::shared_ptr< SceneNodeCore > &sceneNode, ObjectBounds::TrueVolumeType volumeType, const ObjectBounds::TrueVolume &volume, const glm::vec3 &positionOffset, const glm::quat &orientationOffset)
Manually updates the object bounds component of a scene object.
Definition system.hpp:200
void onEntityDisabled(EntityID entityID) override
Removes this entity from the Octree.
Definition system.cpp:252
void resetBoundsOverride(EntityID entity)
Allows bounds to be computed by spatial bounds compute systems.
Definition system.cpp:351
std::set< EntityID > mUpdateQueueAABB
A list of entities whose AABBs should be recomputed at the next simulation step.
Definition system.hpp:310
std::unique_ptr< Octree > mOctree
Pointer to the octree, the data structure responsible for spatially indexing scene objects.
Definition system.hpp:303
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1087
ToyMaker Engine's implementation of an ECS system.
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:103
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
uint16_t InteractionLayerMask
Bit field where each bit represents an interaction layer – a physics/spatial layer that can trigger o...
Definition types.hpp:57
A file that contains definitions for different types of lights, as components to entities,...
Classes, constructors for this engine's representation of 3D models.
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:25
Data structures, functions, and methods relating to the hierarchical organization and representation ...
System classes relating to the SceneSystem, which in some ways lies at the heart of the engine.
A component defining the true bounds of a spatially queryable object situated somewhere in the world.
Definition types.hpp:845
TrueVolumeType
The types of volumes supported by the engine.
Definition types.hpp:857
A set of numbers describing a ray with its source at some finite point in the world,...
Definition types.hpp:447
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:150
A union of supported volume structs.
Definition types.hpp:867