lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
camera.hpp
Go to the documentation of this file.
1
11
12#pragma once
13
14#include <glm/glm.hpp>
15#include <glm/gtc/matrix_transform.hpp>
16
17namespace lmgl {
18
19namespace scene {
20
28class Camera {
29 public:
33 enum class ProjectionMode { Perspective, Orthographic };
34
45 Camera(float fov = 45.0f, float aspect = 16.0f / 9.0f, float near = 0.1f, float far = 100.0f);
46
57 void set_perspective(float fov, float aspect, float near, float far);
58
71 void set_orthographic(float left, float right, float bottom, float top, float near, float far);
72
80 inline void set_position(const glm::vec3 &position) {
81 m_position = position;
82 m_view_dirty = true;
83 }
84
92 inline void set_target(const glm::vec3 &target) {
93 m_target = target;
94 m_view_dirty = true;
95 }
96
104 inline void set_up(const glm::vec3 &up) {
105 m_up = up;
106 m_view_dirty = true;
107 }
108
114 inline const glm::vec3 &get_position() const { return m_position; }
115
121 inline const glm::vec3 &get_target() const { return m_target; }
122
128 inline const glm::vec3 &get_up() const { return m_up; }
129
138 const glm::mat4 &get_view_matrix() const;
139
147 inline const glm::mat4 &get_projection_matrix() const { return m_projection; }
148
156 glm::mat4 get_view_projection_matrix() const;
157
170 glm::vec3 unproject(float screen_x, float screen_y, float screen_width, float screen_height) const;
171
177 inline ProjectionMode get_projection_mode() const { return m_mode; }
178
184 inline float get_aspect() const { return m_aspect; }
185
194 void set_aspect(float aspect);
195
196 private:
197
199 glm::vec3 m_position;
200
202 glm::vec3 m_target;
203
205 glm::vec3 m_up;
206
208 mutable glm::mat4 m_view;
209
211 glm::mat4 m_projection;
212
214 ProjectionMode m_mode;
215
217 mutable bool m_view_dirty;
218
220 float m_fov;
221
223 float m_aspect;
224
226 float m_near;
227
229 float m_far;
230};
231
232} // namespace scene
233
234} // namespace lmgl
void set_orthographic(float left, float right, float bottom, float top, float near, float far)
Set the camera to orthographic projection mode.
Definition camera.cpp:40
void set_up(const glm::vec3 &up)
Set the camera's up vector.
Definition camera.hpp:104
glm::vec3 unproject(float screen_x, float screen_y, float screen_width, float screen_height) const
Unproject screen coordinates to world space.
Definition camera.cpp:57
const glm::vec3 & get_target() const
Get the camera target.
Definition camera.hpp:121
ProjectionMode
Projection mode for the camera.
Definition camera.hpp:33
float get_aspect() const
Get the current aspect ratio.
Definition camera.hpp:184
void set_perspective(float fov, float aspect, float near, float far)
Set the camera to perspective projection mode.
Definition camera.cpp:24
ProjectionMode get_projection_mode() const
Get the current projection mode.
Definition camera.hpp:177
Camera(float fov=45.0f, float aspect=16.0f/9.0f, float near=0.1f, float far=100.0f)
Constructor for the Camera class.
Definition camera.cpp:9
void set_target(const glm::vec3 &target)
Set the camera target/look-at point.
Definition camera.hpp:92
const glm::mat4 & get_projection_matrix() const
Get the projection matrix.
Definition camera.hpp:147
glm::mat4 get_view_projection_matrix() const
Get the combined view-projection matrix.
Definition camera.cpp:55
void set_aspect(float aspect)
Set a new aspect ratio.
Definition camera.cpp:33
const glm::vec3 & get_position() const
Get the camera position.
Definition camera.hpp:114
const glm::mat4 & get_view_matrix() const
Get the view matrix.
Definition camera.cpp:47
void set_position(const glm::vec3 &position)
Set the camera position.
Definition camera.hpp:80
const glm::vec3 & get_up() const
Get the camera up vector.
Definition camera.hpp:128
Namespace for scene-related classes and functions.
Definition camera.cpp:7
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12