Cogs.Core
CameraSystem.h
1#pragma once
2
3#include "Systems/ComponentSystem.h"
4
5#include "Components/Core/CameraComponent.h"
6
7#include "Services/TaskManager.h"
8
9#include "Utilities/FrustumClassification.h"
10
11#include "CullingData.h"
12
13#include "Rendering/Common.h"
14
15#include "Foundation/Collections/Pool.h"
16#include "Foundation/Geometry/BoundingBox.hpp"
17#include "Foundation/Geometry/Frustum.hpp"
18
19namespace Cogs
20{
21 namespace Core
22 {
23 class Context;
24 struct EnvironmentComponent;
25
27 {
28 enum ERenderPass
29 {
30 Main = 0,
31 Reflection,
32 Refraction,
33 Other
34 };
35 };
36
38 {
39 enum struct Flags : uint32_t {
40 None = 0,
41 NoDepthClip,
42 ScissorTest
43 };
44 Flags flags = Flags::None; // Part of hash
45 float depthBias = 0.f; // Part of hash
46 float depthSlopedBias = 0.f; // Part of hash
47 float depthBiasClamp = 0.f; // Part of hash
48 uint32_t multiViews = 0; // Part of hash. If non-zero, multi-view rendering is requested.
49 glm::ivec4 scissorRectangle{}; // Not part of hash
50
51 void setFlag(Flags flag) { flags = Flags(uint32_t(flags) | uint32_t(flag)); }
52 void unsetFlag(Flags flag) { flags = Flags(uint32_t(flags) & ~uint32_t(flag)); }
53 bool getFlag(Flags flag) const { return (uint32_t(flags) & uint32_t(flag)) != 0; }
54
56 size_t hash = 0;
57
59 COGSCORE_DLL_API void updateHash();
60 };
61
67 {
69 CameraData() = default;
70
72 CameraData(const CameraData & other) = delete;
73
75 CameraData& operator=(const CameraData& other) = default;
76
77 void (*bindRenderTargetCallback)(void* clientData) = nullptr;
78 void* bindRenderTargetCallbackData = nullptr;
79
80 RenderLayers layerMask = RenderLayers::Default;
81 LightingLayers lightingMask = LightingLayers::Default;
82
83 bool reflection = false;
84 bool flipWindingOrder = false;
85 bool useClearColor = false;
86
87 glm::mat4 viewMatrix;
88 glm::mat4 projectionMatrix;
89
90 glm::mat4 viewProjection;
91 glm::mat4 inverseViewMatrix;
92 glm::mat4 inverseViewProjectionMatrix;
93 glm::mat4 inverseProjectionMatrix;
94 glm::mat4 viewFromViewportMatrix;
95
96 glm::mat4 prevViewProjection;
97
98 glm::vec2 viewportSize;
99 glm::vec2 viewportOrigin;
100 glm::vec4 clearColor;
101
102 glm::uvec2 blueNoiseOffset;
103
104 Geometry::Frustum frustum;
105 Geometry::BoundingBox sceneBounds;
106
107 RenderPassOptions* passOptions = nullptr;
108
109 float nearDepthBounds = 0.f;
110 float farDepthBounds = 0.f;
111 float nearClipPlanes = 0.f;
112 float farClipPlanes = 0.f;
113
114 float nearDistance = 0.f;
115 float farDistance = 0.f;
116 float fieldOfView = 0.f;
117 float depthClamp = 0.f;
118 float discardThreshold = 0.f;
119 float keepThreshold = 0.f;
120 float exposure = 2.f;
121
122 uint32_t clientFlags = 0; // Custom client flags passed from the camera to the shaders.
123
127
130 glm::mat4 rawViewProjection;
131 glm::mat4 rawViewCullMatrix;
132
133 std::shared_ptr<CullingData> cullingData;
134
135 RenderPass::ERenderPass renderPass = RenderPass::Main;
136 int rayPickId = -1;
137 };
138
142 class CameraSystem : public ComponentSystemWithDataPool<CameraComponent, CameraData>
143 {
145 public:
146 CameraSystem(Memory::Allocator * allocator, SizeType capacity) :
147 ComponentSystemWithDataPool(allocator, capacity),
148 cullingData(MemBlockType::CullingData)
149 {}
150
152 void update(Context * context) override;
153
154 void postUpdate(Context * context) override;
155
157 void updateProjection(Context * context, const CameraComponent & cameraComponent);
158
160 bool updateClippingPlanes(Context * context, const CameraComponent & cameraComponent, bool secondPass);
161
165
167
170
172 const CameraData & getMainCameraData() const { return getData(getMainCamera()); }
173
175 void postRender(const Context* context);
176
178
179 private:
182 int nextRayPickId = 1;
183 };
184 }
185}
The camera system computes CameraData for every CameraDataComponent in the engine.
Definition: CameraSystem.h:143
void postRender(const Context *context)
Called by the renderer after all rendering is complete.
void setMainCamera(ComponentModel::ComponentHandle mainCamera)
Definition: CameraSystem.h:164
const CameraData & getMainCameraData() const
Get the calculated CameraData of the main camera.
Definition: CameraSystem.h:172
CameraComponent * getMainCamera() const
Get the main camera component.
Definition: CameraSystem.h:169
ComponentHandle createComponent() override
bool updateClippingPlanes(Context *context, const CameraComponent &cameraComponent, bool secondPass)
Update near/far clipping plane calculations.
void updateProjection(Context *context, const CameraComponent &cameraComponent)
Update the projection matrix and derived matrices of a camera.
ComponentModel::ComponentHandle mainCamera
Main camera handle.
Definition: CameraSystem.h:181
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 with parallel data per component stored in a pool similar to how the components them...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Base allocator implementation.
Definition: Allocator.h:30
RenderLayers
Contains common render layers.
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Pool used to store elements of ElementType.
Definition: Pool.h:17
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
ComponentType * resolveComponent() const
Definition: Component.h:90
Contains data describing a Camera instance and its derived data structured such as matrix data and vi...
Definition: CameraSystem.h:67
CameraData(const CameraData &other)=delete
Disabled copy constructor operator to avoid accidental copying when referencing.
CameraData()=default
Default constructs camera data.
glm::mat4 rawProjectionMatrix
Projection matrix that has not been adjusted by the renderer, and is thus appropriate for direct scre...
Definition: CameraSystem.h:129
CameraData & operator=(const CameraData &other)=default
Assign operator must be enabled as Copying is done in FilterListTask.cpp & friends.
COGSCORE_DLL_API void updateHash()