From 036590297144b67f96e4de7712989ab58c75b30d Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 3 Jun 2023 19:40:50 +0200 Subject: [PATCH] moved Mat4 definition --- include/Defines.h | 32 ++++++++++++++++++++++++++ include/misc/Maths.h | 30 ------------------------ include/render/shaders/EntityShader.h | 4 ++-- include/render/shaders/ShaderProgram.h | 3 +-- include/render/shaders/WorldShader.h | 4 ++-- src/render/Renderer.cpp | 4 ++-- src/render/shaders/EntityShader.cpp | 4 ++-- src/render/shaders/ShaderProgram.cpp | 2 +- src/render/shaders/WorldShader.cpp | 4 ++-- 9 files changed, 44 insertions(+), 43 deletions(-) diff --git a/include/Defines.h b/include/Defines.h index 921ebd2..78c2487 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -1,5 +1,7 @@ #pragma once +#include + namespace td { template @@ -95,4 +97,34 @@ using Vec4d = Vec4; using Color = Vec3; +template +struct Mat4 { + static const std::size_t MATRIX_SIZE = 4; + + T x0, x1, x2, x3; + T y0, y1, y2, y3; + T z0, z1, z2, z3; + T w0, w1, w2, w3; + + T operator[] (std::size_t offset) const { + return reinterpret_cast(this)[offset]; + } + + T& operator[] (std::size_t offset) { + return reinterpret_cast(this)[offset]; + } + + T at(std::size_t row, std::size_t column) const { + return operator[](row * MATRIX_SIZE + column); + } + + T& at(std::size_t row, std::size_t column) { + return operator[](row * MATRIX_SIZE + column); + } +}; + +typedef Mat4 Mat4f; +typedef Mat4 Mat4i; +typedef Mat4 Mat4d; + } // namespace td diff --git a/include/misc/Maths.h b/include/misc/Maths.h index 7004d5c..1463fe0 100644 --- a/include/misc/Maths.h +++ b/include/misc/Maths.h @@ -55,36 +55,6 @@ T Dot(const Vec4& vect, const Vec4& other) { // Matricies // ////////////////////////////////////////////////////////////////// -template -struct Mat4 { - static const std::size_t MATRIX_SIZE = 4; - - T x0, x1, x2, x3; - T y0, y1, y2, y3; - T z0, z1, z2, z3; - T w0, w1, w2, w3; - - T operator[] (std::size_t offset) const { - return reinterpret_cast(this)[offset]; - } - - T& operator[] (std::size_t offset) { - return reinterpret_cast(this)[offset]; - } - - T at(std::size_t row, std::size_t column) const { - return operator[](row * MATRIX_SIZE + column); - } - - T& at(std::size_t row, std::size_t column) { - return operator[](row * MATRIX_SIZE + column); - } -}; - -typedef Mat4 Mat4f; -typedef Mat4 Mat4i; -typedef Mat4 Mat4d; - template Vec4 Dot(const Mat4& mat, const Vec4& vect) { return { diff --git a/include/render/shaders/EntityShader.h b/include/render/shaders/EntityShader.h index 25309c6..958f4ec 100644 --- a/include/render/shaders/EntityShader.h +++ b/include/render/shaders/EntityShader.h @@ -19,8 +19,8 @@ public: void LoadShader(); void SetColorEffect(const Vec3f& color); - void SetProjectionMatrix(const maths::Mat4f& proj) const; - void SetViewMatrix(const maths::Mat4f& view) const; + void SetProjectionMatrix(const Mat4f& proj) const; + void SetViewMatrix(const Mat4f& view) const; void SetModelPos(const Vec3f& pos) const; }; diff --git a/include/render/shaders/ShaderProgram.h b/include/render/shaders/ShaderProgram.h index 3d0af59..bfc93e9 100755 --- a/include/render/shaders/ShaderProgram.h +++ b/include/render/shaders/ShaderProgram.h @@ -3,7 +3,6 @@ #include #include "Defines.h" #include "render/GL.h" -#include "misc/Maths.h" namespace td { namespace shader { @@ -28,7 +27,7 @@ protected: void LoadVector(unsigned int location, const Vec2f& vector) const; void LoadVector(unsigned int location, const Vec3f& vector) const; void LoadBoolean(unsigned int location, bool value) const; - void LoadMat4(unsigned int location, const maths::Mat4f& mat) const; + void LoadMat4(unsigned int location, const Mat4f& mat) const; void CleanUp() const; private: diff --git a/include/render/shaders/WorldShader.h b/include/render/shaders/WorldShader.h index b10200a..a8c6756 100644 --- a/include/render/shaders/WorldShader.h +++ b/include/render/shaders/WorldShader.h @@ -14,8 +14,8 @@ public: WorldShader(); void LoadShader(); - void SetProjectionMatrix(const maths::Mat4f& proj) const; - void SetViewMatrix(const maths::Mat4f& view) const; + void SetProjectionMatrix(const Mat4f& proj) const; + void SetViewMatrix(const Mat4f& view) const; }; } // namespace shader diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index b5591ff..38869e5 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -98,7 +98,7 @@ void Renderer::Prepare() { } void Renderer::Resize(int width, int height) { - maths::Mat4f projectionMatrix = maths::Perspective(80.0f / 180.0f * M_PI, static_cast(width) / height, 0.1f, 160.0f); + Mat4f projectionMatrix = maths::Perspective(80.0f / 180.0f * M_PI, static_cast(width) / height, 0.1f, 160.0f); m_WorldShader->Start(); m_WorldShader->SetProjectionMatrix(projectionMatrix); m_EntityShader->Start(); @@ -121,7 +121,7 @@ void Renderer::SetCamMovement(const Vec2f& mov) { void Renderer::SetCamPos(const Vec2f& newPos) { m_CamPos = newPos; - maths::Mat4f viewMatrix = maths::Look({m_CamPos.x, 50, m_CamPos.y}, {0, -1, -0.0001}, {0, 1, 0}); + Mat4f viewMatrix = maths::Look({m_CamPos.x, 50, m_CamPos.y}, {0, -1, -0.0001}, {0, 1, 0}); m_WorldShader->Start(); m_WorldShader->SetViewMatrix(viewMatrix); m_EntityShader->Start(); diff --git a/src/render/shaders/EntityShader.cpp b/src/render/shaders/EntityShader.cpp index 216f106..fd7de6a 100644 --- a/src/render/shaders/EntityShader.cpp +++ b/src/render/shaders/EntityShader.cpp @@ -104,11 +104,11 @@ void EntityShader::SetColorEffect(const Vec3f& color) { LoadVector(m_LocationColorEffect, color); } -void EntityShader::SetProjectionMatrix(const maths::Mat4f& proj) const { +void EntityShader::SetProjectionMatrix(const Mat4f& proj) const { LoadMat4(m_LocationProjectionMatrix, proj); } -void EntityShader::SetViewMatrix(const maths::Mat4f& view) const { +void EntityShader::SetViewMatrix(const Mat4f& view) const { LoadMat4(m_LocationViewMatrix, view); } diff --git a/src/render/shaders/ShaderProgram.cpp b/src/render/shaders/ShaderProgram.cpp index d7618f9..bc94a15 100755 --- a/src/render/shaders/ShaderProgram.cpp +++ b/src/render/shaders/ShaderProgram.cpp @@ -67,7 +67,7 @@ void ShaderProgram::LoadBoolean(unsigned int location, bool value) const { glUniform1i(static_cast(location), value); } -void ShaderProgram::LoadMat4(unsigned int location, const maths::Mat4f& mat) const { +void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const { glUniformMatrix4fv(static_cast(location), 1, false, reinterpret_cast(&mat)); } diff --git a/src/render/shaders/WorldShader.cpp b/src/render/shaders/WorldShader.cpp index e9c7880..630bc07 100644 --- a/src/render/shaders/WorldShader.cpp +++ b/src/render/shaders/WorldShader.cpp @@ -92,11 +92,11 @@ void WorldShader::GetAllUniformLocation() { m_LocationView = static_cast(GetUniformLocation("viewMatrix")); } -void WorldShader::SetProjectionMatrix(const maths::Mat4f& proj) const { +void WorldShader::SetProjectionMatrix(const Mat4f& proj) const { LoadMat4(m_LocationProjection, proj); } -void WorldShader::SetViewMatrix(const maths::Mat4f& view) const { +void WorldShader::SetViewMatrix(const Mat4f& view) const { LoadMat4(m_LocationView, view); }