ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
camera_system.hpp
Go to the documentation of this file.
1
10
11#ifndef TOYMAKERENGINE_CAMERASYSTEM_H
12#define TOYMAKERENGINE_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 enum class AspectMode: uint8_t {
68 FIXED, //< The camera's aspect remains fixed irrespective of viewport changes.
69 RESIZE //< The camera's aspect is resized to match that of the owning viewport.
70 };
71
76 ProjectionType mProjectionType { ProjectionType::FRUSTUM };
77
82 AspectMode mAspectMode { AspectMode::RESIZE };
83
88 float mFov {45.f};
89
94 float mAspect { 16.f/9.f };
95
100 glm::vec2 mOrthographicDimensions { 19.f, 12.f };
101
106 glm::vec2 mNearFarPlanes { 100.f, -100.f };
107
112 glm::mat4 mProjectionMatrix {};
113
118 glm::mat4 mViewMatrix {};
119
127 inline static std::string getComponentTypeName() { return "CameraProperties"; }
128 };
129
134 NLOHMANN_JSON_SERIALIZE_ENUM(CameraProperties::ProjectionType, {
135 {CameraProperties::ProjectionType::FRUSTUM, "frustum"},
136 {CameraProperties::ProjectionType::ORTHOGRAPHIC, "orthographic"},
137 });
138
143 NLOHMANN_JSON_SERIALIZE_ENUM(CameraProperties::AspectMode, {
144 { CameraProperties::AspectMode::FIXED, "fixed" },
145 { CameraProperties::AspectMode::RESIZE, "resize" },
146 });
147
153 class CameraSystem: public System<CameraSystem, std::tuple<Transform, CameraProperties>, std::tuple<>> {
154 public:
160 explicit CameraSystem(std::weak_ptr<ECSWorld> world):
161 System<CameraSystem, std::tuple<Transform, CameraProperties>, std::tuple<>>{world}
162 {}
163
169
177 static std::string getSystemTypeName() { return "CameraSystem"; }
178
179 private:
188 void onEntityEnabled(EntityID entityID) override;
189
195 void onEntityDisabled(EntityID entityID) override;
196
202 void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override;
203
208 void onSimulationActivated() override;
209
215 void onPreRenderStep(float simulationProgress) override;
216
221 std::set<EntityID> mProjectionUpdateQueue {};
222
227 std::set<EntityID> mViewUpdateQueue {};
228 };
229
240 template<>
242 const CameraProperties& previousState, const CameraProperties& nextState,
243 float simulationProgress
244 ) const {
245 simulationProgress = mProgressLimits(simulationProgress);
246 return {
247 .mProjectionType {previousState.mProjectionType
248 },
249 .mFov { simulationProgress * nextState.mFov + (1.f-simulationProgress) * previousState.mFov },
250 .mAspect { simulationProgress * nextState.mAspect + (1.f-simulationProgress) * previousState.mAspect},
251 .mOrthographicDimensions {
252 (simulationProgress * nextState.mOrthographicDimensions)
253 + (1.f-simulationProgress) * previousState.mOrthographicDimensions
254 },
255 .mNearFarPlanes {
256 (simulationProgress * nextState.mNearFarPlanes)
257 + (1.f-simulationProgress) * previousState.mNearFarPlanes
258 },
259 .mProjectionMatrix {
260 (simulationProgress * nextState.mProjectionMatrix)
261 + ((1.f-simulationProgress) * previousState.mProjectionMatrix)
262 },
263 .mViewMatrix {
264 (simulationProgress * nextState.mViewMatrix)
265 + ((1.f-simulationProgress) * previousState.mViewMatrix)
266 },
267 };
268 }
269
273 inline void from_json(const nlohmann::json& json, CameraProperties& cameraProperties) {
274 assert(json.at("type").get<std::string>() == CameraProperties::getComponentTypeName() && "Type mismatch, json must be of camera properties type");
275 json.at("projection_mode").get_to(cameraProperties.mProjectionType);
276 json.at("fov").get_to(cameraProperties.mFov);
277 json.at("aspect_mode").get_to(cameraProperties.mAspectMode);
278 json.at("aspect").get_to(cameraProperties.mAspect);
279 json.at("orthographic_dimensions")
280 .at("horizontal")
281 .get_to(cameraProperties.mOrthographicDimensions.x);
282 json.at("orthographic_dimensions")
283 .at("vertical")
284 .get_to(cameraProperties.mOrthographicDimensions.y);
285 json.at("near_far_planes").at("near").get_to(cameraProperties.mNearFarPlanes.x);
286 json.at("near_far_planes").at("far").get_to(cameraProperties.mNearFarPlanes.y);
287 }
288
293 inline void to_json(nlohmann::json& json, const CameraProperties& cameraProperties) {
294 json = {
296 {"projection_mode", cameraProperties.mProjectionType},
297 {"fov", cameraProperties.mFov},
298 {"aspect_mode", cameraProperties.mAspectMode},
299 {"aspect", cameraProperties.mAspect},
300 {"orthographic_dimensions", {
301 {"horizontal", cameraProperties.mOrthographicDimensions.x},
302 {"vertical", cameraProperties.mOrthographicDimensions.y},
303 }},
304 {"near_far_planes", {
305 {"near", cameraProperties.mNearFarPlanes.x},
306 {"far", cameraProperties.mNearFarPlanes.y},
307 }}
308 };
309 }
310
311}
312#endif
CameraSystem(std::weak_ptr< ECSWorld > world)
Construct a new CameraSystem object.
Definition camera_system.hpp:160
static std::string getSystemTypeName()
Returns the ECS system type string for this object.
Definition camera_system.hpp:177
std::set< EntityID > mProjectionUpdateQueue
Entities whose camera properties were updated this frame, whose projection matrix should be recompute...
Definition camera_system.hpp:221
void onEntityEnabled(EntityID entityID) override
Adds enabled entity to the projection update and view update queues.
Definition camera_system.cpp:52
void onEntityUpdated(EntityID entityID, ComponentType updatedComponent) override
Adds entity to projection and view update queues.
Definition camera_system.cpp:61
void updateActiveCameraMatrices()
Updates all matrices associated with active cameras in this world per their properties and positions.
Definition camera_system.cpp:11
void onSimulationActivated() override
Initializes the CameraSystem, querying and adding all eligible entities to update queues.
Definition camera_system.cpp:72
std::set< EntityID > mViewUpdateQueue
Entities whose position or rotation were updated this frame, whose view matrix should be recomputed a...
Definition camera_system.hpp:227
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:67
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:352
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1085
ToyMaker Engine's implementation of an ECS system.
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:101
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:2378
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:66
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:26
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:112
AspectMode
The way this camera's aspect is modified when its owning viewport changes.
Definition camera_system.hpp:67
ProjectionType mProjectionType
The type of projection used by the camera.
Definition camera_system.hpp:76
glm::mat4 mViewMatrix
The view matrix of the camera, which transforms all vertices from their world coordinates to their co...
Definition camera_system.hpp:118
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:106
AspectMode mAspectMode
The way this camera's aspect is modified when its owning viewport changes.
Definition camera_system.hpp:82
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:94
float mFov
(If ProjectionType::FRUSTUM) The vertical Field of View described by the camera, used to calculate mP...
Definition camera_system.hpp:88
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:127
glm::vec2 mOrthographicDimensions
(If ProjectionType::ORTHOGRAPHIC) The dimensions, in scene units, of the screen face of the viewing v...
Definition camera_system.hpp:100
The transform component, which moves the vertices of a model to their world space coordinates during ...
Definition scene_components.hpp:150