Skip to content

Commit

Permalink
Add rotation & scale to transformation class
Browse files Browse the repository at this point in the history
  • Loading branch information
douze committed Dec 6, 2020
1 parent 527d4c6 commit 3eed174
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main() {
// Add terrain
scene::Node terrainNode{std::make_unique<mesh::Terrain>(mesh::Terrain{}),
std::make_unique<material::TerrainMaterial>(material::TerrainMaterial{}),
mesh::Transformation{glm::vec3{0.0f, 0.0f, 0.0f}}};
mesh::Transformation{glm::vec3{0.0f}, glm::vec3{0.0f}, glm::vec3{2.0f}}};
root.add_child(terrainNode);

// Assign main camera
Expand Down
16 changes: 10 additions & 6 deletions renderer/include/transformation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ class Transformation {
/**
* @brief Create a transformation.
* @param position of the mesh
* @param rotation of the mesh
* @param scale of the mesh
*/
explicit Transformation(const glm::vec3& position) noexcept;
explicit Transformation(const glm::vec3& position, const glm::vec3& rotation = glm::vec3{0.0f},
const glm::vec3& scale = glm::vec3{1.0f}) noexcept;

/**
* @return the computed transformation matrix.
*/
glm::mat4 get_matrix();

/**
* @return the position.
*/
const glm::vec3& get_position() const { return position; }

private:
/** Position of the mesh */
glm::vec3 position;

/** Rotation of the mesh */
glm::vec3 rotation;

/** Scale of the mesh */
glm::vec3 scale;

/** Transformation matrix */
glm::mat4 matrix;

Expand Down
8 changes: 6 additions & 2 deletions renderer/src/transformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

using namespace odo::mesh;

Transformation::Transformation(const glm::vec3& position) noexcept
: position{position}, dirty{true} {}
Transformation::Transformation(const glm::vec3& position, const glm::vec3& rotation, const glm::vec3& scale) noexcept
: position{position}, rotation{rotation}, scale{scale}, dirty{true} {}

glm::mat4 Transformation::get_matrix() {
if (dirty) {
dirty = false;
matrix = glm::mat4(1.0f);
matrix = glm::translate(matrix, position);
matrix = glm::rotate(matrix, glm::radians(rotation.x), glm::vec3{1, 0, 0});
matrix = glm::rotate(matrix, glm::radians(rotation.y), glm::vec3{0, 1, 0});
matrix = glm::rotate(matrix, glm::radians(rotation.z), glm::vec3{0, 0, 1});
matrix = glm::scale(matrix, scale);
}
return matrix;
}

0 comments on commit 3eed174

Please sign in to comment.