2019-01-29 23:05:51 +01:00
|
|
|
// Copyright 2019 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
2019-02-05 01:50:07 +01:00
|
|
|
#include <cmath>
|
2019-04-07 14:57:04 +02:00
|
|
|
#include <functional>
|
2019-01-29 21:39:14 +01:00
|
|
|
#include <type_traits>
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
// Tiny matrix/vector library.
|
|
|
|
// Used for things like Free-Look in the gfx backend.
|
|
|
|
|
2019-01-30 00:12:06 +01:00
|
|
|
namespace Common
|
|
|
|
{
|
2019-01-29 21:39:14 +01:00
|
|
|
template <typename T>
|
|
|
|
union TVec3
|
2019-01-29 23:05:51 +01:00
|
|
|
{
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3() = default;
|
|
|
|
constexpr TVec3(T _x, T _y, T _z) : data{_x, _y, _z} {}
|
2019-01-29 21:39:14 +01:00
|
|
|
|
2020-01-22 01:31:02 +01:00
|
|
|
template <typename OtherT>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr explicit TVec3(const TVec3<OtherT>& other) : TVec3(other.x, other.y, other.z)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3 Cross(const TVec3& rhs) const
|
2019-11-06 01:42:52 +01:00
|
|
|
{
|
|
|
|
return {(y * rhs.z) - (rhs.y * z), (z * rhs.x) - (rhs.z * x), (x * rhs.y) - (rhs.x * y)};
|
|
|
|
}
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr T Dot(const TVec3& other) const { return x * other.x + y * other.y + z * other.z; }
|
|
|
|
constexpr T LengthSquared() const { return Dot(*this); }
|
2019-01-29 21:39:14 +01:00
|
|
|
T Length() const { return std::sqrt(LengthSquared()); }
|
|
|
|
TVec3 Normalized() const { return *this / Length(); }
|
2019-01-29 23:05:51 +01:00
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3& operator+=(const TVec3& rhs)
|
2019-01-30 00:12:06 +01:00
|
|
|
{
|
|
|
|
x += rhs.x;
|
|
|
|
y += rhs.y;
|
|
|
|
z += rhs.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3& operator-=(const TVec3& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x -= rhs.x;
|
|
|
|
y -= rhs.y;
|
|
|
|
z -= rhs.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3& operator*=(const TVec3& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x *= rhs.x;
|
|
|
|
y *= rhs.y;
|
|
|
|
z *= rhs.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3& operator/=(const TVec3& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x /= rhs.x;
|
|
|
|
y /= rhs.y;
|
|
|
|
z /= rhs.z;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3 operator-() const { return {-x, -y, -z}; }
|
2019-01-29 21:39:14 +01:00
|
|
|
|
2019-04-07 14:57:04 +02:00
|
|
|
// Apply function to each element and return the result.
|
|
|
|
template <typename F>
|
|
|
|
auto Map(F&& f) const -> TVec3<decltype(f(T{}))>
|
|
|
|
{
|
|
|
|
return {f(x), f(y), f(z)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F, typename T2>
|
|
|
|
auto Map(F&& f, const TVec3<T2>& t) const -> TVec3<decltype(f(T{}, t.x))>
|
|
|
|
{
|
|
|
|
return {f(x, t.x), f(y, t.y), f(z, t.z)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F, typename T2>
|
|
|
|
auto Map(F&& f, T2 scalar) const -> TVec3<decltype(f(T{}, scalar))>
|
|
|
|
{
|
|
|
|
return {f(x, scalar), f(y, scalar), f(z, scalar)};
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
std::array<T, 3> data = {};
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
2019-01-29 21:39:14 +01:00
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
2019-01-29 23:05:51 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
template <typename T>
|
2019-04-07 14:57:04 +02:00
|
|
|
TVec3<bool> operator<(const TVec3<T>& lhs, const TVec3<T>& rhs)
|
2019-01-30 00:12:06 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::less<T>{}, rhs);
|
2019-01-30 00:12:06 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec3<bool> operator!(const TVec3<bool>& vec)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
return {!vec.x, !vec.y, !vec.z};
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
template <typename T>
|
2019-04-07 14:57:04 +02:00
|
|
|
auto operator+(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x + rhs.x)>
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::plus<decltype(lhs.x + rhs.x)>{}, rhs);
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-04-07 14:57:04 +02:00
|
|
|
auto operator-(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x - rhs.x)>
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::minus<decltype(lhs.x - rhs.x)>{}, rhs);
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:57:04 +02:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
auto operator*(const TVec3<T1>& lhs, const TVec3<T2>& rhs) -> TVec3<decltype(lhs.x * rhs.x)>
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::multiplies<decltype(lhs.x * rhs.x)>{}, rhs);
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2019-04-07 14:57:04 +02:00
|
|
|
auto operator/(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x / rhs.x)>
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::divides<decltype(lhs.x / rhs.x)>{}, rhs);
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:57:04 +02:00
|
|
|
template <typename T1, typename T2>
|
|
|
|
auto operator*(const TVec3<T1>& lhs, T2 scalar) -> TVec3<decltype(lhs.x * scalar)>
|
|
|
|
{
|
|
|
|
return lhs.Map(std::multiplies<decltype(lhs.x * scalar)>{}, scalar);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
auto operator/(const TVec3<T1>& lhs, T2 scalar) -> TVec3<decltype(lhs.x / scalar)>
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2019-04-07 14:57:04 +02:00
|
|
|
return lhs.Map(std::divides<decltype(lhs.x / scalar)>{}, scalar);
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
using Vec3 = TVec3<float>;
|
|
|
|
using DVec3 = TVec3<double>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
union TVec4
|
|
|
|
{
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4() = default;
|
|
|
|
constexpr TVec4(TVec3<T> _vec, T _w) : TVec4{_vec.x, _vec.y, _vec.z, _w} {}
|
|
|
|
constexpr TVec4(T _x, T _y, T _z, T _w) : data{_x, _y, _z, _w} {}
|
2019-01-29 21:39:14 +01:00
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr T Dot(const TVec4& other) const
|
|
|
|
{
|
|
|
|
return x * other.x + y * other.y + z * other.z + w * other.w;
|
|
|
|
}
|
2020-10-19 22:27:01 +02:00
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4& operator*=(const TVec4& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x *= rhs.x;
|
|
|
|
y *= rhs.y;
|
|
|
|
z *= rhs.z;
|
|
|
|
w *= rhs.w;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4& operator/=(const TVec4& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x /= rhs.x;
|
|
|
|
y /= rhs.y;
|
|
|
|
z /= rhs.z;
|
|
|
|
w /= rhs.w;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4& operator*=(T scalar) { return *this *= TVec4{scalar, scalar, scalar, scalar}; }
|
|
|
|
constexpr TVec4& operator/=(T scalar) { return *this /= TVec4{scalar, scalar, scalar, scalar}; }
|
2019-01-29 21:39:14 +01:00
|
|
|
|
|
|
|
std::array<T, 4> data = {};
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
|
|
|
T w;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4<T> operator*(TVec4<T> lhs, std::common_type_t<T> scalar)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
return lhs *= scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec4<T> operator/(TVec4<T> lhs, std::common_type_t<T> scalar)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
return lhs /= scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
using Vec4 = TVec4<float>;
|
|
|
|
using DVec4 = TVec4<double>;
|
|
|
|
|
2019-02-05 01:50:07 +01:00
|
|
|
template <typename T>
|
|
|
|
union TVec2
|
|
|
|
{
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2() = default;
|
|
|
|
constexpr TVec2(T _x, T _y) : data{_x, _y} {}
|
2019-02-05 01:50:07 +01:00
|
|
|
|
2020-01-22 01:31:02 +01:00
|
|
|
template <typename OtherT>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr explicit TVec2(const TVec2<OtherT>& other) : TVec2(other.x, other.y)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr T Cross(const TVec2& rhs) const { return (x * rhs.y) - (y * rhs.x); }
|
|
|
|
constexpr T Dot(const TVec2& rhs) const { return (x * rhs.x) + (y * rhs.y); }
|
|
|
|
constexpr T LengthSquared() const { return Dot(*this); }
|
2019-02-05 01:50:07 +01:00
|
|
|
T Length() const { return std::sqrt(LengthSquared()); }
|
|
|
|
TVec2 Normalized() const { return *this / Length(); }
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator+=(const TVec2& rhs)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
|
|
|
x += rhs.x;
|
|
|
|
y += rhs.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator-=(const TVec2& rhs)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
|
|
|
x -= rhs.x;
|
|
|
|
y -= rhs.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator*=(const TVec2& rhs)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
x *= rhs.x;
|
|
|
|
y *= rhs.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator/=(const TVec2& rhs)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
x /= rhs.x;
|
|
|
|
y /= rhs.y;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator*=(T scalar)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
|
|
|
x *= scalar;
|
|
|
|
y *= scalar;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2& operator/=(T scalar)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
|
|
|
x /= scalar;
|
|
|
|
y /= scalar;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2 operator-() const { return {-x, -y}; }
|
2019-02-05 01:50:07 +01:00
|
|
|
|
|
|
|
std::array<T, 2> data = {};
|
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-01-22 01:31:02 +01:00
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<bool> operator<(const TVec2<T>& lhs, const TVec2<T>& rhs)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
return {lhs.x < rhs.x, lhs.y < rhs.y};
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<bool> operator!(const TVec2<bool>& vec)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
return {!vec.x, !vec.y};
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:50:07 +01:00
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<T> operator+(TVec2<T> lhs, const TVec2<T>& rhs)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
|
|
|
return lhs += rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<T> operator-(TVec2<T> lhs, const TVec2<T>& rhs)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
|
|
|
return lhs -= rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<T> operator*(TVec2<T> lhs, const TVec2<T>& rhs)
|
2019-02-05 01:50:07 +01:00
|
|
|
{
|
2020-01-22 01:31:02 +01:00
|
|
|
return lhs *= rhs;
|
2019-02-05 01:50:07 +01:00
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
template <typename T>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr TVec2<T> operator/(TVec2<T> lhs, const TVec2<T>& rhs)
|
2019-01-29 21:39:14 +01:00
|
|
|
{
|
2020-01-22 01:31:02 +01:00
|
|
|
return lhs /= rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename T2>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr auto operator*(TVec2<T> lhs, T2 scalar)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
return TVec2<decltype(lhs.x * scalar)>(lhs) *= scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename T2>
|
2020-10-21 23:41:38 +02:00
|
|
|
constexpr auto operator/(TVec2<T> lhs, T2 scalar)
|
2020-01-22 01:31:02 +01:00
|
|
|
{
|
|
|
|
return TVec2<decltype(lhs.x / scalar)>(lhs) /= scalar;
|
2019-01-29 21:39:14 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 01:50:07 +01:00
|
|
|
using Vec2 = TVec2<float>;
|
|
|
|
using DVec2 = TVec2<double>;
|
|
|
|
|
2020-10-19 22:27:01 +02:00
|
|
|
class Matrix33;
|
|
|
|
|
|
|
|
class Quaternion
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static Quaternion Identity();
|
|
|
|
|
|
|
|
static Quaternion RotateX(float rad);
|
|
|
|
static Quaternion RotateY(float rad);
|
|
|
|
static Quaternion RotateZ(float rad);
|
|
|
|
|
2020-12-27 06:08:40 +01:00
|
|
|
// Returns a quaternion with rotations about each axis simulatenously (e.g processing gyroscope
|
|
|
|
// input)
|
|
|
|
static Quaternion RotateXYZ(const Vec3& rads);
|
|
|
|
|
2020-10-19 22:27:01 +02:00
|
|
|
static Quaternion Rotate(float rad, const Vec3& axis);
|
|
|
|
|
|
|
|
Quaternion() = default;
|
|
|
|
Quaternion(float w, float x, float y, float z);
|
|
|
|
|
|
|
|
float Norm() const;
|
|
|
|
Quaternion Normalized() const;
|
|
|
|
Quaternion Conjugate() const;
|
|
|
|
Quaternion Inverted() const;
|
|
|
|
|
|
|
|
Quaternion& operator*=(const Quaternion& rhs);
|
|
|
|
|
|
|
|
Vec4 data;
|
|
|
|
};
|
|
|
|
|
|
|
|
Quaternion operator*(Quaternion lhs, const Quaternion& rhs);
|
|
|
|
Vec3 operator*(const Quaternion& lhs, const Vec3& rhs);
|
|
|
|
|
2020-05-17 19:41:59 +02:00
|
|
|
Vec3 FromQuaternionToEuler(const Quaternion& q);
|
|
|
|
|
2019-01-29 23:05:51 +01:00
|
|
|
class Matrix33
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static Matrix33 Identity();
|
2020-10-19 22:27:01 +02:00
|
|
|
static Matrix33 FromQuaternion(const Quaternion&);
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
// Return a rotation matrix around the x,y,z axis
|
|
|
|
static Matrix33 RotateX(float rad);
|
|
|
|
static Matrix33 RotateY(float rad);
|
|
|
|
static Matrix33 RotateZ(float rad);
|
|
|
|
|
2019-11-06 01:42:52 +01:00
|
|
|
static Matrix33 Rotate(float rad, const Vec3& axis);
|
|
|
|
|
2019-01-29 23:05:51 +01:00
|
|
|
static Matrix33 Scale(const Vec3& vec);
|
|
|
|
|
|
|
|
// set result = a x b
|
|
|
|
static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result);
|
|
|
|
static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result);
|
|
|
|
|
2020-01-04 01:24:41 +01:00
|
|
|
Matrix33 Inverted() const;
|
|
|
|
|
2019-01-29 23:05:51 +01:00
|
|
|
Matrix33& operator*=(const Matrix33& rhs)
|
|
|
|
{
|
2019-01-29 21:39:14 +01:00
|
|
|
Multiply(*this, rhs, this);
|
2019-01-29 23:05:51 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
// Note: Row-major storage order.
|
2019-01-29 23:05:51 +01:00
|
|
|
std::array<float, 9> data;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline Matrix33 operator*(Matrix33 lhs, const Matrix33& rhs)
|
|
|
|
{
|
|
|
|
return lhs *= rhs;
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
inline Vec3 operator*(const Matrix33& lhs, Vec3 rhs)
|
2019-01-29 23:05:51 +01:00
|
|
|
{
|
2019-01-29 21:39:14 +01:00
|
|
|
Matrix33::Multiply(lhs, rhs, &rhs);
|
|
|
|
return rhs;
|
2019-01-29 23:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class Matrix44
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static Matrix44 Identity();
|
|
|
|
static Matrix44 FromMatrix33(const Matrix33& m33);
|
2020-10-23 05:48:22 +02:00
|
|
|
static Matrix44 FromQuaternion(const Quaternion& q);
|
2019-01-30 00:12:06 +01:00
|
|
|
static Matrix44 FromArray(const std::array<float, 16>& arr);
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
static Matrix44 Translate(const Vec3& vec);
|
|
|
|
static Matrix44 Shear(const float a, const float b = 0);
|
2019-01-29 21:39:14 +01:00
|
|
|
static Matrix44 Perspective(float fov_y, float aspect_ratio, float z_near, float z_far);
|
2019-01-29 23:05:51 +01:00
|
|
|
|
2019-01-30 00:12:06 +01:00
|
|
|
static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result);
|
2019-01-29 21:39:14 +01:00
|
|
|
static void Multiply(const Matrix44& a, const Vec4& vec, Vec4* result);
|
|
|
|
|
|
|
|
// For when a vec4 isn't needed a multiplication function that takes a Vec3 and w:
|
|
|
|
Vec3 Transform(const Vec3& point, float w) const;
|
2019-01-29 23:05:51 +01:00
|
|
|
|
|
|
|
Matrix44& operator*=(const Matrix44& rhs)
|
|
|
|
{
|
2019-01-29 21:39:14 +01:00
|
|
|
Multiply(*this, rhs, this);
|
2019-01-29 23:05:51 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-01-29 21:39:14 +01:00
|
|
|
// Note: Row-major storage order.
|
2019-01-29 23:05:51 +01:00
|
|
|
std::array<float, 16> data;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline Matrix44 operator*(Matrix44 lhs, const Matrix44& rhs)
|
|
|
|
{
|
|
|
|
return lhs *= rhs;
|
2019-01-30 00:12:06 +01:00
|
|
|
}
|
2019-01-29 21:39:14 +01:00
|
|
|
|
|
|
|
inline Vec4 operator*(const Matrix44& lhs, Vec4 rhs)
|
|
|
|
{
|
|
|
|
Matrix44::Multiply(lhs, rhs, &rhs);
|
|
|
|
return rhs;
|
|
|
|
}
|
|
|
|
|
2019-01-30 00:12:06 +01:00
|
|
|
} // namespace Common
|