From 862ef6d34b428cc05bd6545b1ed977c2ea9b9177 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 26 Jan 2021 15:36:57 +0100 Subject: [PATCH] Added normal matrices to transformable --- examples/movement/main.cpp | 2 +- include/transformable.hpp | 9 +++++++++ src/transformable.cpp | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/movement/main.cpp b/examples/movement/main.cpp index 314ba7b..84ce3d0 100644 --- a/examples/movement/main.cpp +++ b/examples/movement/main.cpp @@ -274,7 +274,7 @@ int main(int argc, char** argv) for (oglu::Object& cube : cubes) { shader->SetUniform("model", cube); - shader->SetUniformMatrix3fv("normal", 1, GL_FALSE, glm::value_ptr(glm::mat3(glm::transpose(glm::inverse(cube.GetMatrix()))))); + shader->SetUniformMatrix3fv("normal", 1, GL_FALSE, glm::value_ptr(cube.GetNormalMatrix())); cube.Render(); } diff --git a/include/transformable.hpp b/include/transformable.hpp index 57f85e8..74c6179 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -351,6 +351,15 @@ namespace oglu */ virtual const glm::mat4& GetMatrix(); + /** + * @brief Returns a normal matrix. + * + * For use in shaders to compute normals of surfaces. + * + * @return A 3x3 normal matrix + */ + glm::mat3 GetNormalMatrix(); + /** * @brief Get position as a 3D vector. * diff --git a/src/transformable.cpp b/src/transformable.cpp index 3f51968..5502638 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -197,6 +197,11 @@ namespace oglu return transformation; } + glm::mat3 Transformable::GetNormalMatrix() + { + return glm::mat3(glm::transpose(glm::inverse(GetMatrix()))); + } + const glm::vec3& Transformable::GetPosition() const { return translation;