- Functions to construct matrices from translations, scaling, and rotations

This commit is contained in:
2025-07-16 03:45:54 -04:00
parent 89f59c75f3
commit f1552b89b1
4 changed files with 208 additions and 6 deletions

View File

@@ -108,14 +108,14 @@ add_library(fennec STATIC
include/fennec/math/trigonometric.h
include/fennec/math/relational.h
include/fennec/math/ext/constants.h
include/fennec/math/detail/__fwd.h
include/fennec/math/detail/__math.h
include/fennec/math/detail/__matrix.h
include/fennec/math/detail/__types.h
include/fennec/math/detail/__vector_traits.h
include/fennec/math/ext/constants.h
# FPROC ================================================================================================================
# Strings
@@ -128,6 +128,7 @@ add_library(fennec STATIC
# Filesystem
include/fennec/fproc/filesystem/file.h source/fproc/filesystem/file.cpp
include/fennec/fproc/filesystem/path.h source/fproc/filesystem/path.cpp
include/fennec/math/ext/transform.h
)
# add metaprogramming templates as a dependency and also force documentation to be generated when fennec is compiled

View File

@@ -26,6 +26,9 @@ namespace fennec
///
/// \brief struct for handling file paths
///
/// \details This structure makes no guarantees about the validity of a path.
/// Operations do not examine the system's file structure.
struct path
{
public:

View File

@@ -0,0 +1,198 @@
// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// This program 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.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================
#ifndef FENNEC_MATH_EXT_TRANSFORM_H
#define FENNEC_MATH_EXT_TRANSFORM_H
#include <fennec/math/matrix.h>
#include <fennec/math/trigonometric.h>
#include <fennec/math/ext/constants.h>
namespace fennec
{
///
/// \brief Create a 2d translation matrix
/// \param x a vector containing the position
/// \returns An identity matrix with the last column set to x
template<typename genType>
constexpr mat<genType, 3, 3> translation(const vec<genType, 2>& x) {
return mat<genType, 3, 3>(
1, 0, 0
, 0, 1, 0
, x, 1
);
}
///
/// \brief Create a 3d translation matrix
/// \param x a vector containing the position
/// \returns An identity matrix with the last column set to x
template<typename genType>
constexpr mat<genType, 4, 4> translation(const vec<genType, 3>& x) {
return mat<genType, 4, 4>(
1, 0, 0, 0
, 0, 1, 0, 0
, 0, 0, 1, 0
, x, 1
);
}
///
/// \brief Create a 2d scaling matrix
/// \param x a vector containing the scale for each axis
/// \returns A diagonal matrix with the terms of x
template<typename genType>
constexpr mat<genType, 3, 3> scaling(const vec<genType, 2>& x) {
return mat<genType, 3, 3>(
x.x, 0, 0
, 0, x.y, 0
, 0, 0, 1
);
}
///
/// \brief Create a 3d scaling matrix
/// \param x a vector containing the scale for each axis
/// \returns A diagonal matrix with the terms of x
template<typename genType>
constexpr mat<genType, 4, 4> scaling(const vec<genType, 3>& x) {
return mat<genType, 4, 4>(
x.x, 0, 0, 0
, 0, x.y, 0, 0
, 0, 0, x.z, 0
, 0, 0, 0, 1
);
}
///
/// \brief Create a 2d rotation matrix
/// \param a the angle of the rotation in radians
/// \returns A rotation matrix that rotates about the "z-axis"
template<typename genType>
constexpr mat<genType, 3, 3> rotation(const genType a) {
// Calculate sin and cos terms
const genType c = fennec::cos(a);
const genType s = fennec::sin(a);
// Calculate the matrix
return mat<genType, 3, 3>(
c, -s, 0
, s, c, 0
, 0, 0, 1
);
}
///
/// \brief Create a 3d rotation matrix from axis-angle
/// \param A The axis vector
/// \param a The rotation about the axis in radians
/// \returns A rotation matrix about A
template<typename genType>
constexpr mat<genType, 4, 4> rotation(const vec<genType, 3>& A, genType a) {
// https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
// Calculate sin and cos terms
const genType c = fennec::cos(a);
const genType s = fennec::sin(a);
// Calculate axis term
const vec<genType, 3> u = (fennec::one<genType>() - c) * A;
const vec<genType, 3> v = s * A;
// Calculate the Matrix
return mat<genType, 4, 4>(
A.x * u.x + c, A.x * u.y + v.z, A.x * u.z - v.y, 0
, A.x * u.y - v.z, A.y * u.y + c, A.y * u.z + v.x, 0
, A.x * u.z + v.y, A.y * u.z - v.x, A.z * u.z + c, 0
, 0, 0, 0, 1
);
}
template<typename genType>
constexpr mat<genType, 4, 4> rotation_x(genType a) {
// Calculate sin and cos terms
const genType c = fennec::cos(a);
const genType s = fennec::sin(a);
// Calculate the matrix
return mat<genType, 4, 4>(
1, 0, 0, 0
, 0, c, -s, 0
, 0, s, c, 0
, 0, 0, 0, 1
);
}
template<typename genType>
constexpr mat<genType, 4, 4> rotation_y(genType a) {
// Calculate sin and cos terms
const genType c = fennec::cos(a);
const genType s = fennec::sin(a);
// Calculate the matrix
return mat<genType, 4, 4>(
c, 0, s, 0
, 0, 0, 0, 0
, -s, 0, c, 0
, 0, 0, 0, 1
);
}
template<typename genType>
constexpr mat<genType, 4, 4> rotation_z(const genType a) {
// Calculate sin and cos terms
const genType c = fennec::cos(a);
const genType s = fennec::sin(a);
// Calculate the matrix
return mat<genType, 4, 4>(
c, -s, 0, 0
, s, c, 0, 0
, 0, 0, 1, 0
, 0, 0, 0, 1
);
}
template<typename genType>
constexpr mat<genType, 4, 4> rotation(const vec<genType, 3>& E) {
// Calculate sin and cos terms
const genType ca = fennec::cos(E.x);
const genType sa = fennec::sin(E.x);
const genType cb = fennec::cos(E.y);
const genType sb = fennec::sin(E.y);
const genType cg = fennec::cos(E.z);
const genType sg = fennec::sin(E.z);
// Calculate the matrix
return mat<genType, 4, 4>(
ca*cb, sa*sb, -sb, 0
, ca*sb*sg - sa*cg, sa*sb*sg + ca*cg, cb*sg, 0
, ca*sb*cg + sa*sg, sa*sb*cg - ca*sg, cb*cg, 0
, 0, 0, 0, 1
);
}
}
#endif // FENNEC_MATH_EXT_TRANSFORM_H