Cogs.Core
InstancedMeshRenderSystem.h
1#pragma once
2
3#include "RenderSystem.h"
4#include "Scene/GetBounds.h"
5#include "Scene/RayPick.h"
6
7#include "../../Components/Core/InstancedMeshRenderComponent.h"
8
9#include "Foundation/Geometry/BoundingBox.hpp"
10
11namespace Cogs::Core
12{
13 class Context;
14
15 class InstancedMeshRenderSystem;
16
19 public:
20 explicit InstancedMeshRenderBounds(InstancedMeshRenderSystem* system) : system(system) {}
21
22 virtual void getBounds(Context* context, Cogs::Geometry::BoundingBox& bounds) override;
23 virtual bool getBounds(Context* context, const ComponentModel::Entity* entity, Cogs::Geometry::BoundingBox& bounds, bool ignoreVisibility) const override;
24
25 private:
26 InstancedMeshRenderSystem* system = nullptr;
27 };
28
30 {
31 explicit InstancedMeshPicker(InstancedMeshRenderSystem* system) : system(system) {}
32
33 private:
34 bool pickImpl(Context* context,
35 const glm::mat4& worldPickMatrix,
36 const glm::mat4& rawViewProjection,
37 const glm::mat4& viewMatrix,
38 const RayPicking::RayPickFilter& filter,
39 PickingFlags pickingFlags,
40 PicksReturned returnFlag,
41 std::vector<RayPicking::RayPickHit>& hits) override;
42
43 InstancedMeshRenderSystem* system = nullptr;
44 };
45
46 // ----
47
49 {
50 MeshRenderDataFlags flags = MeshRenderDataFlags::None;
51 uint32_t cullingIndex = NoCullingIndex;
52
53 uint16_t localBoundsGeneration = 0;
54 uint16_t worldBoundsGeneration = 0;
55 uint8_t meshBoundsGeneration = 0;
56 uint8_t instanceMeshBoundsGeneration = 0;
57
58 glm::mat4 localToWorld;
59
62 };
63
67 class InstancedMeshRenderSystem : public ComponentSystemWithDataPools<InstancedMeshRenderComponent, InstancedMeshRenderData, LocalBounds, WorldBounds>
68 {
70 public:
71 InstancedMeshRenderSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPools(allocator, capacity) {}
72
73 void initialize(Context * context) override;
74 void update(Context * context) override;
75 void postUpdate(Context * context) override;
76 void cleanup(Context * context) override;
77
79 void destroyComponent(ComponentHandle component) override;
80
81 TaskId getTaskGroup() const { return geometryGroup; }
82
83 void setLocalBounds(const InstancedMeshRenderComponent * component, const Geometry::BoundingBox & bbox)
84 {
85 getLocalBounds(component) = bbox;
86 auto & data = getData<InstancedMeshRenderData>(component);
87 data.flags |= MeshRenderDataFlags::LocalBoundsOverride;
88 ++data.localBoundsGeneration;
89 }
90
91 Geometry::BoundingBox & getLocalBounds(const InstancedMeshRenderComponent * component)
92 {
93 return this->getData<LocalBounds>(component);
94 }
95
96 Geometry::BoundingBox & getWorldBounds(const InstancedMeshRenderComponent * component)
97 {
98 return this->getData<WorldBounds>(component);
99 }
100
101 void initializeCulling(struct CullingSource* cullSource);
102
103 size_t getGeneration() const { return generation; }
104
105 class TransformSystem * transformSystem = nullptr;
106 private:
107
108 bool needsPost = true;
109 size_t generation = 0;
110 TaskId geometryGroup = NoTask;
111 std::unique_ptr<InstancedMeshRenderBounds> bounds;
112 std::unique_ptr<InstancedMeshPicker> picker;
113 };
114}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
Context * context
Pointer to the Context instance the system lives in.
void postUpdate()
Perform post update logic in the system.
void update()
Updates the system state to that of the current frame.
Component system template with multiple parallel structures per component stored in separate pools si...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Implement custom bounds calculation.
virtual void getBounds(Context *context, Cogs::Geometry::BoundingBox &bounds) override
Expand bounds including bounds of all entities in this system in world coordinates.
ComponentHandle createComponent() override
Create a new component instance.
void destroyComponent(ComponentHandle component) override
Destroy the component held by the given handle.
void initialize(Context *context) override
Initialize the system.
void cleanup(Context *context) override
Provided for custom cleanup logic in derived systems.
Base class for picking meshes.
Definition: RayPick.h:172
The transform system handles TransformComponent instances, calculating local and global transform dat...
Base allocator implementation.
Definition: Allocator.h:30
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
PicksReturned
  * Options for returning picking hits.
Definition: PickingFlags.h:40
PickingFlags
Options for COGS picking.
Definition: PickingFlags.h:12
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
static ComponentHandle Empty()
Returns an empty, invalid handle. Will evaluate to false if tested against using operator bool().
Definition: Component.h:119
bool pickImpl(Context *context, const glm::mat4 &worldPickMatrix, const glm::mat4 &rawViewProjection, const glm::mat4 &viewMatrix, const RayPicking::RayPickFilter &filter, PickingFlags pickingFlags, PicksReturned returnFlag, std::vector< RayPicking::RayPickHit > &hits) override
Each mesh rendering system should implement this function that goes through all components and calls ...
Task id struct used to identify unique Task instances.
Definition: TaskManager.h:20