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); }