PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Camera.hpp
1#pragma once
2
3#include <glm/glm.hpp>
4#include <glm/gtc/constants.hpp>
5#include <glm/gtc/matrix_transform.hpp>
6#include <glm/gtx/quaternion.hpp>
7
8namespace PixelBullet
9{
16 class Camera
17 {
18 public:
25 explicit Camera(float fov = glm::radians(45.0f), float nearPlane = 0.1f, float farPlane = 1000.0f)
26 : m_FOV(fov)
27 , m_Near(nearPlane)
28 , m_Far(farPlane)
29 , m_Position(0.0f)
30 , m_RotationEuler(0.0f)
31 , m_ViewMatrix(1.0f)
32 , m_ProjectionMatrix(1.0f)
33 {
34 }
35
36 virtual ~Camera() = default;
37
38 virtual void Start()
39 {
40 }
41 virtual void Update()
42 {
43 }
44
46 void SetPosition(const glm::vec3& pos)
47 {
48 m_Position = pos;
49 RecalculateView();
50 }
52 void SetRotation(const glm::vec3& rotEuler)
53 {
54 m_RotationEuler = rotEuler;
55 RecalculateView();
56 }
57
65 void SetPerspective(float fov, float aspect, float nearPlane, float farPlane)
66 {
67 m_FOV = fov;
68 m_Near = nearPlane;
69 m_Far = farPlane;
70 m_ProjectionMatrix = glm::perspective(fov, aspect, nearPlane, farPlane);
71 }
72
73 [[nodiscard]] float GetFOV() const
74 {
75 return m_FOV;
76 }
77 [[nodiscard]] float GetNearPlane() const
78 {
79 return m_Near;
80 }
81 [[nodiscard]] float GetFarPlane() const
82 {
83 return m_Far;
84 }
85
86 [[nodiscard]] const glm::vec3& GetPosition() const
87 {
88 return m_Position;
89 }
90 [[nodiscard]] const glm::vec3& GetRotationEuler() const
91 {
92 return m_RotationEuler;
93 }
94
95 [[nodiscard]] const glm::mat4& GetViewMatrix() const
96 {
97 return m_ViewMatrix;
98 }
99 [[nodiscard]] const glm::mat4& GetProjectionMatrix() const
100 {
101 return m_ProjectionMatrix;
102 }
103
104 [[nodiscard]] glm::vec3 GetForward() const
105 {
106 return glm::normalize(glm::rotate(GetRotationQuat(), glm::vec3(0, 0, -1)));
107 }
108
109 [[nodiscard]] glm::vec3 GetRight() const
110 {
111 return glm::normalize(glm::rotate(GetRotationQuat(), glm::vec3(1, 0, 0)));
112 }
113
114 [[nodiscard]] glm::vec3 GetUp() const
115 {
116 return glm::normalize(glm::rotate(GetRotationQuat(), glm::vec3(0, 1, 0)));
117 }
118
119 protected:
120 [[nodiscard]] glm::quat GetRotationQuat() const
121 {
122 return { glm::radians(m_RotationEuler) };
123 }
124
125 void RecalculateView()
126 {
127 auto rotation = GetRotationQuat();
128 auto target = m_Position + glm::rotate(rotation, glm::vec3(0, 0, -1));
129 m_ViewMatrix = glm::lookAt(m_Position, target, glm::rotate(rotation, glm::vec3(0, 1, 0)));
130 }
131
132 protected:
133 float m_FOV;
134 float m_Near;
135 float m_Far;
136
137 glm::vec3 m_Position;
138 glm::vec3 m_RotationEuler;
139
140 glm::mat4 m_ViewMatrix;
142 };
143} // namespace PixelBullet
A base 3D camera class with perspective projection.
Definition Camera.hpp:17
glm::vec3 m_Position
World-space position.
Definition Camera.hpp:137
void SetRotation(const glm::vec3 &rotEuler)
Sets rotation in Euler angles (degrees), recalculates view matrix.
Definition Camera.hpp:52
float m_FOV
Field of view in radians.
Definition Camera.hpp:133
glm::mat4 m_ViewMatrix
View matrix.
Definition Camera.hpp:140
void SetPosition(const glm::vec3 &pos)
Sets world position and recalculates the view matrix.
Definition Camera.hpp:46
Camera(float fov=glm::radians(45.0f), float nearPlane=0.1f, float farPlane=1000.0f)
Constructs a camera with a perspective projection.
Definition Camera.hpp:25
glm::vec3 m_RotationEuler
Rotation in degrees (pitch, yaw, roll).
Definition Camera.hpp:138
float m_Near
Near clipping plane.
Definition Camera.hpp:134
float m_Far
Far clipping plane.
Definition Camera.hpp:135
glm::mat4 m_ProjectionMatrix
Projection matrix.
Definition Camera.hpp:141
void SetPerspective(float fov, float aspect, float nearPlane, float farPlane)
Sets the perspective projection matrix.
Definition Camera.hpp:65