Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Logger.hpp"
5#include <entt/core/hashed_string.hpp>
6#include <glm/glm.hpp>
7
8namespace Object::Component {
9
10struct Camera {
11 using ID = entt::hashed_string;
12
13 glm::mat4 projection;
14 glm::mat4 view;
15 glm::mat4 viewProjection;
17 glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
18 float fov = glm::radians(70.0f);
19 float nearPlane = 0.1f;
20 float farPlane = 100.0f;
21 float aspectRatio = 1.f;
22
23 void Update(const Object::Component::Transform &transform)
24 {
25 glm::vec3 forward = glm::normalize(transform.GetForwardVector() * transform.GetScale());
26 view = glm::lookAtLH(transform.GetPosition(), transform.GetPosition() + forward, this->up);
27 projection = glm::perspectiveLH_ZO(this->fov, aspectRatio, this->nearPlane, this->farPlane);
30 }
31
32 void UpdateAspectRatio(const glm::uvec2 &textureSize)
33 {
34 if (textureSize.y > 0)
35 {
36 aspectRatio = static_cast<float>(textureSize.x) / static_cast<float>(textureSize.y);
37 }
38 else
39 {
40 Log::Warning("GPUCamera::UpdateAspectRatio: texture height is zero, cannot update aspect ratio.");
41 }
42 }
43};
44
45static_assert(sizeof(Camera) == sizeof(glm::mat4) * 4 + sizeof(glm::vec3) + sizeof(float) * 4);
46} // namespace Object::Component
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
Definition AmbientLight.hpp:5
Definition Camera.hpp:10
glm::vec3 up
Definition Camera.hpp:17
glm::mat4 viewProjection
Definition Camera.hpp:15
glm::mat4 inverseViewProjection
Definition Camera.hpp:16
float fov
Definition Camera.hpp:18
entt::hashed_string ID
Definition Camera.hpp:11
glm::mat4 projection
Definition Camera.hpp:13
float nearPlane
Definition Camera.hpp:19
float farPlane
Definition Camera.hpp:20
void Update(const Object::Component::Transform &transform)
Definition Camera.hpp:23
void UpdateAspectRatio(const glm::uvec2 &textureSize)
Definition Camera.hpp:32
glm::mat4 view
Definition Camera.hpp:14
float aspectRatio
Definition Camera.hpp:21
Definition Transform.hpp:18
glm::vec3 GetForwardVector() const
Definition Transform.hpp:73
const glm::vec3 & GetPosition() const
Definition Transform.hpp:37
const glm::vec3 & GetScale() const
Definition Transform.hpp:38