light/modules/camera/src/scene.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

#include <camera/scene.hpp>
#include <glm/gtc/matrix_transform.hpp>
2025-07-11 00:05:48 +03:30
namespace lt {
2022-03-08 21:19:19 +03:30
SceneCamera::SceneCamera()
2025-07-07 15:13:05 +03:30
: m_orthographic_specification { .size = 1000.0f, .near_plane = -1.0f, .far_plane = 10000.0f }
, m_perspective_specification { .vertical_fov = glm::radians(45.0f),
.near_plane = 0.01f,
.far_plane = 10000.0f }
, m_aspect_ratio(16.0f / 9.0f)
2025-07-07 15:13:05 +03:30
2022-03-08 21:19:19 +03:30
{
calculate_projection();
2022-03-08 21:19:19 +03:30
}
void SceneCamera::set_viewport_size(unsigned int width, unsigned int height)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_aspect_ratio = static_cast<float>(width) / static_cast<float>(height);
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_projection_type(ProjectionType projection_type)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_projection_type = projection_type;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
void SceneCamera::set_orthographic_size(float size)
2022-03-08 21:19:19 +03:30
{
m_orthographic_specification.size = size;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_orthographic_far_plane(float far_plane)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_orthographic_specification.far_plane = far_plane;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_orthographic_near_plane(float near_plane)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_orthographic_specification.near_plane = near_plane;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_perspective_vertical_fov(float vertical_fov)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_perspective_specification.vertical_fov = vertical_fov;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_perspective_far_plane(float far_plane)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_perspective_specification.far_plane = far_plane;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
2025-07-07 15:13:05 +03:30
void SceneCamera::set_perspective_near_plane(float near_plane)
2022-03-08 21:19:19 +03:30
{
2025-07-07 15:13:05 +03:30
m_perspective_specification.near_plane = near_plane;
calculate_projection();
2022-03-08 21:19:19 +03:30
}
void SceneCamera::calculate_projection()
2022-03-08 21:19:19 +03:30
{
if (m_projection_type == ProjectionType::Orthographic)
{
m_projection = glm::ortho(
-m_orthographic_specification.size * 0.5f * m_aspect_ratio,
m_orthographic_specification.size * 0.5f * m_aspect_ratio,
-m_orthographic_specification.size * 0.5f,
m_orthographic_specification.size * 0.5f,
2025-07-06 14:02:50 +03:30
m_orthographic_specification.far_plane,
m_orthographic_specification.near_plane
2025-07-05 13:28:41 +03:30
);
}
2022-03-08 21:19:19 +03:30
else // perspective
{
m_projection = glm::perspective(
2025-07-06 14:02:50 +03:30
m_perspective_specification.vertical_fov,
m_aspect_ratio,
2025-07-06 14:02:50 +03:30
m_perspective_specification.near_plane,
m_perspective_specification.far_plane
2025-07-05 13:28:41 +03:30
);
}
2022-03-08 21:19:19 +03:30
}
2025-07-11 00:05:48 +03:30
} // namespace lt