Unravel Engine C++ Reference
Loading...
Searching...
No Matches
transform_component.h
Go to the documentation of this file.
1#pragma once
2
3#include "basic_component.h"
4#include <bitset>
5#include <math/math.h>
6
7namespace unravel
8{
9
10auto is_roots_order_changed() -> bool;
12
17struct root_component : public component_crtp<root_component>
18{
19 static constexpr bool in_place_delete = false;
20
26 static void on_create_component(entt::registry& r, entt::entity e);
27
33 static void on_update_component(entt::registry& r, entt::entity e);
34
40 static void on_destroy_component(entt::registry& r, entt::entity e);
41
42
43
44 uint64_t order{};
45};
46
47struct active_component : public component_crtp<root_component>
48{
49};
50
55class transform_component : public component_crtp<transform_component, owned_component>
56{
57public:
58 using flags_t = std::bitset<8>;
64 static void on_create_component(entt::registry& r, entt::entity e);
65
71 static void on_destroy_component(entt::registry& r, entt::entity e);
72
73 static auto get_top_level_entities(const std::vector<entt::handle>& list) -> std::vector<entt::handle>;
74 static auto get_top_level_entities(const std::vector<entt::handle*>& list) -> std::vector<entt::handle>;
75 static auto is_parent_of(entt::handle parent_to_test, entt::handle child) -> bool;
76 //---------------------------------------------
78 //---------------------------------------------
79 void resolve_transform_global() noexcept;
80
85 auto get_transform_global() const noexcept -> const math::transform&;
86
91 auto set_transform_global(const math::transform& trans) noexcept -> bool;
92 auto set_transform_global_epsilon(const math::transform& trans, float epsilon) noexcept -> bool;
93
98 auto get_transform_local() const noexcept -> const math::transform&;
99
104 void set_transform_local(const math::transform& trans) noexcept;
105
106 //---------------------------------------------
108 //---------------------------------------------
109
114 auto get_position_global() const noexcept -> const math::vec3&;
115
120 void set_position_global(const math::vec3& position) noexcept;
121
126 void move_by_global(const math::vec3& amount) noexcept;
127
131 void reset_position_global() noexcept;
132
137 auto get_position_local() const noexcept -> const math::vec3&;
138
143 void set_position_local(const math::vec3& position) noexcept;
144
149 void move_by_local(const math::vec3& amount) noexcept;
150
154 void reset_position_local() noexcept;
155
156 auto set_position_and_rotation_global(const math::vec3& position,
157 const math::quat& rotation,
158 float epsilon) noexcept -> bool;
159
160 //---------------------------------------------
162 //---------------------------------------------
163
168 auto get_rotation_global() const noexcept -> const math::quat&;
169
174 void set_rotation_global(const math::quat& rotation) noexcept;
175
180 void rotate_by_global(const math::quat& rotation) noexcept;
181
185 void reset_rotation_global() noexcept;
186
191 auto get_rotation_local() const noexcept -> const math::quat&;
192
197 void set_rotation_local(const math::quat& rotation) noexcept;
198
203 void rotate_by_local(const math::quat& rotation) noexcept;
204
208 void reset_rotation_local() noexcept;
209
214 auto get_rotation_euler_global() const noexcept -> math::vec3;
215
220 void set_rotation_euler_global(math::vec3 rotation) noexcept;
221
226 void rotate_by_euler_global(math::vec3 rotation) noexcept;
227
232 auto get_rotation_euler_local() const noexcept -> math::vec3;
233
238 void set_rotation_euler_local(math::vec3 rotation) noexcept;
239
244 void rotate_by_euler_local(math::vec3 rotation) noexcept;
245
251 void rotate_axis_global(float degrees, const math::vec3& axis) noexcept;
252
253 void rotate_around_global(const math::vec3& point, const math::vec3& axis, float degrees);
254
255 void rotate_around_global(const math::vec3& point, const math::quat& rotation);
256
261 void look_at(const math::vec3& point) noexcept;
262 void look_at(const math::vec3& point, const math::vec3& up) noexcept;
263
264 //---------------------------------------------
266 //---------------------------------------------
267
272 auto get_scale_global() const noexcept -> const math::vec3&;
273
278 void set_scale_global(const math::vec3& scale) noexcept;
279
284 void scale_by_global(const math::vec3& scale) noexcept;
285
289 void reset_scale_global() noexcept;
290
295 auto get_scale_local() const noexcept -> const math::vec3&;
296
301 void set_scale_local(const math::vec3& scale) noexcept;
302
307 void scale_by_local(const math::vec3& scale) noexcept;
308
312 void reset_scale_local() noexcept;
313
314 //---------------------------------------------
316 //---------------------------------------------
317
322 auto get_skew_global() const noexcept -> const math::vec3&;
323
328 void set_skew_global(const math::vec3& s) noexcept;
329
334 auto get_skew_local() const noexcept -> const math::vec3&;
335
340 void set_skew_local(const math::vec3& s) noexcept;
341
342 //---------------------------------------------
344 //---------------------------------------------
345
350 auto get_perspective_global() const noexcept -> const math::vec4&;
351
356 void set_perspective_global(const math::vec4& p) noexcept;
357
362 auto get_perspective_local() const noexcept -> const math::vec4&;
363
368 void set_perspective_local(const math::vec4& p) noexcept;
369
370 //---------------------------------------------
372 //---------------------------------------------
373
378 auto get_x_axis_global() const noexcept -> math::vec3;
379
384 auto get_x_axis_local() const noexcept -> math::vec3;
385
390 auto get_y_axis_global() const noexcept -> math::vec3;
391
396 auto get_y_axis_local() const noexcept -> math::vec3;
397
402 auto get_z_axis_global() const noexcept -> math::vec3;
403
408 auto get_z_axis_local() const noexcept -> math::vec3;
409
410 //---------------------------------------------
412 //---------------------------------------------
413
419 auto to_local(const math::vec3& point) const noexcept -> math::vec3;
420
421 //---------------------------------------------
423 //---------------------------------------------
424
429 auto get_parent() const noexcept -> entt::handle;
430
437 auto set_parent(const entt::handle& parent, bool global_stays = true) -> bool;
438
443 auto get_children() const noexcept -> const std::vector<entt::handle>&;
444
449 void set_children(const std::vector<entt::handle>& children);
450
454 void sort_children() noexcept;
455
460 void set_active(bool active) noexcept;
461
466 auto is_active() const noexcept -> bool;
467
468 auto is_active_global() const noexcept -> bool;
469
474 void set_dirty(bool dirty) noexcept;
475
480 auto is_dirty() const noexcept -> bool;
481
487 void set_dirty(uint8_t id, bool dirty) noexcept;
488
494 auto is_dirty(uint8_t id) const noexcept -> bool;
495
500
501private:
506 void set_owner(entt::handle owner);
507
512 void apply_transform(const math::transform& tr) noexcept;
513
519 auto inverse_parent_transform(const entt::handle& parent) noexcept -> math::transform;
520
525 void on_dirty_transform(bool dirty) noexcept;
526 void on_dirty_flags(bool dirty) noexcept;
527 void on_flags_changed(flags_t flags);
528 auto get_flags_global() const noexcept -> flags_t;
533 auto resolve_global_value_transform() const noexcept -> math::transform;
534 auto resolve_global_value_flags() const noexcept -> flags_t;
535
541 void attach_child(const entt::handle& child, transform_component& child_transform);
542
549 auto remove_child(const entt::handle& child, transform_component& child_transform) -> bool;
550
552 int32_t sort_index_{-1};
553
555 entt::handle parent_{};
557 std::vector<entt::handle> children_{};
558
563 template<typename T,
564 typename Owner,
565 void (Owner::*on_dirty)(bool),
566 T (Owner::*resolve_global_value)() const,
567 bool auto_resolve>
568 struct local_global_property
569 {
576 auto set_value(Owner* owner, const T& val) noexcept -> bool
577 {
578 local = val;
579
580 set_dirty(owner, true);
581
582 return true;
583 }
584
590 auto get_value(const Owner* owner) const noexcept -> const T&
591 {
592 return local;
593 }
594
600 auto value(Owner* owner) noexcept -> T&
601 {
602 set_dirty(owner, true);
603
604 return local;
605 }
606
612 void set_dirty(Owner* owner, bool flag) const noexcept
613 {
614 if(dirty == flag)
615 {
616 return;
617 }
618 dirty = flag;
619
620 if(dirty)
621 {
622 assert(owner);
623 (owner->*on_dirty)(flag);
624 }
625 }
626
632 auto get_global_value(const Owner* owner, bool force) const noexcept -> const T&
633 {
634 if(force || auto_resolve && dirty)
635 {
636 assert(owner);
637 global = (owner->*resolve_global_value)();
638
639 set_dirty(const_cast<Owner*>(owner), false);
640 }
641
642 return global;
643 }
644
645 auto has_auto_resolve() const noexcept -> bool
646 {
647 return auto_resolve;
648 }
649
651 T local{};
653 mutable T global{};
655 mutable bool dirty{true};
656 };
657
662 using property_transform = local_global_property<math::transform,
663 transform_component,
664 &transform_component::on_dirty_transform,
665 &transform_component::resolve_global_value_transform,
666 true>;
667
668 using property_flags = local_global_property<flags_t,
669 transform_component,
670 &transform_component::on_dirty_flags,
671 &transform_component::resolve_global_value_flags,
672 true>;
673
674 enum flags_types
675 {
676 active,
677 count = 8,
678 };
679
681 property_transform transform_{};
682
683 property_flags flags_{};
685 std::bitset<32> transform_dirty_{};
686};
687
688} // namespace unravel
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
void rotate_around_global(const math::vec3 &point, const math::vec3 &axis, float degrees)
auto is_dirty() const noexcept -> bool
Checks if the component is dirty.
auto get_scale_local() const noexcept -> const math::vec3 &
Gets the local scale.
void set_perspective_global(const math::vec4 &p) noexcept
Sets the global perspective.
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
auto get_rotation_local() const noexcept -> const math::quat &
Gets the local rotation.
void reset_position_global() noexcept
Resets the global position to the origin.
static auto is_parent_of(entt::handle parent_to_test, entt::handle child) -> bool
void reset_scale_local() noexcept
Resets the local scale to the default value.
auto get_position_local() const noexcept -> const math::vec3 &
Gets the local position.
void scale_by_global(const math::vec3 &scale) noexcept
Scales the component by a specified amount globally.
void reset_scale_global() noexcept
Resets the global scale to the default value.
void set_position_local(const math::vec3 &position) noexcept
Sets the local position.
void look_at(const math::vec3 &point) noexcept
Orients the component to look at a specified point.
auto set_transform_global_epsilon(const math::transform &trans, float epsilon) noexcept -> bool
auto get_position_global() const noexcept -> const math::vec3 &
TRANSLATION.
auto set_position_and_rotation_global(const math::vec3 &position, const math::quat &rotation, float epsilon) noexcept -> bool
auto get_rotation_euler_global() const noexcept -> math::vec3
Gets the global rotation in Euler angles.
void rotate_by_euler_global(math::vec3 rotation) noexcept
Rotates the component by a specified amount in Euler angles globally.
auto get_z_axis_local() const noexcept -> math::vec3
Gets the local Z-axis.
void rotate_axis_global(float degrees, const math::vec3 &axis) noexcept
Rotates the component around a specified axis globally.
auto get_rotation_euler_local() const noexcept -> math::vec3
Gets the local rotation in Euler angles.
void set_rotation_euler_global(math::vec3 rotation) noexcept
Sets the global rotation in Euler angles.
void set_active(bool active) noexcept
Sets the active flag.
void set_rotation_global(const math::quat &rotation) noexcept
Sets the global rotation.
void scale_by_local(const math::vec3 &scale) noexcept
Scales the component by a specified amount locally.
void resolve_transform_global() noexcept
TRANSFORMS.
auto is_active_global() const noexcept -> bool
auto get_skew_local() const noexcept -> const math::vec3 &
Gets the local skew.
void set_perspective_local(const math::vec4 &p) noexcept
Sets the local perspective.
void move_by_global(const math::vec3 &amount) noexcept
Moves the component by a specified amount globally.
void sort_children() noexcept
Sorts the child entities.
void _clear_relationships()
Clears the relationships of the component.
static auto get_top_level_entities(const std::vector< entt::handle > &list) -> std::vector< entt::handle >
void set_skew_local(const math::vec3 &s) noexcept
Sets the local skew.
auto set_parent(const entt::handle &parent, bool global_stays=true) -> bool
Sets the parent entity.
auto get_y_axis_global() const noexcept -> math::vec3
Gets the global Y-axis.
void reset_rotation_local() noexcept
Resets the local rotation to the default orientation.
void rotate_by_euler_local(math::vec3 rotation) noexcept
Rotates the component by a specified amount in Euler angles locally.
void reset_position_local() noexcept
Resets the local position to the origin.
void set_children(const std::vector< entt::handle > &children)
Sets the child entities.
void reset_rotation_global() noexcept
Resets the global rotation to the default orientation.
auto get_rotation_global() const noexcept -> const math::quat &
ROTATION.
void move_by_local(const math::vec3 &amount) noexcept
Moves the component by a specified amount locally.
auto set_transform_global(const math::transform &trans) noexcept -> bool
Sets the global transform.
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
void set_scale_global(const math::vec3 &scale) noexcept
Sets the global scale.
auto get_perspective_local() const noexcept -> const math::vec4 &
Gets the local perspective.
auto get_x_axis_local() const noexcept -> math::vec3
Gets the local X-axis.
void set_rotation_euler_local(math::vec3 rotation) noexcept
Sets the local rotation in Euler angles.
auto get_z_axis_global() const noexcept -> math::vec3
Gets the global Z-axis.
void set_position_global(const math::vec3 &position) noexcept
Sets the global position.
auto get_x_axis_global() const noexcept -> math::vec3
BASIS.
void set_transform_local(const math::transform &trans) noexcept
Sets the local transform.
void set_dirty(bool dirty) noexcept
Sets the dirty flag.
auto get_transform_local() const noexcept -> const math::transform &
Gets the local transform.
void set_scale_local(const math::vec3 &scale) noexcept
Sets the local scale.
auto get_transform_global() const noexcept -> const math::transform &
Gets the global transform.
auto get_scale_global() const noexcept -> const math::vec3 &
SCALE.
void rotate_by_local(const math::quat &rotation) noexcept
Rotates the component by a specified amount locally.
auto get_y_axis_local() const noexcept -> math::vec3
Gets the local Y-axis.
auto is_active() const noexcept -> bool
Checks if the component is active.
auto to_local(const math::vec3 &point) const noexcept -> math::vec3
SPACE UTILS.
void set_skew_global(const math::vec3 &s) noexcept
Sets the global skew.
auto get_skew_global() const noexcept -> const math::vec3 &
SKEW.
void set_rotation_local(const math::quat &rotation) noexcept
Sets the local rotation.
auto get_parent() const noexcept -> entt::handle
RELATIONSHIP.
auto get_perspective_global() const noexcept -> const math::vec4 &
PERSPECTIVE.
void rotate_by_global(const math::quat &rotation) noexcept
Rotates the component by a specified amount globally.
auto get_children() const noexcept -> const std::vector< entt::handle > &
Gets the child entities.
float scale
Definition hub.cpp:25
uint32_t flag
Definition bbox.cpp:5
transform_t< float > transform
auto is_roots_order_changed() -> bool
void reset_roots_order_changed()
CRTP (Curiously Recurring Template Pattern) base structure for components.
Root component structure for the ACE framework, serves as the base component.
static void on_update_component(entt::registry &r, entt::entity e)
Called when the component is updated.
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
static constexpr bool in_place_delete
gfx::uniform_handle handle
Definition uniform.cpp:9