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 glm::mat4 viewportFromViewMatrix;
96
97 glm::mat4 prevViewProjection;
98
99 glm::vec2 viewportSize;
100 glm::vec2 viewportOrigin;
101 glm::vec4 clearColor;
102
103 glm::uvec2 blueNoiseOffset;
104
105 Geometry::Frustum frustum;
106 Geometry::BoundingBox sceneBounds;
107
108 RenderPassOptions* passOptions = nullptr;
109
110 float nearDepthBounds = 0.f;
111 float farDepthBounds = 0.f;
112 float nearClipPlanes = 0.f;
113 float farClipPlanes = 0.f;
114
115 float nearDistance = 0.f;
116 float farDistance = 0.f;
117 float fieldOfView = 0.f;
118 float depthClamp = 0.f;
119 float discardThreshold = 0.f;
120 float keepThreshold = 0.f;
121 float exposure = 2.f;
122
123 uint32_t clientFlags = 0; // Custom client flags passed from the camera to the shaders.
124
128
131 glm::mat4 rawViewProjection;
132 glm::mat4 rawViewCullMatrix;
133
134 std::shared_ptr<CullingData> cullingData;
135
136 RenderPass::ERenderPass renderPass = RenderPass::Main;
137 int rayPickId = -1;
138 };
139
143 class CameraSystem : public ComponentSystemWithDataPool<CameraComponent, CameraData>
144 {
146 public:
147 CameraSystem(Memory::Allocator * allocator, SizeType capacity) :
148 ComponentSystemWithDataPool(allocator, capacity),
149 cullingData(MemBlockType::CullingData)
150 {}
151
153 void update(Context * context) override;
154
155 void postUpdate(Context * context) override;
156
158 void updateProjection(Context * context, const CameraComponent & cameraComponent);
159
161 bool updateClippingPlanes(Context * context, const CameraComponent & cameraComponent, bool secondPass);
162
166
168
171
173 const CameraData & getMainCameraData() const { return getData(getMainCamera()); }
174
176 void postRender(const Context* context);
177
179
180 private:
183 int nextRayPickId = 1;
184 };
185 }
186}
The camera system computes CameraData for every CameraDataComponent in the engine.
Definition: CameraSystem.h:144
void postRender(const Context *context)
Called by the renderer after all rendering is complete.
void setMainCamera(ComponentModel::ComponentHandle mainCamera)
Definition: CameraSystem.h:165
const CameraData & getMainCameraData() const
Get the calculated CameraData of the main camera.
Definition: CameraSystem.h:173
CameraComponent * getMainCamera() const
Get the main camera component.
Definition: CameraSystem.h:170
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:182
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:130
CameraData & operator=(const CameraData &other)=default
Assign operator must be enabled as Copying is done in FilterListTask.cpp & friends.
COGSCORE_DLL_API void updateHash()