Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .idea/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion include/omath/engines/cry_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::cry_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::cry_engine
4 changes: 2 additions & 2 deletions include/omath/engines/frostbite_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::frostbite_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::unity_engine
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::frostbite_engine
2 changes: 1 addition & 1 deletion include/omath/engines/iw_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::iw_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::iw_engine
2 changes: 1 addition & 1 deletion include/omath/engines/opengl_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

namespace omath::opengl_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, true, NDCDepthRange::NEGATIVE_ONE_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::NEGATIVE_ONE_TO_ONE, {.inverted_forward = true}>;
} // namespace omath::opengl_engine
2 changes: 1 addition & 1 deletion include/omath/engines/source_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
#include "traits/camera_trait.hpp"
namespace omath::source_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE>;
} // namespace omath::source_engine
2 changes: 1 addition & 1 deletion include/omath/engines/unity_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::unity_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE, {.inverted_forward = true}>;
} // namespace omath::unity_engine
2 changes: 1 addition & 1 deletion include/omath/engines/unreal_engine/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

namespace omath::unreal_engine
{
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, false, NDCDepthRange::ZERO_TO_ONE>;
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait, NDCDepthRange::ZERO_TO_ONE, {.inverted_right = true}>;
} // namespace omath::unreal_engine
36 changes: 33 additions & 3 deletions include/omath/projection/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ namespace omath::projection
AUTO,
MANUAL,
};
struct CameraAxes
{
bool inverted_forward = false;
bool inverted_right = false;
};

template<class T, class MatType, class ViewAnglesType>
concept CameraEngineConcept =
requires(const Vector3<float>& cam_origin, const Vector3<float>& look_at, const ViewAnglesType& angles,
Expand All @@ -58,8 +64,9 @@ namespace omath::projection
requires noexcept(T::calc_projection_matrix(fov, viewport, znear, zfar, ndc_depth_range));
};

template<class Mat4X4Type, class ViewAnglesType, class TraitClass, bool inverted_z = false,
NDCDepthRange depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
template<class Mat4X4Type, class ViewAnglesType, class TraitClass,
NDCDepthRange depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE,
CameraAxes axes = {}>
requires CameraEngineConcept<TraitClass, Mat4X4Type, ViewAnglesType>
class Camera final
{
Expand All @@ -83,6 +90,27 @@ namespace omath::projection
{
}

[[nodiscard]]
static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{
Vector3<float> forward_vector = {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
if constexpr (axes.inverted_forward)
forward_vector = -forward_vector;
return TraitClass::calc_look_at_angle({}, forward_vector);
}

[[nodiscard]]
static Vector3<float> calc_origin_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{
// The view matrix is R * T(-origin), so the last column stores t = -R * origin.
// Recovering origin: origin = -R^T * t
return {
-(view_matrix[0, 0] * view_matrix[0, 3] + view_matrix[1, 0] * view_matrix[1, 3] + view_matrix[2, 0] * view_matrix[2, 3]),
-(view_matrix[0, 1] * view_matrix[0, 3] + view_matrix[1, 1] * view_matrix[1, 3] + view_matrix[2, 1] * view_matrix[2, 3]),
-(view_matrix[0, 2] * view_matrix[0, 3] + view_matrix[1, 2] * view_matrix[1, 3] + view_matrix[2, 2] * view_matrix[2, 3]),
};
}

void look_at(const Vector3<float>& target)
{
m_view_angles = TraitClass::calc_look_at_angle(m_origin, target);
Expand All @@ -100,7 +128,7 @@ namespace omath::projection
{
const auto& view_matrix = get_view_matrix();

if constexpr (inverted_z)
if constexpr (axes.inverted_forward)
return -Vector3<float>{view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
return {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
}
Expand All @@ -109,6 +137,8 @@ namespace omath::projection
Vector3<float> get_right() const noexcept
{
const auto& view_matrix = get_view_matrix();
if constexpr (axes.inverted_right)
return -Vector3<float>{view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
return {view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
}

Expand Down
Loading
Loading