Added Eigen, replacing ZMath

This commit is contained in:
Vladislav Khorev committed 2026-01-03 20:38:36 +03:00
1 parent 9bfe2bef89
commit 144978bfa2
20 files changed
+167 -936

No files matched your search

+106 -7
View File
@@ -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;
}
}
@@ -592,8 +691,8 @@ 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);
+14 -1
View File
@@ -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 {