282 lines
No EOL
10 KiB
C++
Executable file
282 lines
No EOL
10 KiB
C++
Executable file
/*
|
|
Crafter.Math
|
|
Copyright (C) 2025 Catcrafts
|
|
Catcrafts.net
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 3.0 of the License, or (at your option) any later version.
|
|
|
|
This library 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
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
module;
|
|
|
|
#include <type_traits>
|
|
#include <concepts>
|
|
#include <immintrin.h>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <iostream>
|
|
#include <cmath>
|
|
|
|
export module Crafter.Math:MatrixRowMajor;
|
|
|
|
import :BasicTypes;
|
|
import :Vector;
|
|
import :Misc;
|
|
|
|
namespace Crafter {
|
|
export template <typename T, uint32_t CollumSize, uint32_t RowSize, uint32_t Repeats>
|
|
class MatrixRowMajor {
|
|
public:
|
|
T m[RowSize][CollumSize*Repeats];
|
|
|
|
MatrixRowMajor() = default;
|
|
|
|
MatrixRowMajor(
|
|
float x0, float y0, float z0, float w0,
|
|
float x1, float y1, float z1, float w1,
|
|
float x2, float y2, float z2, float w2,
|
|
float x3, float y3, float z3, float w3
|
|
) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
m[0][0] = x0;
|
|
m[0][1] = y0;
|
|
m[0][2] = z0;
|
|
m[0][3] = w0;
|
|
|
|
m[1][0] = x1;
|
|
m[1][1] = y1;
|
|
m[1][2] = z1;
|
|
m[1][3] = w1;
|
|
|
|
m[2][0] = x2;
|
|
m[2][1] = y2;
|
|
m[2][2] = z2;
|
|
m[2][3] = w2;
|
|
|
|
m[3][0] = x3;
|
|
m[3][1] = y3;
|
|
m[3][2] = z3;
|
|
m[3][3] = w3;
|
|
}
|
|
|
|
Vector<T, 3> operator*(Vector<T, 3> b) const requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return Vector<T, 3>(
|
|
b.x * m[0][0] + b.y * m[1][0] + b.z * m[2][0] + m[3][0],
|
|
b.x * m[0][1] + b.y * m[1][1] + b.z * m[2][1] + m[3][1],
|
|
b.x * m[0][2] + b.y * m[1][2] + b.z * m[2][2] + m[3][2]
|
|
);
|
|
}
|
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> operator*(MatrixRowMajor<T, CollumSize, RowSize, Repeats> b) const requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> result;
|
|
|
|
result.m[0][0] = b.m[0][0] * m[0][0] + b.m[0][1] * m[1][0] + b.m[0][2] * m[2][0] + b.m[0][3] * m[3][0];
|
|
result.m[1][0] = b.m[1][0] * m[0][0] + b.m[1][1] * m[1][0] + b.m[1][2] * m[2][0] + b.m[1][3] * m[3][0];
|
|
result.m[2][0] = b.m[2][0] * m[0][0] + b.m[2][1] * m[1][0] + b.m[2][2] * m[2][0] + b.m[2][3] * m[3][0];
|
|
result.m[3][0] = b.m[3][0] * m[0][0] + b.m[3][1] * m[1][0] + b.m[3][2] * m[2][0] + b.m[3][3] * m[3][0];
|
|
|
|
result.m[0][1] = b.m[0][0] * m[0][1] + b.m[0][1] * m[1][1] + b.m[0][2] * m[2][1] + b.m[0][3] * m[3][1];
|
|
result.m[1][1] = b.m[1][0] * m[0][1] + b.m[1][1] * m[1][1] + b.m[1][2] * m[2][1] + b.m[1][3] * m[3][1];
|
|
result.m[2][1] = b.m[2][0] * m[0][1] + b.m[2][1] * m[1][1] + b.m[2][2] * m[2][1] + b.m[2][3] * m[3][1];
|
|
result.m[3][1] = b.m[3][0] * m[0][1] + b.m[3][1] * m[1][1] + b.m[3][2] * m[2][1] + b.m[3][3] * m[3][1];
|
|
|
|
result.m[0][2] = b.m[0][0] * m[0][2] + b.m[0][1] * m[1][2] + b.m[0][2] * m[2][2] + b.m[0][3] * m[3][2];
|
|
result.m[1][2] = b.m[1][0] * m[0][2] + b.m[1][1] * m[1][2] + b.m[1][2] * m[2][2] + b.m[1][3] * m[3][2];
|
|
result.m[2][2] = b.m[2][0] * m[0][2] + b.m[2][1] * m[1][2] + b.m[2][2] * m[2][2] + b.m[2][3] * m[3][2];
|
|
result.m[3][2] = b.m[3][0] * m[0][2] + b.m[3][1] * m[1][2] + b.m[3][2] * m[2][2] + b.m[3][3] * m[3][2];
|
|
|
|
result.m[0][3] = b.m[0][0] * m[0][3] + b.m[0][1] * m[1][3] + b.m[0][2] * m[2][3] + b.m[0][3] * m[3][3];
|
|
result.m[1][3] = b.m[1][0] * m[0][3] + b.m[1][1] * m[1][3] + b.m[1][2] * m[2][3] + b.m[1][3] * m[3][3];
|
|
result.m[2][3] = b.m[2][0] * m[0][3] + b.m[2][1] * m[1][3] + b.m[2][2] * m[2][3] + b.m[2][3] * m[3][3];
|
|
result.m[3][3] = b.m[3][0] * m[0][3] + b.m[3][1] * m[1][3] + b.m[3][2] * m[2][3] + b.m[3][3] * m[3][3];
|
|
|
|
return result;
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Perspective(float fovAngleY, float aspectRatio, float nearZ, float farZ) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
|
float SinFov;
|
|
float CosFov;
|
|
XMScalarSinCos(&SinFov, &CosFov, 0.5f * fovAngleY);
|
|
|
|
float Height = CosFov / SinFov;
|
|
float Width = Height / aspectRatio;
|
|
float fRange = farZ / (nearZ - farZ);
|
|
|
|
M.m[0][0] = Width;
|
|
M.m[0][1] = 0.0f;
|
|
M.m[0][2] = 0.0f;
|
|
M.m[0][3] = 0.0f;
|
|
|
|
M.m[1][0] = 0.0f;
|
|
M.m[1][1] = Height;
|
|
M.m[1][2] = 0.0f;
|
|
M.m[1][3] = 0.0f;
|
|
|
|
M.m[2][0] = 0.0f;
|
|
M.m[2][1] = 0.0f;
|
|
M.m[2][2] = fRange;
|
|
M.m[2][3] = -1.0f;
|
|
|
|
M.m[3][0] = 0.0f;
|
|
M.m[3][1] = 0.0f;
|
|
M.m[3][2] = fRange * nearZ;
|
|
M.m[3][3] = 0.0f;
|
|
return M;
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
|
1, 0, 0, 0,
|
|
0, 1, 0, 0,
|
|
0, 0, 1, 0,
|
|
0, 0, 0, 1
|
|
);
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
|
x, 0, 0, 0,
|
|
0, y, 0, 0,
|
|
0, 0, z, 0,
|
|
0, 0, 0, 1
|
|
);
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return Scaling(vector.x, vector.y, vector.z);
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(float x, float y, float z) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
|
1, 0, 0, 0,
|
|
0, 1, 0, 0,
|
|
0, 0, 1, 0,
|
|
x, y, z, 1
|
|
);
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return Translation(vector.x, vector.y, vector.z);
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(float Pitch, float Yaw, float Roll) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
float cp = cosf(Pitch);
|
|
float sp = sinf(Pitch);
|
|
|
|
float cy = cosf(Yaw);
|
|
float sy = sinf(Yaw);
|
|
|
|
float cr = cosf(Roll);
|
|
float sr = sinf(Roll);
|
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
|
M.m[0][0] = cr * cy + sr * sp * sy;
|
|
M.m[0][1] = sr * cp;
|
|
M.m[0][2] = sr * sp * cy - cr * sy;
|
|
M.m[0][3] = 0.0f;
|
|
|
|
M.m[1][0] = cr * sp * sy - sr * cy;
|
|
M.m[1][1] = cr * cp;
|
|
M.m[1][2] = sr * sy + cr * sp * cy;
|
|
M.m[1][3] = 0.0f;
|
|
|
|
M.m[2][0] = cp * sy;
|
|
M.m[2][1] = -sp;
|
|
M.m[2][2] = cp * cy;
|
|
M.m[2][3] = 0.0f;
|
|
|
|
M.m[3][0] = 0.0f;
|
|
M.m[3][1] = 0.0f;
|
|
M.m[3][2] = 0.0f;
|
|
M.m[3][3] = 1.0f;
|
|
|
|
return M;
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> LookAt(Vector<float, 3> eyePosition, Vector<float, 3> focusPosition, Vector<float, 3> upDirection) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
|
|
|
Vector<float, 3> negEyeDirection = eyePosition - focusPosition;
|
|
return LookTo(eyePosition, negEyeDirection, upDirection);
|
|
|
|
return M;
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> LookTo(Vector<float, 3> eyePosition, Vector<float, 3> eyeDirection, Vector<float, 3> upDirection) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
Vector<float, 3> R2 = eyeDirection.Normalize();
|
|
|
|
Vector<float, 3> R0 = upDirection.Cross(R2);
|
|
R0 = R0.Normalize();
|
|
|
|
Vector<float, 3> R1 = R2.Cross(R0);
|
|
|
|
Vector<float, 3> NegEyePosition = -eyePosition;
|
|
|
|
float D0 = R0.Dot(NegEyePosition);
|
|
float D1 = R1.Dot(NegEyePosition);
|
|
float D2 = R2.Dot(NegEyePosition);
|
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
|
M.m[0][0] = R0.v[0];
|
|
M.m[1][0] = R0.v[1];
|
|
M.m[2][0] = R0.v[2];
|
|
M.m[3][0] = D0;
|
|
|
|
M.m[0][1] = R1.v[0];
|
|
M.m[1][1] = R1.v[1];
|
|
M.m[2][1] = R1.v[2];
|
|
M.m[3][1] = D1;
|
|
|
|
M.m[0][2] = R2.v[0];
|
|
M.m[1][2] = R2.v[1];
|
|
M.m[2][2] = R2.v[2];
|
|
M.m[3][2] = D2;
|
|
|
|
M.m[0][3] = 0;
|
|
M.m[1][3] = 0;
|
|
M.m[2][3] = 0;
|
|
M.m[3][3] = 1;
|
|
|
|
return M;
|
|
}
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(Vector<float, 3> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
return Rotation(vector.x, vector.y, vector.z);
|
|
}
|
|
|
|
Vector<float, 3> TransformNormal(Vector<float, 3> V) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
Vector<float, 3> Z = Vector<float, 3>(V.z, V.z, V.z);
|
|
Vector<float, 3> Y = Vector<float, 3>(V.y, V.y, V.y);
|
|
Vector<float, 3> X = Vector<float, 3>(V.x, V.x, V.x);
|
|
|
|
Vector<float, 3> Result = Z * Vector<float, 3>(m[2][0], m[2][1], m[2][2]);
|
|
Result = Y * Vector<float, 3>(m[1][0], m[1][1], m[1][2]) + Result;
|
|
Result = X * Vector<float, 3>(m[0][0], m[0][1], m[0][2]) + Result;
|
|
|
|
return Result;
|
|
}
|
|
};
|
|
}
|
|
|
|
template <>
|
|
struct std::formatter<Crafter::MatrixRowMajor<float, 4, 4, 1>> : std::formatter<std::string> {
|
|
auto format(const Crafter::MatrixRowMajor<float, 4, 4, 1>& obj, format_context& ctx) const {
|
|
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}}}",
|
|
obj.m[0][0], obj.m[0][1], obj.m[0][2], obj.m[0][3],
|
|
obj.m[1][0], obj.m[1][1], obj.m[1][2], obj.m[1][3],
|
|
obj.m[2][0], obj.m[2][1], obj.m[2][2], obj.m[2][3],
|
|
obj.m[3][0], obj.m[3][1], obj.m[3][2], obj.m[3][3]
|
|
), ctx);
|
|
}
|
|
}; |