| @@ -1 +1 @@ | |||
| Subproject commit f30c17f180002170d3d25bcc133b6792175c8bb7 | |||
| Subproject commit ca3fb5a4ba22c012ed6349aba52186a961a39341 | |||
| @@ -36,10 +36,9 @@ liblol_core_headers = \ | |||
| lol/base/log.h \ | |||
| \ | |||
| lol/math/all.h \ | |||
| lol/math/functions.h lol/math/vector.h lol/math/half.h \ | |||
| lol/math/functions.h lol/math/half.h \ | |||
| lol/math/geometry.h lol/math/interp.h lol/math/arraynd.h \ | |||
| lol/math/constants.h lol/math/matrix.h lol/math/ops.h \ | |||
| lol/math/transform.h lol/math/bigint.h \ | |||
| lol/math/constants.h lol/math/bigint.h \ | |||
| lol/math/noise/gradient.h lol/math/noise/perlin.h \ | |||
| lol/math/noise/simplex.h \ | |||
| \ | |||
| @@ -92,7 +91,7 @@ liblol_core_sources = \ | |||
| \ | |||
| base/assert.cpp base/features.cpp base/log.cpp base/string.cpp \ | |||
| \ | |||
| math/vector.cpp math/matrix.cpp math/transform.cpp math/half.cpp \ | |||
| math/half.cpp \ | |||
| math/geometry.cpp \ | |||
| \ | |||
| gpu/shader.cpp gpu/indexbuffer.cpp gpu/vertexbuffer.cpp \ | |||
| @@ -15,7 +15,7 @@ | |||
| // ---------------- | |||
| // | |||
| #include "lol/math/vector.h" | |||
| #include <lol/math/vector.h> | |||
| namespace lol | |||
| { | |||
| @@ -17,7 +17,7 @@ | |||
| // ---------------- | |||
| // | |||
| #include <lol/math/matrix.h> | |||
| #include <lol/math/transform.h> | |||
| #include "engine/worldentity.h" | |||
| @@ -177,9 +177,6 @@ | |||
| <ClCompile Include="lolua\baselua.cpp" /> | |||
| <ClCompile Include="math\geometry.cpp" /> | |||
| <ClCompile Include="math\half.cpp" /> | |||
| <ClCompile Include="math\matrix.cpp" /> | |||
| <ClCompile Include="math\transform.cpp" /> | |||
| <ClCompile Include="math\vector.cpp" /> | |||
| <ClCompile Include="mesh\mesh.cpp" /> | |||
| <ClCompile Include="mesh\primitivemesh.cpp" /> | |||
| <ClCompile Include="messageservice.cpp" /> | |||
| @@ -291,13 +288,9 @@ | |||
| <ClInclude Include="lol\math\geometry.h" /> | |||
| <ClInclude Include="lol\math\half.h" /> | |||
| <ClInclude Include="lol\math\interp.h" /> | |||
| <ClInclude Include="lol\math\matrix.h" /> | |||
| <ClInclude Include="lol\math\noise\gradient.h" /> | |||
| <ClInclude Include="lol\math\noise\perlin.h" /> | |||
| <ClInclude Include="lol\math\noise\simplex.h" /> | |||
| <ClInclude Include="lol\math\ops.h" /> | |||
| <ClInclude Include="lol\math\transform.h" /> | |||
| <ClInclude Include="lol\math\vector.h" /> | |||
| <ClInclude Include="lol\net\all.h" /> | |||
| <ClInclude Include="lol\net\http.h" /> | |||
| <ClInclude Include="lol\public.h" /> | |||
| @@ -207,15 +207,6 @@ | |||
| <ClCompile Include="math\half.cpp"> | |||
| <Filter>math</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="math\matrix.cpp"> | |||
| <Filter>math</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="math\transform.cpp"> | |||
| <Filter>math</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="math\vector.cpp"> | |||
| <Filter>math</Filter> | |||
| </ClCompile> | |||
| <ClCompile Include="mesh\mesh.cpp"> | |||
| <Filter>mesh</Filter> | |||
| </ClCompile> | |||
| @@ -458,9 +449,6 @@ | |||
| <ClInclude Include="lol\math\interp.h"> | |||
| <Filter>lol\math</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\math\matrix.h"> | |||
| <Filter>lol\math</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\math\noise\gradient.h"> | |||
| <Filter>lol\math\noise</Filter> | |||
| </ClInclude> | |||
| @@ -470,15 +458,6 @@ | |||
| <ClInclude Include="lol\math\noise\simplex.h"> | |||
| <Filter>lol\math\noise</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\math\ops.h"> | |||
| <Filter>lol\math</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\math\transform.h"> | |||
| <Filter>lol\math</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\math\vector.h"> | |||
| <Filter>lol\math</Filter> | |||
| </ClInclude> | |||
| <ClInclude Include="lol\net\all.h"> | |||
| <Filter>lol\net</Filter> | |||
| </ClInclude> | |||
| @@ -15,9 +15,7 @@ | |||
| #include <lol/math/half.h> | |||
| #include <lol/math/bigint.h> | |||
| #include <lol/math/real.h> | |||
| #include <lol/math/ops.h> | |||
| #include <lol/math/vector.h> | |||
| #include <lol/math/matrix.h> | |||
| #include <lol/math/transform.h> | |||
| #include <lol/math/arraynd.h> | |||
| #include <lol/math/geometry.h> | |||
| @@ -1,638 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // Lol Engine is free software. It comes without any warranty, to | |||
| // the extent permitted by applicable law. You can redistribute it | |||
| // and/or modify it under the terms of the Do What the Fuck You Want | |||
| // to Public License, Version 2, as published by the WTFPL Task Force. | |||
| // See http://www.wtfpl.net/ for more details. | |||
| // | |||
| #pragma once | |||
| // | |||
| // The matrix classes | |||
| // ------------------ | |||
| // | |||
| #include <ostream> | |||
| #include <lol/math/vector.h> | |||
| #include <lol/math/transform.h> | |||
| #if _WIN32 | |||
| # pragma push_macro("near") | |||
| # pragma push_macro("far") | |||
| # undef near | |||
| # undef far | |||
| #endif | |||
| namespace lol | |||
| { | |||
| /* | |||
| * The generic “mat_t” type, which is fixed-size | |||
| */ | |||
| template<typename T, int COLS, int ROWS> | |||
| struct LOL_ATTR_NODISCARD mat_t | |||
| : public linear_ops::base<vec_t<T,ROWS>> | |||
| { | |||
| static int const count = COLS; | |||
| typedef T scalar_element; | |||
| typedef vec_t<T,ROWS> element; | |||
| typedef mat_t<T,COLS,ROWS> type; | |||
| inline mat_t() {} | |||
| explicit inline mat_t(T const &val) | |||
| { | |||
| T const zero = T(0); | |||
| for (int i = 0; i < COLS; ++i) | |||
| for (int j = 0; j < ROWS; ++j) | |||
| m_data[i][j] = i == j ? val : zero; | |||
| } | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline mat_t(mat_t<U, COLS, ROWS> const &m) | |||
| { | |||
| for (int i = 0; i < COLS; ++i) | |||
| m_data[i] = (vec_t<T,ROWS>)m[i]; | |||
| } | |||
| inline vec_t<T,ROWS>& operator[](size_t n) { return m_data[n]; } | |||
| inline vec_t<T,ROWS> const& operator[](size_t n) const { return m_data[n]; } | |||
| private: | |||
| vec_t<T,ROWS> m_data[COLS]; | |||
| }; | |||
| /* | |||
| * 2×2-element matrices | |||
| */ | |||
| template <typename T> | |||
| struct LOL_ATTR_NODISCARD mat_t<T, 2, 2> | |||
| : public linear_ops::base<vec_t<T,2>> | |||
| { | |||
| static int const count = 2; | |||
| typedef T scalar_element; | |||
| typedef vec_t<T,2> element; | |||
| typedef mat_t<T,2,2> type; | |||
| inline mat_t() {} | |||
| inline mat_t(vec_t<T,2> v0, vec_t<T,2> v1) | |||
| : m_data{ v0, v1 } {} | |||
| explicit inline mat_t(T const &val) | |||
| : m_data{ vec_t<T,2>(val, T(0)), | |||
| vec_t<T,2>(T(0), val) } {} | |||
| explicit inline mat_t(mat_t<T,4,4> const &m) | |||
| : m_data{ m[0].xy, m[1].xy } {} | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline mat_t(mat_t<U,2,2> const &m) | |||
| : m_data{ (element)m[0], (element)m[1] } {} | |||
| inline vec_t<T,2>& operator[](size_t n) { return m_data[n]; } | |||
| inline vec_t<T,2> const& operator[](size_t n) const { return m_data[n]; } | |||
| /* Helpers for transformation matrices */ | |||
| static mat_t<T,2,2> rotate(T radians); | |||
| static inline mat_t<T,2,2> rotate(mat_t<T,2,2> m, T radians) | |||
| { | |||
| return rotate(radians) * m; | |||
| } | |||
| void printf() const; | |||
| std::string tostring() const; | |||
| static const mat_t<T,2,2> identity; | |||
| private: | |||
| vec_t<T,2> m_data[2]; | |||
| }; | |||
| static_assert(sizeof(imat2) == 16, "sizeof(imat2) == 16"); | |||
| static_assert(sizeof(f16mat2) == 8, "sizeof(f16mat2) == 8"); | |||
| static_assert(sizeof(mat2) == 16, "sizeof(mat2) == 16"); | |||
| static_assert(sizeof(dmat2) == 32, "sizeof(dmat2) == 32"); | |||
| /* | |||
| * 3×3-element matrices | |||
| */ | |||
| template <typename T> | |||
| struct LOL_ATTR_NODISCARD mat_t<T, 3, 3> | |||
| : public linear_ops::base<vec_t<T,3>> | |||
| { | |||
| static int const count = 3; | |||
| typedef T scalar_element; | |||
| typedef vec_t<T,3> element; | |||
| typedef mat_t<T,3,3> type; | |||
| inline mat_t() {} | |||
| inline mat_t(vec_t<T,3> v0, vec_t<T,3> v1, vec_t<T,3> v2) | |||
| : m_data{ v0, v1, v2 } {} | |||
| explicit inline mat_t(T const &val) | |||
| : m_data{ vec_t<T,3>(val, (T)0, (T)0), | |||
| vec_t<T,3>((T)0, val, (T)0), | |||
| vec_t<T,3>((T)0, (T)0, val) } {} | |||
| explicit inline mat_t(mat_t<T,2,2> m, T const &val = T(1)) | |||
| : m_data{ vec_t<T,3>(m[0], (T)0), | |||
| vec_t<T,3>(m[1], (T)0), | |||
| vec_t<T,3>((T)0, (T)0, val) } {} | |||
| explicit inline mat_t(mat_t<T,4,4> const &m) | |||
| : m_data{ m[0].xyz, m[1].xyz, m[2].xyz } {} | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline mat_t(mat_t<U,3,3> const &m) | |||
| : m_data{ (element)m[0], (element)m[1], (element)m[2] } {} | |||
| explicit mat_t(quat_t<T> const &q); | |||
| inline vec_t<T,3>& operator[](size_t n) { return m_data[n]; } | |||
| inline vec_t<T,3> const& operator[](size_t n) const { return m_data[n]; } | |||
| /* Helpers for transformation matrices */ | |||
| static mat_t<T,3,3> scale(T x); | |||
| static mat_t<T,3,3> scale(T x, T y, T z); | |||
| static mat_t<T,3,3> scale(vec_t<T,3> v); | |||
| static mat_t<T,3,3> rotate(T radians, T x, T y, T z); | |||
| static mat_t<T,3,3> rotate(T radians, vec_t<T,3> v); | |||
| static mat_t<T,3,3> fromeuler_xyz(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_xzy(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_yxz(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_yzx(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_zxy(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_zyx(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_xyz(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_xzy(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_yxz(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_yzx(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_zxy(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_zyx(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_xyx(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_xzx(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_yxy(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_yzy(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_zxz(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_zyz(vec_t<T,3> const &v); | |||
| static mat_t<T,3,3> fromeuler_xyx(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_xzx(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_yxy(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_yzy(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_zxz(T phi, T theta, T psi); | |||
| static mat_t<T,3,3> fromeuler_zyz(T phi, T theta, T psi); | |||
| static inline mat_t<T,3,3> rotate(mat_t<T,3,3> m, T radians, vec_t<T,3> v) | |||
| { | |||
| return rotate(radians, v) * m; | |||
| } | |||
| void printf() const; | |||
| std::string tostring() const; | |||
| static const mat_t<T,3,3> identity; | |||
| private: | |||
| vec_t<T,3> m_data[3]; | |||
| }; | |||
| static_assert(sizeof(imat3) == 36, "sizeof(imat3) == 36"); | |||
| static_assert(sizeof(f16mat3) == 18, "sizeof(f16mat3) == 18"); | |||
| static_assert(sizeof(mat3) == 36, "sizeof(mat3) == 36"); | |||
| static_assert(sizeof(dmat3) == 72, "sizeof(dmat3) == 72"); | |||
| /* | |||
| * 4×4-element matrices | |||
| */ | |||
| template <typename T> | |||
| struct LOL_ATTR_NODISCARD mat_t<T, 4, 4> | |||
| : public linear_ops::base<vec_t<T,4>> | |||
| { | |||
| static int const count = 4; | |||
| typedef T scalar_element; | |||
| typedef vec_t<T,4> element; | |||
| typedef mat_t<T,4,4> type; | |||
| inline mat_t() {} | |||
| inline mat_t(vec_t<T,4> v0, vec_t<T,4> v1, vec_t<T,4> v2, vec_t<T,4> v3) | |||
| : m_data{ v0, v1, v2, v3 } {} | |||
| explicit inline mat_t(T const &val) | |||
| : m_data{ vec_t<T,4>(val, (T)0, (T)0, (T)0), | |||
| vec_t<T,4>((T)0, val, (T)0, (T)0), | |||
| vec_t<T,4>((T)0, (T)0, val, (T)0), | |||
| vec_t<T,4>((T)0, (T)0, (T)0, val) } {} | |||
| explicit inline mat_t(mat_t<T,2,2> m, T const &val = T(1)) | |||
| : m_data{ vec_t<T,4>(m[0], (T)0, (T)0), | |||
| vec_t<T,4>(m[1], (T)0, (T)0), | |||
| vec_t<T,4>((T)0, (T)0, val, (T)0), | |||
| vec_t<T,4>((T)0, (T)0, (T)0, val) } {} | |||
| explicit inline mat_t(mat_t<T,3,3> m, T const &val = T(1)) | |||
| : m_data{ vec_t<T,4>(m[0], (T)0), | |||
| vec_t<T,4>(m[1], (T)0), | |||
| vec_t<T,4>(m[2], (T)0), | |||
| vec_t<T,4>((T)0, (T)0, (T)0, val) } {} | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline mat_t(mat_t<U,4,4> const &m) | |||
| : m_data{ (element)m[0], (element)m[1], | |||
| (element)m[2], (element)m[3] } {} | |||
| explicit mat_t(quat_t<T> const &q); | |||
| inline vec_t<T,4>& operator[](size_t n) { return m_data[n]; } | |||
| inline vec_t<T,4> const& operator[](size_t n) const { return m_data[n]; } | |||
| /* Helpers for transformation matrices */ | |||
| static mat_t<T,4,4> translate(T x, T y, T z); | |||
| static mat_t<T,4,4> translate(vec_t<T,3> v); | |||
| static inline mat_t<T,4,4> scale(T x) | |||
| { | |||
| return mat_t<T,4,4>(mat_t<T,3,3>::scale(x), (T)1); | |||
| } | |||
| static inline mat_t<T,4,4> scale(T x, T y, T z) | |||
| { | |||
| return mat_t<T,4,4>(mat_t<T,3,3>::scale(x, y, z), (T)1); | |||
| } | |||
| static inline mat_t<T,4,4> scale(vec_t<T,3> v) | |||
| { | |||
| return mat_t<T,4,4>(mat_t<T,3,3>::scale(v), (T)1); | |||
| } | |||
| static inline mat_t<T,4,4> translate(mat_t<T,4,4> const &m, vec_t<T,3> v) | |||
| { | |||
| return translate(v) * m; | |||
| } | |||
| static inline mat_t<T,4,4> rotate(T radians, T x, T y, T z) | |||
| { | |||
| return mat_t<T,4,4>(mat_t<T,3,3>::rotate(radians, x, y, z), (T)1); | |||
| } | |||
| static inline mat_t<T,4,4> rotate(T radians, vec_t<T,3> v) | |||
| { | |||
| return mat_t<T,4,4>(mat_t<T,3,3>::rotate(radians, v), (T)1); | |||
| } | |||
| static inline mat_t<T,4,4> rotate(mat_t<T,4,4> &m, T radians, vec_t<T,3> v) | |||
| { | |||
| return rotate(radians, v) * m; | |||
| } | |||
| static mat_t<T,4,4> fromeuler_xyz(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_xzy(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_yxz(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_yzx(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_zxy(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_zyx(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_xyz(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_xzy(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_yxz(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_yzx(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_zxy(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_zyx(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_xyx(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_xzx(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_yxy(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_yzy(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_zxz(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_zyz(vec_t<T,3> const &v); | |||
| static mat_t<T,4,4> fromeuler_xyx(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_xzx(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_yxy(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_yzy(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_zxz(T phi, T theta, T psi); | |||
| static mat_t<T,4,4> fromeuler_zyz(T phi, T theta, T psi); | |||
| /* Helpers for view matrices */ | |||
| static mat_t<T,4,4> lookat(vec_t<T,3> eye, vec_t<T,3> center, vec_t<T,3> up); | |||
| /* Helpers for projection matrices; FOV values are in radians */ | |||
| static mat_t<T,4,4> ortho(T left, T right, T bottom, T top, T near, T far); | |||
| static mat_t<T,4,4> ortho(T width, T height, T near, T far); | |||
| static mat_t<T,4,4> frustum(T left, T right, T bottom, T top, T near, T far); | |||
| static mat_t<T,4,4> perspective(T fov_y, T width, T height, T near, T far); | |||
| static mat_t<T,4,4> shifted_perspective(T fov_y, T screen_size, T screen_ratio_yx, T near, T far); | |||
| void printf() const; | |||
| std::string tostring() const; | |||
| static const mat_t<T,4,4> identity; | |||
| private: | |||
| vec_t<T,4> m_data[4]; | |||
| }; | |||
| static_assert(sizeof(imat4) == 64, "sizeof(imat4) == 64"); | |||
| static_assert(sizeof(f16mat4) == 32, "sizeof(f16mat4) == 32"); | |||
| static_assert(sizeof(mat4) == 64, "sizeof(mat4) == 64"); | |||
| static_assert(sizeof(dmat4) == 128, "sizeof(dmat4) == 128"); | |||
| /* | |||
| * stdstream method implementations | |||
| */ | |||
| template<class U, int COLS, int ROWS> | |||
| static std::ostream &operator<<(std::ostream &stream, | |||
| mat_t<U,COLS,ROWS> const &m) | |||
| { | |||
| for (int y = 0; y < ROWS; ++y) | |||
| { | |||
| stream << (y == 0 ? "(" : ", "); | |||
| for (int x = 0; x < COLS; ++x) | |||
| stream << (x == 0 ? "(" : ", ") << m[x][y]; | |||
| stream << ")"; | |||
| } | |||
| return stream << ")"; | |||
| } | |||
| /* | |||
| * Transpose any matrix | |||
| */ | |||
| template<typename T, int COLS, int ROWS> | |||
| static inline mat_t<T, ROWS, COLS> transpose(mat_t<T, COLS, ROWS> const &m) | |||
| { | |||
| mat_t<T, ROWS, COLS> ret; | |||
| for (int i = 0; i < COLS; ++i) | |||
| for (int j = 0; j < ROWS; ++j) | |||
| ret[j][i] = m[i][j]; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Compute a square submatrix, useful for minors and cofactor matrices | |||
| */ | |||
| template<typename T, int N> | |||
| mat_t<T, N - 1, N - 1> submatrix(mat_t<T, N, N> const &m, int i, int j) | |||
| { | |||
| ASSERT(i >= 0); ASSERT(j >= 0); ASSERT(i < N); ASSERT(j < N); | |||
| mat_t<T, N - 1, N - 1> ret; | |||
| for (int i2 = 0; i2 < N - 1; ++i2) | |||
| for (int j2 = 0; j2 < N - 1; ++j2) | |||
| ret[i2][j2] = m[i2 + (i2 >= i)][j2 + (j2 >= j)]; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Compute square matrix cofactor | |||
| */ | |||
| template<typename T, int N> LOL_ATTR_NODISCARD | |||
| T cofactor(mat_t<T, N, N> const &m, int i, int j) | |||
| { | |||
| ASSERT(i >= 0); ASSERT(j >= 0); ASSERT(i < N); ASSERT(j < N); | |||
| T tmp = determinant(submatrix(m, i, j)); | |||
| return ((i + j) & 1) ? -tmp : tmp; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| T cofactor(mat_t<T, 2, 2> const &m, int i, int j) | |||
| { | |||
| /* This specialisation shouldn't be needed, but Visual Studio. */ | |||
| ASSERT(i >= 0); ASSERT(j >= 0); ASSERT(i < 2); ASSERT(j < 2); | |||
| T tmp = m[1 - i][1 - j]; | |||
| return (i ^ j) ? -tmp : tmp; | |||
| } | |||
| // Lu decomposition with partial pivoting | |||
| template<typename T, int N> LOL_ATTR_NODISCARD | |||
| std::tuple<mat_t<T, N, N>, vec_t<int, N>, int> lu_decomposition(mat_t<T, N, N> const &m) | |||
| { | |||
| mat_t<T, N, N> lu = m; | |||
| vec_t<int, N> perm; | |||
| int sign = 1; | |||
| for (int i = 0; i < N; ++i) | |||
| perm[i] = i; | |||
| for (int k = 0; k < N; ++k) | |||
| { | |||
| // Find row with the largest absolute value | |||
| int best_j = k; | |||
| for (int j = k + 1; j < N; ++j) | |||
| if (abs(lu[k][j]) > lol::abs(lu[k][best_j])) | |||
| best_j = j; | |||
| // Swap rows in result | |||
| if (best_j != k) | |||
| { | |||
| std::swap(perm[k], perm[best_j]); | |||
| sign = -sign; | |||
| for (int i = 0; i < N; ++i) | |||
| std::swap(lu[i][k], lu[i][best_j]); | |||
| } | |||
| // Compute the Schur complement in the lower triangular part | |||
| for (int j = k + 1; j < N; ++j) | |||
| { | |||
| lu[k][j] /= lu[k][k]; | |||
| for (int i = k + 1; i < N; ++i) | |||
| lu[i][j] -= lu[i][k] * lu[k][j]; | |||
| } | |||
| } | |||
| return std::make_tuple(lu, perm, sign); | |||
| } | |||
| /* | |||
| * Compute square matrix determinant, with a specialisation for 1×1 matrices | |||
| */ | |||
| template<typename T, int N> LOL_ATTR_NODISCARD | |||
| T determinant(mat_t<T, N, N> const &m) | |||
| { | |||
| auto lup = lu_decomposition(m); | |||
| T det(T(std::get<2>(lup))); | |||
| for (int i = 0; i < N; ++i) | |||
| det *= std::get<0>(lup)[i][i]; | |||
| return det; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| T const & determinant(mat_t<T, 1, 1> const &m) | |||
| { | |||
| return m[0][0]; | |||
| } | |||
| // Compute inverse of the L matrix of an LU decomposition | |||
| template<typename T, int N> | |||
| mat_t<T, N, N> l_inverse(mat_t<T, N, N> const & lu) | |||
| { | |||
| mat_t<T, N, N> ret { 0 }; | |||
| for (int j = 0; j < N; ++j) | |||
| { | |||
| for (int i = j; i >= 0; --i) | |||
| { | |||
| T sum = 0; | |||
| for (int k = i + 1; k <= j; ++k) | |||
| sum += ret[k][j] * lu[i][k]; | |||
| ret[i][j] = T(j == i ? 1 : 0) - sum; | |||
| } | |||
| } | |||
| return ret; | |||
| } | |||
| // Compute inverse of the U matrix of an LU decomposition | |||
| template<typename T, int N> | |||
| mat_t<T, N, N> u_inverse(mat_t<T, N, N> const & lu) | |||
| { | |||
| mat_t<T, N, N> ret { 0 }; | |||
| for (int i = 0; i < N; ++i) | |||
| { | |||
| for (int j = i; j < N; ++j) | |||
| { | |||
| T sum = 0; | |||
| for (int k = i; k < j; ++k) | |||
| sum += ret[k][i] * lu[j][k]; | |||
| ret[j][i] = ((i == j ? 1 : 0) - sum) / lu[j][j]; | |||
| } | |||
| } | |||
| return ret; | |||
| } | |||
| /* | |||
| * Compute square matrix inverse | |||
| */ | |||
| template<typename T, int N> | |||
| mat_t<T, N, N> inverse(mat_t<T, N, N> const &m) | |||
| { | |||
| auto lup = lu_decomposition(m); | |||
| auto lu = std::get<0>(lup); | |||
| auto p = std::get<1>(lup); | |||
| auto invlu = u_inverse(lu) * l_inverse(lu); | |||
| // Rearrange columns according to the original permutation vector | |||
| mat_t<T, N, N> ret; | |||
| for (int i = 0; i < N; ++i) | |||
| ret[p[i]] = invlu[i]; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Matrix-vector and vector-matrix multiplication | |||
| */ | |||
| template<typename T, int COLS, int ROWS, int SWIZZLE> | |||
| static inline vec_t<T, ROWS> operator *(mat_t<T, COLS, ROWS> const &m, | |||
| vec_t<T, COLS, SWIZZLE> const &v) | |||
| { | |||
| vec_t<T, ROWS> ret(T(0)); | |||
| for (int i = 0; i < COLS; ++i) | |||
| ret += m[i] * v[i]; | |||
| return ret; | |||
| } | |||
| template<typename T, int COLS, int ROWS, int SWIZZLE> | |||
| static inline vec_t<T, COLS> operator *(vec_t<T, ROWS, SWIZZLE> const &v, | |||
| mat_t<T, COLS, ROWS> const &m) | |||
| { | |||
| vec_t<T, COLS> ret(T(0)); | |||
| for (int i = 0; i < COLS; ++i) | |||
| ret[i] = dot(v, m[i]); | |||
| return ret; | |||
| } | |||
| /* | |||
| * Matrix-matrix multiplication | |||
| */ | |||
| template<typename T, int COLS, int N, int ROWS> | |||
| static inline mat_t<T, COLS, ROWS> operator *(mat_t<T, N, ROWS> const &a, | |||
| mat_t<T, COLS, N> const &b) | |||
| { | |||
| mat_t<T, COLS, ROWS> ret; | |||
| for (int i = 0; i < COLS; ++i) | |||
| ret[i] = a * b[i]; | |||
| return ret; | |||
| } | |||
| template<typename T, int N> | |||
| static inline mat_t<T, N, N> &operator *=(mat_t<T, N, N> &a, | |||
| mat_t<T, N, N> const &b) | |||
| { | |||
| return a = a * b; | |||
| } | |||
| /* | |||
| * Vector-vector outer product | |||
| */ | |||
| template<typename T, int COLS, int ROWS> | |||
| static inline mat_t<T, COLS, ROWS> outer(vec_t<T, ROWS> const &a, | |||
| vec_t<T, COLS> const &b) | |||
| { | |||
| /* Valid cast because mat_t and vec_t have similar layouts */ | |||
| return *reinterpret_cast<mat_t<T, 1, ROWS> const *>(&a) | |||
| * *reinterpret_cast<mat_t<T, COLS, 1> const *>(&b); | |||
| } | |||
| /* | |||
| * Matrix-matrix outer product (Kronecker product) | |||
| */ | |||
| template<typename T, int COLS1, int COLS2, int ROWS1, int ROWS2> | |||
| static inline mat_t<T, COLS1 * COLS2, ROWS1 * ROWS2> | |||
| outer(mat_t<T, COLS1, ROWS1> const &a, mat_t<T, COLS2, ROWS2> const &b) | |||
| { | |||
| mat_t<T, COLS1 * COLS2, ROWS1 * ROWS2> ret; | |||
| for (int i1 = 0; i1 < COLS1; ++i1) | |||
| for (int i2 = 0; i2 < COLS2; ++i2) | |||
| { | |||
| /* Valid cast because mat_t and vec_t have similar layouts */ | |||
| *reinterpret_cast<mat_t<T, ROWS1, ROWS2> *>(&ret[i1 * COLS2 + i2]) | |||
| = outer(b[i2], a[i1]); | |||
| } | |||
| return ret; | |||
| } | |||
| /* | |||
| * Constants | |||
| */ | |||
| template<typename T> | |||
| mat_t<T,2,2> const mat_t<T,2,2>::identity = mat_t<T,2,2>((T)1); | |||
| template<typename T> | |||
| mat_t<T,3,3> const mat_t<T,3,3>::identity = mat_t<T,3,3>((T)1); | |||
| template<typename T> | |||
| mat_t<T,4,4> const mat_t<T,4,4>::identity = mat_t<T,4,4>((T)1); | |||
| } /* namespace lol */ | |||
| #if _WIN32 | |||
| # pragma pop_macro("near") | |||
| # pragma pop_macro("far") | |||
| #endif | |||
| @@ -1,362 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net> | |||
| // This program is free software; you can redistribute it and/or | |||
| // modify it under the terms of the Do What The Fuck You Want To | |||
| // Public License, Version 2, as published by Sam Hocevar. See | |||
| // http://www.wtfpl.net/ for more details. | |||
| // | |||
| #pragma once | |||
| // | |||
| // Operations for vector classes | |||
| // ----------------------------- | |||
| // | |||
| #include <ostream> | |||
| #include <type_traits> | |||
| #include <lol/math/half.h> | |||
| #include <lol/math/real.h> | |||
| namespace lol | |||
| { | |||
| /* | |||
| * Utility namespaces for traits -- this file uses a combination of | |||
| * ADL black magic and enable_if to ensure that only the expected type | |||
| * conversions are done. | |||
| * | |||
| * vec_t (swizzle) needs swizzle_ops | |||
| * vec_t (generic) needs linear_ops + componentwise_ops | |||
| * vec_t (specialisation) needs swizzle_ops + linear_ops + componentwise_ops | |||
| * mat_t (all) needs linear_ops | |||
| * cmplx_t quat_t need linear_ops | |||
| * | |||
| * We can only inherit from one class, because Visual Studio will not | |||
| * perform EBCO (empty base class optimisation) when there is multiple | |||
| * inheritance. | |||
| */ | |||
| namespace linear_ops | |||
| { | |||
| template<typename T> | |||
| struct base {}; | |||
| } | |||
| namespace componentwise_ops | |||
| { | |||
| template<typename T> | |||
| struct base : public linear_ops::base<T> {}; | |||
| } | |||
| namespace swizzle_ops | |||
| { | |||
| template<typename T, int SWIZZLE = FULL_SWIZZLE> | |||
| struct base {}; | |||
| template<typename T> | |||
| struct base<T, FULL_SWIZZLE> : public componentwise_ops::base<T> {}; | |||
| } | |||
| /* | |||
| * Operators for swizzled vectors. Since template deduction cannot be | |||
| * done for two arbitrary vec_t<> values, we help the compiler understand | |||
| * the expected type. | |||
| */ | |||
| namespace swizzle_ops | |||
| { | |||
| template<typename T, int N, int SWIZZLE1, int SWIZZLE2> LOL_ATTR_NODISCARD | |||
| static inline typename std::enable_if<SWIZZLE1 != FULL_SWIZZLE || SWIZZLE2 != FULL_SWIZZLE, bool>::type | |||
| operator ==(vec_t<T,N,SWIZZLE1> const &a, vec_t<T,N,SWIZZLE2> const &b) | |||
| { | |||
| return vec_t<T,N>(a) == vec_t<T,N>(b); | |||
| } | |||
| template<typename T, int N, int SWIZZLE1, int SWIZZLE2> LOL_ATTR_NODISCARD | |||
| static inline typename std::enable_if<SWIZZLE1 != FULL_SWIZZLE || SWIZZLE2 != FULL_SWIZZLE, bool>::type | |||
| operator !=(vec_t<T,N,SWIZZLE1> const &a, vec_t<T,N,SWIZZLE2> const &b) | |||
| { | |||
| return vec_t<T,N>(a) != vec_t<T,N>(b); | |||
| } | |||
| #define LOL_SWIZZLE_V_VV_OP(op) \ | |||
| template<typename T, int N, int SWIZZLE1, int SWIZZLE2> \ | |||
| inline typename std::enable_if<SWIZZLE1 != FULL_SWIZZLE \ | |||
| || SWIZZLE2 != FULL_SWIZZLE,vec_t<T,N>>::type \ | |||
| operator op(vec_t<T,N,SWIZZLE1> const &a, \ | |||
| vec_t<T,N,SWIZZLE2> const &b) \ | |||
| { \ | |||
| return vec_t<T,N>(a) op vec_t<T,N>(b); \ | |||
| } \ | |||
| \ | |||
| template<typename T, int N, int SWIZZLE> \ | |||
| inline typename std::enable_if<SWIZZLE != FULL_SWIZZLE,vec_t<T,N>>::type & \ | |||
| operator op##=(vec_t<T,N> &a, \ | |||
| vec_t<T,N,SWIZZLE> const &b) \ | |||
| { \ | |||
| return a op##= vec_t<T,N>(b); \ | |||
| } \ | |||
| \ | |||
| template<typename T, int N, int SWIZZLE> \ | |||
| inline typename std::enable_if<SWIZZLE != FULL_SWIZZLE,vec_t<T,N>>::type \ | |||
| operator op(vec_t<T,N,SWIZZLE> const &a, T const &b) \ | |||
| { \ | |||
| return vec_t<T,N>(a) op b; \ | |||
| } | |||
| LOL_SWIZZLE_V_VV_OP(+) | |||
| LOL_SWIZZLE_V_VV_OP(-) | |||
| LOL_SWIZZLE_V_VV_OP(*) | |||
| LOL_SWIZZLE_V_VV_OP(/) | |||
| #undef LOL_SWIZZLE_V_VV_OP | |||
| } /* namespace swizzle_ops */ | |||
| /* | |||
| * Linear operations: operators and functions that work on all types | |||
| * (vectors, matrices, quaternions...) such as addition or equality test. | |||
| * | |||
| * Others, e.g. multiplication, cannot be implemented here, since it should | |||
| * be implemented as per-component multiplication for vectors, and matrix | |||
| * product for matrices. | |||
| */ | |||
| namespace linear_ops | |||
| { | |||
| /* | |||
| * Comparisons | |||
| */ | |||
| template<typename V> LOL_ATTR_NODISCARD | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator ==(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (!(a[i] == b[i])) | |||
| return false; | |||
| return true; | |||
| } | |||
| template<typename V> LOL_ATTR_NODISCARD | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator !=(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (a[i] != b[i]) | |||
| return true; | |||
| return false; | |||
| } | |||
| /* | |||
| * Unary plus and minus | |||
| */ | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator +(V const &v) | |||
| { | |||
| return v; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator -(V const &v) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = -v[i]; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Addition and subtraction | |||
| */ | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator +(V const &a, V const &b) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] + b[i]; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| &operator +=(V &a, V const &b) | |||
| { | |||
| return a = a + b; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator -(V const &a, V const &b) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] - b[i]; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| &operator -=(V &a, V const &b) | |||
| { | |||
| return a = a - b; | |||
| } | |||
| /* | |||
| * Multiplication by scalar (left) | |||
| */ | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::scalar_element>, V>::value, typename V::type>::type | |||
| operator *(typename V::scalar_element const &val, V const &a) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = val * a[i]; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Multiplication/division by scalar (right) | |||
| */ | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::scalar_element>, V>::value, typename V::type>::type | |||
| operator *(V const &a, typename V::scalar_element const &val) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] * val; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::scalar_element>, V>::value, typename V::type>::type & | |||
| operator *=(V &a, typename V::scalar_element const &val) | |||
| { | |||
| return a = a * val; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::scalar_element>, V>::value, typename V::type>::type | |||
| operator /(V const &a, typename V::scalar_element const &val) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] / val; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::scalar_element>, V>::value, typename V::type>::type & | |||
| operator /=(V &a, typename V::scalar_element const &val) | |||
| { | |||
| return a = a / val; | |||
| } | |||
| } /* namespace linear_ops */ | |||
| /* | |||
| * Operations that work component-wise, such as comparisons or multiplication. | |||
| * This is only for vector types, as the other types (matrices, quaternions, | |||
| * complexes) have different meanings. | |||
| */ | |||
| namespace componentwise_ops | |||
| { | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator *(V const &a, V const &b) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] * b[i]; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| &operator *=(V &a, V const &b) | |||
| { | |||
| return a = a * b; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| operator /(V const &a, V const &b) | |||
| { | |||
| typename V::type ret; | |||
| for (int i = 0; i < V::count; ++i) | |||
| ret[i] = a[i] / b[i]; | |||
| return ret; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, typename V::type>::type | |||
| &operator /=(V &a, V const &b) | |||
| { | |||
| return a = a / b; | |||
| } | |||
| /* | |||
| * Comparisons | |||
| */ | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator <(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (!(a[i] < b[i])) | |||
| return false; | |||
| return true; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator >(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (!(a[i] > b[i])) | |||
| return false; | |||
| return true; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator <=(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (!(a[i] <= b[i])) | |||
| return false; | |||
| return true; | |||
| } | |||
| template<typename V> | |||
| static inline typename std::enable_if<std::is_base_of<base<typename V::element>, V>::value, bool>::type | |||
| operator >=(V const &a, V const &b) | |||
| { | |||
| for (int i = 0; i < V::count; ++i) | |||
| if (!(a[i] >= b[i])) | |||
| return false; | |||
| return true; | |||
| } | |||
| } /* namespace componentwise_ops */ | |||
| } /* namespace lol */ | |||
| @@ -1,499 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // Lol Engine is free software. It comes without any warranty, to | |||
| // the extent permitted by applicable law. You can redistribute it | |||
| // and/or modify it under the terms of the Do What the Fuck You Want | |||
| // to Public License, Version 2, as published by the WTFPL Task Force. | |||
| // See http://www.wtfpl.net/ for more details. | |||
| // | |||
| #pragma once | |||
| // | |||
| // The complex, quaternion and dual quaternion classes | |||
| // --------------------------------------------------- | |||
| // | |||
| #include <lol/math/vector.h> | |||
| #include <ostream> | |||
| namespace lol | |||
| { | |||
| /* | |||
| * 2-element transforms: complex numbers | |||
| */ | |||
| template<typename T> | |||
| struct LOL_ATTR_NODISCARD cmplx_t : public linear_ops::base<T> | |||
| { | |||
| static int const count = 2; | |||
| typedef T scalar_element; | |||
| typedef T element; | |||
| typedef cmplx_t<T> type; | |||
| inline constexpr cmplx_t() = default; | |||
| inline constexpr cmplx_t(cmplx_t<T> const &) = default; | |||
| inline constexpr cmplx_t(T X) : x(X), y(T(0)) {} | |||
| inline constexpr cmplx_t(T X, T Y) : x(X), y(Y) {} | |||
| template<typename U> | |||
| explicit inline constexpr cmplx_t(cmplx_t<U> const &z) | |||
| : x(z.x), y(z.y) {} | |||
| LOL_COMMON_MEMBER_OPS(x) | |||
| inline cmplx_t<T> operator *(cmplx_t<T> const &val) const | |||
| { | |||
| return cmplx_t<T>(x * val.x - y * val.y, x * val.y + y * val.x); | |||
| } | |||
| inline cmplx_t<T> operator *=(cmplx_t<T> const &val) | |||
| { | |||
| return *this = (*this) * val; | |||
| } | |||
| inline cmplx_t<T> operator ~() const | |||
| { | |||
| return cmplx_t<T>(x, -y); | |||
| } | |||
| template<typename U> | |||
| friend std::ostream &operator<<(std::ostream &stream, cmplx_t<U> const &v); | |||
| T x, y; | |||
| }; | |||
| static_assert(sizeof(f16cmplx) == 4, "sizeof(f16cmplx) == 4"); | |||
| static_assert(sizeof(cmplx) == 8, "sizeof(cmplx) == 8"); | |||
| static_assert(sizeof(dcmplx) == 16, "sizeof(dcmplx) == 16"); | |||
| /* | |||
| * 4-element transforms: quaternions | |||
| */ | |||
| template<typename T> | |||
| struct LOL_ATTR_NODISCARD quat_t : public linear_ops::base<T> | |||
| { | |||
| static int const count = 4; | |||
| typedef T scalar_element; | |||
| typedef T element; | |||
| typedef quat_t<T> type; | |||
| /* Default constructor and copy constructor */ | |||
| inline constexpr quat_t() = default; | |||
| inline constexpr quat_t(quat_t<T> const &) = default; | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline constexpr quat_t(quat_t<U> const &q) | |||
| : w(q.w), x(q.x), y(q.y), z(q.z) {} | |||
| /* Various explicit constructors */ | |||
| explicit inline constexpr quat_t(T W, T X, T Y, T Z) | |||
| : w(W), x(X), y(Y), z(Z) {} | |||
| explicit inline constexpr quat_t(T W) | |||
| : w(W), x(0), y(0), z(0) {} | |||
| /* Construct a unit quaternion from a pure rotation matrix */ | |||
| explicit quat_t(mat_t<T,3,3> const &m) | |||
| { | |||
| T tr = m[0][0] + m[1][1] + m[2][2]; | |||
| if (tr > T(0)) | |||
| { | |||
| T const p = T(0.5) * std::sqrt(T(1) + tr); | |||
| T const q = T(0.25) / p; | |||
| w = p; | |||
| x = q * (m[1][2] - m[2][1]); | |||
| y = q * (m[2][0] - m[0][2]); | |||
| z = q * (m[0][1] - m[1][0]); | |||
| } | |||
| else | |||
| { | |||
| int i = (m[0][0] > m[1][1] && m[0][0] > m[2][2]) ? 0 | |||
| : (m[1][1] > m[2][2]) ? 1 | |||
| : 2; | |||
| int j = (i + 1) % 3, k = (i + 2) % 3; | |||
| T const p = T(0.5) * lol::sqrt(T(1) - tr + m[i][i] + m[i][i]); | |||
| T const q = T(0.25) / p; | |||
| w = q * (m[j][k] - m[k][j]); | |||
| (*this)[1 + i] = p; | |||
| (*this)[1 + j] = q * (m[i][j] + m[j][i]); | |||
| (*this)[1 + k] = q * (m[k][i] + m[i][k]); | |||
| } | |||
| } | |||
| LOL_COMMON_MEMBER_OPS(w) | |||
| inline quat_t operator *(quat_t const &val) const | |||
| { | |||
| vec_t<T,3> v1(x, y, z); | |||
| vec_t<T,3> v2(val.x, val.y, val.z); | |||
| vec_t<T,3> v3 = cross(v1, v2) + w * v2 + val.w * v1; | |||
| return quat_t(w * val.w - dot(v1, v2), v3.x, v3.y, v3.z); | |||
| } | |||
| inline quat_t operator *=(quat_t const &val) | |||
| { | |||
| return *this = (*this * val); | |||
| } | |||
| /* Create a unit quaternion representing a rotation around an axis. */ | |||
| static quat_t rotate(T radians, T x, T y, T z); | |||
| static quat_t rotate(T radians, vec_t<T,3> const &v); | |||
| /* Create a unit quaternion representing a rotation between two vectors. | |||
| * Input vectors need not be normalised. */ | |||
| static quat_t rotate(vec_t<T,3> const &src, vec_t<T,3> const &dst); | |||
| /* Convert from Euler angles. The axes in fromeuler_xyx are | |||
| * x, then y', then x", ie. the axes are attached to the model. | |||
| * If you want to rotate around static axes, just reverse the order | |||
| * of the arguments. Angle values are in radians. */ | |||
| static quat_t fromeuler_xyx(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_xzx(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_yxy(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_yzy(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_zxz(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_zyz(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_xyx(T phi, T theta, T psi); | |||
| static quat_t fromeuler_xzx(T phi, T theta, T psi); | |||
| static quat_t fromeuler_yxy(T phi, T theta, T psi); | |||
| static quat_t fromeuler_yzy(T phi, T theta, T psi); | |||
| static quat_t fromeuler_zxz(T phi, T theta, T psi); | |||
| static quat_t fromeuler_zyz(T phi, T theta, T psi); | |||
| /* Convert from Tait-Bryan angles (incorrectly called Euler angles, | |||
| * but since everyone does it…). The axes in fromeuler_xyz are | |||
| * x, then y', then z", ie. the axes are attached to the model. | |||
| * If you want to apply yaw around x, pitch around y, and roll | |||
| * around z, use fromeuler_xyz. Angle values are in radians. | |||
| * If you want to rotate around static axes, reverse the order in | |||
| * the function name (_zyx instead of _xyz) AND reverse the order | |||
| * of the arguments. */ | |||
| static quat_t fromeuler_xyz(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_xzy(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_yxz(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_yzx(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_zxy(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_zyx(vec_t<T,3> const &v); | |||
| static quat_t fromeuler_xyz(T phi, T theta, T psi); | |||
| static quat_t fromeuler_xzy(T phi, T theta, T psi); | |||
| static quat_t fromeuler_yxz(T phi, T theta, T psi); | |||
| static quat_t fromeuler_yzx(T phi, T theta, T psi); | |||
| static quat_t fromeuler_zxy(T phi, T theta, T psi); | |||
| static quat_t fromeuler_zyx(T phi, T theta, T psi); | |||
| inline quat_t operator ~() const | |||
| { | |||
| return quat_t(w, -x, -y, -z); | |||
| } | |||
| /* Transform vectors or points */ | |||
| inline vec_t<T,3> transform(vec_t<T,3> const &v) const | |||
| { | |||
| quat_t p = quat_t(0, v.x, v.y, v.z); | |||
| quat_t q = *this * p / *this; | |||
| return vec_t<T,3>(q.x, q.y, q.z); | |||
| } | |||
| inline vec_t<T,4> transform(vec_t<T,4> const &v) const | |||
| { | |||
| quat_t p = quat_t(0, v.x, v.y, v.z); | |||
| quat_t q = *this * p / *this; | |||
| return vec_t<T,4>(q.x, q.y, q.z, v.w); | |||
| } | |||
| inline vec_t<T,3> operator *(vec_t<T,3> const &v) const | |||
| { | |||
| return transform(v); | |||
| } | |||
| inline vec_t<T,4> operator *(vec_t<T,4> const &v) const | |||
| { | |||
| return transform(v); | |||
| } | |||
| inline vec_t<T,3> axis() | |||
| { | |||
| vec_t<T,3> v(x, y, z); | |||
| T n2 = sqlength(v); | |||
| if (n2 <= (T)1e-6) | |||
| return vec_t<T,3>::axis_x; | |||
| return normalize(v); | |||
| } | |||
| LOL_ATTR_NODISCARD inline T angle() | |||
| { | |||
| vec_t<T,3> v(x, y, z); | |||
| T n2 = sqlength(v); | |||
| if (n2 <= (T)1e-6) | |||
| return (T)0; | |||
| return (T)2 * lol::atan2(lol::sqrt(n2), w); | |||
| } | |||
| template<typename U> | |||
| friend std::ostream &operator<<(std::ostream &stream, quat_t<U> const &v); | |||
| /* XXX: storage order is wxyz, unlike vectors! */ | |||
| T w, x, y, z; | |||
| }; | |||
| static_assert(sizeof(f16quat) == 8, "sizeof(f16quat) == 8"); | |||
| static_assert(sizeof(quat) == 16, "sizeof(quat) == 16"); | |||
| static_assert(sizeof(dquat) == 32, "sizeof(dquat) == 32"); | |||
| /* | |||
| * SQT transforms: scale / rotation / translation | |||
| */ | |||
| template<typename T> | |||
| struct LOL_ATTR_NODISCARD sqt_t | |||
| { | |||
| /* Default constructor and copy constructor */ | |||
| inline constexpr sqt_t() = default; | |||
| inline constexpr sqt_t(sqt_t<T> const &) = default; | |||
| inline constexpr sqt_t<T>& operator =(const sqt_t<T>&) = default; | |||
| /* Explicit constructor for type conversion */ | |||
| template<typename U> | |||
| explicit inline constexpr sqt_t(sqt_t<U> const &other) | |||
| : q(other.q), t(other.t), s(other.s) {} | |||
| /* Various explicit constructors */ | |||
| explicit inline constexpr sqt_t(T const &s_, | |||
| quat_t<T> const &q_, | |||
| vec_t<T,3> const &t_) | |||
| : q(q_), t(t_), s(s_) {} | |||
| explicit inline constexpr sqt_t(T const &s_) | |||
| : q(1.f), t(0.f), s(s_) {} | |||
| explicit inline constexpr sqt_t(quat_t<T> const &q_) | |||
| : q(q_), t(0.f), s(1.f) {} | |||
| explicit inline constexpr sqt_t(vec_t<T,3> const &t_) | |||
| : q(1.f), t(t_), s(1.f) {} | |||
| /* Transform vectors or points */ | |||
| inline vec_t<T,3> transform(vec_t<T,3> const &v) const | |||
| { | |||
| return t + q.transform(s * v); | |||
| } | |||
| inline vec_t<T,4> transform(vec_t<T,4> const &v) const | |||
| { | |||
| // XXX: needs serious testing for w != 1 | |||
| vec_t<T,4> tmp = q.transform(vec_t<T,4>(s * v.xyz, v.w)); | |||
| return vec_t<T,4>(tmp.xyz, 0.f) + vec_t<T,4>(t, 1.f) * tmp.w; | |||
| } | |||
| inline vec_t<T,3> operator *(vec_t<T,3> const &v) const | |||
| { | |||
| return transform(v); | |||
| } | |||
| inline vec_t<T,4> operator *(vec_t<T,4> const &v) const | |||
| { | |||
| return transform(v); | |||
| } | |||
| /* Compose two SQTs together */ | |||
| inline sqt_t<T> operator *(sqt_t<T> const &other) const | |||
| { | |||
| return sqt_t<T>(s * other.s, | |||
| q * other.q, | |||
| transform(other.t)); | |||
| } | |||
| quat_t<T> q; | |||
| vec_t<T,3> t; | |||
| T s; | |||
| }; | |||
| /* | |||
| * stdstream method implementations | |||
| */ | |||
| template<typename U> | |||
| std::ostream &operator<<(std::ostream &stream, cmplx_t<U> const &c) | |||
| { | |||
| return stream << "(" << c.x << ", " << c.y << ")"; | |||
| } | |||
| template<typename U> | |||
| std::ostream &operator<<(std::ostream &stream, quat_t<U> const &q) | |||
| { | |||
| return stream << "(" << q.w << ", " << q.x << ", " | |||
| << q.y << ", " << q.z << ")"; | |||
| } | |||
| /* | |||
| * Common operations on transforms | |||
| */ | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T dot(cmplx_t<T> const &t1, cmplx_t<T> const &t2) | |||
| { | |||
| T ret(0); | |||
| for (size_t i = 0; i < sizeof(t1) / sizeof(T); ++i) | |||
| ret += t1[i] * t2[i]; | |||
| return ret; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T sqlength(cmplx_t<T> const &t) | |||
| { | |||
| return dot(t, t); | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T length(cmplx_t<T> const &t) | |||
| { | |||
| /* FIXME: this is not very nice */ | |||
| return (T)sqrt((double)sqlength(t)); | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T norm(cmplx_t<T> const &t) | |||
| { | |||
| return length(t); | |||
| } | |||
| template<typename T> | |||
| static inline cmplx_t<T> normalize(cmplx_t<T> const &z) | |||
| { | |||
| T norm = (T)length(z); | |||
| return norm ? z / norm : cmplx_t<T>(T(0)); | |||
| } | |||
| /* XXX: duplicate */ | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T dot(quat_t<T> const &t1, quat_t<T> const &t2) | |||
| { | |||
| T ret(0); | |||
| for (size_t i = 0; i < sizeof(t1) / sizeof(T); ++i) | |||
| ret += t1[i] * t2[i]; | |||
| return ret; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T sqlength(quat_t<T> const &t) | |||
| { | |||
| return dot(t, t); | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T length(quat_t<T> const &t) | |||
| { | |||
| /* FIXME: this is not very nice */ | |||
| return (T)sqrt((double)sqlength(t)); | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline T norm(quat_t<T> const &t) | |||
| { | |||
| return length(t); | |||
| } | |||
| template<typename T> | |||
| static inline quat_t<T> normalize(quat_t<T> const &z) | |||
| { | |||
| T norm = (T)length(z); | |||
| return norm ? z / norm : quat_t<T>(T(0)); | |||
| } | |||
| /* | |||
| * Complex numbers only | |||
| */ | |||
| template<typename T> | |||
| static inline cmplx_t<T> inverse(cmplx_t<T> const &z) | |||
| { | |||
| return ~z / sqlength(z); | |||
| } | |||
| template<typename T> | |||
| static inline cmplx_t<T> operator /(T a, cmplx_t<T> const &b) | |||
| { | |||
| return a * inverse(b); | |||
| } | |||
| template<typename T> | |||
| static inline cmplx_t<T> operator /(cmplx_t<T> a, cmplx_t<T> const &b) | |||
| { | |||
| return a * inverse(b); | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline bool operator ==(cmplx_t<T> const &a, T b) | |||
| { | |||
| return (a.x == b) && !a.y; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline bool operator !=(cmplx_t<T> const &a, T b) | |||
| { | |||
| return (a.x != b) || a.y; | |||
| } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline bool operator ==(T a, cmplx_t<T> const &b) { return b == a; } | |||
| template<typename T> LOL_ATTR_NODISCARD | |||
| static inline bool operator !=(T a, cmplx_t<T> const &b) { return b != a; } | |||
| /* | |||
| * Quaternions only | |||
| */ | |||
| template<typename T> | |||
| static inline quat_t<T> inverse(quat_t<T> const &q) | |||
| { | |||
| return ~q / sqlength(q); | |||
| } | |||
| template<typename T> | |||
| static inline quat_t<T> operator /(T x, quat_t<T> const &y) | |||
| { | |||
| return x * inverse(y); | |||
| } | |||
| template<typename T> | |||
| static inline quat_t<T> operator /(quat_t<T> const &x, quat_t<T> const &y) | |||
| { | |||
| return x * inverse(y); | |||
| } | |||
| template<typename T> | |||
| extern quat_t<T> slerp(quat_t<T> const &qa, quat_t<T> const &qb, T f); | |||
| /* | |||
| * SQTs only | |||
| */ | |||
| template<typename T> | |||
| static inline sqt_t<T> inverse(sqt_t<T> const &tr) | |||
| { | |||
| auto inv_s = T(1) / tr.s; | |||
| auto inv_q = inverse(tr.q); | |||
| return sqt_t<T>(inv_s, inv_q, inv_q * tr.t * -inv_s); | |||
| } | |||
| template<typename T> | |||
| static inline sqt_t<T> operator /(sqt_t<T> const &x, sqt_t<T> const &y) | |||
| { | |||
| return x * inverse(y); | |||
| } | |||
| } /* namespace lol */ | |||
| @@ -1,226 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // Lol Engine is free software. It comes without any warranty, to | |||
| // the extent permitted by applicable law. You can redistribute it | |||
| // and/or modify it under the terms of the Do What the Fuck You Want | |||
| // to Public License, Version 2, as published by the WTFPL Task Force. | |||
| // See http://www.wtfpl.net/ for more details. | |||
| // | |||
| #include <lol/engine-internal.h> | |||
| namespace lol | |||
| { | |||
| template<> mat3 mat3::scale(float x, float y, float z) | |||
| { | |||
| mat3 ret(1.0f); | |||
| ret[0][0] = x; | |||
| ret[1][1] = y; | |||
| ret[2][2] = z; | |||
| return ret; | |||
| } | |||
| template<> mat3 mat3::scale(float x) | |||
| { | |||
| return scale(x, x, x); | |||
| } | |||
| template<> mat3 mat3::scale(vec3 v) | |||
| { | |||
| return scale(v.x, v.y, v.z); | |||
| } | |||
| template<> mat4 mat4::translate(float x, float y, float z) | |||
| { | |||
| mat4 ret(1.0f); | |||
| ret[3][0] = x; | |||
| ret[3][1] = y; | |||
| ret[3][2] = z; | |||
| return ret; | |||
| } | |||
| template<> mat4 mat4::translate(vec3 v) | |||
| { | |||
| return translate(v.x, v.y, v.z); | |||
| } | |||
| template<> mat2 mat2::rotate(float radians) | |||
| { | |||
| float st = sin(radians); | |||
| float ct = cos(radians); | |||
| mat2 ret; | |||
| ret[0][0] = ct; | |||
| ret[0][1] = st; | |||
| ret[1][0] = -st; | |||
| ret[1][1] = ct; | |||
| return ret; | |||
| } | |||
| template<> mat3 mat3::rotate(float radians, float x, float y, float z) | |||
| { | |||
| float st = sin(radians); | |||
| float ct = cos(radians); | |||
| float len = std::sqrt(x * x + y * y + z * z); | |||
| float invlen = len ? 1.0f / len : 0.0f; | |||
| x *= invlen; | |||
| y *= invlen; | |||
| z *= invlen; | |||
| float mtx = (1.0f - ct) * x; | |||
| float mty = (1.0f - ct) * y; | |||
| float mtz = (1.0f - ct) * z; | |||
| mat3 ret; | |||
| ret[0][0] = x * mtx + ct; | |||
| ret[0][1] = x * mty + st * z; | |||
| ret[0][2] = x * mtz - st * y; | |||
| ret[1][0] = y * mtx - st * z; | |||
| ret[1][1] = y * mty + ct; | |||
| ret[1][2] = y * mtz + st * x; | |||
| ret[2][0] = z * mtx + st * y; | |||
| ret[2][1] = z * mty - st * x; | |||
| ret[2][2] = z * mtz + ct; | |||
| return ret; | |||
| } | |||
| template<> mat3 mat3::rotate(float radians, vec3 v) | |||
| { | |||
| return rotate(radians, v.x, v.y, v.z); | |||
| } | |||
| template<> mat3::mat_t(quat const &q) | |||
| { | |||
| float n = norm(q); | |||
| if (!n) | |||
| { | |||
| for (int j = 0; j < 3; j++) | |||
| for (int i = 0; i < 3; i++) | |||
| (*this)[i][j] = (i == j) ? 1.f : 0.f; | |||
| return; | |||
| } | |||
| float s = 2.0f / n; | |||
| (*this)[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z); | |||
| (*this)[0][1] = s * (q.x * q.y + q.z * q.w); | |||
| (*this)[0][2] = s * (q.x * q.z - q.y * q.w); | |||
| (*this)[1][0] = s * (q.x * q.y - q.z * q.w); | |||
| (*this)[1][1] = 1.0f - s * (q.z * q.z + q.x * q.x); | |||
| (*this)[1][2] = s * (q.y * q.z + q.x * q.w); | |||
| (*this)[2][0] = s * (q.x * q.z + q.y * q.w); | |||
| (*this)[2][1] = s * (q.y * q.z - q.x * q.w); | |||
| (*this)[2][2] = 1.0f - s * (q.x * q.x + q.y * q.y); | |||
| } | |||
| template<> mat4::mat_t(quat const &q) | |||
| { | |||
| *this = mat4(mat3(q), 1.f); | |||
| } | |||
| template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up) | |||
| { | |||
| vec3 v3 = normalize(eye - center); | |||
| vec3 v1 = normalize(cross(up, v3)); | |||
| vec3 v2 = cross(v3, v1); | |||
| return mat4(vec4(v1.x, v2.x, v3.x, 0.f), | |||
| vec4(v1.y, v2.y, v3.y, 0.f), | |||
| vec4(v1.z, v2.z, v3.z, 0.f), | |||
| vec4(-dot(eye, v1), -dot(eye, v2), -dot(eye, v3), 1.f)); | |||
| } | |||
| template<> mat4 mat4::ortho(float left, float right, float bottom, | |||
| float top, float near, float far) | |||
| { | |||
| float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; | |||
| float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; | |||
| float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; | |||
| mat4 ret(0.0f); | |||
| ret[0][0] = 2.0f * invrl; | |||
| ret[1][1] = 2.0f * invtb; | |||
| ret[2][2] = -2.0f * invfn; | |||
| ret[3][0] = - (right + left) * invrl; | |||
| ret[3][1] = - (top + bottom) * invtb; | |||
| ret[3][2] = - (far + near) * invfn; | |||
| ret[3][3] = 1.0f; | |||
| return ret; | |||
| } | |||
| template<> mat4 mat4::ortho(float width, float height, | |||
| float near, float far) | |||
| { | |||
| return mat4::ortho(-0.5f * width, 0.5f * width, | |||
| -0.5f * height, 0.5f * height, near, far); | |||
| } | |||
| template<> mat4 mat4::frustum(float left, float right, float bottom, | |||
| float top, float near, float far) | |||
| { | |||
| float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; | |||
| float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; | |||
| float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; | |||
| mat4 ret(0.0f); | |||
| ret[0][0] = 2.0f * near * invrl; | |||
| ret[1][1] = 2.0f * near * invtb; | |||
| ret[2][0] = (right + left) * invrl; | |||
| ret[2][1] = (top + bottom) * invtb; | |||
| ret[2][2] = - (far + near) * invfn; | |||
| ret[2][3] = -1.0f; | |||
| ret[3][2] = -2.0f * far * near * invfn; | |||
| return ret; | |||
| } | |||
| /* | |||
| * Return a standard perspective matrix | |||
| */ | |||
| template<> mat4 mat4::perspective(float fov_y, float width, | |||
| float height, float near, float far) | |||
| { | |||
| float t2 = lol::tan(fov_y * 0.5f); | |||
| float t1 = t2 * width / height; | |||
| return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); | |||
| } | |||
| /* | |||
| * Return a perspective matrix with the camera location shifted to be on | |||
| * the near plane | |||
| */ | |||
| template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size, | |||
| float screen_ratio_yx, | |||
| float near, float far) | |||
| { | |||
| float tan_y = tanf(fov_y * .5f); | |||
| ASSERT(tan_y > 0.000001f); | |||
| float dist_scr = (screen_size * screen_ratio_yx * .5f) / tan_y; | |||
| return mat4::perspective(fov_y, screen_size, screen_size * screen_ratio_yx, | |||
| max(.001f, dist_scr + near), | |||
| max(.001f, dist_scr + far)) * | |||
| mat4::translate(.0f, .0f, -dist_scr); | |||
| } | |||
| } /* namespace lol */ | |||
| @@ -1,262 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // Lol Engine is free software. It comes without any warranty, to | |||
| // the extent permitted by applicable law. You can redistribute it | |||
| // and/or modify it under the terms of the Do What the Fuck You Want | |||
| // to Public License, Version 2, as published by the WTFPL Task Force. | |||
| // See http://www.wtfpl.net/ for more details. | |||
| // | |||
| #include <lol/engine-internal.h> | |||
| namespace lol | |||
| { | |||
| template<> quat quat::rotate(float radians, vec3 const &v) | |||
| { | |||
| float half_angle = radians * 0.5f; | |||
| vec3 tmp = normalize(v) * sin(half_angle); | |||
| return quat(cos(half_angle), tmp.x, tmp.y, tmp.z); | |||
| } | |||
| template<> quat quat::rotate(float radians, float x, float y, float z) | |||
| { | |||
| return quat::rotate(radians, vec3(x, y, z)); | |||
| } | |||
| template<> quat quat::rotate(vec3 const &src, vec3 const &dst) | |||
| { | |||
| /* Algorithm directly taken from Sam Hocevar's article "Quaternion from | |||
| * two vectors: the final version". | |||
| * http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final */ | |||
| float magnitude = lol::sqrt(sqlength(src) * sqlength(dst)); | |||
| float real_part = magnitude + dot(src, dst); | |||
| vec3 w; | |||
| if (real_part < 1.e-6f * magnitude) | |||
| { | |||
| /* If src and dst are exactly opposite, rotate 180 degrees | |||
| * around an arbitrary orthogonal axis. Axis normalisation | |||
| * can happen later, when we normalise the quaternion. */ | |||
| real_part = 0.0f; | |||
| w = abs(src.x) > abs(src.z) ? vec3(-src.y, src.x, 0.f) | |||
| : vec3(0.f, -src.z, src.y); | |||
| } | |||
| else | |||
| { | |||
| /* Otherwise, build quaternion the standard way. */ | |||
| w = cross(src, dst); | |||
| } | |||
| return normalize(quat(real_part, w.x, w.y, w.z)); | |||
| } | |||
| template<> quat slerp(quat const &qa, quat const &qb, float f) | |||
| { | |||
| float const magnitude = lol::sqrt(sqlength(qa) * sqlength(qb)); | |||
| float const product = lol::dot(qa, qb) / magnitude; | |||
| /* If quaternions are equal or opposite, there is no need | |||
| * to slerp anything, just return qa. */ | |||
| if (std::abs(product) >= 1.0f) | |||
| return qa; | |||
| float const sign = (product < 0.0f) ? -1.0f : 1.0f; | |||
| float const theta = lol::acos(sign * product); | |||
| float const s1 = lol::sin(sign * f * theta); | |||
| float const s0 = lol::sin((1.0f - f) * theta); | |||
| /* This is the same as 1/sin(theta) */ | |||
| float const d = 1.0f / lol::sqrt(1.f - product * product); | |||
| return qa * (s0 * d) + qb * (s1 * d); | |||
| } | |||
| static inline vec3 quat_toeuler_generic(quat const &q, int i, int j, int k) | |||
| { | |||
| float n = norm(q); | |||
| if (!n) | |||
| return vec3::zero; | |||
| /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */ | |||
| float const sign = ((2 + i - j) % 3) ? 1.f : -1.f; | |||
| vec3 ret; | |||
| /* k == i means X-Y-X style Euler angles; otherwise we’re | |||
| * actually handling X-Y-Z style Tait-Bryan angles. */ | |||
| if (k == i) | |||
| { | |||
| k = 3 - i - j; | |||
| ret[0] = atan2(q[1 + i] * q[1 + j] + sign * (q.w * q[1 + k]), | |||
| q.w * q[1 + j] - sign * (q[1 + i] * q[1 + k])); | |||
| ret[1] = acos(2.f * (sq(q.w) + sq(q[1 + i])) - 1.f); | |||
| ret[2] = atan2(q[1 + i] * q[1 + j] - sign * (q.w * q[1 + k]), | |||
| q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k])); | |||
| } | |||
| else | |||
| { | |||
| ret[0] = atan2(2.f * (q.w * q[1 + i] - sign * (q[1 + j] * q[1 + k])), | |||
| 1.f - 2.f * (sq(q[1 + i]) + sq(q[1 + j]))); | |||
| ret[1] = asin(2.f * (q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k]))); | |||
| ret[2] = atan2(2.f * (q.w * q[1 + k] - sign * (q[1 + j] * q[1 + i])), | |||
| 1.f - 2.f * (sq(q[1 + k]) + sq(q[1 + j]))); | |||
| } | |||
| return ret / n; | |||
| } | |||
| static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k) | |||
| { | |||
| mat3 ret; | |||
| float const s0 = sin(v[0]), c0 = cos(v[0]); | |||
| float const s1 = sin(v[1]), c1 = cos(v[1]); | |||
| float const s2 = sin(v[2]), c2 = cos(v[2]); | |||
| /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */ | |||
| float const sign = ((2 + i - j) % 3) ? 1.f : -1.f; | |||
| /* k == i means X-Y-X style Euler angles; otherwise we’re | |||
| * actually handling X-Y-Z style Tait-Bryan angles. */ | |||
| if (k == i) | |||
| { | |||
| k = 3 - i - j; | |||
| ret[i][i] = c1; | |||
| ret[i][j] = s0 * s1; | |||
| ret[i][k] = - sign * (c0 * s1); | |||
| ret[j][i] = s1 * s2; | |||
| ret[j][j] = c0 * c2 - s0 * c1 * s2; | |||
| ret[j][k] = sign * (s0 * c2 + c0 * c1 * s2); | |||
| ret[k][i] = sign * (s1 * c2); | |||
| ret[k][j] = - sign * (c0 * s2 + s0 * c1 * c2); | |||
| ret[k][k] = - s0 * s2 + c0 * c1 * c2; | |||
| } | |||
| else | |||
| { | |||
| ret[i][i] = c1 * c2; | |||
| ret[i][j] = sign * (c0 * s2) + s0 * s1 * c2; | |||
| ret[i][k] = s0 * s2 - sign * (c0 * s1 * c2); | |||
| ret[j][i] = - sign * (c1 * s2); | |||
| ret[j][j] = c0 * c2 - sign * (s0 * s1 * s2); | |||
| ret[j][k] = sign * (s0 * c2) + c0 * s1 * s2; | |||
| ret[k][i] = sign * s1; | |||
| ret[k][j] = - sign * (s0 * c1); | |||
| ret[k][k] = c0 * c1; | |||
| } | |||
| return ret; | |||
| } | |||
| static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k) | |||
| { | |||
| vec3 const half_angles = v * 0.5f; | |||
| float const s0 = sin(half_angles[0]), c0 = cos(half_angles[0]); | |||
| float const s1 = sin(half_angles[1]), c1 = cos(half_angles[1]); | |||
| float const s2 = sin(half_angles[2]), c2 = cos(half_angles[2]); | |||
| quat ret; | |||
| /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */ | |||
| float const sign = ((2 + i - j) % 3) ? 1.f : -1.f; | |||
| /* k == i means X-Y-X style Euler angles; otherwise we’re | |||
| * actually handling X-Y-Z style Tait-Bryan angles. */ | |||
| if (k == i) | |||
| { | |||
| k = 3 - i - j; | |||
| ret[0] = c1 * (c0 * c2 - s0 * s2); | |||
| ret[1 + i] = c1 * (c0 * s2 + s0 * c2); | |||
| ret[1 + j] = s1 * (c0 * c2 + s0 * s2); | |||
| ret[1 + k] = sign * (s1 * (s0 * c2 - c0 * s2)); | |||
| } | |||
| else | |||
| { | |||
| ret[0] = c0 * c1 * c2 - sign * (s0 * s1 * s2); | |||
| ret[1 + i] = s0 * c1 * c2 + sign * (c0 * s1 * s2); | |||
| ret[1 + j] = c0 * s1 * c2 - sign * (s0 * c1 * s2); | |||
| ret[1 + k] = c0 * c1 * s2 + sign * (s0 * s1 * c2); | |||
| } | |||
| return ret; | |||
| } | |||
| #define DEFINE_GENERIC_EULER_CONVERSIONS(a1, a2, a3) \ | |||
| DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, a1##a2##a3) \ | |||
| #define DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, name) \ | |||
| /* Create quaternions from Euler angles */ \ | |||
| template<> quat quat::fromeuler_##name(vec3 const &v) \ | |||
| { \ | |||
| int x = 0, y = 1, z = 2; UNUSED(x, y, z); \ | |||
| return quat_fromeuler_generic(v, a1, a2, a3); \ | |||
| } \ | |||
| \ | |||
| template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \ | |||
| { \ | |||
| return quat::fromeuler_##name(vec3(phi, theta, psi)); \ | |||
| } \ | |||
| \ | |||
| /* Create 3×3 matrices from Euler angles */ \ | |||
| template<> mat3 mat3::fromeuler_##name(vec3 const &v) \ | |||
| { \ | |||
| int x = 0, y = 1, z = 2; UNUSED(x, y, z); \ | |||
| return mat3_fromeuler_generic(v, a1, a2, a3); \ | |||
| } \ | |||
| \ | |||
| template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \ | |||
| { \ | |||
| return mat3::fromeuler_##name(vec3(phi, theta, psi)); \ | |||
| } \ | |||
| \ | |||
| /* Create 4×4 matrices from Euler angles */ \ | |||
| template<> mat4 mat4::fromeuler_##name(vec3 const &v) \ | |||
| { \ | |||
| int x = 0, y = 1, z = 2; UNUSED(x, y, z); \ | |||
| return mat4(mat3_fromeuler_generic(v, a1, a2, a3), 1.f); \ | |||
| } \ | |||
| \ | |||
| template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \ | |||
| { \ | |||
| return mat4::fromeuler_##name(vec3(phi, theta, psi)); \ | |||
| } \ | |||
| \ | |||
| /* Retrieve Euler angles from a quaternion */ \ | |||
| template<> vec3 vec3::toeuler_##name(quat const &q) \ | |||
| { \ | |||
| int x = 0, y = 1, z = 2; UNUSED(x, y, z); \ | |||
| return quat_toeuler_generic(q, a1, a2, a3); \ | |||
| } | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(x, y, x) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(x, z, x) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(y, x, y) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(y, z, y) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(z, x, z) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(z, y, z) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(x, y, z) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(x, z, y) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(y, x, z) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(y, z, x) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(z, x, y) | |||
| DEFINE_GENERIC_EULER_CONVERSIONS(z, y, x) | |||
| #undef DEFINE_GENERIC_EULER_CONVERSIONS | |||
| #undef DEFINE_GENERIC_EULER_CONVERSIONS_INNER | |||
| } /* namespace lol */ | |||
| @@ -1,96 +0,0 @@ | |||
| // | |||
| // Lol Engine | |||
| // | |||
| // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // Lol Engine is free software. It comes without any warranty, to | |||
| // the extent permitted by applicable law. You can redistribute it | |||
| // and/or modify it under the terms of the Do What the Fuck You Want | |||
| // to Public License, Version 2, as published by the WTFPL Task Force. | |||
| // See http://www.wtfpl.net/ for more details. | |||
| // | |||
| #include <lol/engine-internal.h> | |||
| #include <ostream> /* std::ostream */ | |||
| namespace lol | |||
| { | |||
| #define LOL_PRINTF_TOSTRING(type, ...) \ | |||
| template<> void type::printf() const { msg::debug(__VA_ARGS__); } \ | |||
| template<> std::string type::tostring() const { return format(__VA_ARGS__); } | |||
| LOL_PRINTF_TOSTRING(vec2, "[ %6.6f %6.6f ]\n", x, y); | |||
| LOL_PRINTF_TOSTRING(ivec2, "[ %i %i ]\n", x, y); | |||
| LOL_PRINTF_TOSTRING(cmplx, "[ %6.6f %6.6f ]\n", x, y); | |||
| LOL_PRINTF_TOSTRING(vec3, "[ %6.6f %6.6f %6.6f ]\n", x, y, z); | |||
| LOL_PRINTF_TOSTRING(ivec3, "[ %i %i %i ]\n", x, y, z); | |||
| LOL_PRINTF_TOSTRING(vec4, "[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); | |||
| LOL_PRINTF_TOSTRING(ivec4, "[ %i %i %i %i ]\n", x, y, z, w); | |||
| LOL_PRINTF_TOSTRING(quat, "[ %6.6f %6.6f %6.6f %6.6f ]\n", w, x, y, z); | |||
| template<> void mat2::printf() const | |||
| { | |||
| mat2 const &p = *this; | |||
| msg::debug("[ %6.6f %6.6f\n", p[0][0], p[1][0]); | |||
| msg::debug(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); | |||
| } | |||
| template<> std::string mat2::tostring() const | |||
| { | |||
| mat2 const &p = *this; | |||
| return format("[ %6.6f %6.6f\n", p[0][0], p[1][0]) + | |||
| format(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); | |||
| } | |||
| template<> void mat3::printf() const | |||
| { | |||
| mat3 const &p = *this; | |||
| msg::debug("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]); | |||
| msg::debug(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]); | |||
| msg::debug(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); | |||
| } | |||
| template<> std::string mat3::tostring() const | |||
| { | |||
| mat3 const &p = *this; | |||
| return format("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]) + | |||
| format(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]) + | |||
| format(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); | |||
| } | |||
| template<> void mat4::printf() const | |||
| { | |||
| mat4 const &p = *this; | |||
| msg::debug("[ %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][0], p[1][0], p[2][0], p[3][0]); | |||
| msg::debug(" %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][1], p[1][1], p[2][1], p[3][1]); | |||
| msg::debug(" %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][2], p[1][2], p[2][2], p[3][2]); | |||
| msg::debug(" %6.6f %6.6f %6.6f %6.6f ]\n", | |||
| p[0][3], p[1][3], p[2][3], p[3][3]); | |||
| } | |||
| template<> std::string mat4::tostring() const | |||
| { | |||
| mat4 const &p = *this; | |||
| return format("[ %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][0], p[1][0], p[2][0], p[3][0]) + | |||
| format(" %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][1], p[1][1], p[2][1], p[3][1]) + | |||
| format(" %6.6f %6.6f %6.6f %6.6f\n", | |||
| p[0][2], p[1][2], p[2][2], p[3][2]) + | |||
| format(" %6.6f %6.6f %6.6f %6.6f ]\n", | |||
| p[0][3], p[1][3], p[2][3], p[3][3]); | |||
| } | |||
| } /* namespace lol */ | |||