Skip to content

Commit

Permalink
Add scale and rotate to Model3D
Browse files Browse the repository at this point in the history
The model now can scale and rotate in 3D space.
  • Loading branch information
Notiooo committed Nov 30, 2023
1 parent 740bdef commit 756e56c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
59 changes: 54 additions & 5 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
52 changes: 52 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "Core/Mesh.h"
#include "Utils/Rotation3D.h"

#include <Renderer/Renderer.h>
#include <Resources/ObjLoader.h>
Expand Down Expand Up @@ -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
Expand All @@ -62,4 +111,7 @@ class Model
ObjLoader mObjLoader;
std::unique_ptr<Mesh> mMesh;
glm::vec3 mPosition{0, 0, 0};
glm::vec3 mRotationOrigin{0, 0, 0};
float mScale{1};
Rotation3D mRotation;
};

0 comments on commit 756e56c

Please sign in to comment.