From 3e6884356f63c5036070eb000553c8209b943ddf Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 23 Jan 2021 13:19:13 +0100 Subject: [PATCH 1/3] Transformable now uses a matrix --- examples/model_loading/main.cpp | 5 +- include/transformable.hpp | 13 +- src/camera.cpp | 28 +---- src/transformable.cpp | 216 ++++++++------------------------ 4 files changed, 63 insertions(+), 199 deletions(-) diff --git a/examples/model_loading/main.cpp b/examples/model_loading/main.cpp index 0ec4612..1db6552 100644 --- a/examples/model_loading/main.cpp +++ b/examples/model_loading/main.cpp @@ -72,6 +72,8 @@ int main(int argc, char** argv) oglu::Enable(GL_DEPTH_TEST); oglu::Camera camera(45.0f, 1.0f, 0.1f, 100.0f); + camera.Move(0.0f, -5.0f, -10.0f); + camera.LookAt(glm::value_ptr(glm::make_vec3(utah.GetPosition()) + glm::vec3(0.0f, 2.0f, 0.0f))); float t = 0.0f; @@ -82,8 +84,7 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); - camera.SetPosition(10.0f * cosf(t), -5.0f, 10.0f * sinf(t)); - camera.LookAt(utah); + utah.Rotate(0.0f, 10.0f, 0.0f); shader->Use(); shader->SetUniform("model", utah); diff --git a/include/transformable.hpp b/include/transformable.hpp index 165faf7..ce71386 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -10,6 +10,8 @@ #define TRANSFORMABLE_HPP #include +#include +#include namespace oglu { @@ -258,12 +260,13 @@ namespace oglu protected: // TODO: Separate translation, rotation and scaling matrices. // Combine them only when the user wants the transformation matrix - float* position; ///< Position vector - float* rotation; ///< Rotation matrix - float* scaling; ///< Scaling vector + glm::mat4 transformation; - float* transformation; - bool calculateMatrix; ///< Wether GetMatrix() needs to re-calculate the transformation matrix + glm::vec3 scale; + glm::quat orientation; + glm::vec3 translation; + glm::vec3 skew; + glm::vec4 perspective; }; } diff --git a/src/camera.cpp b/src/camera.cpp index 4e7ee94..f0c49d3 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -53,32 +53,8 @@ namespace oglu void Camera::LookAt(GLfloat x, GLfloat y, GLfloat z) { - glm::mat4 newTransform = glm::lookAt(glm::make_vec3(position), glm::vec3(x, y, z), glm::vec3(0.0f, 1.0f, 0.0f)); - glm::vec3 scale; - glm::vec3 pos; - glm::quat rot; - glm::vec3 skew; - glm::vec4 pers; - glm::decompose(newTransform, scale, rot, pos, skew, pers); - - memcpy( - position, - glm::value_ptr(pos), - 3 * sizeof(float) - ); - - memcpy( - rotation, - glm::value_ptr(glm::toMat4(rot)), - 16 * sizeof(float) - ); - - memcpy( - scaling, - glm::value_ptr(scale), - 3 * sizeof(float) - ); - calculateMatrix = true; + transformation = glm::lookAt(translation, glm::vec3(x, y, z), glm::vec3(0.0f, 1.0f, 0.0f)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Camera::LookAt(const GLfloat* target) diff --git a/src/transformable.cpp b/src/transformable.cpp index b12110c..70e2853 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -2,266 +2,150 @@ #include -#include #include +#include +#include #include namespace oglu { oglu::Transformable::Transformable() : - position(new float[3]{ 0.f }), rotation(new float[16]), scaling(new float[3]{ 1.f, 1.f, 1.f }), transformation(new float[16]), calculateMatrix(false) + transformation(glm::mat4(1.0f)) { - glm::mat4 identity(1.0f); - memcpy( - rotation, - glm::value_ptr(identity), - 16 * sizeof(float) - ); - memcpy( - transformation, - glm::value_ptr(identity), - 16 * sizeof(float) - ); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } Transformable::Transformable(const Transformable& other) : - position(new float[3]), rotation(new float[16]), scaling(new float[3]), transformation(new float[16]), calculateMatrix(true) + transformation(other.transformation) { - memcpy( - this->position, - other.position, - 3 * sizeof(float) - ); - - memcpy( - this->rotation, - other.rotation, - 16 * sizeof(float) - ); - - memcpy( - this->scaling, - other.scaling, - 3 * sizeof(float) - ); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } Transformable::~Transformable() { - delete[] scaling; - delete[] rotation; - delete[] position; } void Transformable::SetPosition(float x, float y, float z) { - this->position[0] = x; - this->position[1] = y; - this->position[2] = z; - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + translation = glm::vec3(x, y, z) - translation; + transformation = glm::translate(transformation, translation); } void Transformable::SetPosition(const float* translation) { - memcpy( - this->position, - translation, - 3 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); + this->translation = glm::make_vec3(translation) - this->translation; + transformation = glm::translate(transformation, this->translation); } void Transformable::SetRotation(float rotX, float rotY, float rotZ) { - // TODO: Using rotation matrices is stupid. Eventually this could (should) be done with quaternions - // For now we'll just risk gimbal locking the model - memcpy( - this->rotation, - glm::value_ptr( - glm::rotate( - glm::rotate( - glm::rotate( - glm::mat4(1.0f), glm::radians(rotX), glm::vec3(1.0f, 0.0f, 0.0f) - ), glm::radians(rotY), glm::vec3(0.0f, 1.0f, 0.0f) - ), glm::radians(rotZ), glm::vec3(0.0f, 0.0f, 1.0f) - ) - ), - 16 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + orientation = glm::quat(glm::vec3(rotX, rotY, rotZ)) - orientation; + transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); } void Transformable::SetRotation(const float* rotation) { - memcpy( - this->rotation, - glm::value_ptr( - glm::rotate( - glm::rotate( - glm::rotate( - glm::mat4(1.0f), glm::radians(rotation[0]), glm::vec3(1.0f, 0.0f, 0.0f) - ), glm::radians(rotation[1]), glm::vec3(0.0f, 1.0f, 0.0f) - ), glm::radians(rotation[2]), glm::vec3(0.0f, 0.0f, 1.0f) - ) - ), - 16 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + orientation = glm::quat(glm::make_vec3(rotation)) - orientation; + transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); } void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis) { - memcpy( - this->rotation, - glm::value_ptr(glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(xAxis, yAxis, zAxis))), - 16 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + orientation = glm::quat(glm::vec4(xAxis, yAxis, zAxis, angle)) - orientation; + transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); } void Transformable::SetRotation(float angle, const float* axis) { - memcpy( - this->rotation, - glm::value_ptr(glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::make_vec3(axis))), - 16 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + orientation = glm::quat(glm::vec4(axis[0], axis[1], axis[2], angle)) - orientation; + transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); } void Transformable::SetScale(float scaleX, float scaleY, float scaleZ) { - this->scaling[0] = scaleX; - this->scaling[1] = scaleY; - this->scaling[2] = scaleZ; - calculateMatrix = true; + glm::decompose(transformation, scale, orientation, translation, skew, perspective); + scale = glm::vec3(scaleX, scaleY, scaleZ) - scale; + transformation = glm::scale(transformation, scale); } void Transformable::SetScale(const float* scale) { - memcpy( - this->scaling, - scale, - 3 * sizeof(float) - ); - calculateMatrix = true; + glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); + this->scale = glm::make_vec3(scale) - this->scale; + transformation = glm::scale(transformation, this->scale); } void Transformable::Move(float x, float y, float z) { - this->position[0] += x; - this->position[1] += y; - this->position[2] += z; - calculateMatrix = true; + transformation = glm::translate(transformation, glm::vec3(x, y, z)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Move(const float* translation) { - this->position[0] += translation[0]; - this->position[1] += translation[1]; - this->position[2] += translation[2]; - calculateMatrix = true; + transformation = glm::translate(transformation, glm::make_vec3(translation)); + glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); } void Transformable::Rotate(float rotX, float rotY, float rotZ) { - memcpy( - this->rotation, - glm::value_ptr( - glm::rotate( - glm::rotate( - glm::rotate( - glm::make_mat4(this->rotation), glm::radians(rotX), glm::vec3(1.0f, 0.0f, 0.0f) - ), glm::radians(rotY), glm::vec3(0.0f, 1.0f, 0.0f) - ), glm::radians(rotZ), glm::vec3(0.0f, 0.0f, 1.0f) - ) - ), - 16 * sizeof(float) - ); - calculateMatrix = true; + transformation = glm::rotate(transformation, glm::radians(1.0f), glm::vec3(rotX, rotY, rotZ)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Rotate(const float* rotation) { - memcpy( - this->rotation, - glm::value_ptr( - glm::rotate( - glm::rotate( - glm::rotate( - glm::make_mat4(this->rotation), glm::radians(rotation[0]), glm::vec3(1.0f, 0.0f, 0.0f) - ), glm::radians(rotation[1]), glm::vec3(0.0f, 1.0f, 0.0f) - ), glm::radians(rotation[2]), glm::vec3(0.0f, 0.0f, 1.0f) - ) - ), - 16 * sizeof(float) - ); - calculateMatrix = true; + transformation = glm::rotate(transformation, 1.0f, glm::make_vec3(rotation)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis) { - memcpy( - this->rotation, - glm::value_ptr(glm::rotate(glm::make_mat4(this->rotation), glm::radians(angle), glm::vec3(xAxis, yAxis, zAxis))), - 16 * sizeof(float) - ); - calculateMatrix = true; + transformation = glm::rotate(transformation, angle, glm::vec3(xAxis, yAxis, zAxis)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Rotate(float angle, const float* axis) { - memcpy( - this->rotation, - glm::value_ptr(glm::rotate(glm::make_mat4(this->rotation), glm::radians(angle), glm::make_vec3(axis))), - 16 * sizeof(float) - ); - calculateMatrix = true; + transformation = glm::rotate(transformation, angle, glm::make_vec3(axis)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Scale(float scaleX, float scaleY, float scaleZ) { - this->scaling[0] += scaleX; - this->scaling[1] += scaleY; - this->scaling[2] += scaleZ; - calculateMatrix = true; + transformation = glm::scale(transformation, glm::vec3(scaleX, scaleY, scaleZ)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Scale(const float* scale) { - this->scaling[0] += scale[0]; - this->scaling[1] += scale[1]; - this->scaling[2] += scale[2]; - calculateMatrix = true; + transformation = glm::scale(transformation, glm::make_vec3(scale)); + glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); } const float* Transformable::GetMatrix() { - if (calculateMatrix) - { - memcpy( - transformation, - glm::value_ptr(glm::translate(glm::mat4(1.0f), glm::make_vec3(position)) * glm::make_mat4(rotation) * glm::scale(glm::mat4(1.0f), glm::make_vec3(scaling))), - 16 * sizeof(float) - ); - calculateMatrix = false; - } - - return transformation; + return glm::value_ptr(transformation); } const float* Transformable::GetPosition() const { - return position; + return glm::value_ptr(translation); } const float* Transformable::GetRotation() const { - return rotation; + return glm::value_ptr(orientation); } const float* Transformable::GetScaling() const { - return scaling; + return glm::value_ptr(scale); } } From a17c73d3c949b734500c5b680bff7f88cf8f867f Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 23 Jan 2021 16:23:49 +0100 Subject: [PATCH 2/3] Reworked transformable --- CMakeLists.txt | 3 +- examples/debug/CMakeLists.txt | 2 +- examples/debug/main.cpp | 17 +++-- include/camera.hpp | 2 + include/transformable.hpp | 125 +++++++++++++++++++++++++++------- src/camera.cpp | 4 ++ src/transformable.cpp | 92 +++++++++++++++++-------- 7 files changed, 177 insertions(+), 68 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a80f56a..f48157e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ add_library(openglu SHARED ${include_files} ${source_files} "cpp.hint" - "src/transformable.cpp") +) target_compile_definitions(openglu PRIVATE OGLU_BUILD_DLL) @@ -48,7 +48,6 @@ if(${build_documentation}) COMMAND ${DOXYGEN_EXECUTABLE} "${CMAKE_BINARY_DIR}/doxyfile" WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Building documentation..." - VERBATIM ) endif() diff --git a/examples/debug/CMakeLists.txt b/examples/debug/CMakeLists.txt index 60ca621..a48be0d 100644 --- a/examples/debug/CMakeLists.txt +++ b/examples/debug/CMakeLists.txt @@ -1,4 +1,4 @@ -add_executable(debug "main.cpp") +add_executable(debug "main.cpp" "shaders/fragmentShader.frag" "shaders/vertexShader.vert") find_package(glfw3 REQUIRED) diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index 3794886..cfdad20 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -86,13 +86,11 @@ int main(int argc, char** argv) oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg"); oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png"); - glm::mat4 view = glm::mat4(1.0f); - view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f)); - - glm::mat4 projection; - projection = glm::perspective(glm::radians(45.f), 1.0f, 0.1f, 100.0f); + oglu::Camera camera; + camera.Move(0.0f, 0.0f, -5.0f); // Window loop + float t = 0.0f; while (!glfwWindowShouldClose(window)) { processInput(window); @@ -101,15 +99,14 @@ int main(int argc, char** argv) square.Rotate(6.0f, 0.0f, 0.0f); square2.Rotate(-6.0f, 0.0f, 0.0f); - - view = glm::rotate(view, glm::radians(1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); + camera.Rotate(0.0f, 1.0f, 0.0f); shader->Use(); shader->SetUniform("texture1", crate, 0); shader->SetUniform("texture2", opengl, 1); shader->SetUniform("model", square); - shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view)); - shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection)); + shader->SetUniform("view", camera); + shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix()); square.Render(); @@ -118,6 +115,8 @@ int main(int argc, char** argv) glfwSwapBuffers(window); glfwPollEvents(); + + t += 0.1f; } glfwTerminate(); diff --git a/include/camera.hpp b/include/camera.hpp index d87adc8..78cd480 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -88,6 +88,8 @@ namespace oglu */ void LookAt(const Transformable& target); + // void Move(float x, float y, float z) override; + /** * @brief Gets the projection matrix of the camera. * diff --git a/include/transformable.hpp b/include/transformable.hpp index ce71386..17d389f 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -50,7 +50,7 @@ namespace oglu * @param[in] y New y position * @param[in] z New z position */ - void SetPosition(float x, float y, float z); + virtual void SetPosition(float x, float y, float z); /** * @brief Sets the position. @@ -60,7 +60,17 @@ namespace oglu * * @param[in] position An array of floats containing three scalars */ - void SetPosition(const float* position); + virtual void SetPosition(const float* position); + + /** + * @brief Sets the position. + * + * This sets an absolute position, means that it resets any previous + * translations. + * + * @param[in] position A 3D position vector + */ + virtual void SetPosition(const glm::vec3& position); /** * @brief Sets the rotation. @@ -72,7 +82,7 @@ namespace oglu * @param[in] rotY New rotation around y axis * @param[in] rotZ New rotation around z axis */ - void SetRotation(float rotX, float rotY, float rotZ); + virtual void SetRotation(float rotX, float rotY, float rotZ); /** * @brief Sets the rotation. @@ -82,7 +92,17 @@ namespace oglu * * @param[in] rotation An array of floats containing three scalars */ - void SetRotation(const float* rotation); + virtual void SetRotation(const float* rotation); + + /** + * @brief Sets the rotation. + * + * This sets an absolute rotation, means that it resets any previous + * rotations. + * + * @param[in] rotation A glm::vec3 containing Euler angles + */ + virtual void SetRotation(const glm::vec3& rotation); /** * @brief Sets the rotation. @@ -95,7 +115,7 @@ namespace oglu * @param[in] yAxis The y component of the rotation axis * @param[in] zAxis The z component of the rotation axis */ - void SetRotation(float angle, float xAxis, float yAxis, float zAxis); + virtual void SetRotation(float angle, float xAxis, float yAxis, float zAxis); /** * @brief Sets the rotation. @@ -106,7 +126,18 @@ namespace oglu * @param[in] angle The angle to rotate by * @param[in] axis An array of floats containing the three rotation axis components */ - void SetRotation(float angle, const float* axis); + virtual void SetRotation(float angle, const float* axis); + + /** + * @brief Sets the rotation. + * + * This sets an absolute rotation, means that it resets any previous + * rotations. + * + * @param[in] angle The angle to rotate by + * @param[in] axis The axis to rotate around + */ + virtual void SetRotation(float angle, const glm::vec3& axis); /** * @brief Sets the scaling. @@ -118,7 +149,7 @@ namespace oglu * @param[in] scaleY The scaling in y direction * @param[in] scaleZ The scaling in z direction */ - void SetScale(float scaleX, float scaleY, float scaleZ); + virtual void SetScale(float scaleX, float scaleY, float scaleZ); /** * @brief Sets the scaling. @@ -128,7 +159,17 @@ namespace oglu * * @param[in] scale An array of floats containing three scalars */ - void SetScale(const float* scale); + virtual void SetScale(const float* scale); + + /** + * @brief Sets the scaling. + * + * This sets an absolute scaling, means that it resets any previous + * scaling. + * + * @param[in] scale A 3D scaling vector + */ + virtual void SetScale(const glm::vec3& scale); /** * @brief Performs a translation. @@ -140,7 +181,7 @@ namespace oglu * @param[in] y Offset along the y axis * @param[in] z Offset along the z axis */ - void Move(float x, float y, float z); + virtual void Move(float x, float y, float z); /** * @brief Performs a translation. @@ -150,7 +191,17 @@ namespace oglu * * @param[in] position An array of floats containing the offset values */ - void Move(const float* translation); + virtual void Move(const float* translation); + + /** + * @brief Performs a translation. + * + * This function applies a translation to the object, it operates + * operates on the current position. + * + * @param[in] position A 3D displacement vector + */ + virtual void Move(const glm::vec3& translation); /** * @brief Performs a rotation. @@ -162,7 +213,7 @@ namespace oglu * @param[in] rotY Rotation around the y axis * @param[in] rotZ Rotation around the z axis */ - void Rotate(float rotX, float rotY, float rotZ); + virtual void Rotate(float rotX, float rotY, float rotZ); /** * @brief Performs a rotation. @@ -172,7 +223,17 @@ namespace oglu * * @param[in] rotation An array of floats containing the rotation values */ - void Rotate(const float* rotation); + virtual void Rotate(const float* rotation); + + /** + * @brief Performs a rotation. + * + * This function applies a rotation to the object, it operates + * operates on the current rotation. + * + * @param[in] rotation An 3D vector containing Euler angles + */ + virtual void Rotate(const glm::vec3& rotation); /** * @brief Performs a rotation. @@ -185,7 +246,7 @@ namespace oglu * @param[in] yAxis y component of the rotation axis * @param[in] zAxis z component of the rotation axis */ - void Rotate(float angle, float xAxis, float yAxis, float zAxis); + virtual void Rotate(float angle, float xAxis, float yAxis, float zAxis); /** * @brief Performs a rotation. @@ -194,9 +255,20 @@ namespace oglu * operates on the current rotation. * * @param[in] angle The angle to rotate by - * @param[in] An array of floats containing the components of the rotation axis + * @param[in] axis An array of floats containing the components of the rotation axis */ - void Rotate(float angle, const float* axis); + virtual void Rotate(float angle, const float* axis); + + /** + * @brief Performs a rotation. + * + * This function applies a rotation to the object, it operates + * operates on the current rotation. + * + * @param[in] angle The angle to rotate by + * @param[in] axis The axis to rotate around + */ + virtual void Rotate(float angle, const glm::vec3& axis); /** * @brief Performs scaling. @@ -208,7 +280,7 @@ namespace oglu * @param[in] scaleX Scaling in y direction * @param[in] scaleX Scaling in z direction */ - void Scale(float scaleX, float scaleY, float scaleZ); + virtual void Scale(float scaleX, float scaleY, float scaleZ); /** * @brief Performs scaling. @@ -218,20 +290,21 @@ namespace oglu * * @param[in] scale An array of floats containing three scaling values */ - void Scale(const float* scale); + virtual void Scale(const float* scale); + + /** + * @brief Performs scaling. + * + * This function applies scaling to the object, it operates + * operates on the current scaling. + * + * @param[in] scale A 3D scaling vector + */ + virtual void Scale(const glm::vec3& scale); /** * @brief Returns a transformation matrix. * - * Internally, this function multiplies the translation, rotation, - * and scaling matrices into one transformation matrix which is - * then returned. - * - * This multiplication is only performed when a change occured to one - * of those matrices (so when there was a translation, rotation or scaling). - * So it is okay to call this function multiple times without huge performance - * loss, as long as no transformations occur inbetween. - * * @return An array of 16 floats representing the transformation matrix */ const float* GetMatrix(); diff --git a/src/camera.cpp b/src/camera.cpp index f0c49d3..7017326 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -13,6 +13,10 @@ namespace oglu Camera::Camera() : fov(45.0f), aspectRatio(0.0f), zNear(0.1f), zFar(100.0f), projection(new float[16]{ 0.0f }) { + GLint viewport[4]; + glGetIntegerv(GL_VIEWPORT, viewport); + aspectRatio = (float)viewport[2] / (float)viewport[3]; + memcpy( projection, glm::value_ptr(glm::perspective(glm::radians(fov), aspectRatio, zNear, zFar)), diff --git a/src/transformable.cpp b/src/transformable.cpp index 70e2853..f604543 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -27,105 +27,137 @@ namespace oglu void Transformable::SetPosition(float x, float y, float z) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - translation = glm::vec3(x, y, z) - translation; - transformation = glm::translate(transformation, translation); + SetPosition(glm::vec3(x, y, z)); } void Transformable::SetPosition(const float* translation) + { + SetPosition(glm::make_vec3(translation)); + } + + void Transformable::SetPosition(const glm::vec3& position) { glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); - this->translation = glm::make_vec3(translation) - this->translation; + this->translation = translation - this->translation; transformation = glm::translate(transformation, this->translation); } void Transformable::SetRotation(float rotX, float rotY, float rotZ) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::quat(glm::vec3(rotX, rotY, rotZ)) - orientation; - transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); + SetRotation(glm::vec3(rotX, rotY, rotZ)); } void Transformable::SetRotation(const float* rotation) + { + SetRotation(glm::make_vec3(rotation)); + } + + void Transformable::SetRotation(const glm::vec3& rotation) { glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::quat(glm::make_vec3(rotation)) - orientation; - transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); + orientation = glm::quat(glm::radians(rotation)) * (-orientation); + transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation)); } void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::quat(glm::vec4(xAxis, yAxis, zAxis, angle)) - orientation; - transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); + SetRotation(angle, glm::vec3(xAxis, yAxis, zAxis)); } void Transformable::SetRotation(float angle, const float* axis) + { + SetRotation(angle, glm::make_vec3(axis)); + } + + void Transformable::SetRotation(float angle, const glm::vec3& axis) { glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::quat(glm::vec4(axis[0], axis[1], axis[2], angle)) - orientation; - transformation = glm::rotate(transformation, orientation.w, glm::vec3(orientation.x, orientation.y, orientation.z)); + orientation = glm::angleAxis(glm::radians(angle), axis) * (-orientation); + transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation)); } void Transformable::SetScale(float scaleX, float scaleY, float scaleZ) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - scale = glm::vec3(scaleX, scaleY, scaleZ) - scale; - transformation = glm::scale(transformation, scale); + SetScale(glm::vec3(scaleX, scaleY, scaleZ)); } void Transformable::SetScale(const float* scale) + { + SetScale(glm::make_vec3(scale)); + } + + void Transformable::SetScale(const glm::vec3& scale) { glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); - this->scale = glm::make_vec3(scale) - this->scale; + this->scale = scale / this->scale; + if (this->scale.x == INFINITY) this->scale.x = scale.x; + if (this->scale.y == INFINITY) this->scale.y = scale.y; + if (this->scale.z == INFINITY) this->scale.z = scale.z; transformation = glm::scale(transformation, this->scale); } void Transformable::Move(float x, float y, float z) { - transformation = glm::translate(transformation, glm::vec3(x, y, z)); - glm::decompose(transformation, scale, orientation, translation, skew, perspective); + Move(glm::vec3(x, y, z)); } void Transformable::Move(const float* translation) { - transformation = glm::translate(transformation, glm::make_vec3(translation)); + Move(glm::make_vec3(translation)); + } + + void Transformable::Move(const glm::vec3& translation) + { + transformation = glm::translate(transformation, translation); glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); } void Transformable::Rotate(float rotX, float rotY, float rotZ) { - transformation = glm::rotate(transformation, glm::radians(1.0f), glm::vec3(rotX, rotY, rotZ)); - glm::decompose(transformation, scale, orientation, translation, skew, perspective); + Rotate(glm::vec3(rotX, rotY, rotZ)); } void Transformable::Rotate(const float* rotation) { - transformation = glm::rotate(transformation, 1.0f, glm::make_vec3(rotation)); + Rotate(glm::make_vec3(rotation)); + } + + void Transformable::Rotate(const glm::vec3& rotation) + { + glm::quat rot = glm::quat(glm::radians(rotation)); + transformation = glm::rotate(transformation, glm::angle(rot), glm::axis(rot)); glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis) { - transformation = glm::rotate(transformation, angle, glm::vec3(xAxis, yAxis, zAxis)); - glm::decompose(transformation, scale, orientation, translation, skew, perspective); + Rotate(angle, glm::vec3(xAxis, yAxis, zAxis)); } void Transformable::Rotate(float angle, const float* axis) { - transformation = glm::rotate(transformation, angle, glm::make_vec3(axis)); + Rotate(angle, glm::make_vec3(axis)); + } + + void Transformable::Rotate(float angle, const glm::vec3& axis) + { + transformation = glm::rotate(transformation, glm::radians(angle), axis); glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Transformable::Scale(float scaleX, float scaleY, float scaleZ) { - transformation = glm::scale(transformation, glm::vec3(scaleX, scaleY, scaleZ)); - glm::decompose(transformation, scale, orientation, translation, skew, perspective); + Scale(glm::vec3(scaleX, scaleY, scaleZ)); } void Transformable::Scale(const float* scale) { - transformation = glm::scale(transformation, glm::make_vec3(scale)); + Scale(glm::make_vec3(scale)); + } + + void Transformable::Scale(const glm::vec3& scale) + { + transformation = glm::scale(transformation, scale); glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); } From d74c49fed50c5ce9201153215af2cf0ef7cc032b Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 23 Jan 2021 16:43:59 +0100 Subject: [PATCH 3/3] i forgot --- examples/model_loading/main.cpp | 2 +- include/camera.hpp | 12 ++++++++++-- include/transformable.hpp | 8 ++++---- src/camera.cpp | 11 ++++++++--- src/shader.cpp | 4 +++- src/transformable.cpp | 16 ++++++++-------- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/examples/model_loading/main.cpp b/examples/model_loading/main.cpp index 1db6552..e53cfe8 100644 --- a/examples/model_loading/main.cpp +++ b/examples/model_loading/main.cpp @@ -88,7 +88,7 @@ int main(int argc, char** argv) shader->Use(); shader->SetUniform("model", utah); - shader->SetUniformMatrix4fv("view", 1, GL_FALSE, camera.GetMatrix()); + shader->SetUniform("view", camera); shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix()); oglu::PolygonMode(GL_FRONT_AND_BACK, GL_LINE); diff --git a/include/camera.hpp b/include/camera.hpp index 78cd480..e5e51e1 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -78,6 +78,16 @@ namespace oglu */ void LookAt(const GLfloat* target); + /** + * @brief Have camera face at a certain position. + * + * This will adjust the camera's rotation in order to put the + * specified coordinate at the center of the screen. + * + * @param[in] target 3D vector with the target position + */ + void LookAt(const glm::vec3& target); + /** * @brief Have camera face at a certain position. * @@ -88,8 +98,6 @@ namespace oglu */ void LookAt(const Transformable& target); - // void Move(float x, float y, float z) override; - /** * @brief Gets the projection matrix of the camera. * diff --git a/include/transformable.hpp b/include/transformable.hpp index 17d389f..c2e1a59 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -307,28 +307,28 @@ namespace oglu * * @return An array of 16 floats representing the transformation matrix */ - const float* GetMatrix(); + virtual const glm::mat4& GetMatrix(); /** * @brief Get position as a 3D vector. * * @returns A pointer to an array of floats */ - const float* GetPosition() const; + virtual const glm::vec3& GetPosition() const; /** * @brief Get rotation as a matrix. * * @returns A pointer to a 4x4 matrix */ - const float* GetRotation() const; + virtual const glm::quat& GetRotation() const; /** * @brief Get scaling as a 3D vector. * * @returns A pointer to an array of floats */ - const float* GetScaling() const; + virtual const glm::vec3& GetScaling() const; protected: // TODO: Separate translation, rotation and scaling matrices. diff --git a/src/camera.cpp b/src/camera.cpp index 7017326..107d03a 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -57,13 +57,18 @@ namespace oglu void Camera::LookAt(GLfloat x, GLfloat y, GLfloat z) { - transformation = glm::lookAt(translation, glm::vec3(x, y, z), glm::vec3(0.0f, 1.0f, 0.0f)); - glm::decompose(transformation, scale, orientation, translation, skew, perspective); + LookAt(glm::vec3(x, y, z)); } void Camera::LookAt(const GLfloat* target) { - LookAt(target[0], target[1], target[2]); + LookAt(glm::make_vec3(target)); + } + + void Camera::LookAt(const glm::vec3& target) + { + transformation = glm::lookAt(translation, target, glm::vec3(0.0f, 1.0f, 0.0f)); + glm::decompose(transformation, scale, orientation, translation, skew, perspective); } void Camera::LookAt(const Transformable& target) diff --git a/src/shader.cpp b/src/shader.cpp index 5a19faa..03f88de 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -9,6 +9,8 @@ #include #include +#include + namespace oglu { AbstractShader::AbstractShader(const AbstractShader& other) : @@ -254,7 +256,7 @@ namespace oglu void AbstractShader::SetUniform(GLint location, Transformable& v0, GLboolean transpose) { - glUniformMatrix4fv(location, 1, transpose, v0.GetMatrix()); + glUniformMatrix4fv(location, 1, transpose, glm::value_ptr(v0.GetMatrix())); } void AbstractShader::SetUniform1fv(const GLchar* name, GLsizei count, const GLfloat* value) diff --git a/src/transformable.cpp b/src/transformable.cpp index f604543..18c9be1 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -161,23 +161,23 @@ namespace oglu glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); } - const float* Transformable::GetMatrix() + const glm::mat4& Transformable::GetMatrix() { - return glm::value_ptr(transformation); + return transformation; } - const float* Transformable::GetPosition() const + const glm::vec3& Transformable::GetPosition() const { - return glm::value_ptr(translation); + return translation; } - const float* Transformable::GetRotation() const + const glm::quat& Transformable::GetRotation() const { - return glm::value_ptr(orientation); + return orientation; } - const float* Transformable::GetScaling() const + const glm::vec3& Transformable::GetScaling() const { - return glm::value_ptr(scale); + return scale; } }