From 1d1795217b960db9a6611aa488cb7faed39b685b Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Fri, 21 Oct 2022 16:53:26 +0800 Subject: [PATCH] Improve and consolidate matrix type --- src/game/state/nuptial-flight.cpp | 15 + src/game/system/collision.cpp | 14 +- src/geom/primitive/intersection.hpp | 10 +- src/geom/view-frustum.hpp | 2 +- src/math/math.hpp | 5 +- src/math/matrix-operators.hpp | 86 ---- src/math/matrix-type.hpp | 91 ---- src/math/{matrix-functions.hpp => matrix.hpp} | 408 +++++++++++++++++- src/math/quaternion-functions.hpp | 2 +- src/math/stream-operators.hpp | 53 --- src/math/transform-functions.hpp | 1 - src/math/vector.hpp | 55 ++- src/render/passes/ground-pass.cpp | 2 +- src/render/passes/shadow-map-pass.cpp | 8 +- src/render/passes/sky-pass.cpp | 2 +- src/scene/camera.cpp | 6 +- src/utility/fundamental-types.hpp | 3 +- 17 files changed, 484 insertions(+), 279 deletions(-) delete mode 100644 src/math/matrix-operators.hpp delete mode 100644 src/math/matrix-type.hpp rename src/math/{matrix-functions.hpp => matrix.hpp} (70%) diff --git a/src/game/state/nuptial-flight.cpp b/src/game/state/nuptial-flight.cpp index c6d73dd..89df43c 100644 --- a/src/game/state/nuptial-flight.cpp +++ b/src/game/state/nuptial-flight.cpp @@ -76,6 +76,21 @@ nuptial_flight::nuptial_flight(game::context& ctx): ctx.logger->push_task("Entering nuptial flight state"); + + math::matrix m = + { + 1, 2, 3, + 4, 5, 6, + 7, 8, 9 + }; + + auto m2 = math::matrix::one(); + auto m3 = math::matrix(m2); + + std::cout << "m: " << m << std::endl; + std::cout << "m2: " << m2 << std::endl; + std::cout << "m3: " << m3 << std::endl; + // Init selected picking flag selected_picking_flag = std::uint32_t{1} << (sizeof(std::uint32_t) * 8 - 1); selected_eid = entt::null; diff --git a/src/game/system/collision.cpp b/src/game/system/collision.cpp index a79a63c..9b26c2d 100644 --- a/src/game/system/collision.cpp +++ b/src/game/system/collision.cpp @@ -60,15 +60,21 @@ entity::id collision::pick_nearest(const geom::primitive::ray& ray, st const geom::primitive::sphere sphere = { transform.world * picking.sphere.center, - picking.sphere.radius * std::max(std::max(transform.world.scale[0], transform.world.scale[1]), transform.world.scale[2]) + picking.sphere.radius * math::max(transform.world.scale) }; // Test for intersection between ray and sphere auto result = geom::primitive::intersection(ray, sphere); - if (result && std::get<0>(*result) < nearest_distance) + if (result) { - nearest_eid = entity_id; - nearest_distance = std::get<0>(*result); + float t0 = std::get<0>(*result); + float t1 = std::get<1>(*result); + + if (t0 < nearest_distance) + { + nearest_eid = entity_id; + nearest_distance = t0; + } } } ); diff --git a/src/geom/primitive/intersection.hpp b/src/geom/primitive/intersection.hpp index 1c8e8dd..438b1af 100644 --- a/src/geom/primitive/intersection.hpp +++ b/src/geom/primitive/intersection.hpp @@ -125,7 +125,15 @@ std::optional> intersection(const ray& ray, const hypersp h = std::sqrt(h); - return std::tuple{-b - h, -b + h}; + T t0 = -b - h; + T t1 = -b + h; + if (t0 > t1) + std::swap(t0, t1); + + if (t0 < T{0}) + return std::nullopt; + + return std::tuple{t0, t1}; } /** diff --git a/src/geom/view-frustum.hpp b/src/geom/view-frustum.hpp index df38f24..ebfa09f 100644 --- a/src/geom/view-frustum.hpp +++ b/src/geom/view-frustum.hpp @@ -99,7 +99,7 @@ view_frustum::view_frustum(const matrix_type& view_projection): template view_frustum::view_frustum(): - view_frustum(math::matrix4::identity) + view_frustum(math::matrix4::identity()) {} template diff --git a/src/math/math.hpp b/src/math/math.hpp index c5e034f..fd4d678 100644 --- a/src/math/math.hpp +++ b/src/math/math.hpp @@ -24,10 +24,7 @@ namespace math {} #include "math/vector.hpp" - -#include "math/matrix-type.hpp" -#include "math/matrix-functions.hpp" -#include "math/matrix-operators.hpp" +#include "math/matrix.hpp" #include "math/quaternion-type.hpp" #include "math/quaternion-functions.hpp" diff --git a/src/math/matrix-operators.hpp b/src/math/matrix-operators.hpp deleted file mode 100644 index f885f6c..0000000 --- a/src/math/matrix-operators.hpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2021 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#ifndef ANTKEEPER_MATH_MATRIX_OPERATORS_HPP -#define ANTKEEPER_MATH_MATRIX_OPERATORS_HPP - -#include "math/matrix-type.hpp" -#include "math/matrix-functions.hpp" - -/// @copydoc math::add(const math::matrix&, const math::matrix&) -template -constexpr math::matrix operator+(const math::matrix& x, const math::matrix& y); - -/// @copydoc math::mul(const math::matrix&, const math::matrix&) -template -constexpr math::matrix operator*(const math::matrix& x, const math::matrix& y); - -/// @copydoc math::mul(const math::matrix&, T) -template -constexpr math::matrix operator*(const math::matrix& m, T s); - -/// @copydoc math::mul(const math::matrix&, T) -template -constexpr math::matrix operator*(T s, const math::matrix& m); - -/// @copydoc math::mul(const math::matrix&, const math::vector&) -template -constexpr math::vector operator*(const math::matrix& m, const math::vector& v); - -/// @copydoc math::sub(const math::matrix&, const math::matrix&) -template -constexpr math::matrix operator-(const math::matrix& x, const math::matrix& y); - -template -constexpr inline math::matrix operator+(const math::matrix& x, const math::matrix& y) -{ - return add(x, y); -} - -template -constexpr inline math::matrix operator*(const math::matrix& x, const math::matrix& y) -{ - return mul(x, y); -} - -template -constexpr inline math::matrix operator*(const math::matrix& m, T s) -{ - return mul(m, s); -} - -template -constexpr inline math::matrix operator*(T s, const math::matrix& m) -{ - return mul(m, s); -} - -template -constexpr inline math::vector operator*(const math::matrix& m, const math::vector& v) -{ - return mul(m, v); -} - -template -constexpr inline math::matrix operator-(const math::matrix& x, const math::matrix& y) -{ - return sub(x, y); -} - -#endif // ANTKEEPER_MATH_MATRIX_OPERATORS_HPP diff --git a/src/math/matrix-type.hpp b/src/math/matrix-type.hpp deleted file mode 100644 index a45b463..0000000 --- a/src/math/matrix-type.hpp +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2021 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#ifndef ANTKEEPER_MATH_MATRIX_TYPE_HPP -#define ANTKEEPER_MATH_MATRIX_TYPE_HPP - -#include "math/vector.hpp" -#include -#include - -namespace math { - -/** - * An NxM matrix. - * - * @tparam T Matrix element type. - * @tparam N Number of columns. - * @tparam M Number of rows. - */ -template -struct matrix -{ - typedef T element_type; - typedef vector row_type; - row_type columns[N]; - - /// Identity matrix. - static const matrix identity; - - inline constexpr row_type& operator[](std::size_t i) noexcept { return columns[i]; } - inline constexpr const row_type& operator[](std::size_t i) const noexcept { return columns[i]; } -}; - -template -constexpr vector identity_matrix_row(const std::index_sequence&) -{ - return {(Is == I ? T{1} : T{0})...}; -} - -template -constexpr matrix identity_matrix(const std::index_sequence& is) -{ - return {{identity_matrix_row(is)...}}; -} - -template -constexpr matrix matrix::identity = identity_matrix(std::make_index_sequence{}); - -/// 2x2 matrix. -template -using matrix2 = matrix; - -/// 2x2 matrix. -template -using matrix2x2 = matrix; - -/// 3x3 matrix. -template -using matrix3 = matrix; - -/// 3x3 matrix. -template -using matrix3x3 = matrix; - -/// 4x4 matrix. -template -using matrix4 = matrix; - -/// 4x4 matrix. -template -using matrix4x4 = matrix; - -} // namespace math - -#endif // ANTKEEPER_MATH_MATRIX_TYPE_HPP diff --git a/src/math/matrix-functions.hpp b/src/math/matrix.hpp similarity index 70% rename from src/math/matrix-functions.hpp rename to src/math/matrix.hpp index 7859c8d..4fea00b 100644 --- a/src/math/matrix-functions.hpp +++ b/src/math/matrix.hpp @@ -17,15 +17,313 @@ * along with Antkeeper source code. If not, see . */ -#ifndef ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP -#define ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP +#ifndef ANTKEEPER_MATH_MATRIX_HPP +#define ANTKEEPER_MATH_MATRIX_HPP -#include "math/matrix-type.hpp" #include "math/vector.hpp" +#include +#include +#include +#include #include +#include namespace math { +/** + * *n*-by-*m* column-major matrix. + * + * @tparam T Matrix element data type. + * @tparam N Number of columns. + * @tparam M Number of rows. + * + * @see https://en.wikipedia.org/wiki/Row-_and_column-major_order + */ +template +struct matrix +{ + /// Matrix element data type. + typedef T element_type; + + /// Number of matrix columns. + static constexpr std::size_t column_count = N; + + /// Number of matrix rows. + static constexpr std::size_t row_count = M; + + /// Number of matrix elements. + static constexpr std::size_t element_count = column_count * row_count; + + /// Matrix column vector data type. + typedef vector column_vector_type; + + /// Matrix row vector data type. + typedef vector row_vector_type; + + /// Array of matrix column vectors. + column_vector_type columns[column_count]; + + /** + * Returns a reference to the column vector at a given index. + * + * @param i Index of a column vector. + * + * @return Reference to the column vector at index @p i. + */ + /// @{ + constexpr inline column_vector_type& operator[](std::size_t i) noexcept + { + return columns[i]; + } + constexpr inline const column_vector_type& operator[](std::size_t i) const noexcept + { + return columns[i]; + } + constexpr inline column_vector_type& column(std::size_t i) noexcept + { + return columns[i]; + } + constexpr inline const column_vector_type& column(std::size_t i) const noexcept + { + return columns[i]; + } + /// @} + + /** + * Returns a reference to the element at a given index, in column-major order. + * + * @param i Index of a matrix element. + * + * @return Reference to the element at index @p i. + */ + /// @{ + constexpr inline T& element(std::size_t i) noexcept + { + return columns[i / row_count][i % row_count]; + } + constexpr inline const T& element(std::size_t i) const noexcept + { + return columns[i / row_count][i % row_count]; + } + /// @} + + /// Returns a reference to the first column vector. + /// @{ + constexpr inline column_vector_type& front() noexcept + { + return columns[0]; + } + constexpr inline const column_vector_type& front() const noexcept + { + return columns[0]; + } + /// @} + + /// Returns a reference to the last column vector. + /// @{ + constexpr inline column_vector_type& back() noexcept + { + return elements[column_count - 1]; + } + constexpr inline const column_vector_type& back() const noexcept + { + return elements[column_count - 1]; + } + /// @} + + /// Returns a pointer to the column vector array. + /// @{ + constexpr inline column_vector_type* data() noexcept + { + return columns; + }; + constexpr inline const column_vector_type* data() const noexcept + { + return columns; + }; + /// @} + + /// Returns an iterator to the first column vector. + /// @{ + constexpr inline column_vector_type* begin() noexcept + { + return columns; + } + constexpr inline const column_vector_type* begin() const noexcept + { + return columns; + } + constexpr inline const column_vector_type* cbegin() const noexcept + { + return columns; + } + /// @} + + /// Returns an iterator to the column vector following the last column vector. + /// @{ + constexpr inline column_vector_type* end() noexcept + { + return columns + column_count; + } + constexpr inline const column_vector_type* end() const noexcept + { + return columns + column_count; + } + constexpr inline const column_vector_type* cend() const noexcept + { + return columns + column_count; + } + /// @} + + /// Returns a reverse iterator to the first column vector of the reversed matrix. + /// @{ + constexpr inline std::reverse_iterator rbegin() noexcept + { + return std::reverse_iterator(columns + column_count); + } + constexpr inline std::reverse_iterator rbegin() const noexcept + { + return std::reverse_iterator(columns + column_count); + } + constexpr inline std::reverse_iterator crbegin() const noexcept + { + return std::reverse_iterator(columns + column_count); + } + /// @} + + /// Returns a reverse iterator to the column vector following the last column vector of the reversed matrix. + /// @{ + constexpr inline std::reverse_iterator rend() noexcept + { + return std::reverse_iterator(columns); + } + constexpr inline std::reverse_iterator rend() const noexcept + { + return std::reverse_iterator(columns); + } + constexpr inline std::reverse_iterator crend() const noexcept + { + return std::reverse_iterator(columns); + } + /// @} + + /// Returns the number of elements in the matrix. + constexpr inline std::size_t size() const noexcept + { + return element_count; + }; + + /// @private + template + constexpr inline matrix type_cast(std::index_sequence) const noexcept + { + return {vector(columns[I])...}; + } + + /** + * Type-casts the elements of this matrix using `static_cast`. + * + * @tparam U Target element type. + * + * @return Matrix containing the type-casted elements. + */ + template + constexpr inline explicit operator matrix() const noexcept + { + return type_cast(std::make_index_sequence{}); + } + + /// @private + template + constexpr inline matrix size_cast(std::index_sequence) const noexcept + { + if constexpr (O == M) + return {((I < N) ? columns[I] : vector::zero()) ...}; + else + return {((I < N) ? vector(columns[I]) : vector::zero()) ...}; + } + + /** + * Size-casts this matrix to a matrix with different dimensions. Casting to greater dimensions causes new elements to be set to zero. + * + * @tparam P Target number of columns. + * @tparam O Target number of rows. + * + * @return *p*-by-*o* matrix. + */ + template + constexpr inline explicit operator matrix() const noexcept + { + return size_cast(std::make_index_sequence

{}); + } + + /// Returns a zero matrix, where every element is equal to zero. + static constexpr matrix zero() noexcept + { + return {}; + } + + /// @private + template + static constexpr inline matrix one(std::index_sequence) noexcept + { + //return {column_vector_type::one() ...}; + + // MSVC bug workaround (I must be referenced for parameter pack expansion) + return {(I ? column_vector_type::one() : column_vector_type::one()) ...}; + } + + /// Returns a matrix of ones, where every element is equal to one. + static constexpr matrix one() noexcept + { + return one(std::make_index_sequence{}); + } + + /// @private + template + static constexpr inline column_vector_type identity_column(std::size_t i, std::index_sequence) noexcept + { + return {(I == i ? T{1} : T{0}) ...}; + } + + /// @private + template + static constexpr inline matrix identity(std::index_sequence) noexcept + { + return {identity_column(I, std::make_index_sequence{}) ...}; + } + + /// Returns an identity matrix, with ones on the main diagonal and zeros elsewhere. + static constexpr matrix identity() noexcept + { + return identity(std::make_index_sequence{}); + } +}; + +/// 2x2 matrix. +template +using matrix2 = matrix; + +/// 2x2 matrix. +template +using matrix2x2 = matrix; + +/// 3x3 matrix. +template +using matrix3 = matrix; + +/// 3x3 matrix. +template +using matrix3x3 = matrix; + +/// 4x4 matrix. +template +using matrix4 = matrix; + +/// 4x4 matrix. +template +using matrix4x4 = matrix; + /** * Adds two matrices. * @@ -926,4 +1224,106 @@ constexpr matrix type_cast(const matrix& m) } // namespace math -#endif // ANTKEEPER_MATH_MATRIX_FUNCTIONS_HPP +/// @copydoc math::add(const math::matrix&, const math::matrix&) +template +constexpr math::matrix operator+(const math::matrix& x, const math::matrix& y); + +/// @copydoc math::mul(const math::matrix&, const math::matrix&) +template +constexpr math::matrix operator*(const math::matrix& x, const math::matrix& y); + +/// @copydoc math::mul(const math::matrix&, T) +template +constexpr math::matrix operator*(const math::matrix& m, T s); + +/// @copydoc math::mul(const math::matrix&, T) +template +constexpr math::matrix operator*(T s, const math::matrix& m); + +/// @copydoc math::mul(const math::matrix&, const math::vector&) +template +constexpr math::vector operator*(const math::matrix& m, const math::vector& v); + +/// @copydoc math::sub(const math::matrix&, const math::matrix&) +template +constexpr math::matrix operator-(const math::matrix& x, const math::matrix& y); + +/** + * Writes the elements of a matrix to an output stream, with each element delimeted by a space. + * + * @param os Output stream. + * @param m Matrix. + * @return Output stream. + */ +template +std::ostream& operator<<(std::ostream& os, const math::matrix& m); + +/** + * Reads the elements of a matrix from an input stream, with each element delimeted by a space. + * + * @param is Input stream. + * @param m Matrix. + * @return Input stream. + */ +template +std::istream& operator>>(std::istream& is, math::matrix& m); + +template +constexpr inline math::matrix operator+(const math::matrix& x, const math::matrix& y) +{ + return math::add(x, y); +} + +template +constexpr inline math::matrix operator*(const math::matrix& x, const math::matrix& y) +{ + return math::mul(x, y); +} + +template +constexpr inline math::matrix operator*(const math::matrix& m, T s) +{ + return math::mul(m, s); +} + +template +constexpr inline math::matrix operator*(T s, const math::matrix& m) +{ + return math::mul(m, s); +} + +template +constexpr inline math::vector operator*(const math::matrix& m, const math::vector& v) +{ + return math::mul(m, v); +} + +template +constexpr inline math::matrix operator-(const math::matrix& x, const math::matrix& y) +{ + return math::sub(x, y); +} + +template +std::ostream& operator<<(std::ostream& os, const math::matrix& m) +{ + for (std::size_t i = 0; i < m.size(); ++i) + { + if (i) + os << ' '; + os << m.element(i); + } + + return os; +} + +template +std::istream& operator>>(std::istream& is, math::matrix& m) +{ + for (std::size_t i = 0; i < m.size(); ++i) + is >> m.element(i); + + return is; +} + +#endif // ANTKEEPER_MATH_MATRIX_HPP diff --git a/src/math/quaternion-functions.hpp b/src/math/quaternion-functions.hpp index dff58a0..c838f0e 100644 --- a/src/math/quaternion-functions.hpp +++ b/src/math/quaternion-functions.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_MATH_QUATERNION_FUNCTIONS_HPP #include "math/constants.hpp" -#include "math/matrix-type.hpp" +#include "math/matrix.hpp" #include "math/quaternion-type.hpp" #include "math/vector.hpp" #include diff --git a/src/math/stream-operators.hpp b/src/math/stream-operators.hpp index 5e9872d..4cb5677 100644 --- a/src/math/stream-operators.hpp +++ b/src/math/stream-operators.hpp @@ -20,21 +20,10 @@ #ifndef ANTKEEPER_MATH_STREAM_OPERATORS_HPP #define ANTKEEPER_MATH_STREAM_OPERATORS_HPP -#include "math/matrix-type.hpp" #include "math/quaternion-type.hpp" #include #include -/** - * Writes the elements of a matrix to an output stream, with each element delimeted by a space. - * - * @param os Output stream. - * @param m Matrix. - * @return Output stream. - */ -template -std::ostream& operator<<(std::ostream& os, const math::matrix& m); - /** * Writes the real and imaginary parts of a quaternion to an output stream, with each number delimeted by a space. * @@ -45,16 +34,6 @@ std::ostream& operator<<(std::ostream& os, const math::matrix& m); template std::ostream& operator<<(std::ostream& os, const math::quaternion& q); -/** - * Reads the elements of a matrix from an input stream, with each element delimeted by a space. - * - * @param is Input stream. - * @param m Matrix. - * @return Input stream. - */ -template -std::istream& operator>>(std::istream& is, math::matrix& m); - /** * Reads the real and imaginary parts of a quaternion from an input stream, with each number delimeted by a space. * @@ -65,25 +44,6 @@ std::istream& operator>>(std::istream& is, math::matrix& m); template std::istream& operator>>(std::istream& is, const math::quaternion& q); -template -std::ostream& operator<<(std::ostream& os, const math::matrix& m) -{ - for (std::size_t i = 0; i < N; ++i) - { - for (std::size_t j = 0; j < M; ++j) - { - os << m[i][j]; - - if (i < N - 1 || j < M - 1) - { - os << ' '; - } - } - } - - return os; -} - template std::ostream& operator<<(std::ostream& os, const math::quaternion& q) { @@ -91,19 +51,6 @@ std::ostream& operator<<(std::ostream& os, const math::quaternion& q) return os; } -template -std::istream& operator>>(std::istream& is, math::matrix& m) -{ - for (std::size_t i = 0; i < N * M; ++i) - { - std::size_t j = i / M; - std::size_t k = i % M; - is >> m[j][k]; - } - - return is; -} - template std::istream& operator>>(std::istream& is, const math::quaternion& q) { diff --git a/src/math/transform-functions.hpp b/src/math/transform-functions.hpp index 9230dfc..18fcfbf 100644 --- a/src/math/transform-functions.hpp +++ b/src/math/transform-functions.hpp @@ -21,7 +21,6 @@ #define ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP #include "math/transform-type.hpp" -#include "math/matrix-functions.hpp" #include "math/quaternion-functions.hpp" namespace math { diff --git a/src/math/vector.hpp b/src/math/vector.hpp index 371658c..67ee3ca 100644 --- a/src/math/vector.hpp +++ b/src/math/vector.hpp @@ -32,7 +32,7 @@ namespace math { /** - * *n*-dimensional Euclidean vector. + * *n*-dimensional vector. * * @tparam T Element type. * @tparam N Number of elements. @@ -43,7 +43,7 @@ struct vector /// Vector element data type. typedef T element_type; - /// Number of elements. + /// Number of vector elements. static constexpr std::size_t element_count = N; /// Array of vector elements. @@ -243,11 +243,11 @@ struct vector } /** - * Size-casts this vector to a vector with a different number of elements. Casting to a greater number of elements causes new elements to be set to `0`. + * Size-casts this vector to a vector with a different number of elements. Casting to a greater number of elements causes new elements to be set to zero. * * @tparam M Target number of elements. * - * @return Vector containing the type-casted elements. + * @return *m*-dimensional vector. */ template constexpr inline explicit operator vector() const noexcept @@ -255,18 +255,29 @@ struct vector return size_cast(std::make_index_sequence{}); } - /// Returns a zero vector. - static constexpr inline vector zero() noexcept + /// Returns a zero vector, where every element is equal to zero. + static constexpr vector zero() noexcept { return {}; } + + /// @private + template + static constexpr vector one(std::index_sequence) noexcept + { + //return {T{1}...}; + + // MSVC bug workaround (I must be referenced for parameter pack expansion) + return {(I ? T{1} : T{1})...}; + } + + /// Returns a vector of ones, where every element is equal to one. + static constexpr vector one() noexcept + { + return one(std::make_index_sequence{}); + } }; -// Ensure vector is a POD type -static_assert(std::is_standard_layout>::value, "Vector is not a standard-layout type."); -static_assert(std::is_trivial>::value, "Vector is not a trivial type."); -static_assert(sizeof(vector) == sizeof(float) * 3, "Vector size is greater than the size of its elements."); - /// Vector with two elements. template using vector2 = vector; @@ -1477,67 +1488,67 @@ std::istream& operator>>(std::istream& is, math::vector& v); template constexpr inline math::vector operator+(const math::vector& x, const math::vector& y) { - return add(x, y); + return math::add(x, y); } template constexpr inline math::vector operator+(const math::vector& x, T y) { - return add(x, y); + return math::add(x, y); } template constexpr inline math::vector operator+(T x, const math::vector& y) { - return add(y, x); + return math::add(y, x); } template constexpr inline math::vector operator-(const math::vector& x) { - return negate(x); + return math::negate(x); } template constexpr inline math::vector operator-(const math::vector& x, const math::vector& y) { - return sub(x, y); + return math::sub(x, y); } template constexpr inline math::vector operator-(const math::vector& x, T y) { - return sub(x, y); + return math::sub(x, y); } template constexpr inline math::vector operator*(const math::vector& x, const math::vector& y) { - return mul(x, y); + return math::mul(x, y); } template constexpr inline math::vector operator*(const math::vector& x, T y) { - return mul(x, y); + return math::mul(x, y); } template constexpr inline math::vector operator*(T x, const math::vector& y) { - return mul(y, x); + return math::mul(y, x); } template constexpr inline math::vector operator/(const math::vector& x, const math::vector& y) { - return div(x, y); + return math::div(x, y); } template constexpr inline math::vector operator/(const math::vector& x, T y) { - return div(x, y); + return math::div(x, y); } template diff --git a/src/render/passes/ground-pass.cpp b/src/render/passes/ground-pass.cpp index 67dd525..1feeece 100644 --- a/src/render/passes/ground-pass.cpp +++ b/src/render/passes/ground-pass.cpp @@ -85,7 +85,7 @@ void ground_pass::render(const render::context& ctx, render::queue& queue) const float clip_near = camera.get_clip_near_tween().interpolate(ctx.alpha); float clip_far = camera.get_clip_far_tween().interpolate(ctx.alpha); float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f; - float4x4 model = math::scale(math::matrix4::identity, model_scale); + float4x4 model = math::scale(math::matrix4::identity(), model_scale); float4x4 view = math::resize<4, 4>(math::resize<3, 3>(ctx.view)); float4x4 model_view = view * model; const float4x4& projection = ctx.projection; diff --git a/src/render/passes/shadow-map-pass.cpp b/src/render/passes/shadow-map-pass.cpp index f413da8..fea52a2 100644 --- a/src/render/passes/shadow-map-pass.cpp +++ b/src/render/passes/shadow-map-pass.cpp @@ -72,13 +72,13 @@ shadow_map_pass::shadow_map_pass(gl::rasterizer* rasterizer, const gl::framebuff skinned_model_view_projection_input = skinned_shader_program->get_input("model_view_projection"); // Calculate bias-tile matrices - float4x4 bias_matrix = math::translate(math::matrix4::identity, float3{0.5f, 0.5f, 0.5f}) * math::scale(math::matrix4::identity, float3{0.5f, 0.5f, 0.5f}); - float4x4 tile_scale = math::scale(math::matrix4::identity, float3{0.5f, 0.5f, 1.0f}); + float4x4 bias_matrix = math::translate(math::matrix4::identity(), float3{0.5f, 0.5f, 0.5f}) * math::scale(math::matrix4::identity(), float3{0.5f, 0.5f, 0.5f}); + float4x4 tile_scale = math::scale(math::matrix4::identity(), float3{0.5f, 0.5f, 1.0f}); for (int i = 0; i < 4; ++i) { float x = static_cast(i % 2) * 0.5f; float y = static_cast(i / 2) * 0.5f; - float4x4 tile_matrix = math::translate(math::matrix4::identity, float3{x, y, 0.0f}) * tile_scale; + float4x4 tile_matrix = math::translate(math::matrix4::identity(), float3{x, y, 0.0f}) * tile_scale; bias_tile_matrices[i] = tile_matrix * bias_matrix; } } @@ -210,7 +210,7 @@ void shadow_map_pass::render(const render::context& ctx, render::queue& queue) c offset.y() = std::ceil(offset.y() * half_shadow_map_resolution) / half_shadow_map_resolution; // Crop the light view-projection matrix - crop_matrix = math::translate(math::matrix4::identity, offset) * math::scale(math::matrix4::identity, scale); + crop_matrix = math::translate(math::matrix4::identity(), offset) * math::scale(math::matrix4::identity(), scale); cropped_view_projection = crop_matrix * light_view_projection; // Calculate shadow matrix diff --git a/src/render/passes/sky-pass.cpp b/src/render/passes/sky-pass.cpp index 739ef3c..e675210 100644 --- a/src/render/passes/sky-pass.cpp +++ b/src/render/passes/sky-pass.cpp @@ -193,7 +193,7 @@ void sky_pass::render(const render::context& ctx, render::queue& queue) const float clip_near = camera.get_clip_near_tween().interpolate(ctx.alpha); float clip_far = camera.get_clip_far_tween().interpolate(ctx.alpha); float3 model_scale = float3{1.0f, 1.0f, 1.0f} * (clip_near + clip_far) * 0.5f; - float4x4 model = math::scale(math::matrix4::identity, model_scale); + float4x4 model = math::scale(math::matrix4::identity(), model_scale); float4x4 view = math::resize<4, 4>(math::resize<3, 3>(ctx.view)); float4x4 model_view = view * model; const float4x4& projection = ctx.projection; diff --git a/src/scene/camera.cpp b/src/scene/camera.cpp index 26194a5..d77e1e6 100644 --- a/src/scene/camera.cpp +++ b/src/scene/camera.cpp @@ -71,9 +71,9 @@ camera::camera(): clip_far(1.0f, math::lerp), fov(math::half_pi, math::lerp), aspect_ratio(1.0f, math::lerp), - view(math::matrix4::identity, std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), - projection(math::matrix4::identity, std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), - view_projection(math::matrix4::identity, std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + view(math::matrix4::identity(), std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + projection(math::matrix4::identity(), std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + view_projection(math::matrix4::identity(), std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), exposure(0.0f, math::lerp) {} diff --git a/src/utility/fundamental-types.hpp b/src/utility/fundamental-types.hpp index 01a773f..3dbe648 100644 --- a/src/utility/fundamental-types.hpp +++ b/src/utility/fundamental-types.hpp @@ -21,8 +21,7 @@ #define ANTKEEPER_FUNDAMENTAL_TYPES_HPP #include "math/vector.hpp" -#include "math/matrix-type.hpp" -#include "math/matrix-operators.hpp" +#include "math/matrix.hpp" /// 2D vector of bools using bool2 = math::vector;