Move Vectormath main
C++20 game and graphics mathematics
Loading...
Searching...
No Matches
View.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <type_traits>
5
6#include <mv/math/Mat4.hpp>
10
11namespace mv::math
12{
13 // Look-at basis construction follows the standard Gram-Schmidt form,
14 // cross-checked against DirectXMath XMMatrixLookAtLH/RH (MIT) and GLM
15 // matrix_transform.inl lookAtLH/RH (MIT). Failure is explicit when eye and
16 // target coincide or the supplied up direction is parallel to the view.
17 template <typename T>
18 requires std::is_floating_point_v<T>
19 [[nodiscard]] inline std::optional<Mat4<T>> TryLookAt(
20 const Point3<T>& eye,
21 const Point3<T>& target,
22 const Direction3<T>& up,
23 Handedness handedness) noexcept
24 {
26 if (!forward)
27 {
28 return std::nullopt;
29 }
30
31 const Direction3<T> viewZ =
33 const auto viewX =
34 Direction3<T>::TryFrom(Cross(up.Vector(), viewZ.Vector()));
35 if (!viewX)
36 {
37 return std::nullopt;
38 }
39 const Vec3<T> viewY = Cross(viewZ.Vector(), viewX->Vector());
40 const Vec3<T>& x = viewX->Vector();
41 const Vec3<T>& z = viewZ.Vector();
42 const Vec3<T>& position = eye.Vector();
43
44 const Mat4<T> result(x.X(), viewY.X(), z.X(), T(0), x.Y(), viewY.Y(),
45 z.Y(), T(0), x.Z(), viewY.Z(), z.Z(), T(0),
47 -Dot(z, position), T(1));
48 return result.IsFinite() ? std::optional<Mat4<T>>(result)
49 : std::nullopt;
50 }
51
52 template <typename T>
53 requires std::is_floating_point_v<T>
54 [[nodiscard]] inline std::optional<Mat4<T>> TryLookAtLH(
55 const Point3<T>& eye,
56 const Point3<T>& target,
57 const Direction3<T>& up) noexcept
58 {
60 }
61
62 template <typename T>
63 requires std::is_floating_point_v<T>
64 [[nodiscard]] inline std::optional<Mat4<T>> TryLookAtRH(
65 const Point3<T>& eye,
66 const Point3<T>& target,
67 const Direction3<T>& up) noexcept
68 {
70 }
71} // namespace mv::math
static std::optional< Direction3 > TryFrom(const Vec3< T > &value) noexcept
constexpr T Dot(const Quat< T > &left, const Quat< T > &right) noexcept
Definition Quat.hpp:102
Vec3< T > Cross(const Vec3< T > &left, const Vec3< T > &right) noexcept
Definition Vec3.hpp:367
std::optional< Mat4< T > > TryLookAtRH(const Point3< T > &eye, const Point3< T > &target, const Direction3< T > &up) noexcept
Definition View.hpp:64
std::optional< Mat4< T > > TryLookAt(const Point3< T > &eye, const Point3< T > &target, const Direction3< T > &up, Handedness handedness) noexcept
Definition View.hpp:19
std::optional< Mat4< T > > TryLookAtLH(const Point3< T > &eye, const Point3< T > &target, const Direction3< T > &up) noexcept
Definition View.hpp:54