Browse Source

Improve and consolidate matrix type

master
C. J. Howard 1 year ago
parent
commit
1d1795217b
17 changed files with 484 additions and 279 deletions
  1. +15
    -0
      src/game/state/nuptial-flight.cpp
  2. +10
    -4
      src/game/system/collision.cpp
  3. +9
    -1
      src/geom/primitive/intersection.hpp
  4. +1
    -1
      src/geom/view-frustum.hpp
  5. +1
    -4
      src/math/math.hpp
  6. +0
    -86
      src/math/matrix-operators.hpp
  7. +0
    -91
      src/math/matrix-type.hpp
  8. +404
    -4
      src/math/matrix.hpp
  9. +1
    -1
      src/math/quaternion-functions.hpp
  10. +0
    -53
      src/math/stream-operators.hpp
  11. +0
    -1
      src/math/transform-functions.hpp
  12. +33
    -22
      src/math/vector.hpp
  13. +1
    -1
      src/render/passes/ground-pass.cpp
  14. +4
    -4
      src/render/passes/shadow-map-pass.cpp
  15. +1
    -1
      src/render/passes/sky-pass.cpp
  16. +3
    -3
      src/scene/camera.cpp
  17. +1
    -2
      src/utility/fundamental-types.hpp

+ 15
- 0
src/game/state/nuptial-flight.cpp View File

@ -76,6 +76,21 @@ nuptial_flight::nuptial_flight(game::context& ctx):
ctx.logger->push_task("Entering nuptial flight state");
math::matrix<float, 3, 3> m =
{
1, 2, 3,
4, 5, 6,
7, 8, 9
};
auto m2 = math::matrix<float, 3, 3>::one();
auto m3 = math::matrix<int, 3, 3>(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;

+ 10
- 4
src/game/system/collision.cpp View File

@ -60,15 +60,21 @@ entity::id collision::pick_nearest(const geom::primitive::ray& ray, st
const geom::primitive::sphere<float> 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;
}
}
}
);

+ 9
- 1
src/geom/primitive/intersection.hpp View File

@ -125,7 +125,15 @@ std::optional> intersection(const ray& ray, const hypersp
h = std::sqrt(h);
return std::tuple<float, float>{-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<T, T>{t0, t1};
}
/**

+ 1
- 1
src/geom/view-frustum.hpp View File

@ -99,7 +99,7 @@ view_frustum::view_frustum(const matrix_type& view_projection):
template <class T>
view_frustum<T>::view_frustum():
view_frustum(math::matrix4<T>::identity)
view_frustum(math::matrix4<T>::identity())
{}
template <class T>

+ 1
- 4
src/math/math.hpp View File

@ -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"

+ 0
- 86
src/math/matrix-operators.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator+(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y);
/// @copydoc math::mul(const math::matrix<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y);
/// @copydoc math::mul(const math::matrix<T, N, M>&, T)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& m, T s);
/// @copydoc math::mul(const math::matrix<T, N, M>&, T)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(T s, const math::matrix<T, N, M>& m);
/// @copydoc math::mul(const math::matrix<T, 2, 2>&, const math::vector<T, 2>&)
template <class T, std::size_t N>
constexpr math::vector<T, N> operator*(const math::matrix<T, N, N>& m, const math::vector<T, N>& v);
/// @copydoc math::sub(const math::matrix<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator-(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y);
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator+(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return add(x, y);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return mul(x, y);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& m, T s)
{
return mul(m, s);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(T s, const math::matrix<T, N, M>& m)
{
return mul(m, s);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator*(const math::matrix<T, N, N>& m, const math::vector<T, N>& v)
{
return mul(m, v);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator-(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return sub(x, y);
}
#endif // ANTKEEPER_MATH_MATRIX_OPERATORS_HPP

+ 0
- 91
src/math/matrix-type.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_MATH_MATRIX_TYPE_HPP
#define ANTKEEPER_MATH_MATRIX_TYPE_HPP
#include "math/vector.hpp"
#include <cstddef>
#include <utility>
namespace math {
/**
* An NxM matrix.
*
* @tparam T Matrix element type.
* @tparam N Number of columns.
* @tparam M Number of rows.
*/
template <typename T, std::size_t N, std::size_t M>
struct matrix
{
typedef T element_type;
typedef vector<element_type, M> 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 <typename T, std::size_t I, std::size_t... Is>
constexpr vector<T, sizeof...(Is)> identity_matrix_row(const std::index_sequence<Is...>&)
{
return {(Is == I ? T{1} : T{0})...};
}
template <typename T, std::size_t... Is>
constexpr matrix<T, sizeof...(Is), sizeof...(Is)> identity_matrix(const std::index_sequence<Is...>& is)
{
return {{identity_matrix_row<T, Is>(is)...}};
}
template <typename T, std::size_t N, std::size_t M>
constexpr matrix<T, N, M> matrix<T, N, M>::identity = identity_matrix<T>(std::make_index_sequence<N>{});
/// 2x2 matrix.
template <typename T>
using matrix2 = matrix<T, 2, 2>;
/// 2x2 matrix.
template <typename T>
using matrix2x2 = matrix<T, 2, 2>;
/// 3x3 matrix.
template <typename T>
using matrix3 = matrix<T, 3, 3>;
/// 3x3 matrix.
template <typename T>
using matrix3x3 = matrix<T, 3, 3>;
/// 4x4 matrix.
template <typename T>
using matrix4 = matrix<T, 4, 4>;
/// 4x4 matrix.
template <typename T>
using matrix4x4 = matrix<T, 4, 4>;
} // namespace math
#endif // ANTKEEPER_MATH_MATRIX_TYPE_HPP

src/math/matrix-functions.hpp → src/math/matrix.hpp View File

@ -17,15 +17,313 @@
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#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 <cstddef>
#include <istream>
#include <iterator>
#include <ostream>
#include <type_traits>
#include <utility>
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 <typename T, std::size_t N, std::size_t M>
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<element_type, row_count> column_vector_type;
/// Matrix row vector data type.
typedef vector<element_type, column_count> 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<column_vector_type*> rbegin() noexcept
{
return std::reverse_iterator<column_vector_type*>(columns + column_count);
}
constexpr inline std::reverse_iterator<const column_vector_type*> rbegin() const noexcept
{
return std::reverse_iterator<const column_vector_type*>(columns + column_count);
}
constexpr inline std::reverse_iterator<const column_vector_type*> crbegin() const noexcept
{
return std::reverse_iterator<const column_vector_type*>(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<column_vector_type*> rend() noexcept
{
return std::reverse_iterator<column_vector_type*>(columns);
}
constexpr inline std::reverse_iterator<const column_vector_type*> rend() const noexcept
{
return std::reverse_iterator<const column_vector_type*>(columns);
}
constexpr inline std::reverse_iterator<const column_vector_type*> crend() const noexcept
{
return std::reverse_iterator<const column_vector_type*>(columns);
}
/// @}
/// Returns the number of elements in the matrix.
constexpr inline std::size_t size() const noexcept
{
return element_count;
};
/// @private
template <class U, std::size_t... I>
constexpr inline matrix<U, N, M> type_cast(std::index_sequence<I...>) const noexcept
{
return {vector<U, M>(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 <class U>
constexpr inline explicit operator matrix<U, N, M>() const noexcept
{
return type_cast<U>(std::make_index_sequence<N>{});
}
/// @private
template <std::size_t P, std::size_t O, std::size_t... I>
constexpr inline matrix<T, P, O> size_cast(std::index_sequence<I...>) const noexcept
{
if constexpr (O == M)
return {((I < N) ? columns[I] : vector<T, O>::zero()) ...};
else
return {((I < N) ? vector<T, O>(columns[I]) : vector<T, O>::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 <std::size_t P, std::size_t O>
constexpr inline explicit operator matrix<T, P, O>() const noexcept
{
return size_cast<P, O>(std::make_index_sequence<P>{});
}
/// Returns a zero matrix, where every element is equal to zero.
static constexpr matrix zero() noexcept
{
return {};
}
/// @private
template <std::size_t... I>
static constexpr inline matrix one(std::index_sequence<I...>) 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<column_count>{});
}
/// @private
template <std::size_t... I>
static constexpr inline column_vector_type identity_column(std::size_t i, std::index_sequence<I...>) noexcept
{
return {(I == i ? T{1} : T{0}) ...};
}
/// @private
template <std::size_t... I>
static constexpr inline matrix identity(std::index_sequence<I...>) noexcept
{
return {identity_column(I, std::make_index_sequence<row_count>{}) ...};
}
/// Returns an identity matrix, with ones on the main diagonal and zeros elsewhere.
static constexpr matrix identity() noexcept
{
return identity(std::make_index_sequence<column_count>{});
}
};
/// 2x2 matrix.
template <typename T>
using matrix2 = matrix<T, 2, 2>;
/// 2x2 matrix.
template <typename T>
using matrix2x2 = matrix<T, 2, 2>;
/// 3x3 matrix.
template <typename T>
using matrix3 = matrix<T, 3, 3>;
/// 3x3 matrix.
template <typename T>
using matrix3x3 = matrix<T, 3, 3>;
/// 4x4 matrix.
template <typename T>
using matrix4 = matrix<T, 4, 4>;
/// 4x4 matrix.
template <typename T>
using matrix4x4 = matrix<T, 4, 4>;
/**
* 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<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator+(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y);
/// @copydoc math::mul(const math::matrix<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y);
/// @copydoc math::mul(const math::matrix<T, N, M>&, T)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& m, T s);
/// @copydoc math::mul(const math::matrix<T, N, M>&, T)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator*(T s, const math::matrix<T, N, M>& m);
/// @copydoc math::mul(const math::matrix<T, 2, 2>&, const math::vector<T, 2>&)
template <class T, std::size_t N>
constexpr math::vector<T, N> operator*(const math::matrix<T, N, N>& m, const math::vector<T, N>& v);
/// @copydoc math::sub(const math::matrix<T, 2, 2>&, const math::matrix<T, 2, 2>&)
template <class T, std::size_t N, std::size_t M>
constexpr math::matrix<T, N, M> operator-(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& 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 <class T, std::size_t N, std::size_t M>
std::ostream& operator<<(std::ostream& os, const math::matrix<T, N, M>& 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 <class T, std::size_t N, std::size_t M>
std::istream& operator>>(std::istream& is, math::matrix<T, N, M>& m);
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator+(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return math::add(x, y);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return math::mul(x, y);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(const math::matrix<T, N, M>& m, T s)
{
return math::mul(m, s);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator*(T s, const math::matrix<T, N, M>& m)
{
return math::mul(m, s);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator*(const math::matrix<T, N, N>& m, const math::vector<T, N>& v)
{
return math::mul(m, v);
}
template <class T, std::size_t N, std::size_t M>
constexpr inline math::matrix<T, N, M> operator-(const math::matrix<T, N, M>& x, const math::matrix<T, N, M>& y)
{
return math::sub(x, y);
}
template <class T, std::size_t N, std::size_t M>
std::ostream& operator<<(std::ostream& os, const math::matrix<T, N, M>& m)
{
for (std::size_t i = 0; i < m.size(); ++i)
{
if (i)
os << ' ';
os << m.element(i);
}
return os;
}
template <class T, std::size_t N, std::size_t M>
std::istream& operator>>(std::istream& is, math::matrix<T, N, M>& m)
{
for (std::size_t i = 0; i < m.size(); ++i)
is >> m.element(i);
return is;
}
#endif // ANTKEEPER_MATH_MATRIX_HPP

+ 1
- 1
src/math/quaternion-functions.hpp View File

@ -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 <cmath>

+ 0
- 53
src/math/stream-operators.hpp View File

@ -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 <istream>
#include <ostream>
/**
* 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 <class T, std::size_t N, std::size_t M>
std::ostream& operator<<(std::ostream& os, const math::matrix<T, N, M>& 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 <class T>
std::ostream& operator<<(std::ostream& os, const math::quaternion<T>& 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 <class T, std::size_t N, std::size_t M>
std::istream& operator>>(std::istream& is, math::matrix<T, N, M>& 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 <class T>
std::istream& operator>>(std::istream& is, const math::quaternion<T>& q);
template <class T, std::size_t N, std::size_t M>
std::ostream& operator<<(std::ostream& os, const math::matrix<T, N, M>& 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 <class T>
std::ostream& operator<<(std::ostream& os, const math::quaternion<T>& q)
{
@ -91,19 +51,6 @@ std::ostream& operator<<(std::ostream& os, const math::quaternion& q)
return os;
}
template <class T, std::size_t N, std::size_t M>
std::istream& operator>>(std::istream& is, math::matrix<T, N, M>& 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 <class T>
std::istream& operator>>(std::istream& is, const math::quaternion<T>& q)
{

+ 0
- 1
src/math/transform-functions.hpp View File

@ -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 {

+ 33
- 22
src/math/vector.hpp View File

@ -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 <std::size_t M>
constexpr inline explicit operator vector<T, M>() const noexcept
@ -255,18 +255,29 @@ struct vector
return size_cast<M>(std::make_index_sequence<M>{});
}
/// 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 <std::size_t... I>
static constexpr vector one(std::index_sequence<I...>) 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<N>{});
}
};
// Ensure vector is a POD type
static_assert(std::is_standard_layout<vector<float, 3>>::value, "Vector is not a standard-layout type.");
static_assert(std::is_trivial<vector<float, 3>>::value, "Vector is not a trivial type.");
static_assert(sizeof(vector<float, 3>) == sizeof(float) * 3, "Vector size is greater than the size of its elements.");
/// Vector with two elements.
template <typename T>
using vector2 = vector<T, 2>;
@ -1477,67 +1488,67 @@ std::istream& operator>>(std::istream& is, math::vector& v);
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator+(const math::vector<T, N>& x, const math::vector<T, N>& y)
{
return add(x, y);
return math::add(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator+(const math::vector<T, N>& x, T y)
{
return add(x, y);
return math::add(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator+(T x, const math::vector<T, N>& y)
{
return add(y, x);
return math::add(y, x);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator-(const math::vector<T, N>& x)
{
return negate(x);
return math::negate(x);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator-(const math::vector<T, N>& x, const math::vector<T, N>& y)
{
return sub(x, y);
return math::sub(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator-(const math::vector<T, N>& x, T y)
{
return sub(x, y);
return math::sub(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator*(const math::vector<T, N>& x, const math::vector<T, N>& y)
{
return mul(x, y);
return math::mul(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator*(const math::vector<T, N>& x, T y)
{
return mul(x, y);
return math::mul(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator*(T x, const math::vector<T, N>& y)
{
return mul(y, x);
return math::mul(y, x);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator/(const math::vector<T, N>& x, const math::vector<T, N>& y)
{
return div(x, y);
return math::div(x, y);
}
template <class T, std::size_t N>
constexpr inline math::vector<T, N> operator/(const math::vector<T, N>& x, T y)
{
return div(x, y);
return math::div(x, y);
}
template <class T, std::size_t N>

+ 1
- 1
src/render/passes/ground-pass.cpp View File

@ -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<float>::identity, model_scale);
float4x4 model = math::scale(math::matrix4<float>::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;

+ 4
- 4
src/render/passes/shadow-map-pass.cpp View File

@ -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<float>::identity, float3{0.5f, 0.5f, 0.5f}) * math::scale(math::matrix4<float>::identity, float3{0.5f, 0.5f, 0.5f});
float4x4 tile_scale = math::scale(math::matrix4<float>::identity, float3{0.5f, 0.5f, 1.0f});
float4x4 bias_matrix = math::translate(math::matrix4<float>::identity(), float3{0.5f, 0.5f, 0.5f}) * math::scale(math::matrix4<float>::identity(), float3{0.5f, 0.5f, 0.5f});
float4x4 tile_scale = math::scale(math::matrix4<float>::identity(), float3{0.5f, 0.5f, 1.0f});
for (int i = 0; i < 4; ++i)
{
float x = static_cast<float>(i % 2) * 0.5f;
float y = static_cast<float>(i / 2) * 0.5f;
float4x4 tile_matrix = math::translate(math::matrix4<float>::identity, float3{x, y, 0.0f}) * tile_scale;
float4x4 tile_matrix = math::translate(math::matrix4<float>::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<float>::identity, offset) * math::scale(math::matrix4<float>::identity, scale);
crop_matrix = math::translate(math::matrix4<float>::identity(), offset) * math::scale(math::matrix4<float>::identity(), scale);
cropped_view_projection = crop_matrix * light_view_projection;
// Calculate shadow matrix

+ 1
- 1
src/render/passes/sky-pass.cpp View File

@ -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<float>::identity, model_scale);
float4x4 model = math::scale(math::matrix4<float>::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;

+ 3
- 3
src/scene/camera.cpp View File

@ -71,9 +71,9 @@ camera::camera():
clip_far(1.0f, math::lerp<float, float>),
fov(math::half_pi<float>, math::lerp<float, float>),
aspect_ratio(1.0f, math::lerp<float, float>),
view(math::matrix4<float>::identity, std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
projection(math::matrix4<float>::identity, std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
view_projection(math::matrix4<float>::identity, std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
view(math::matrix4<float>::identity(), std::bind(&interpolate_view, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
projection(math::matrix4<float>::identity(), std::bind(&interpolate_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
view_projection(math::matrix4<float>::identity(), std::bind(&interpolate_view_projection, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
exposure(0.0f, math::lerp<float, float>)
{}

+ 1
- 2
src/utility/fundamental-types.hpp View File

@ -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<bool, 2>;

Loading…
Cancel
Save