From a5ab0c9adb9645b60c79bfd95ff396ebbf7eddf4 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 22 Jan 2021 19:46:09 +0100 Subject: [PATCH] Finished camera for now --- examples/model_loading/main.cpp | 12 +++++++----- include/camera.hpp | 4 +--- include/transformable.hpp | 8 ++++---- src/camera.cpp | 32 +++++++++++++++++++++++++++++++- src/transformable.cpp | 6 +++--- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/examples/model_loading/main.cpp b/examples/model_loading/main.cpp index ab63ac5..0ec4612 100644 --- a/examples/model_loading/main.cpp +++ b/examples/model_loading/main.cpp @@ -71,10 +71,9 @@ int main(int argc, char** argv) oglu::Enable(GL_DEPTH_TEST); - oglu::Camera camera(60.0f, 0.0f, 0.1f, 100.0f); - camera.Move(0.0f, -4.0f, -10.0f); - //camera.LookAt(utah); - //camera.GetMatrix(); + oglu::Camera camera(45.0f, 1.0f, 0.1f, 100.0f); + + float t = 0.0f; // Window loop while (!glfwWindowShouldClose(window)) @@ -83,7 +82,8 @@ int main(int argc, char** argv) oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, oglu::Color(0.29f, 0.13f, 0.23f)); - utah.Rotate(0.0f, 1.0f, 0.0f); + camera.SetPosition(10.0f * cosf(t), -5.0f, 10.0f * sinf(t)); + camera.LookAt(utah); shader->Use(); shader->SetUniform("model", utah); @@ -95,6 +95,8 @@ int main(int argc, char** argv) glfwSwapBuffers(window); glfwPollEvents(); + + t += 0.01f; } glfwTerminate(); diff --git a/include/camera.hpp b/include/camera.hpp index f5c6567..d87adc8 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -13,8 +13,6 @@ namespace oglu { - class Object; - /** * @brief A camera object in 3D space. * @@ -88,7 +86,7 @@ namespace oglu * * @param[in] target An object to target */ - void LookAt(const Object& target); + void LookAt(const Transformable& target); /** * @brief Gets the projection matrix of the camera. diff --git a/include/transformable.hpp b/include/transformable.hpp index c5137a9..165faf7 100644 --- a/include/transformable.hpp +++ b/include/transformable.hpp @@ -239,23 +239,23 @@ namespace oglu * * @returns A pointer to an array of floats */ - const float* GetPosition(); + const float* GetPosition() const; /** * @brief Get rotation as a matrix. * * @returns A pointer to a 4x4 matrix */ - const float* GetRotation(); + const float* GetRotation() const; /** * @brief Get scaling as a 3D vector. * * @returns A pointer to an array of floats */ - const float* GetScaling(); + const float* GetScaling() const; - private: + protected: // TODO: Separate translation, rotation and scaling matrices. // Combine them only when the user wants the transformation matrix float* position; ///< Position vector diff --git a/src/camera.cpp b/src/camera.cpp index b924a76..4e7ee94 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -4,6 +4,8 @@ #include #include +#include +#include #include namespace oglu @@ -51,14 +53,42 @@ 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; } void Camera::LookAt(const GLfloat* target) { + LookAt(target[0], target[1], target[2]); } - void Camera::LookAt(const Object& target) + void Camera::LookAt(const Transformable& target) { + LookAt(target.GetPosition()); } const float* Camera::GetProjectionMatrix() diff --git a/src/transformable.cpp b/src/transformable.cpp index 844e66a..b12110c 100644 --- a/src/transformable.cpp +++ b/src/transformable.cpp @@ -250,17 +250,17 @@ namespace oglu return transformation; } - const float* Transformable::GetPosition() + const float* Transformable::GetPosition() const { return position; } - const float* Transformable::GetRotation() + const float* Transformable::GetRotation() const { return rotation; } - const float* Transformable::GetScaling() + const float* Transformable::GetScaling() const { return scaling; }