Added Eigen, replacing ZMath
This commit is contained in:
parent
9bfe2bef89
commit
144978bfa2
@ -462,8 +462,6 @@ add_executable(space-game001
|
||||
src/AudioPlayerAsync.h
|
||||
src/BoneAnimatedModel.cpp
|
||||
src/BoneAnimatedModel.h
|
||||
src/utils/ZLMath.cpp
|
||||
src/utils/ZLMath.h
|
||||
src/render/OpenGlExtensions.cpp
|
||||
src/render/OpenGlExtensions.h
|
||||
src/utils/Utils.cpp
|
||||
|
||||
@ -638,23 +638,30 @@ namespace ZL
|
||||
nextFrameBonesMatrix.data()[7] = nextFrameBones[i].boneMatrixWorld.m[1 + 2 * 4];
|
||||
nextFrameBonesMatrix.data()[8] = nextFrameBones[i].boneMatrixWorld.m[2 + 2 * 4];
|
||||
*/
|
||||
Eigen::Quaternionf q1 = MatrixToQuat(oneFrameBonesMatrix);
|
||||
Eigen::Quaternionf q2 = MatrixToQuat(nextFrameBonesMatrix);
|
||||
Eigen::Quaternionf q1 = Eigen::Quaternionf(oneFrameBonesMatrix).normalized();
|
||||
Eigen::Quaternionf q2 = Eigen::Quaternionf(nextFrameBonesMatrix).normalized();
|
||||
Eigen::Quaternionf q1_norm = q1.normalized();
|
||||
Eigen::Quaternionf q2_norm = q2.normalized();
|
||||
|
||||
Eigen::Quaternionf result = q1_norm.slerp(t, q2_norm);
|
||||
|
||||
Matrix3f boneMatrixWorld3 = QuatToMatrix(result);
|
||||
Matrix3f boneMatrixWorld3 = result.toRotationMatrix();
|
||||
|
||||
currentBones[i].boneMatrixWorld = Eigen::Matrix4f::Identity();
|
||||
|
||||
// Копируем 3x3 матрицу в верхний левый угол
|
||||
currentBones[i].boneMatrixWorld.block<3, 3>(0, 0) = boneMatrixWorld3;
|
||||
|
||||
// Копируем позицию в последний столбец (первые 3 элемента)
|
||||
currentBones[i].boneMatrixWorld.block<3, 1>(0, 3) = currentBones[i].boneStartWorld;
|
||||
|
||||
currentBones[i].boneMatrixWorld = MakeMatrix4x4(boneMatrixWorld3, currentBones[i].boneStartWorld);
|
||||
|
||||
Matrix4f currentBoneMatrixWorld4 = currentBones[i].boneMatrixWorld;
|
||||
Matrix4f startBoneMatrixWorld4 = animations[0].keyFrames[0].bones[i].boneMatrixWorld;
|
||||
|
||||
Matrix4f inverstedStartBoneMatrixWorld4 = InverseMatrix(startBoneMatrixWorld4);
|
||||
Matrix4f inverstedStartBoneMatrixWorld4 = startBoneMatrixWorld4.inverse();
|
||||
|
||||
skinningMatrixForEachBone[i] = MultMatrixMatrix(currentBoneMatrixWorld4, inverstedStartBoneMatrixWorld4);
|
||||
skinningMatrixForEachBone[i] = currentBoneMatrixWorld4 * inverstedStartBoneMatrixWorld4;
|
||||
|
||||
}
|
||||
|
||||
@ -675,7 +682,7 @@ namespace ZL
|
||||
{
|
||||
vMoved = true;
|
||||
//finalPos = finalPos + MultVectorMatrix(originalPos, skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex]) * verticesBoneWeight[i][j].weight;
|
||||
finalPos = finalPos + MultMatrixVector(skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex], originalPos) * verticesBoneWeight[i][j].weight;
|
||||
finalPos = finalPos + (skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex] * originalPos) * verticesBoneWeight[i][j].weight;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
@ -23,15 +23,15 @@ bool Environment::showMouse = false;
|
||||
|
||||
bool Environment::exitGameLoop = false;
|
||||
|
||||
Matrix3f Environment::shipMatrix = Matrix3f::Identity();
|
||||
Matrix3f Environment::inverseShipMatrix = Matrix3f::Identity();
|
||||
Eigen::Matrix3f Environment::shipMatrix = Eigen::Matrix3f::Identity();
|
||||
Eigen::Matrix3f Environment::inverseShipMatrix = Eigen::Matrix3f::Identity();
|
||||
|
||||
|
||||
bool Environment::tapDownHold = false;
|
||||
Vector2f Environment::tapDownStartPos = { 0, 0 };
|
||||
Vector2f Environment::tapDownCurrentPos = { 0, 0 };
|
||||
Eigen::Vector2f Environment::tapDownStartPos = { 0, 0 };
|
||||
Eigen::Vector2f Environment::tapDownCurrentPos = { 0, 0 };
|
||||
|
||||
Vector3f Environment::shipPosition = {0,0,45000.f};
|
||||
Eigen::Vector3f Environment::shipPosition = {0,0,45000.f};
|
||||
|
||||
float Environment::shipVelocity = 0.f;
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
#include "utils/ZLMath.h"
|
||||
#ifdef __linux__
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
#include "render/OpenGlExtensions.h"
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
@ -21,8 +21,8 @@ public:
|
||||
|
||||
static bool settings_inverseVertical;
|
||||
|
||||
static Matrix3f shipMatrix;
|
||||
static Matrix3f inverseShipMatrix;
|
||||
static Eigen::Matrix3f shipMatrix;
|
||||
static Eigen::Matrix3f inverseShipMatrix;
|
||||
|
||||
static SDL_Window* window;
|
||||
|
||||
@ -31,10 +31,10 @@ public:
|
||||
|
||||
|
||||
static bool tapDownHold;
|
||||
static Vector2f tapDownStartPos;
|
||||
static Vector2f tapDownCurrentPos;
|
||||
static Eigen::Vector2f tapDownStartPos;
|
||||
static Eigen::Vector2f tapDownCurrentPos;
|
||||
|
||||
static Vector3f shipPosition;
|
||||
static Eigen::Vector3f shipPosition;
|
||||
static float shipVelocity;
|
||||
|
||||
static const float CONST_Z_NEAR;
|
||||
|
||||
18
src/Game.cpp
18
src/Game.cpp
@ -83,7 +83,7 @@ namespace ZL
|
||||
{
|
||||
Eigen::Quaternionf randomQuat = generateRandomQuaternion(gen);
|
||||
|
||||
Matrix3f randomMatrix = QuatToMatrix(randomQuat);
|
||||
Matrix3f randomMatrix = randomQuat.toRotationMatrix();
|
||||
|
||||
boxCoordsArr.emplace_back(BoxCoords{ newPos, randomMatrix });
|
||||
generatedCount++;
|
||||
@ -205,7 +205,7 @@ namespace ZL
|
||||
//Load texture
|
||||
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/DefaultMaterial_BaseColor_shine.png", CONST_ZIP_FILE));
|
||||
spaceshipBase = LoadFromTextFile02("./resources/spaceship006.txt", CONST_ZIP_FILE);
|
||||
spaceshipBase.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundY(M_PI / 2.0)));
|
||||
spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI / 2.0, Eigen::Vector3f::UnitY())).toRotationMatrix());// QuatFromRotateAroundY(M_PI / 2.0).toRotationMatrix());
|
||||
//spaceshipBase.Move(Vector3f{ -0.52998, -13, 0 });
|
||||
//spaceshipBase.Move(Vector3f{ -0.52998, -10, 10 });
|
||||
|
||||
@ -571,10 +571,10 @@ namespace ZL
|
||||
// Конструктор принимает (угол_в_радианах, ось_вращения)
|
||||
Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, rotationDirection));
|
||||
|
||||
Matrix3f rotateMat = QuatToMatrix(rotateQuat);
|
||||
Matrix3f rotateMat = rotateQuat.toRotationMatrix();
|
||||
|
||||
Environment::shipMatrix = MultMatrixMatrix(Environment::shipMatrix, rotateMat);
|
||||
Environment::inverseShipMatrix = InverseMatrix(Environment::shipMatrix);
|
||||
Environment::shipMatrix = Environment::shipMatrix * rotateMat;
|
||||
Environment::inverseShipMatrix = Environment::shipMatrix.inverse();
|
||||
|
||||
}
|
||||
}
|
||||
@ -582,7 +582,7 @@ namespace ZL
|
||||
if (fabs(Environment::shipVelocity) > 0.01f)
|
||||
{
|
||||
Vector3f velocityDirection = { 0,0, -Environment::shipVelocity * delta / 1000.f };
|
||||
Vector3f velocityDirectionAdjusted = MultMatrixVector(Environment::shipMatrix, velocityDirection);
|
||||
Vector3f velocityDirectionAdjusted = Environment::shipMatrix * velocityDirection;
|
||||
Environment::shipPosition = Environment::shipPosition + velocityDirectionAdjusted;
|
||||
}
|
||||
|
||||
@ -597,7 +597,7 @@ namespace ZL
|
||||
if (p && p->isActive()) {
|
||||
Vector3f worldPos = p->getPosition();
|
||||
Vector3f rel = worldPos - Environment::shipPosition;
|
||||
Vector3f camPos = MultMatrixVector(Environment::inverseShipMatrix, rel);
|
||||
Vector3f camPos = Environment::inverseShipMatrix * rel;
|
||||
projCameraPoints.push_back(camPos);
|
||||
}
|
||||
}
|
||||
@ -636,10 +636,10 @@ namespace ZL
|
||||
const float size = 0.5f;
|
||||
|
||||
Vector3f localForward = { 0,0,-1 };
|
||||
Vector3f worldForward = MultMatrixVector(Environment::shipMatrix, localForward).normalized();
|
||||
Vector3f worldForward = (Environment::shipMatrix * localForward).normalized();
|
||||
|
||||
for (const auto& lo : localOffsets) {
|
||||
Vector3f worldPos = Environment::shipPosition + MultMatrixVector(Environment::shipMatrix, lo);
|
||||
Vector3f worldPos = Environment::shipPosition + Environment::shipMatrix * lo;
|
||||
Vector3f worldVel = worldForward * projectileSpeed;
|
||||
|
||||
for (auto& p : projectiles) {
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "render/TextureManager.h"
|
||||
#include <memory>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "render/TextureManager.h"
|
||||
#include <vector>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/ZLMath.h"
|
||||
#include "utils/Perlin.h"
|
||||
#include "render/Renderer.h"
|
||||
#include <vector>
|
||||
|
||||
@ -124,9 +124,9 @@ namespace ZL {
|
||||
// 2. Трансформируем вершины в локальное пространство экрана, чтобы найти габариты
|
||||
// Используем MultMatrixVector(Matrix, Vector).
|
||||
// Если ваша функция считает V * M, то передайте Inverse(mr).
|
||||
Vector3f rA = MultMatrixVector(mr, tr.data[0]);
|
||||
Vector3f rB = MultMatrixVector(mr, tr.data[1]);
|
||||
Vector3f rC = MultMatrixVector(mr, tr.data[2]);
|
||||
Vector3f rA = mr * tr.data[0];
|
||||
Vector3f rB = mr * tr.data[1];
|
||||
Vector3f rC = mr * tr.data[2];
|
||||
|
||||
// 3. Вычисляем реальные границы треугольника после поворота
|
||||
float minX = min(rA(0), min(rB(0), rC(0)));
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "render/TextureManager.h"
|
||||
#include <vector>
|
||||
|
||||
@ -111,9 +111,9 @@ namespace ZL {
|
||||
|
||||
/*
|
||||
// Случайный поворот (например, вокруг трех осей)
|
||||
Vector4f qx = QuatFromRotateAroundX(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qy = QuatFromRotateAroundY(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qz = QuatFromRotateAroundZ(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qx = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitX()));//QuatFromRotateAroundX(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qy = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitY()));//QuatFromRotateAroundY(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qz = Eigen::Quaternionf(Eigen::AngleAxisf(getRandomFloat(engine, 0.0f, 360.0f), Eigen::Vector3f::UnitZ()));//QuatFromRotateAroundZ(getRandomFloat(engine, 0.0f, 360.0f));
|
||||
Vector4f qFinal = slerp(qx, qy, 0.5f); // Простой пример комбинирования
|
||||
qFinal = slerp(qFinal, qz, 0.5f).normalized();
|
||||
Matrix3f rotationMatrix = QuatToMatrix(qFinal);
|
||||
@ -320,7 +320,7 @@ namespace ZL {
|
||||
|
||||
|
||||
for (const auto& inst : allInstances[index]) {
|
||||
Matrix3f rotMat = QuatToMatrix(inst.rotation);
|
||||
Matrix3f rotMat = inst.rotation.toRotationMatrix();
|
||||
|
||||
for (size_t j = 0; j < baseStone.PositionData.size(); ++j) {
|
||||
Vector3f p = baseStone.PositionData[j];
|
||||
@ -330,8 +330,8 @@ namespace ZL {
|
||||
p(1) *= inst.scale(1) * scaleModifier;
|
||||
p(2) *= inst.scale(2) * scaleModifier;
|
||||
|
||||
result.data.PositionData.push_back(MultMatrixVector(rotMat, p) + inst.position);
|
||||
result.data.NormalData.push_back(MultMatrixVector(rotMat, n));
|
||||
result.data.PositionData.push_back(rotMat * p + inst.position);
|
||||
result.data.NormalData.push_back(rotMat * n);
|
||||
result.data.TexCoordData.push_back(baseStone.TexCoordData[j]);
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#pragma once
|
||||
#include "utils/ZLMath.h"
|
||||
#include "render/Renderer.h"
|
||||
#include "PlanetData.h"
|
||||
|
||||
|
||||
@ -3,6 +3,105 @@
|
||||
|
||||
namespace ZL {
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar)
|
||||
{
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("zFar must be greater than zNear");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
r.data()[0] = 2.f / width;
|
||||
r.data()[1] = 0;
|
||||
r.data()[2] = 0;
|
||||
r.data()[3] = 0;
|
||||
|
||||
r.data()[4] = 0;
|
||||
r.data()[5] = 2.f / height;
|
||||
r.data()[6] = 0;
|
||||
r.data()[7] = 0;
|
||||
|
||||
r.data()[8] = 0;
|
||||
r.data()[9] = 0;
|
||||
r.data()[10] = -1 / depthRange;
|
||||
r.data()[11] = 0;
|
||||
|
||||
r.data()[12] = -1;
|
||||
r.data()[13] = -1;
|
||||
r.data()[14] = zNear / depthRange;
|
||||
r.data()[15] = 1;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar)
|
||||
{
|
||||
float width = xmax - xmin;
|
||||
float height = ymax - ymin;
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (width <= 0 || height <= 0 || depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid dimensions for orthogonal matrix");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
// Ìàñøòàáèðîâàíèå
|
||||
r.data()[0] = 2.f / width;
|
||||
r.data()[5] = 2.f / height;
|
||||
r.data()[10] = -1.f / depthRange;
|
||||
|
||||
// Îáíóëåíèå íåèñïîëüçóåìûõ êîìïîíåíòîâ
|
||||
r.data()[1] = r.data()[2] = r.data()[3] = 0;
|
||||
r.data()[4] = r.data()[6] = r.data()[7] = 0;
|
||||
r.data()[8] = r.data()[9] = r.data()[11] = 0;
|
||||
|
||||
// Òðàíñëÿöèÿ (ñìåùåíèå)
|
||||
r.data()[12] = -(xmax + xmin) / width;
|
||||
r.data()[13] = -(ymax + ymin) / height;
|
||||
r.data()[14] = zNear / depthRange;
|
||||
r.data()[15] = 1.f;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
float tanHalfFovy = tan(fovY / 2.f);
|
||||
Matrix4f r;
|
||||
|
||||
if (zNear >= zFar || aspectRatio == 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid perspective parameters");
|
||||
}
|
||||
|
||||
r.data()[0] = 1.f / (aspectRatio * tanHalfFovy);
|
||||
r.data()[1] = 0;
|
||||
r.data()[2] = 0;
|
||||
r.data()[3] = 0;
|
||||
|
||||
r.data()[4] = 0;
|
||||
r.data()[5] = 1.f / (tanHalfFovy);
|
||||
r.data()[6] = 0;
|
||||
r.data()[7] = 0;
|
||||
|
||||
r.data()[8] = 0;
|
||||
r.data()[9] = 0;
|
||||
r.data()[10] = -(zFar + zNear) / (zFar - zNear);
|
||||
r.data()[11] = -1;
|
||||
|
||||
r.data()[12] = 0;
|
||||
r.data()[13] = 0;
|
||||
r.data()[14] = -(2.f * zFar * zNear) / (zFar - zNear);
|
||||
r.data()[15] = 0;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
VBOHolder::VBOHolder()
|
||||
{
|
||||
glGenBuffers(1, &Buffer);
|
||||
@ -374,22 +473,22 @@ namespace ZL {
|
||||
|
||||
for (int i = 0; i < PositionData.size(); i++)
|
||||
{
|
||||
PositionData[i] = MultVectorMatrix(PositionData[i], m);
|
||||
PositionData[i] = PositionData[i].transpose() * m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NormalData.size(); i++)
|
||||
{
|
||||
NormalData[i] = MultVectorMatrix(NormalData[i], m);
|
||||
NormalData[i] = NormalData[i].transpose() * m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < TangentData.size(); i++)
|
||||
{
|
||||
TangentData[i] = MultVectorMatrix(TangentData[i], m);
|
||||
TangentData[i] = TangentData[i].transpose() * m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < BinormalData.size(); i++)
|
||||
{
|
||||
BinormalData[i] = MultVectorMatrix(BinormalData[i], m);
|
||||
BinormalData[i] = BinormalData[i].transpose() * m;
|
||||
}
|
||||
}
|
||||
|
||||
@ -593,7 +692,7 @@ namespace ZL {
|
||||
void Renderer::RotateMatrix(const Eigen::Quaternionf& q)
|
||||
{
|
||||
|
||||
Matrix3f m3 = QuatToMatrix(q);
|
||||
Matrix3f m3 = q.toRotationMatrix();
|
||||
Matrix4f m = Matrix4f::Identity();
|
||||
|
||||
m.block<3, 3>(0, 0) = m3;
|
||||
@ -823,7 +922,7 @@ namespace ZL {
|
||||
int& screenX, int& screenY) {
|
||||
|
||||
Vector4f inx = { objectPos(0), objectPos(1), objectPos(2), 1.0f };
|
||||
Vector4f clipCoords = MultMatrixVector(projectionModelView, inx);
|
||||
Vector4f clipCoords = projectionModelView * inx;
|
||||
|
||||
float ndcX = clipCoords(0) / clipCoords(3);
|
||||
float ndcY = clipCoords(1) / clipCoords(3);
|
||||
|
||||
@ -1,13 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "OpenGlExtensions.h"
|
||||
#include "utils/ZLMath.h"
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include "ShaderManager.h"
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using Eigen::Vector2f;
|
||||
using Eigen::Vector3f;
|
||||
using Eigen::Vector4f;
|
||||
|
||||
using Eigen::Matrix3f;
|
||||
using Eigen::Matrix4f;
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar);
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar);
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
|
||||
|
||||
constexpr size_t CONST_MATRIX_STACK_SIZE = 64;
|
||||
|
||||
class VBOHolder {
|
||||
|
||||
@ -57,7 +57,7 @@ namespace ZL {
|
||||
lerp(u, grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1))));
|
||||
}
|
||||
|
||||
float PerlinNoise::getSurfaceHeight(Vector3f pos, float noiseCoeff) {
|
||||
float PerlinNoise::getSurfaceHeight(Eigen::Vector3f pos, float noiseCoeff) {
|
||||
// ×àñòîòà øóìà (÷åì áîëüøå, òåì áîëüøå "õîëìîâ")
|
||||
float frequency = 7.0f;
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "utils/ZLMath.h"
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
@ -18,7 +18,7 @@ namespace ZL {
|
||||
|
||||
float noise(float x, float y, float z);
|
||||
|
||||
float getSurfaceHeight(Vector3f pos, float noiseCoeff);
|
||||
float getSurfaceHeight(Eigen::Vector3f pos, float noiseCoeff);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
@ -1,682 +0,0 @@
|
||||
#include "utils/ZLMath.h"
|
||||
|
||||
#include <exception>
|
||||
#include <cmath>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
/*
|
||||
Vector2f operator+(const Vector2f& x, const Vector2f& y)
|
||||
{
|
||||
Vector2f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2f operator-(const Vector2f& x, const Vector2f& y)
|
||||
{
|
||||
Vector2f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
Vector3f operator+(const Vector3f& x, const Vector3f& y)
|
||||
{
|
||||
Vector3f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
result.v[2] = x.v[2] + y.v[2];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator-(const Vector3f& x, const Vector3f& y)
|
||||
{
|
||||
Vector3f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
result.v[2] = x.v[2] - y.v[2];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator-(const Vector3f& x)
|
||||
{
|
||||
Vector3f result;
|
||||
|
||||
result.v[0] = -x.v[0];
|
||||
result.v[1] = -x.v[1];
|
||||
result.v[2] = -x.v[2];
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
Vector4f operator+(const Vector4f& x, const Vector4f& y)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
result.v[2] = x.v[2] + y.v[2];
|
||||
result.v[3] = x.v[3] + y.v[3];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector4f operator-(const Vector4f& x, const Vector4f& y)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
result.v[2] = x.v[2] - y.v[2];
|
||||
result.v[3] = x.v[3] - y.v[3];
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Matrix3f Matrix3f::Identity()
|
||||
{
|
||||
Matrix3f r;
|
||||
|
||||
r.m[0] = 1.f;
|
||||
r.m[1] = 0.f;
|
||||
r.m[2] = 0.f;
|
||||
|
||||
r.m[3] = 0.f;
|
||||
r.m[4] = 1.f;
|
||||
r.m[5] = 0.f;
|
||||
|
||||
r.m[6] = 0.f;
|
||||
r.m[7] = 0.f;
|
||||
r.m[8] = 1.f;
|
||||
|
||||
return r;
|
||||
|
||||
}*/
|
||||
/*
|
||||
Matrix4f Matrix4f::Identity()
|
||||
{
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = 1.f;
|
||||
r.m[1] = 0.f;
|
||||
r.m[2] = 0.f;
|
||||
r.m[3] = 0.f;
|
||||
|
||||
r.m[4] = 0.f;
|
||||
r.m[5] = 1.f;
|
||||
r.m[6] = 0.f;
|
||||
r.m[7] = 0.f;
|
||||
|
||||
r.m[8] = 0.f;
|
||||
r.m[9] = 0.f;
|
||||
r.m[10] = 1.f;
|
||||
r.m[11] = 0.f;
|
||||
|
||||
r.m[12] = 0.f;
|
||||
r.m[13] = 0.f;
|
||||
r.m[14] = 0.f;
|
||||
r.m[15] = 1.f;
|
||||
|
||||
return r;
|
||||
|
||||
}*/
|
||||
/*
|
||||
Matrix4f operator*(const Matrix4f& m1, const Matrix4f& m2)
|
||||
{
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = m1.m[0] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[8] * m2.m[2] + m1.m[12] * m2.m[3];
|
||||
r.m[1] = m1.m[1] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[9] * m2.m[2] + m1.m[13] * m2.m[3];
|
||||
r.m[2] = m1.m[2] * m2.m[0] + m1.m[6] * m2.m[1] + m1.m[10] * m2.m[2] + m1.m[14] * m2.m[3];
|
||||
r.m[3] = m1.m[3] * m2.m[0] + m1.m[7] * m2.m[1] + m1.m[11] * m2.m[2] + m1.m[15] * m2.m[3];
|
||||
|
||||
r.m[4] = m1.m[0] * m2.m[4] + m1.m[4] * m2.m[5] + m1.m[8] * m2.m[6] + m1.m[12] * m2.m[7];
|
||||
r.m[5] = m1.m[1] * m2.m[4] + m1.m[5] * m2.m[5] + m1.m[9] * m2.m[6] + m1.m[13] * m2.m[7];
|
||||
r.m[6] = m1.m[2] * m2.m[4] + m1.m[6] * m2.m[5] + m1.m[10] * m2.m[6] + m1.m[14] * m2.m[7];
|
||||
r.m[7] = m1.m[3] * m2.m[4] + m1.m[7] * m2.m[5] + m1.m[11] * m2.m[6] + m1.m[15] * m2.m[7];
|
||||
|
||||
|
||||
r.m[8] = m1.m[0] * m2.m[8] + m1.m[4] * m2.m[9] + m1.m[8] * m2.m[10] + m1.m[12] * m2.m[11];
|
||||
r.m[9] = m1.m[1] * m2.m[8] + m1.m[5] * m2.m[9] + m1.m[9] * m2.m[10] + m1.m[13] * m2.m[11];
|
||||
r.m[10] = m1.m[2] * m2.m[8] + m1.m[6] * m2.m[9] + m1.m[10] * m2.m[10] + m1.m[14] * m2.m[11];
|
||||
r.m[11] = m1.m[3] * m2.m[8] + m1.m[7] * m2.m[9] + m1.m[11] * m2.m[10] + m1.m[15] * m2.m[11];
|
||||
|
||||
r.m[12] = m1.m[0] * m2.m[12] + m1.m[4] * m2.m[13] + m1.m[8] * m2.m[14] + m1.m[12] * m2.m[15];
|
||||
r.m[13] = m1.m[1] * m2.m[12] + m1.m[5] * m2.m[13] + m1.m[9] * m2.m[14] + m1.m[13] * m2.m[15];
|
||||
r.m[14] = m1.m[2] * m2.m[12] + m1.m[6] * m2.m[13] + m1.m[10] * m2.m[14] + m1.m[14] * m2.m[15];
|
||||
r.m[15] = m1.m[3] * m2.m[12] + m1.m[7] * m2.m[13] + m1.m[11] * m2.m[14] + m1.m[15] * m2.m[15];
|
||||
|
||||
return r;
|
||||
}*/
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar)
|
||||
{
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("zFar must be greater than zNear");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
r.data()[0] = 2.f / width;
|
||||
r.data()[1] = 0;
|
||||
r.data()[2] = 0;
|
||||
r.data()[3] = 0;
|
||||
|
||||
r.data()[4] = 0;
|
||||
r.data()[5] = 2.f / height;
|
||||
r.data()[6] = 0;
|
||||
r.data()[7] = 0;
|
||||
|
||||
r.data()[8] = 0;
|
||||
r.data()[9] = 0;
|
||||
r.data()[10] = -1 / depthRange;
|
||||
r.data()[11] = 0;
|
||||
|
||||
r.data()[12] = -1;
|
||||
r.data()[13] = -1;
|
||||
r.data()[14] = zNear / depthRange;
|
||||
r.data()[15] = 1;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar)
|
||||
{
|
||||
float width = xmax - xmin;
|
||||
float height = ymax - ymin;
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (width <= 0 || height <= 0 || depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid dimensions for orthogonal matrix");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
// Масштабирование
|
||||
r.data()[0] = 2.f / width;
|
||||
r.data()[5] = 2.f / height;
|
||||
r.data()[10] = -1.f / depthRange;
|
||||
|
||||
// Обнуление неиспользуемых компонентов
|
||||
r.data()[1] = r.data()[2] = r.data()[3] = 0;
|
||||
r.data()[4] = r.data()[6] = r.data()[7] = 0;
|
||||
r.data()[8] = r.data()[9] = r.data()[11] = 0;
|
||||
|
||||
// Трансляция (смещение)
|
||||
r.data()[12] = -(xmax + xmin) / width;
|
||||
r.data()[13] = -(ymax + ymin) / height;
|
||||
r.data()[14] = zNear / depthRange;
|
||||
r.data()[15] = 1.f;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
float tanHalfFovy = tan(fovY / 2.f);
|
||||
Matrix4f r;
|
||||
|
||||
if (zNear >= zFar || aspectRatio == 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid perspective parameters");
|
||||
}
|
||||
|
||||
r.data()[0] = 1.f / (aspectRatio * tanHalfFovy);
|
||||
r.data()[1] = 0;
|
||||
r.data()[2] = 0;
|
||||
r.data()[3] = 0;
|
||||
|
||||
r.data()[4] = 0;
|
||||
r.data()[5] = 1.f / (tanHalfFovy);
|
||||
r.data()[6] = 0;
|
||||
r.data()[7] = 0;
|
||||
|
||||
r.data()[8] = 0;
|
||||
r.data()[9] = 0;
|
||||
r.data()[10] = -(zFar + zNear) / (zFar - zNear);
|
||||
r.data()[11] = -1;
|
||||
|
||||
r.data()[12] = 0;
|
||||
r.data()[13] = 0;
|
||||
r.data()[14] = -(2.f * zFar * zNear) / (zFar - zNear);
|
||||
r.data()[15] = 0;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f QuatToMatrix(const Eigen::Quaternionf& q)
|
||||
{
|
||||
return q.toRotationMatrix();
|
||||
|
||||
}
|
||||
|
||||
Eigen::Quaternionf MatrixToQuat(const Matrix3f& m)
|
||||
{
|
||||
return Eigen::Quaternionf(m).normalized();
|
||||
}
|
||||
|
||||
Eigen::Quaternionf QuatFromRotateAroundX(float angle) {
|
||||
return Eigen::Quaternionf(Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitX()));
|
||||
}
|
||||
|
||||
Eigen::Quaternionf QuatFromRotateAroundY(float angle) {
|
||||
return Eigen::Quaternionf(Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitY()));
|
||||
}
|
||||
|
||||
Eigen::Quaternionf QuatFromRotateAroundZ(float angle) {
|
||||
return Eigen::Quaternionf(Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitZ()));
|
||||
}
|
||||
|
||||
Matrix3f TransposeMatrix(const Matrix3f& m)
|
||||
{
|
||||
return m.transpose();
|
||||
}
|
||||
|
||||
Matrix3f InverseMatrix(const Matrix3f& m)
|
||||
{
|
||||
/*float d;
|
||||
Matrix3f r;
|
||||
|
||||
d = m.m[0] * (m.m[4] * m.m[8] - m.m[5] * m.m[7]);
|
||||
d -= m.m[1] * (m.m[3] * m.m[8] - m.m[6] * m.m[5]);
|
||||
d += m.m[2] * (m.m[3] * m.m[7] - m.m[6] * m.m[4]);
|
||||
|
||||
if (fabs(d) < 0.01f)
|
||||
{
|
||||
throw std::runtime_error("Error: matrix cannot be inversed!");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
r.m[0] = (m.m[4] * m.m[8] - m.m[5] * m.m[7]) / d;
|
||||
r.m[1] = -(m.m[1] * m.m[8] - m.m[2] * m.m[7]) / d;
|
||||
r.m[2] = (m.m[1] * m.m[5] - m.m[2] * m.m[4]) / d;
|
||||
|
||||
r.m[3] = -(m.m[3] * m.m[8] - m.m[5] * m.m[6]) / d;
|
||||
r.m[4] = (m.m[0] * m.m[8] - m.m[2] * m.m[6]) / d;
|
||||
r.m[5] = -(m.m[0] * m.m[5] - m.m[2] * m.m[3]) / d;
|
||||
|
||||
r.m[6] = (m.m[3] * m.m[7] - m.m[6] * m.m[4]) / d;
|
||||
r.m[7] = -(m.m[0] * m.m[7] - m.m[6] * m.m[1]) / d;
|
||||
r.m[8] = (m.m[0] * m.m[4] - m.m[1] * m.m[3]) / d;
|
||||
|
||||
};
|
||||
return r;*/
|
||||
return m.inverse();
|
||||
}
|
||||
|
||||
|
||||
Matrix4f InverseMatrix(const Matrix4f& mat)
|
||||
{
|
||||
/*
|
||||
Matrix4f inv;
|
||||
float det;
|
||||
|
||||
inv.m[0] = mat.m[5] * mat.m[10] * mat.m[15] -
|
||||
mat.m[5] * mat.m[11] * mat.m[14] -
|
||||
mat.m[9] * mat.m[6] * mat.m[15] +
|
||||
mat.m[9] * mat.m[7] * mat.m[14] +
|
||||
mat.m[13] * mat.m[6] * mat.m[11] -
|
||||
mat.m[13] * mat.m[7] * mat.m[10];
|
||||
|
||||
inv.m[4] = -mat.m[4] * mat.m[10] * mat.m[15] +
|
||||
mat.m[4] * mat.m[11] * mat.m[14] +
|
||||
mat.m[8] * mat.m[6] * mat.m[15] -
|
||||
mat.m[8] * mat.m[7] * mat.m[14] -
|
||||
mat.m[12] * mat.m[6] * mat.m[11] +
|
||||
mat.m[12] * mat.m[7] * mat.m[10];
|
||||
|
||||
inv.m[8] = mat.m[4] * mat.m[9] * mat.m[15] -
|
||||
mat.m[4] * mat.m[11] * mat.m[13] -
|
||||
mat.m[8] * mat.m[5] * mat.m[15] +
|
||||
mat.m[8] * mat.m[7] * mat.m[13] +
|
||||
mat.m[12] * mat.m[5] * mat.m[11] -
|
||||
mat.m[12] * mat.m[7] * mat.m[9];
|
||||
|
||||
inv.m[12] = -mat.m[4] * mat.m[9] * mat.m[14] +
|
||||
mat.m[4] * mat.m[10] * mat.m[13] +
|
||||
mat.m[8] * mat.m[5] * mat.m[14] -
|
||||
mat.m[8] * mat.m[6] * mat.m[13] -
|
||||
mat.m[12] * mat.m[5] * mat.m[10] +
|
||||
mat.m[12] * mat.m[6] * mat.m[9];
|
||||
|
||||
inv.m[1] = -mat.m[1] * mat.m[10] * mat.m[15] +
|
||||
mat.m[1] * mat.m[11] * mat.m[14] +
|
||||
mat.m[9] * mat.m[2] * mat.m[15] -
|
||||
mat.m[9] * mat.m[3] * mat.m[14] -
|
||||
mat.m[13] * mat.m[2] * mat.m[11] +
|
||||
mat.m[13] * mat.m[3] * mat.m[10];
|
||||
|
||||
inv.m[5] = mat.m[0] * mat.m[10] * mat.m[15] -
|
||||
mat.m[0] * mat.m[11] * mat.m[14] -
|
||||
mat.m[8] * mat.m[2] * mat.m[15] +
|
||||
mat.m[8] * mat.m[3] * mat.m[14] +
|
||||
mat.m[12] * mat.m[2] * mat.m[11] -
|
||||
mat.m[12] * mat.m[3] * mat.m[10];
|
||||
|
||||
inv.m[9] = -mat.m[0] * mat.m[9] * mat.m[15] +
|
||||
mat.m[0] * mat.m[11] * mat.m[13] +
|
||||
mat.m[8] * mat.m[1] * mat.m[15] -
|
||||
mat.m[8] * mat.m[3] * mat.m[13] -
|
||||
mat.m[12] * mat.m[1] * mat.m[11] +
|
||||
mat.m[12] * mat.m[3] * mat.m[9];
|
||||
|
||||
inv.m[13] = mat.m[0] * mat.m[9] * mat.m[14] -
|
||||
mat.m[0] * mat.m[10] * mat.m[13] -
|
||||
mat.m[8] * mat.m[1] * mat.m[14] +
|
||||
mat.m[8] * mat.m[2] * mat.m[13] +
|
||||
mat.m[12] * mat.m[1] * mat.m[10] -
|
||||
mat.m[12] * mat.m[2] * mat.m[9];
|
||||
|
||||
inv.m[2] = mat.m[1] * mat.m[6] * mat.m[15] -
|
||||
mat.m[1] * mat.m[7] * mat.m[14] -
|
||||
mat.m[5] * mat.m[2] * mat.m[15] +
|
||||
mat.m[5] * mat.m[3] * mat.m[14] +
|
||||
mat.m[13] * mat.m[2] * mat.m[7] -
|
||||
mat.m[13] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[6] = -mat.m[0] * mat.m[6] * mat.m[15] +
|
||||
mat.m[0] * mat.m[7] * mat.m[14] +
|
||||
mat.m[4] * mat.m[2] * mat.m[15] -
|
||||
mat.m[4] * mat.m[3] * mat.m[14] -
|
||||
mat.m[12] * mat.m[2] * mat.m[7] +
|
||||
mat.m[12] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[10] = mat.m[0] * mat.m[5] * mat.m[15] -
|
||||
mat.m[0] * mat.m[7] * mat.m[13] -
|
||||
mat.m[4] * mat.m[1] * mat.m[15] +
|
||||
mat.m[4] * mat.m[3] * mat.m[13] +
|
||||
mat.m[12] * mat.m[1] * mat.m[7] -
|
||||
mat.m[12] * mat.m[3] * mat.m[5];
|
||||
|
||||
inv.m[14] = -mat.m[0] * mat.m[5] * mat.m[14] +
|
||||
mat.m[0] * mat.m[6] * mat.m[13] +
|
||||
mat.m[4] * mat.m[1] * mat.m[14] -
|
||||
mat.m[4] * mat.m[2] * mat.m[13] -
|
||||
mat.m[12] * mat.m[1] * mat.m[6] +
|
||||
mat.m[12] * mat.m[2] * mat.m[5];
|
||||
|
||||
inv.m[3] = -mat.m[1] * mat.m[6] * mat.m[11] +
|
||||
mat.m[1] * mat.m[7] * mat.m[10] +
|
||||
mat.m[5] * mat.m[2] * mat.m[11] -
|
||||
mat.m[5] * mat.m[3] * mat.m[10] -
|
||||
mat.m[9] * mat.m[2] * mat.m[7] +
|
||||
mat.m[9] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[7] = mat.m[0] * mat.m[6] * mat.m[11] -
|
||||
mat.m[0] * mat.m[7] * mat.m[10] -
|
||||
mat.m[4] * mat.m[2] * mat.m[11] +
|
||||
mat.m[4] * mat.m[3] * mat.m[10] +
|
||||
mat.m[8] * mat.m[2] * mat.m[7] -
|
||||
mat.m[8] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[11] = -mat.m[0] * mat.m[5] * mat.m[11] +
|
||||
mat.m[0] * mat.m[7] * mat.m[9] +
|
||||
mat.m[4] * mat.m[1] * mat.m[11] -
|
||||
mat.m[4] * mat.m[3] * mat.m[9] -
|
||||
mat.m[8] * mat.m[1] * mat.m[7] +
|
||||
mat.m[8] * mat.m[3] * mat.m[5];
|
||||
|
||||
inv.m[15] = mat.m[0] * mat.m[5] * mat.m[10] -
|
||||
mat.m[0] * mat.m[6] * mat.m[9] -
|
||||
mat.m[4] * mat.m[1] * mat.m[10] +
|
||||
mat.m[4] * mat.m[2] * mat.m[9] +
|
||||
mat.m[8] * mat.m[1] * mat.m[6] -
|
||||
mat.m[8] * mat.m[2] * mat.m[5];
|
||||
|
||||
det = mat.m[0] * inv.m[0] + mat.m[1] * inv.m[4] + mat.m[2] * inv.m[8] + mat.m[3] * inv.m[12];
|
||||
|
||||
if (std::fabs(det) < 0.01f)
|
||||
{
|
||||
throw std::runtime_error("Error: matrix cannot be inversed!");
|
||||
}
|
||||
|
||||
det = 1.0f / det;
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
inv.m[i] *= det;
|
||||
}
|
||||
|
||||
return inv;*/
|
||||
|
||||
return mat.inverse();
|
||||
}
|
||||
|
||||
Matrix3f CreateZRotationMatrix(float angle)
|
||||
{
|
||||
Eigen::Matrix3f m = Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitZ()).toRotationMatrix();
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix4f MultMatrixMatrix(const Matrix4f& m1, const Matrix4f& m2)
|
||||
{
|
||||
return m1 * m2;
|
||||
/*
|
||||
Matrix4f rx;
|
||||
|
||||
rx.m[0] = m1.m[0] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[8] * m2.m[2] + m1.m[12] * m2.m[3];
|
||||
rx.m[1] = m1.m[1] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[9] * m2.m[2] + m1.m[13] * m2.m[3];
|
||||
rx.m[2] = m1.m[2] * m2.m[0] + m1.m[6] * m2.m[1] + m1.m[10] * m2.m[2] + m1.m[14] * m2.m[3];
|
||||
rx.m[3] = m1.m[3] * m2.m[0] + m1.m[7] * m2.m[1] + m1.m[11] * m2.m[2] + m1.m[15] * m2.m[3];
|
||||
|
||||
rx.m[4] = m1.m[0] * m2.m[4] + m1.m[4] * m2.m[5] + m1.m[8] * m2.m[6] + m1.m[12] * m2.m[7];
|
||||
rx.m[5] = m1.m[1] * m2.m[4] + m1.m[5] * m2.m[5] + m1.m[9] * m2.m[6] + m1.m[13] * m2.m[7];
|
||||
rx.m[6] = m1.m[2] * m2.m[4] + m1.m[6] * m2.m[5] + m1.m[10] * m2.m[6] + m1.m[14] * m2.m[7];
|
||||
rx.m[7] = m1.m[3] * m2.m[4] + m1.m[7] * m2.m[5] + m1.m[11] * m2.m[6] + m1.m[15] * m2.m[7];
|
||||
|
||||
|
||||
rx.m[8] = m1.m[0] * m2.m[8] + m1.m[4] * m2.m[9] + m1.m[8] * m2.m[10] + m1.m[12] * m2.m[11];
|
||||
rx.m[9] = m1.m[1] * m2.m[8] + m1.m[5] * m2.m[9] + m1.m[9] * m2.m[10] + m1.m[13] * m2.m[11];
|
||||
rx.m[10] = m1.m[2] * m2.m[8] + m1.m[6] * m2.m[9] + m1.m[10] * m2.m[10] + m1.m[14] * m2.m[11];
|
||||
rx.m[11] = m1.m[3] * m2.m[8] + m1.m[7] * m2.m[9] + m1.m[11] * m2.m[10] + m1.m[15] * m2.m[11];
|
||||
|
||||
rx.m[12] = m1.m[0] * m2.m[12] + m1.m[4] * m2.m[13] + m1.m[8] * m2.m[14] + m1.m[12] * m2.m[15];
|
||||
rx.m[13] = m1.m[1] * m2.m[12] + m1.m[5] * m2.m[13] + m1.m[9] * m2.m[14] + m1.m[13] * m2.m[15];
|
||||
rx.m[14] = m1.m[2] * m2.m[12] + m1.m[6] * m2.m[13] + m1.m[10] * m2.m[14] + m1.m[14] * m2.m[15];
|
||||
rx.m[15] = m1.m[3] * m2.m[12] + m1.m[7] * m2.m[13] + m1.m[11] * m2.m[14] + m1.m[15] * m2.m[15];
|
||||
|
||||
return rx;*/
|
||||
}
|
||||
|
||||
Matrix3f MultMatrixMatrix(const Matrix3f& m1, const Matrix3f& m2)
|
||||
{
|
||||
return m1 * m2;
|
||||
}
|
||||
|
||||
/*
|
||||
Matrix4f MakeTranslationMatrix(const Vector3f& p)
|
||||
{
|
||||
Matrix4f r = Matrix4f::Identity();
|
||||
|
||||
r.m[12] = p(0);
|
||||
r.m[13] = p(1);
|
||||
r.m[14] = p(2);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f MakeScaleMatrix(float scale)
|
||||
{
|
||||
Matrix3f r = Matrix3f::Identity();
|
||||
|
||||
r.m[0] = scale;
|
||||
r.m[5] = scale;
|
||||
r.m[10] = scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f MakeRotationMatrix(const Vector3f& p)
|
||||
{
|
||||
Matrix3f r = Matrix3f::Identity();
|
||||
|
||||
r.m[12] = p.v[0];
|
||||
r.m[13] = p.v[1];
|
||||
r.m[14] = p.v[2];
|
||||
|
||||
return r;
|
||||
}*/
|
||||
/*
|
||||
Vector2f operator*(Vector2f v, float scale)
|
||||
{
|
||||
Vector2f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
|
||||
return r;
|
||||
}*/
|
||||
/*
|
||||
Vector3f operator*(Vector3f v, float scale)
|
||||
{
|
||||
Vector3f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
r.v[2] = v.v[2] * scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
Vector4f operator*(Vector4f v, float scale)
|
||||
{
|
||||
Vector4f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
r.v[2] = v.v[2] * scale;
|
||||
r.v[3] = v.v[3] * scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
*/
|
||||
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt)
|
||||
{
|
||||
return v.transpose() * mt;
|
||||
/*
|
||||
Vector3f r;
|
||||
|
||||
r(0) = v(0) * mt.m[0] + v(1) * mt.m[1] + v(2) * mt.m[2];
|
||||
r(1) = v(0) * mt.m[3] + v(1) * mt.m[4] + v(2) * mt.m[5];
|
||||
r(2) = v(0) * mt.m[6] + v(1) * mt.m[7] + v(2) * mt.m[8];
|
||||
|
||||
return r;*/
|
||||
}
|
||||
|
||||
Vector4f MultVectorMatrix(Vector4f v, Matrix4f mt)
|
||||
{
|
||||
/*Vector4f r;
|
||||
|
||||
r(0) = v(0) * mt.m[0] + v(1) * mt.m[1] + v(2) * mt.m[2] + v(3) * mt.m[3];
|
||||
r(1) = v(0) * mt.m[4] + v(1) * mt.m[5] + v(2) * mt.m[6] + v(3) * mt.m[7];
|
||||
r(2) = v(0) * mt.m[8] + v(1) * mt.m[9] + v(2) * mt.m[10] + v(3) * mt.m[11];
|
||||
r(3) = v(0) * mt.m[12] + v(1) * mt.m[13] + v(2) * mt.m[14] + v(3) * mt.m[15];
|
||||
|
||||
return r;*/
|
||||
return v.transpose() * mt;
|
||||
}
|
||||
|
||||
Vector4f MultMatrixVector(Matrix4f mt, Vector4f v)
|
||||
{
|
||||
return mt * v;
|
||||
/*Vector4f r;
|
||||
|
||||
r(0) = v(0) * mt.m[0] + v(1) * mt.m[4] + v(2) * mt.m[8] + v(3) * mt.m[12];
|
||||
r(1) = v(0) * mt.m[1] + v(1) * mt.m[5] + v(2) * mt.m[9] + v(3) * mt.m[13];
|
||||
r(2) = v(0) * mt.m[2] + v(1) * mt.m[6] + v(2) * mt.m[10] + v(3) * mt.m[14];
|
||||
r(3) = v(0) * mt.m[3] + v(1) * mt.m[7] + v(2) * mt.m[11] + v(3) * mt.m[15];
|
||||
|
||||
return r;*/
|
||||
}
|
||||
|
||||
Vector3f MultMatrixVector(Matrix3f mt, Vector3f v)
|
||||
{
|
||||
return mt * v;
|
||||
/*Vector3f r;
|
||||
|
||||
r(0) = v(0) * mt.m[0] + v(1) * mt.m[3] + v(2) * mt.m[6];
|
||||
r(1) = v(0) * mt.m[1] + v(1) * mt.m[4] + v(2) * mt.m[7];
|
||||
r(2) = v(0) * mt.m[2] + v(1) * mt.m[5] + v(2) * mt.m[8];
|
||||
|
||||
return r;*/
|
||||
}
|
||||
/*
|
||||
Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t)
|
||||
{
|
||||
const float epsilon = 1e-6f;
|
||||
|
||||
// Нормализация входных кватернионов
|
||||
Vector4f q1_norm = q1.normalized();
|
||||
Vector4f q2_norm = q2.normalized();
|
||||
|
||||
float cosTheta = q1_norm.dot(q2_norm);
|
||||
|
||||
// Если q1 и q2 близки к противоположным направлениям, корректируем q2
|
||||
Vector4f q2_adjusted = q2_norm;
|
||||
if (cosTheta < 0.0f) {
|
||||
q2_adjusted.v[0] = -q2_adjusted.v[0];
|
||||
q2_adjusted.v[1] = -q2_adjusted.v[1];
|
||||
q2_adjusted.v[2] = -q2_adjusted.v[2];
|
||||
q2_adjusted.v[3] = -q2_adjusted.v[3];
|
||||
cosTheta = -cosTheta;
|
||||
}
|
||||
|
||||
// Если кватернионы близки, используем линейную интерполяцию
|
||||
if (cosTheta > 1.0f - epsilon) {
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = q1_norm.v[0] + t * (q2_adjusted.v[0] - q1_norm.v[0]);
|
||||
result.v[1] = q1_norm.v[1] + t * (q2_adjusted.v[1] - q1_norm.v[1]);
|
||||
result.v[2] = q1_norm.v[2] + t * (q2_adjusted.v[2] - q1_norm.v[2]);
|
||||
result.v[3] = q1_norm.v[3] + t * (q2_adjusted.v[3] - q1_norm.v[3]);
|
||||
|
||||
return result.normalized();
|
||||
}
|
||||
|
||||
// Иначе используем сферическую интерполяцию
|
||||
float theta = std::acos(cosTheta);
|
||||
float sinTheta = std::sin(theta);
|
||||
|
||||
float coeff1 = std::sin((1.0f - t) * theta) / sinTheta;
|
||||
float coeff2 = std::sin(t * theta) / sinTheta;
|
||||
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = coeff1 * q1_norm.v[0] + coeff2 * q2_adjusted.v[0];
|
||||
result.v[1] = coeff1 * q1_norm.v[1] + coeff2 * q2_adjusted.v[1];
|
||||
result.v[2] = coeff1 * q1_norm.v[2] + coeff2 * q2_adjusted.v[2];
|
||||
result.v[3] = coeff1 * q1_norm.v[3] + coeff2 * q2_adjusted.v[3];
|
||||
|
||||
return result.normalized();
|
||||
}*/
|
||||
|
||||
Eigen::Matrix4f MakeMatrix4x4(const Eigen::Matrix3f& m, const Eigen::Vector3f& pos) {
|
||||
Eigen::Matrix4f r = Eigen::Matrix4f::Identity();
|
||||
|
||||
// Копируем 3x3 матрицу в верхний левый угол
|
||||
r.block<3, 3>(0, 0) = m;
|
||||
|
||||
// Копируем позицию в последний столбец (первые 3 элемента)
|
||||
r.block<3, 1>(0, 3) = pos;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
@ -1,197 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
using Eigen::Vector2f;
|
||||
using Eigen::Vector3f;
|
||||
using Eigen::Vector4f;
|
||||
|
||||
using Eigen::Matrix3f;
|
||||
using Eigen::Matrix4f;
|
||||
/*
|
||||
struct Vector4f
|
||||
{
|
||||
Vector4f()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Vector4f(float x, float y, float z, float t)
|
||||
: v{ x,y,z,t }
|
||||
{
|
||||
}
|
||||
|
||||
std::array<float, 4> v = { 0.f, 0.f, 0.f, 0.f };
|
||||
|
||||
Vector4f normalized() const {
|
||||
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
|
||||
if (norm <= 0.000001f) {
|
||||
return { 0,0, 0, 0};
|
||||
}
|
||||
Vector4f r;
|
||||
|
||||
r.v[0] = v[0] / norm;
|
||||
r.v[1] = v[1] / norm;
|
||||
r.v[2] = v[2] / norm;
|
||||
r.v[3] = v[3] / norm;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
double dot(const Vector4f& other) const {
|
||||
return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2] + v[3] * other.v[3];
|
||||
}
|
||||
};*/
|
||||
/*
|
||||
struct Vector3f
|
||||
{
|
||||
std::array<float, 3> v = { 0.f, 0.f, 0.f };
|
||||
|
||||
Vector3f()
|
||||
{
|
||||
}
|
||||
|
||||
Vector3f(float x, float y, float z)
|
||||
: v{x,y,z}
|
||||
{
|
||||
}
|
||||
|
||||
Vector3f normalized() const {
|
||||
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||
if (norm <= 0.000001f) {
|
||||
return { 0,0,0 };
|
||||
}
|
||||
Vector3f r;
|
||||
|
||||
r.v[0] = v[0] / norm;
|
||||
r.v[1] = v[1] / norm;
|
||||
r.v[2] = v[2] / norm;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
float squaredNorm() const {
|
||||
return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
|
||||
}
|
||||
|
||||
float length() const
|
||||
{
|
||||
return sqrt(squaredNorm());
|
||||
}
|
||||
|
||||
float dot(const Vector3f& other) const {
|
||||
return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2];
|
||||
}
|
||||
|
||||
Vector3f cross(const Vector3f& other) const {
|
||||
return Vector3f(
|
||||
v[1] * other.v[2] - v[2] * other.v[1],
|
||||
v[2] * other.v[0] - v[0] * other.v[2],
|
||||
v[0] * other.v[1] - v[1] * other.v[0]
|
||||
);
|
||||
}
|
||||
|
||||
bool operator<(const Vector3f& other) const {
|
||||
if (v[0] != other.v[0]) return v[0] < other.v[0];
|
||||
if (v[1] != other.v[1]) return v[1] < other.v[1];
|
||||
return v[2] < other.v[2];
|
||||
}
|
||||
};*/
|
||||
|
||||
|
||||
/*
|
||||
struct Vector2f
|
||||
{
|
||||
std::array<float, 2> v = {0.f, 0.f};
|
||||
|
||||
Vector2f()
|
||||
{
|
||||
}
|
||||
|
||||
Vector2f(float x, float y)
|
||||
: v{ x,y }
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
Vector2f operator+(const Vector2f& x, const Vector2f& y);
|
||||
|
||||
Vector2f operator-(const Vector2f& x, const Vector2f& y);
|
||||
*/
|
||||
//Vector3f operator+(const Vector3f& x, const Vector3f& y);
|
||||
|
||||
//Vector3f operator-(const Vector3f& x, const Vector3f& y);
|
||||
//Vector4f operator+(const Vector4f& x, const Vector4f& y);
|
||||
|
||||
//Vector4f operator-(const Vector4f& x, const Vector4f& y);
|
||||
|
||||
//Vector3f operator-(const Vector3f& x);
|
||||
|
||||
/*
|
||||
struct Matrix3f
|
||||
{
|
||||
std::array<float, 9> m = { 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, };
|
||||
|
||||
static Matrix3f Identity();
|
||||
};*/
|
||||
/*
|
||||
struct Matrix4f
|
||||
{
|
||||
std::array<float, 16> m = { 0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f };
|
||||
|
||||
static Matrix4f Identity();
|
||||
|
||||
float& operator()(int row, int col) {
|
||||
//return m[row * 4 + col]; //OpenGL specific
|
||||
return m[col * 4 + row];
|
||||
}
|
||||
|
||||
const float& operator()(int row, int col) const {
|
||||
//return m[row * 4 + col];
|
||||
return m[col * 4 + row];
|
||||
}
|
||||
};
|
||||
*/
|
||||
//Matrix4f operator*(const Matrix4f& m1, const Matrix4f& m2);
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar);
|
||||
Matrix4f MakeOrthoMatrix(float xmin, float xmax, float ymin, float ymax, float zNear, float zFar);
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
|
||||
Matrix3f QuatToMatrix(const Eigen::Quaternionf& q);
|
||||
|
||||
Eigen::Quaternionf MatrixToQuat(const Matrix3f& m);
|
||||
|
||||
Eigen::Quaternionf QuatFromRotateAroundX(float angle);
|
||||
Eigen::Quaternionf QuatFromRotateAroundY(float angle);
|
||||
Eigen::Quaternionf QuatFromRotateAroundZ(float angle);
|
||||
|
||||
//Vector2f operator*(Vector2f v, float scale);
|
||||
//Vector3f operator*(Vector3f v, float scale);
|
||||
//Vector4f operator*(Vector4f v, float scale);
|
||||
|
||||
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt);
|
||||
Vector4f MultVectorMatrix(Vector4f v, Matrix4f mt);
|
||||
Vector4f MultMatrixVector(Matrix4f mt, Vector4f v);
|
||||
Vector3f MultMatrixVector(Matrix3f mt, Vector3f v);
|
||||
|
||||
//Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t);
|
||||
Matrix3f InverseMatrix(const Matrix3f& m);
|
||||
Matrix4f InverseMatrix(const Matrix4f& m);
|
||||
Matrix3f MultMatrixMatrix(const Matrix3f& m1, const Matrix3f& m2);
|
||||
Matrix4f MultMatrixMatrix(const Matrix4f& m1, const Matrix4f& m2);
|
||||
Matrix4f MakeMatrix4x4(const Eigen::Matrix3f& m, const Eigen::Vector3f& pos);
|
||||
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user