1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

WIP matrix & rotation lua APIs

This commit is contained in:
Lars Mueller 2025-05-10 20:38:44 +02:00
parent a87ce1bad7
commit 513532a93c
15 changed files with 1509 additions and 5 deletions

View file

@ -12,6 +12,7 @@
#include "aabbox3d.h"
#include "rect.h"
#include <cassert>
#include <ostream>
namespace irr
{
@ -213,6 +214,9 @@ public:
//! Get Scale
vector3d<T> getScale() const;
//! Scale the matrix rows ("axes") by the components of a vector
void scaleAxes(const vector3d<T> &v);
//! Translate a vector by the inverse of the translation part of this matrix.
void inverseTranslateVect(vector3df &vect) const;
@ -220,6 +224,7 @@ public:
[[nodiscard]] vector3d<T> scaleThenInvRotVect(const vector3d<T> &vect) const;
//! Rotate and scale a vector. Applies both rotation & scale part of the matrix.
// TODO rename to transformDirection
[[nodiscard]] vector3d<T> rotateAndScaleVect(const vector3d<T> &vect) const;
//! Transforms the vector by this matrix
@ -426,6 +431,16 @@ public:
//! Compare two matrices using the equal method
bool equals(const CMatrix4<T> &other, const T tolerance = (T)ROUNDING_ERROR_f64) const;
//! Check whether matrix is a 3d affine transform (last column is approximately 0, 0, 0, 1)
bool isAffine(const T tolerance = (T)ROUNDING_ERROR_f64) const
{
const auto &m = *this;
return core::equals(m(0, 3), (T) 0) &&
core::equals(m(1, 3), (T) 0) &&
core::equals(m(2, 3), (T) 0) &&
core::equals(m(3, 3), (T) 1);
}
private:
template <bool degrees>
vector3d<T> getRotation(const vector3d<T> &scale) const;
@ -751,6 +766,20 @@ inline vector3d<T> CMatrix4<T>::getScale() const
};
}
template <class T>
void CMatrix4<T>::scaleAxes(const vector3d<T> &v)
{
auto scale_row = [this](int row, T scale) {
auto &m = *this;
m(row, 0) *= scale;
m(row, 1) *= scale;
m(row, 2) *= scale;
};
scale_row(0, v.X);
scale_row(1, v.Y);
scale_row(2, v.Z);
}
template <class T>
inline CMatrix4<T> &CMatrix4<T>::setRotationDegrees(const vector3d<T> &rotation)
{
@ -1631,6 +1660,19 @@ inline void CMatrix4<T>::getTransposed(CMatrix4<T> &o) const
o[15] = M[15];
}
template <class T>
std::ostream& operator<<(std::ostream& os, const CMatrix4<T>& matrix)
{
for (int row = 0; row < 4; ++row) {
for (int col = 0; col < 4; ++col) {
os << "\t";
os << matrix(row, col);
}
os << "\n";
}
return os;
}
// used to scale <-1,-1><1,1> to viewport
template <class T>
inline CMatrix4<T> &CMatrix4<T>::buildNDCToDCMatrix(const rect<s32> &viewport, f32 zScale)