3 #include <entt/entt.hpp>
4 #include <core/Assertions.h>
5 #include <math/Types.h>
6 #include <utility/Types.h>
7 #include "LayerComponents.h"
8 #include "TransformComponent.h"
13 enum class ProjectionMode {
18 explicit CameraComponent(ProjectionMode mode = ProjectionMode::PERSPECTIVE,
float fov_ = 70.f,
19 float nearDistance_ = 0.1f,
float farDistance_ = 1024.f,
float orthoSize_ = 10.f,
21 : projectionMode(mode)
24 , nearDistance(nearDistance_)
25 , farDistance(farDistance_)
26 , orthoSize(orthoSize_)
28 foreach(LAYER_COMPONENTS, [&](
auto layer) {
29 this->activeLayers += decltype(layer)::index;
33 [[nodiscard]] glm::mat4 getProjection(glm::vec2i size)
const {
34 size.x = std::max(size.x, 1);
35 size.y = std::max(size.y, 1);
36 float aspectRatio =
static_cast<float>(size.x) /
static_cast<float>(size.y);
38 switch (this->projectionMode) {
39 using enum ProjectionMode;
41 return glm::perspective(glm::radians(this->fov), aspectRatio, this->nearDistance, this->farDistance);
44 float orthoRight = this->orthoSize * aspectRatio / 2;
45 float orthoLeft = -orthoRight;
46 float orthoTop = this->orthoSize / 2;
47 float orthoBottom = -orthoTop;
48 return glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, this->nearDistance, this->farDistance);
54 [[nodiscard]] glm::mat4 getView()
const {
55 const glm::vec3 position = this->transform->getPosition();
56 return glm::lookAt(position, position + this->transform->getFrontVector(), this->transform->getUpVector());
69 ProjectionMode projectionMode;
72 std::size_t activeLayers;