Skip to content

Commit

Permalink
feat(transform): unify components
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Oct 8, 2023
1 parent 9227231 commit da9946d
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 203 deletions.
5 changes: 1 addition & 4 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ set(CUBOS_ENGINE_SOURCE
"src/cubos/engine/tools/scene_editor/plugin.cpp"

"src/cubos/engine/transform/plugin.cpp"
"src/cubos/engine/transform/local_to_world.cpp"
"src/cubos/engine/transform/position.cpp"
"src/cubos/engine/transform/rotation.cpp"
"src/cubos/engine/transform/scale.cpp"
"src/cubos/engine/transform/transform.cpp"

"src/cubos/engine/assets/plugin.cpp"
"src/cubos/engine/assets/assets.cpp"
Expand Down
31 changes: 0 additions & 31 deletions engine/include/cubos/engine/transform/local_to_world.hpp

This file was deleted.

24 changes: 3 additions & 21 deletions engine/include/cubos/engine/transform/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,16 @@
#pragma once

#include <cubos/engine/cubos.hpp>
#include <cubos/engine/transform/local_to_world.hpp>
#include <cubos/engine/transform/position.hpp>
#include <cubos/engine/transform/rotation.hpp>
#include <cubos/engine/transform/scale.hpp>
#include <cubos/engine/transform/transform.hpp>

namespace cubos::engine
{
/// @defgroup transform-plugin Transform
/// @ingroup engine
/// @brief Adds transform components which assign positions, rotations and scaling to entities.
///
/// This plugin operates on entities with a @ref LocalToWorld component and any combination of
/// the @ref Position, @ref Rotation and @ref Scale components. For example, if you have an
/// entity which doesn't need rotation, but has a position and a scale, you do not need to add
/// the @ref Rotation component, and its transform will still be updated.
///
/// @note Any entity with either a @ref Position, @ref Rotation or @ref Scale component
/// automatically gets a @ref LocalToWorld component.
/// @brief Adds transform components.
///
/// ## Components
/// - @ref LocalToWorld - holds the local to world transform matrix.
/// - @ref Position - holds the position of an entity.
/// - @ref Rotation - holds the rotation of an entity.
/// - @ref Scale - holds the scaling of an entity.
///
/// ## Tags
/// - `cubos.transform.update` - the @ref LocalToWorld components are updated with the
/// information from the @ref Position, @ref Rotation and @ref Scale components.
/// - @ref Transform - Component which stores the transformation matrix of an entity.

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
Expand Down
22 changes: 0 additions & 22 deletions engine/include/cubos/engine/transform/position.hpp

This file was deleted.

22 changes: 0 additions & 22 deletions engine/include/cubos/engine/transform/rotation.hpp

This file was deleted.

20 changes: 0 additions & 20 deletions engine/include/cubos/engine/transform/scale.hpp

This file was deleted.

68 changes: 68 additions & 0 deletions engine/include/cubos/engine/transform/transform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// @file
/// @brief Component @ref cubos::engine::Transform.
/// @ingroup transform-plugin

#pragma once

#include <cubos/core/reflection/reflect.hpp>

namespace cubos::engine
{
/// @brief Component which stores the transformation matrix of an entity.
///
/// @ingroup transform-plugin
struct [[cubos::component("cubos/transform", VecStorage)]] Transform
{
CUBOS_REFLECT;

glm::mat4 mat = glm::mat4(1.0F); ///< Local to world space matrix.

/// @brief Get the transformation matrix from local to world space.
/// @return The transformation matrix from local to world space.
glm::mat4 localToWorld() const;

/// @brief Get the position of the transform.
/// @return The position of the transform.
glm::vec3 position() const;

/// @brief Set the position of the transform.
/// @param pos The new position.
void position(const glm::vec3& pos);

/// @brief Translate the transform.
/// @param delta The translation vector.
void translate(const glm::vec3& delta);

/// @brief Get the rotation of the transform.
/// @return The rotation of the transform.
glm::quat rotation() const;

/// @brief Get the rotation of the transform as euler angles.
/// @return The rotation of the transform as euler angles.
glm::vec3 eulerRotation() const;

/// @brief Set the rotation of the transform.
/// @param rot The new rotation.
void rotation(const glm::quat& rot);

/// @brief Set the rotation of the transform.
/// @param euler The new rotation as euler angles.
void rotation(const glm::vec3& euler);

/// @brief Rotate the transform.
/// @param delta The rotation quaternion.
void rotate(const glm::quat& delta);

/// @brief Rotate the transform.
/// @param eulerDelta The rotation as euler angles.
void rotate(const glm::vec3& eulerDelta);

/// @brief Get the scale of the transform.
/// @return The scale of the transform.
float scale() const;

/// @brief Set the scale of the transform.
/// @param scale The new scale.
void scale(float scale);
};
} // namespace cubos::engine
11 changes: 0 additions & 11 deletions engine/src/cubos/engine/transform/local_to_world.cpp

This file was deleted.

45 changes: 4 additions & 41 deletions engine/src/cubos/engine/transform/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,15 @@
#include <cubos/engine/transform/plugin.hpp>

using cubos::core::ecs::Commands;
using cubos::core::ecs::OptRead;
using cubos::core::ecs::Query;
using cubos::core::ecs::Write;
using namespace cubos::engine;

static void autoLocalToWorld(Commands cmds,
Query<OptRead<LocalToWorld>, OptRead<Position>, OptRead<Rotation>, OptRead<Scale>> query)
static void updateTransform()
{
for (auto [entity, localToWorld, position, rotation, scale] : query)
{
if (!localToWorld && (position || rotation || scale))
{
cmds.add(entity, LocalToWorld{});
}
}
}

static void applyTransform(Query<Write<LocalToWorld>, OptRead<Position>, OptRead<Rotation>, OptRead<Scale>> query)
{
for (auto [entity, localToWorld, position, rotation, scale] : query)
{
localToWorld->mat = glm::mat4(1.0F);
if (position)
{
localToWorld->mat = glm::translate(localToWorld->mat, position->vec);
}

if (rotation)
{
localToWorld->mat *= glm::toMat4(rotation->quat);
}

if (scale)
{
localToWorld->mat = glm::scale(localToWorld->mat, glm::vec3(scale->factor));
}
}
// In the future, parent-child relationships will be handled here.
}

void cubos::engine::transformPlugin(Cubos& cubos)
{
cubos.addComponent<Position>();
cubos.addComponent<Rotation>();
cubos.addComponent<Scale>();
cubos.addComponent<LocalToWorld>();
cubos.addComponent<Transform>();

cubos.system(autoLocalToWorld).before("cubos.transform.update");
cubos.system(applyTransform).tagged("cubos.transform.update");
cubos.system(updateTransform).tagged("cubos.transform.update");
}
11 changes: 0 additions & 11 deletions engine/src/cubos/engine/transform/position.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions engine/src/cubos/engine/transform/rotation.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions engine/src/cubos/engine/transform/scale.cpp

This file was deleted.

74 changes: 74 additions & 0 deletions engine/src/cubos/engine/transform/transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <cubos/core/ecs/component/reflection.hpp>
#include <cubos/core/geom/decomposition.hpp>
#include <cubos/core/reflection/external/glm.hpp>

#include <cubos/engine/transform/transform.hpp>

using namespace cubos::engine;

glm::mat4 Transform::localToWorld() const
{
return mat;
}

glm::vec3 Transform::position() const
{
return core::geom::position(mat);
}

void Transform::position(const glm::vec3& pos)
{
core::geom::position(mat, pos);
}

void Transform::translate(const glm::vec3& delta)
{
core::geom::translate(mat, delta);
}

glm::quat Transform::rotation() const
{
return core::geom::rotation(mat);
}

glm::vec3 Transform::eulerRotation() const
{
return core::geom::eulerRotation(mat);
}

void Transform::rotation(const glm::quat& rot)
{
core::geom::rotation(mat, rot);
}

void Transform::rotation(const glm::vec3& euler)
{
core::geom::rotation(mat, euler);
}

void Transform::rotate(const glm::quat& delta)
{
core::geom::rotate(mat, delta);
}

void Transform::rotate(const glm::vec3& eulerDelta)
{
core::geom::rotate(mat, eulerDelta);
}

float Transform::scale() const
{
return core::geom::scale(mat);
}

void Transform::scale(float scale)
{
core::geom::scale(mat, scale);
}

CUBOS_REFLECT_IMPL(cubos::engine::Transform)
{
return core::ecs::ComponentTypeBuilder<Transform>("cubos::engine::Transform")
.withField("mat", &Transform::mat)
.build();
}

0 comments on commit da9946d

Please sign in to comment.