From 83b5453088a64710db323aeb363f291174f6d9ad Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 23 Jan 2021 19:11:55 +0100 Subject: [PATCH] I think transformations should somewhat work now --- examples/debug/main.cpp | 21 ++++-- examples/model_loading/main.cpp | 10 +-- include/camera.hpp | 118 -------------------------------- include/openglu.hpp | 1 - include/transformable.hpp | 1 + src/camera.cpp | 83 ---------------------- src/transformable.cpp | 53 +++++++------- 7 files changed, 47 insertions(+), 240 deletions(-) delete mode 100644 include/camera.hpp delete mode 100644 src/camera.cpp diff --git a/examples/debug/main.cpp b/examples/debug/main.cpp index cfdad20..8d412a9 100644 --- a/examples/debug/main.cpp +++ b/examples/debug/main.cpp @@ -70,6 +70,8 @@ int main(int argc, char** argv) square.Move(-0.6f, 0.0f, 0.0f); square2.Move(0.6f, 0.0f, 0.0f); + square.Rotate(0.0f, 0.0f, 45.0f); + // Create a shader oglu::Shader shader; try @@ -86,10 +88,14 @@ int main(int argc, char** argv) oglu::Texture crate = oglu::MakeTexture("assets/crate.jpg"); oglu::Texture opengl = oglu::MakeTexture("assets/opengl.png"); - oglu::Camera camera; - camera.Move(0.0f, 0.0f, -5.0f); + glm::mat4 view(1.0f); + view = glm::translate(view, glm::vec3(0.0f, 0.0f, -5.0f)); + + glm::mat4 projection(1.0f); + projection = glm::perspective(45.0f, 1.0f, 0.1f, 100.0f); // Window loop + oglu::Enable(GL_DEPTH_TEST); float t = 0.0f; while (!glfwWindowShouldClose(window)) { @@ -97,16 +103,17 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); - square.Rotate(6.0f, 0.0f, 0.0f); - square2.Rotate(-6.0f, 0.0f, 0.0f); - camera.Rotate(0.0f, 1.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); + // camera.Pan(1.f); shader->Use(); shader->SetUniform("texture1", crate, 0); shader->SetUniform("texture2", opengl, 1); shader->SetUniform("model", square); - shader->SetUniform("view", camera); - shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix()); + shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(view)); + shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(projection)); square.Render(); diff --git a/examples/model_loading/main.cpp b/examples/model_loading/main.cpp index e53cfe8..653b471 100644 --- a/examples/model_loading/main.cpp +++ b/examples/model_loading/main.cpp @@ -71,9 +71,9 @@ 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))); + // 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; @@ -88,8 +88,8 @@ int main(int argc, char** argv) shader->Use(); shader->SetUniform("model", utah); - shader->SetUniform("view", camera); - shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix()); + //shader->SetUniform("view", camera); + //shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, camera.GetProjectionMatrix()); oglu::PolygonMode(GL_FRONT_AND_BACK, GL_LINE); utah.Render(); diff --git a/include/camera.hpp b/include/camera.hpp deleted file mode 100644 index e5e51e1..0000000 --- a/include/camera.hpp +++ /dev/null @@ -1,118 +0,0 @@ -/*****************************************************************//** - * \file camera.hpp - * \brief Contains anything relevant to cameras - * - * \author Lauchmelder - * \date January 2021 - *********************************************************************/ -#ifndef CAMERA_HPP -#define CAMERA_HPP - -#include -#include - -namespace oglu -{ - /** - * @brief A camera object in 3D space. - * - * This class unites a world- and projection matrix. - */ - class OGLU_API Camera : public Transformable - { - public: - /** - * @brief Create a default camera. - * - * Sits at the origin, looks in negative z-direction. - * FOV: 45.0° - * Aspect ratio: Matches the viewport - * zNear: 0.1f - * zFar: 100.0f - */ - Camera(); - - /** - * @brief Copy another camera. - * - * Every property of @p other is copied. - * - * @param[in] other The camera to copy from - */ - Camera(const Camera& other); - - /** - * @brief Create a new camera. - * - * Sits at the origin, looks in negative z-direction. - * The other properties are set from the parameters - * - * @param[in] fov The FOV (field of view) of the camera - * @param[in] aspectRatio The aspect ratio of the camera. (Setting this to 0.f will use your viewport's aspect ratio) - * @param[in] zNear Near z clipping plane - * @param[in] zNear Far z clipping plane - */ - Camera(float fov, float aspectRatio, float zNear, float zFar); - - ~Camera(); - - /** - * @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] x Target x coordinate - * @param[in] y Target y coordinate - * @param[in] z Target z coordinate - */ - void LookAt(GLfloat x, GLfloat y, GLfloat z); - - /** - * @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 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. - * - * This will adjust the camera's rotation in order to put the - * specified coordinate at the center of the screen. - * - * @param[in] target An object to target - */ - void LookAt(const Transformable& target); - - /** - * @brief Gets the projection matrix of the camera. - * - * @returns a 4x4 projection matrix. - */ - const float* GetProjectionMatrix(); - - private: - float aspectRatio; ///< Aspect ration of the camera - float fov; ///< FOV of the camera - float zNear; ///< Near z clipping plane - float zFar; ///< Far z clipping plane - - float* projection; - }; -} - -#endif \ No newline at end of file diff --git a/include/openglu.hpp b/include/openglu.hpp index 46c93bd..51a5840 100644 --- a/include/openglu.hpp +++ b/include/openglu.hpp @@ -13,7 +13,6 @@ #include #include #include -#include namespace oglu { diff --git a/include/transformable.hpp b/include/transformable.hpp index c2e1a59..33db5c6 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -334,6 +334,7 @@ namespace oglu // TODO: Separate translation, rotation and scaling matrices. // Combine them only when the user wants the transformation matrix glm::mat4 transformation; + bool recalculateMatrix; glm::vec3 scale; glm::quat orientation; diff --git a/src/camera.cpp b/src/camera.cpp deleted file mode 100644 index 107d03a..0000000 --- a/src/camera.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "camera.hpp" - -#include - -#include -#include -#include -#include -#include - -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)), - 16 * sizeof(float) - ); - } - - Camera::Camera(const Camera& other) : - fov(other.fov), aspectRatio(other.aspectRatio), zNear(other.zNear), zFar(other.zFar), projection(new float[16]{ 0.0f }) - { - memcpy( - projection, - other.projection, - 16 * sizeof(float) - ); - } - - Camera::Camera(float fov, float aspectRatio, float zNear, float zFar) : - fov(fov), aspectRatio(aspectRatio), zNear(zNear), zFar(zFar), projection(new float[16]{ 0.0f }) - { - if (aspectRatio == 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)), - 16 * sizeof(float) - ); - } - - Camera::~Camera() - { - } - - void Camera::LookAt(GLfloat x, GLfloat y, GLfloat z) - { - LookAt(glm::vec3(x, y, z)); - } - - void Camera::LookAt(const GLfloat* target) - { - 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) - { - LookAt(target.GetPosition()); - } - - const float* Camera::GetProjectionMatrix() - { - return projection; - } -} diff --git a/src/transformable.cpp b/src/transformable.cpp index 18c9be1..d8b1e96 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -10,13 +10,13 @@ namespace oglu { oglu::Transformable::Transformable() : - transformation(glm::mat4(1.0f)) + transformation(glm::mat4(1.0f)), recalculateMatrix(false) { glm::decompose(transformation, scale, orientation, translation, skew, perspective); } Transformable::Transformable(const Transformable& other) : - transformation(other.transformation) + transformation(other.transformation), recalculateMatrix(false) { glm::decompose(transformation, scale, orientation, translation, skew, perspective); } @@ -37,9 +37,8 @@ namespace oglu void Transformable::SetPosition(const glm::vec3& position) { - glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); - this->translation = translation - this->translation; - transformation = glm::translate(transformation, this->translation); + translation = position; + recalculateMatrix = true; } void Transformable::SetRotation(float rotX, float rotY, float rotZ) @@ -54,9 +53,8 @@ namespace oglu void Transformable::SetRotation(const glm::vec3& rotation) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::quat(glm::radians(rotation)) * (-orientation); - transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation)); + orientation = glm::quat(glm::radians(rotation)); + recalculateMatrix = true; } void Transformable::SetRotation(float angle, float xAxis, float yAxis, float zAxis) @@ -71,9 +69,8 @@ namespace oglu void Transformable::SetRotation(float angle, const glm::vec3& axis) { - glm::decompose(transformation, scale, orientation, translation, skew, perspective); - orientation = glm::angleAxis(glm::radians(angle), axis) * (-orientation); - transformation = glm::rotate(transformation, glm::angle(orientation), glm::axis(orientation)); + orientation = glm::angleAxis(glm::radians(angle), axis); + recalculateMatrix = true; } void Transformable::SetScale(float scaleX, float scaleY, float scaleZ) @@ -88,12 +85,8 @@ namespace oglu void Transformable::SetScale(const glm::vec3& scale) { - glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); - 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); + this->scale = scale; + recalculateMatrix = true; } void Transformable::Move(float x, float y, float z) @@ -108,8 +101,8 @@ namespace oglu void Transformable::Move(const glm::vec3& translation) { - transformation = glm::translate(transformation, translation); - glm::decompose(transformation, scale, orientation, this->translation, skew, perspective); + this->translation += translation; + recalculateMatrix = true; } void Transformable::Rotate(float rotX, float rotY, float rotZ) @@ -124,9 +117,8 @@ namespace oglu 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); + orientation *= glm::quat(glm::radians(rotation)); + recalculateMatrix = true; } void Transformable::Rotate(float angle, float xAxis, float yAxis, float zAxis) @@ -141,8 +133,8 @@ namespace oglu 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); + orientation *= glm::angleAxis(glm::radians(angle), axis); + recalculateMatrix = true; } void Transformable::Scale(float scaleX, float scaleY, float scaleZ) @@ -157,12 +149,21 @@ namespace oglu void Transformable::Scale(const glm::vec3& scale) { - transformation = glm::scale(transformation, scale); - glm::decompose(transformation, this->scale, orientation, translation, skew, perspective); + this->scale += scale; + recalculateMatrix = true; } const glm::mat4& Transformable::GetMatrix() { + if (recalculateMatrix) + { + transformation = glm::mat4(1.0f); + transformation = glm::translate(transformation, translation); + transformation *= glm::toMat4(orientation); + transformation = glm::scale(transformation, scale); + + recalculateMatrix = false; + } return transformation; }