diff --git a/AimGL/src/Renderer/Graphics/3D/Model.cpp b/AimGL/src/Renderer/Graphics/3D/Model.cpp index d5299ae..16a0a2d 100644 --- a/AimGL/src/Renderer/Graphics/3D/Model.cpp +++ b/AimGL/src/Renderer/Graphics/3D/Model.cpp @@ -31,19 +31,68 @@ void Model::setPosition(const glm::vec3& newPosition, Origin origin) switch (origin) { case Origin::Center: break; - case Origin::LeftBottom: mPosition += mObjLoader.dimensions() / 2.f; break; - case Origin::CenterBottom: mPosition.y += mObjLoader.dimensions().y / 2.f; break; + case Origin::LeftBottom: mPosition += dimensions() / 2.f; break; + case Origin::CenterBottom: mPosition.y += dimensions().y / 2.f; break; } updateModel(); } +void Model::setScale(float scale) +{ + mScale = scale; + updateModel(); +} + +Rotation3D Model::rotation() const +{ + return mRotation; +} + +void Model::setRotation(const Rotation3D& rotation) +{ + mRotation = rotation; +} + +void Model::resetRotationOrigin() +{ + mRotationOrigin = {0, 0, 0}; +} + +void Model::setRotationOrigin(const glm::vec3& vec) +{ + mRotationOrigin = vec; +} + +void Model::showDebugImGui(std::string name) +{ + name = "[Model] " + name; + ImGui::Begin(name.c_str()); + ImGui::SliderFloat3("Position", &mPosition[0], -50, 50.f); + mRotation.imGuiRotationSlider(); + ImGui::SliderFloat("Scale", &mScale, 0, 4.0f); + ImGui::End(); + updateModel(); +} + +glm::vec3 Model::position() const +{ + return mPosition; +} + +glm::vec3 Model::dimensions() const +{ + return mObjLoader.dimensions() * mScale; +} + void Model::updateModel() { glm::mat4 model = glm::mat4(1.0f); model = glm::translate(model, glm::vec3(mPosition)); - // TODO: Add rotation and scale to the model - // model = glm::rotate(model, glm::radians(mRotation.first), mRotation.second); - // model = glm::scale(model, glm::vec3(mDimensionsNormalized * mScale / 2.f, 1.0f)); + model = glm::translate(model, -mRotationOrigin); + model = mRotation.rotate(model); + model = glm::translate(model, mRotationOrigin); + model = glm::scale(model, glm::vec3(mScale, mScale, mScale)); + mShader.bind(); mShader.setUniform("model", model); mShader.unbind(); diff --git a/AimGL/src/Renderer/Graphics/3D/Model.h b/AimGL/src/Renderer/Graphics/3D/Model.h index 52235ba..75b4398 100644 --- a/AimGL/src/Renderer/Graphics/3D/Model.h +++ b/AimGL/src/Renderer/Graphics/3D/Model.h @@ -1,5 +1,6 @@ #pragma once #include "Core/Mesh.h" +#include "Utils/Rotation3D.h" #include #include @@ -51,6 +52,54 @@ class Model */ void setPosition(const glm::vec3& newPosition, Origin origin = Origin::CenterBottom); + /** + * \brief Scales the model uniformly. + * \param scale The uniform scale factor. + */ + void setScale(float scale); + + /** + * \brief Retrieves the current rotation of the object. + * \return The object's rotation as a Rotation3D instance. + */ + Rotation3D rotation() const; + + /** + * \brief Sets the object's rotation. + * \param rotation The new rotation that the object should take + */ + void setRotation(const Rotation3D& rotation); + + /** + * \brief Resets the model's rotation origin to its default position (0,0,0). + */ + void resetRotationOrigin(); + + /** + * \brief Sets the model's rotation origin. + * \param vec The 3D vector defining the new rotation origin. + */ + void setRotationOrigin(const glm::vec3& vec); + + /** + * \brief Gets the model's current position. + * \return The 3D position vector of the model. + */ + glm::vec3 position() const; + + /** + * \brief Gets the model's dimensions. + * \return The 3D vector representing the model's dimensions. + */ + glm::vec3 dimensions() const; + + /** + * \brief Displays a debug ImGui window that allows to change the internal + * variables of the 3d model. + * \param name Optional name of the model (it can be seen in the window name). + */ + void showDebugImGui(std::string name = ""); + private: /** * \brief Updates the model in the shader @@ -62,4 +111,7 @@ class Model ObjLoader mObjLoader; std::unique_ptr mMesh; glm::vec3 mPosition{0, 0, 0}; + glm::vec3 mRotationOrigin{0, 0, 0}; + float mScale{1}; + Rotation3D mRotation; };