2025-05-07 19:18:58 +02:00
|
|
|
/*
|
2026-01-29 20:19:20 +01:00
|
|
|
Crafter®.Math
|
|
|
|
|
Copyright (C) 2026 Catcrafts®
|
|
|
|
|
catcrafts.net
|
2025-05-07 19:18:58 +02:00
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2026-01-29 20:19:20 +01:00
|
|
|
License version 3.0 as published by the Free Software Foundation;
|
2025-05-07 19:18:58 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-05 05:14:39 +02:00
|
|
|
export module Crafter.Math:MatrixRowMajor;
|
|
|
|
|
|
|
|
|
|
import :BasicTypes;
|
|
|
|
|
import :Vector;
|
|
|
|
|
import :Misc;
|
2026-01-29 20:19:20 +01:00
|
|
|
import std;
|
2025-05-05 05:14:39 +02:00
|
|
|
|
|
|
|
|
namespace Crafter {
|
2026-01-29 20:19:20 +01:00
|
|
|
export template <typename T, std::uint32_t CollumSize, std::uint32_t RowSize, std::uint32_t Repeats>
|
2025-05-05 05:14:39 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 20:19:20 +01:00
|
|
|
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
|
|
|
|
|
) requires(CollumSize == 4 && RowSize == 3 && 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
Vector<T, 3, VAligment> operator*(Vector<T, 3, VAligment> b) const requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
return Vector<T, 3, VAligment>(
|
2025-05-05 05:14:39 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 02:45:27 +01:00
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> operator*(MatrixRowMajor<T, CollumSize, RowSize, Repeats> b) const requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> result;
|
|
|
|
|
|
2026-02-05 02:55:08 +01:00
|
|
|
// Column 0
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
// Column 1
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
// Column 2
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
// Translation column
|
|
|
|
|
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];
|
|
|
|
|
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];
|
|
|
|
|
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];
|
2026-02-05 02:45:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 05:14:39 +02:00
|
|
|
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>) {
|
2026-02-05 02:45:27 +01:00
|
|
|
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
2025-05-05 05:14:39 +02:00
|
|
|
1, 0, 0, 0,
|
|
|
|
|
0, 1, 0, 0,
|
|
|
|
|
0, 0, 1, 0,
|
|
|
|
|
0, 0, 0, 1
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-29 20:19:20 +01:00
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Identity() requires(CollumSize == 4 && RowSize == 3 && 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
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 05:14:39 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-29 20:19:20 +01:00
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(float x, float y, float z) requires(CollumSize == 4 && RowSize == 3 && 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
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Scaling(Vector<float, 3, VAligment> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
2025-05-07 19:18:58 +02:00
|
|
|
return Scaling(vector.x, vector.y, vector.z);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 05:14:39 +02:00
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-29 20:19:20 +01:00
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(float x, float y, float z) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
return MatrixRowMajor<T, CollumSize, RowSize, Repeats>(
|
|
|
|
|
1, 0, 0, x,
|
|
|
|
|
0, 1, 0, y,
|
|
|
|
|
0, 0, 1, z
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-02-05 01:18:05 +01:00
|
|
|
|
|
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Translation(Vector<T, 3, VAligment> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
2025-05-07 19:18:58 +02:00
|
|
|
return Translation(vector.x, vector.y, vector.z);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 05:14:39 +02:00
|
|
|
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>) {
|
2026-01-29 20:19:20 +01:00
|
|
|
float cp = std::cosf(Pitch);
|
|
|
|
|
float sp = std::sinf(Pitch);
|
2025-05-05 05:14:39 +02:00
|
|
|
|
2026-01-29 20:19:20 +01:00
|
|
|
float cy = std::cosf(Yaw);
|
|
|
|
|
float sy = std::sinf(Yaw);
|
2025-05-05 05:14:39 +02:00
|
|
|
|
2026-01-29 20:19:20 +01:00
|
|
|
float cr = std::cosf(Roll);
|
|
|
|
|
float sr = std::sinf(Roll);
|
2025-05-05 05:14:39 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2025-05-07 19:18:58 +02:00
|
|
|
|
2026-01-29 20:19:20 +01:00
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(float Pitch, float Yaw, float Roll) requires(CollumSize == 4 && RowSize == 3 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
float cp = std::cosf(Pitch);
|
|
|
|
|
float sp = std::sinf(Pitch);
|
|
|
|
|
|
|
|
|
|
float cy = std::cosf(Yaw);
|
|
|
|
|
float sy = std::sinf(Yaw);
|
|
|
|
|
|
|
|
|
|
float cr = std::cosf(Roll);
|
|
|
|
|
float sr = std::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;
|
|
|
|
|
|
|
|
|
|
return M;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> LookAt(Vector<T, 3, VAligment> eyePosition, Vector<T, 3, VAligment> focusPosition, Vector<T, 3, VAligment> upDirection) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
2025-05-07 19:18:58 +02:00
|
|
|
MatrixRowMajor<T, CollumSize, RowSize, Repeats> M;
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
Vector<T, 3, VAligment> negEyeDirection = eyePosition - focusPosition;
|
2025-05-07 19:18:58 +02:00
|
|
|
return LookTo(eyePosition, negEyeDirection, upDirection);
|
|
|
|
|
|
|
|
|
|
return M;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> LookTo(Vector<T, 3, VAligment> eyePosition, Vector<T, 3, VAligment> eyeDirection, Vector<T, 3, VAligment> upDirection) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
Vector<T, 3, 3> R2 = eyeDirection.Normalize();
|
2025-05-07 19:18:58 +02:00
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
Vector<T, 3, 3> R0 = upDirection.Cross(R2);
|
2025-05-07 19:18:58 +02:00
|
|
|
R0 = R0.Normalize();
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
Vector<T, 3, 3> R1 = R2.Cross(R0);
|
2025-05-07 19:18:58 +02:00
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
Vector<T, 3, 3> NegEyePosition = -eyePosition;
|
2025-05-07 19:18:58 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
static MatrixRowMajor<T, CollumSize, RowSize, Repeats> Rotation(Vector<T, 3, VAligment> vector) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
2025-05-07 19:18:58 +02:00
|
|
|
return Rotation(vector.x, vector.y, vector.z);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
template <std::uint32_t VAligment>
|
|
|
|
|
Vector<T, 3, VAligment> TransformNormal(Vector<T, 3, VAligment> V) requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
Vector<T, 3, 3> Z = Vector<T, 3, 3>(V.z, V.z, V.z);
|
|
|
|
|
Vector<T, 3, 3> Y = Vector<T, 3, 3>(V.y, V.y, V.y);
|
|
|
|
|
Vector<T, 3, 3> X = Vector<T, 3, 3>(V.x, V.x, V.x);
|
2025-05-07 19:18:58 +02:00
|
|
|
|
2026-02-05 01:18:05 +01:00
|
|
|
Vector<T, 3, VAligment> Result = Z * Vector<T, 3, VAligment>(m[2][0], m[2][1], m[2][2]);
|
|
|
|
|
Result = Y * Vector<T, 3, VAligment>(m[1][0], m[1][1], m[1][2]) + Result;
|
|
|
|
|
Result = X * Vector<T, 3, VAligment>(m[0][0], m[0][1], m[0][2]) + Result;
|
2025-05-07 19:18:58 +02:00
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
|
}
|
2026-01-29 20:19:20 +01:00
|
|
|
|
|
|
|
|
// MatrixRowMajor<T, CollumSize, RowSize, Repeats> Inverse() requires(CollumSize == 4 && RowSize == 4 && Repeats == 1 && std::same_as<T, float>) {
|
|
|
|
|
// Vector<float, 4> V0[4], V1[4];
|
|
|
|
|
// V0[0] = Vector<float, 4>(m[0][2], m[0][2], m[1][2], m[1][2]);
|
|
|
|
|
// V1[0] = Vector<float, 4>(m[2][3], m[3][3], m[2][3], m[3][3]);
|
|
|
|
|
// V0[1] = Vector<float, 4>(m[0][0], m[0][0], m[1][0], m[1][0]);
|
|
|
|
|
// V1[1] = Vector<float, 4>(m[2][1], m[3][1], m[2][1], m[3][1]);
|
|
|
|
|
// V0[2] = Vector<float, 4>(m[0][2], m[2][2], m[0][0], m[2][0]);
|
|
|
|
|
// V1[2] = Vector<float, 4>(m[1][3], m[3][3], m[1][1], m[3][1]);
|
|
|
|
|
|
|
|
|
|
// Vector<float, 4> D0 = V0[0] * V1[0];
|
|
|
|
|
// Vector<float, 4> D1 = V0[1] * V1[1];
|
|
|
|
|
// Vector<float, 4> D2 = V0[2] * V1[2];
|
|
|
|
|
|
|
|
|
|
// V0[0] = Vector<float, 4>(m[2][2], m[3][2], m[2][2], m[3][2]);
|
|
|
|
|
// V1[0] = Vector<float, 4>(m[0][3], m[0][3], m[1][3], m[1][3]);
|
|
|
|
|
// V0[1] = Vector<float, 4>(m[2][0], m[3][0], m[2][0], m[3][0]);
|
|
|
|
|
// V1[1] = Vector<float, 4>(m[0][1], m[0][1], m[1][1], m[1][1]);
|
|
|
|
|
// V0[2] = Vector<float, 4>(m[1][2], m[3][2], m[1][0], m[3][0]);
|
|
|
|
|
// V1[2] = Vector<float, 4>(m[0][3], m[2][3], m[0][1], m[2][1]);
|
|
|
|
|
|
|
|
|
|
// D0 = Vector<float, 4>::NegativeMultiplySubtract(V0[0], V1[0], D0);
|
|
|
|
|
// D1 = Vector<float, 4>::NegativeMultiplySubtract(V0[1], V1[1], D1);
|
|
|
|
|
// D2 = Vector<float, 4>::NegativeMultiplySubtract(V0[2], V1[2], D2);
|
|
|
|
|
|
|
|
|
|
// V0[0] = Vector<float, 4>(m[1][1], m[2][1], m[0][1], m[1][1]);
|
|
|
|
|
// V1[0] = Vector<float, 4>(D2.v[1], D0.v[1], D0.v[3], D0.v[0]);
|
|
|
|
|
// V0[1] = Vector<float, 4>(m[2][0], m[0][0], m[1][0], m[0][0]);
|
|
|
|
|
// V1[1] = Vector<float, 4>(D0.v[3], D2.v[1], D0.v[1], D0.v[2]);
|
|
|
|
|
// V0[2] = Vector<float, 4>(m[1][3], m[2][3], m[0][3], m[1][3]);
|
|
|
|
|
// V1[2] = Vector<float, 4>(D2.v[3], D1.v[1], D1.v[3], D1.v[0]);
|
|
|
|
|
// V0[3] = Vector<float, 4>(m[2][2], m[0][2], m[1][2], m[0][2]);
|
|
|
|
|
// V1[3] = Vector<float, 4>(D1.v[3], D2.v[3], D1.v[1], D1.v[2]);
|
|
|
|
|
|
|
|
|
|
// Vector<float, 4> C0 = V0[0] * V1[0];
|
|
|
|
|
// Vector<float, 4> C2 = V0[1] * V1[1];
|
|
|
|
|
// Vector<float, 4> C4 = V0[2] * V1[2];
|
|
|
|
|
// Vector<float, 4> C6 = V0[3] * V1[3];
|
|
|
|
|
|
|
|
|
|
// V0[0] = Vector<float, 4>(m[2][1], m[3][1], m[1][1], m[2][1]);
|
|
|
|
|
// V1[0] = Vector<float, 4>(D0.v[3], D0.v[0], D0.v[1], D2.v[0]);
|
|
|
|
|
// V0[1] = Vector<float, 4>(m[3][0], m[2][0], m[3][0], m[1][0]);
|
|
|
|
|
// V1[1] = Vector<float, 4>(D0.v[2], D0.v[1], D2.v[0], D0.v[0]);
|
|
|
|
|
// V0[2] = Vector<float, 4>(m[2][3], m[3][3], m[1][3], m[2][3]);
|
|
|
|
|
// V1[2] = Vector<float, 4>(D1.v[3], D1.v[0], D1.v[1], D2.v[2]);
|
|
|
|
|
// V0[3] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_Z, XM_SWIZZLE_W, XM_SWIZZLE_Y>(MT.r[2]);
|
|
|
|
|
// V1[3] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_0Y, XM_PERMUTE_1Z, XM_PERMUTE_0X>(D1, D2);
|
|
|
|
|
|
|
|
|
|
// C0 = XMVectorNegativeMultiplySubtract(V0[0], V1[0], C0);
|
|
|
|
|
// C2 = XMVectorNegativeMultiplySubtract(V0[1], V1[1], C2);
|
|
|
|
|
// C4 = XMVectorNegativeMultiplySubtract(V0[2], V1[2], C4);
|
|
|
|
|
// C6 = XMVectorNegativeMultiplySubtract(V0[3], V1[3], C6);
|
|
|
|
|
|
|
|
|
|
// V0[0] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_X>(MT.r[1]);
|
|
|
|
|
// V1[0] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_1Y, XM_PERMUTE_1X, XM_PERMUTE_0Z>(D0, D2);
|
|
|
|
|
// V0[1] = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Z>(MT.r[0]);
|
|
|
|
|
// V1[1] = XMVectorPermute<XM_PERMUTE_1Y, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_1X>(D0, D2);
|
|
|
|
|
// V0[2] = XMVectorSwizzle<XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_W, XM_SWIZZLE_X>(MT.r[3]);
|
|
|
|
|
// V1[2] = XMVectorPermute<XM_PERMUTE_0Z, XM_PERMUTE_1W, XM_PERMUTE_1Z, XM_PERMUTE_0Z>(D1, D2);
|
|
|
|
|
// V0[3] = XMVectorSwizzle<XM_SWIZZLE_Y, XM_SWIZZLE_W, XM_SWIZZLE_X, XM_SWIZZLE_Z>(MT.r[2]);
|
|
|
|
|
// V1[3] = XMVectorPermute<XM_PERMUTE_1W, XM_PERMUTE_0X, XM_PERMUTE_0W, XM_PERMUTE_1Z>(D1, D2);
|
|
|
|
|
|
|
|
|
|
// XMVECTOR C1 = XMVectorNegativeMultiplySubtract(V0[0], V1[0], C0);
|
|
|
|
|
// C0 = XMVectorMultiplyAdd(V0[0], V1[0], C0);
|
|
|
|
|
// XMVECTOR C3 = XMVectorMultiplyAdd(V0[1], V1[1], C2);
|
|
|
|
|
// C2 = XMVectorNegativeMultiplySubtract(V0[1], V1[1], C2);
|
|
|
|
|
// XMVECTOR C5 = XMVectorNegativeMultiplySubtract(V0[2], V1[2], C4);
|
|
|
|
|
// C4 = XMVectorMultiplyAdd(V0[2], V1[2], C4);
|
|
|
|
|
// XMVECTOR C7 = XMVectorMultiplyAdd(V0[3], V1[3], C6);
|
|
|
|
|
// C6 = XMVectorNegativeMultiplySubtract(V0[3], V1[3], C6);
|
|
|
|
|
|
|
|
|
|
// XMMATRIX R;
|
|
|
|
|
// R.r[0] = XMVectorSelect(C0, C1, g_XMSelect0101.v);
|
|
|
|
|
// R.r[1] = XMVectorSelect(C2, C3, g_XMSelect0101.v);
|
|
|
|
|
// R.r[2] = XMVectorSelect(C4, C5, g_XMSelect0101.v);
|
|
|
|
|
// R.r[3] = XMVectorSelect(C6, C7, g_XMSelect0101.v);
|
|
|
|
|
|
|
|
|
|
// XMVECTOR Determinant = XMVector4Dot(R.r[0], MT.r[0]);
|
|
|
|
|
// XMVECTOR Reciprocal = XMVectorReciprocal(Determinant);
|
|
|
|
|
|
|
|
|
|
// XMMATRIX Result;
|
|
|
|
|
// Result.r[0] = XMVectorMultiply(R.r[0], Reciprocal);
|
|
|
|
|
// Result.r[1] = XMVectorMultiply(R.r[1], Reciprocal);
|
|
|
|
|
// Result.r[2] = XMVectorMultiply(R.r[2], Reciprocal);
|
|
|
|
|
// Result.r[3] = XMVectorMultiply(R.r[3], Reciprocal);
|
|
|
|
|
// return Result;
|
|
|
|
|
// }
|
2025-05-05 05:14:39 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2026-01-29 20:19:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct std::formatter<Crafter::MatrixRowMajor<float, 4, 3, 1>> : std::formatter<std::string> {
|
|
|
|
|
auto format(const Crafter::MatrixRowMajor<float, 4, 3, 1>& obj, format_context& ctx) const {
|
|
|
|
|
return std::formatter<std::string>::format(std::format("{{{}, {}, {}, {}\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]
|
|
|
|
|
), ctx);
|
|
|
|
|
}
|
2025-05-05 05:14:39 +02:00
|
|
|
};
|