Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
camera_system.hpp
Go to the documentation of this file.
1
10
11#ifndef FOOLSENGINE_CAMERASYSTEM_H
12#define FOOLSENGINE_CAMERASYSTEM_H
13
14#include <glm/glm.hpp>
15#include <nlohmann/json.hpp>
16
17#include "core/ecs_world.hpp"
18#include "scene_components.hpp"
19
20namespace ToyMaker {
21
56 enum class ProjectionType: uint8_t {
57
58 FRUSTUM, //< A frustum camera, or a camera whose view looks like a pyramid. Objects further off appear smaller than objects close by.
59
60 ORTHOGRAPHIC, //< A camera where measurements along a dimension are the same regardless of how close or far an object is, where the view space looks like a cuboid.
61 };
62
67 ProjectionType mProjectionType { ProjectionType::FRUSTUM };
68
73 float mFov {45.f};
74
79 float mAspect { 16.f/9.f };
80
85 glm::vec2 mOrthographicDimensions { 19.f, 12.f };
86
91 glm::vec2 mNearFarPlanes { 100.f, -100.f };
92
97 glm::mat4 mProjectionMatrix {};
98
103 glm::mat4 mViewMatrix {};
104
112 inline static std::string getComponentTypeName() { return "CameraProperties"; }
113 };
114
119 NLOHMANN_JSON_SERIALIZE_ENUM(CameraProperties::ProjectionType, {
120 {CameraProperties::ProjectionType::FRUSTUM, "frustum"},
121 {CameraProperties::ProjectionType::ORTHOGRAPHIC, "orthographic"},
122 });
123
129 class CameraSystem: public System<CameraSystem, std::tuple<Transform, CameraProperties>, std::tuple<>> {
130 public:
136 explicit CameraSystem(std::weak_ptr<ECSWorld> world):
137 System<CameraSystem, std::tuple<Transform, CameraProperties>, std::tuple<>>{world}
138 {}
139
145
153 static std::string getSystemTypeName() { return "CameraSystem"; }
154
155 private:
164 void onEntityEnabled(EntityID entityID) override;
165
171 void onEntityDisabled(EntityID entityID) override;
172
178 void onEntityUpdated(EntityID entityID) override;
179
184 void onSimulationActivated() override;
185
191 void onPreRenderStep(float simulationProgress) override;
192
197 std::set<EntityID> mProjectionUpdateQueue {};
198
203 std::set<EntityID> mViewUpdateQueue {};
204 };
205
216 template<>
218 const CameraProperties& previousState, const CameraProperties& nextState,
219 float simulationProgress
220 ) const {
221 simulationProgress = mProgressLimits(simulationProgress);
222 return {
223 .mProjectionType {previousState.mProjectionType
224 },
225 .mFov { simulationProgress * nextState.mFov + (1.f-simulationProgress) * previousState.mFov },
226 .mAspect { simulationProgress * nextState.mAspect + (1.f-simulationProgress) * previousState.mAspect},
227 .mOrthographicDimensions {
228 (simulationProgress * nextState.mOrthographicDimensions)
229 + (1.f-simulationProgress) * previousState.mOrthographicDimensions
230 },
231 .mNearFarPlanes {
232 (simulationProgress * nextState.mNearFarPlanes)
233 + (1.f-simulationProgress) * previousState.mNearFarPlanes
234 },
235 .mProjectionMatrix {
236 (simulationProgress * nextState.mProjectionMatrix)
237 + ((1.f-simulationProgress) * previousState.mProjectionMatrix)
238 },
239 .mViewMatrix {
240 (simulationProgress * nextState.mViewMatrix)
241 + ((1.f-simulationProgress) * previousState.mViewMatrix)
242 },
243 };
244 }
245
249 inline void from_json(const nlohmann::json& json, CameraProperties& cameraProperties) {
250 assert(json.at("type").get<std::string>() == CameraProperties::getComponentTypeName() && "Type mismatch, json must be of camera properties type");
251 json.at("projection_mode").get_to(cameraProperties.mProjectionType);
252 json.at("fov").get_to(cameraProperties.mFov);
253 json.at("aspect").get_to(cameraProperties.mAspect);
254 json.at("orthographic_dimensions")
255 .at("horizontal")
256 .get_to(cameraProperties.mOrthographicDimensions.x);
257 json.at("orthographic_dimensions")
258 .at("vertical")
259 .get_to(cameraProperties.mOrthographicDimensions.y);
260 json.at("near_far_planes").at("near").get_to(cameraProperties.mNearFarPlanes.x);
261 json.at("near_far_planes").at("far").get_to(cameraProperties.mNearFarPlanes.y);
262 }
263
265 inline void to_json(nlohmann::json& json, const CameraProperties& cameraProperties) {
266 json = {
268 {"projection_mode", cameraProperties.mProjectionType},
269 {"fov", cameraProperties.mFov},
270 {"aspect", cameraProperties.mAspect},
271 {"orthographic_dimensions", {
272 {"horizontal", cameraProperties.mOrthographicDimensions.x},
273 {"vertical", cameraProperties.mOrthographicDimensions.y},
274 }},
275 {"near_far_planes", {
276 {"near", cameraProperties.mNearFarPlanes.x},
277 {"far", cameraProperties.mNearFarPlanes.y},
278 }}
279 };
280 }
281
282}
283#endif
CameraSystem(std::weak_ptr< ECSWorld > world)
Construct a new CameraSystem object.
Definition camera_system.hpp:136
static std::string getSystemTypeName()
Returns the ECS system type string for this object.
Definition camera_system.hpp:153
std::set< EntityID > mProjectionUpdateQueue
Entities whose camera properties were updated this frame, whose projection matrix should be recompute...
Definition camera_system.hpp:197
void onEntityEnabled(EntityID entityID) override
Adds enabled entity to the projection update and view update queues.
Definition camera_system.cpp:52
void updateActiveCameraMatrices()
Updates all matrices associated with active cameras in this world per their properties and positions.
Definition camera_system.cpp:12
void onSimulationActivated() override
Initializes the CameraSystem, querying and adding all eligible entities to update queues.
Definition camera_system.cpp:71
std::set< EntityID > mViewUpdateQueue
Entities whose position or rotation were updated this frame, whose view matrix should be recomputed a...
Definition camera_system.hpp:203
void onPreRenderStep(float simulationProgress) override
The step in which new projection and view matrices are actually computed for all active cameras.
Definition camera_system.cpp:66
void onEntityUpdated(EntityID entityID) override
Adds entity to projection and view update queues.
Definition camera_system.cpp:61
void onEntityDisabled(EntityID entityID) override
Removes extra entity related structures from system bookkeeping, if necessary.
Definition camera_system.cpp:56
RangeMapperLinear mProgressLimits
a functor that performs the actual interpolation in the default case
Definition ecs_world.hpp:354
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1062
ToyMaker Engine's implementation of an ECS system.
T operator()(const T &previousState, const T &nextState, float simulationProgress=1.f) const
Returns an interpolated value for a component between two given states.
Definition ecs_world.hpp:2346
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
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
STL namespace.
Stores structs and classes for common components used by the SceneSystem and other related Systems.
Struct that encapsulates properties which define the (geometric) aspects of a scene camera.
Definition camera_system.hpp:51
glm::mat4 mProjectionMatrix
The projection matrix of the camera, computed based on its other properties.
Definition camera_system.hpp:97
ProjectionType mProjectionType
The type of projection used by the camera.
Definition camera_system.hpp:67
glm::mat4 mViewMatrix
The view matrix of the camera, which transforms all vertices from their world coordinates to their co...
Definition camera_system.hpp:103
glm::vec2 mNearFarPlanes
The distance, in scene units, at which the near and far planes of the viewing volume of are located r...
Definition camera_system.hpp:91
float mAspect
The ratio of the x dimension to the y dimension of the screen or image associated with the camera.
Definition camera_system.hpp:79
float mFov
(If ProjectionType::FRUSTUM) The vertical Field of View described by the camera, used to calculate mP...
Definition camera_system.hpp:73
ProjectionType
The type of projection used by this camera.
Definition camera_system.hpp:56
static std::string getComponentTypeName()
The component type string of the camera properties component.
Definition camera_system.hpp:112
glm::vec2 mOrthographicDimensions
(If ProjectionType::ORTHOGRAPHIC) The dimensions, in scene units, of the screen face of the viewing v...
Definition camera_system.hpp:85
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:94