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>;
59
67 struct dirty_ids
68 {
69 enum : uint8_t
70 {
76 };
77 };
83 static void on_create_component(entt::registry& r, entt::entity e);
84
90 static void on_destroy_component(entt::registry& r, entt::entity e);
91
92 static auto get_top_level_entities(const std::vector<entt::handle>& list) -> std::vector<entt::handle>;
93 static auto get_top_level_entities(const std::vector<entt::handle*>& list) -> std::vector<entt::handle>;
94 static auto is_parent_of(entt::handle parent_to_test, entt::handle child) -> bool;
95 //---------------------------------------------
97 //---------------------------------------------
98 void resolve_transform_global() noexcept;
99
104 auto get_transform_global() const noexcept -> const math::transform&;
105
110 auto set_transform_global(const math::transform& trans) noexcept -> bool;
111 auto set_transform_global_epsilon(const math::transform& trans, float epsilon) noexcept -> bool;
112
117 auto get_transform_local() const noexcept -> const math::transform&;
118
123 void set_transform_local(const math::transform& trans) noexcept;
124
125 //---------------------------------------------
127 //---------------------------------------------
128
133 auto get_position_global() const noexcept -> const math::vec3&;
134
139 void set_position_global(const math::vec3& position) noexcept;
140
145 void move_by_global(const math::vec3& amount) noexcept;
146
150 void reset_position_global() noexcept;
151
156 auto get_position_local() const noexcept -> const math::vec3&;
157
162 void set_position_local(const math::vec3& position) noexcept;
163
168 void move_by_local(const math::vec3& amount) noexcept;
169
173 void reset_position_local() noexcept;
174
176 const math::quat& rotation,
177 float epsilon) noexcept -> bool;
178
179 //---------------------------------------------
181 //---------------------------------------------
182
187 auto get_rotation_global() const noexcept -> const math::quat&;
188
193 void set_rotation_global(const math::quat& rotation) noexcept;
194
199 void rotate_by_global(const math::quat& rotation) noexcept;
200
204 void reset_rotation_global() noexcept;
205
210 auto get_rotation_local() const noexcept -> const math::quat&;
211
216 void set_rotation_local(const math::quat& rotation) noexcept;
217
222 void rotate_by_local(const math::quat& rotation) noexcept;
223
227 void reset_rotation_local() noexcept;
228
233 auto get_rotation_euler_global() const noexcept -> math::vec3;
234
239 void set_rotation_euler_global(math::vec3 rotation) noexcept;
240
245 void rotate_by_euler_global(math::vec3 rotation) noexcept;
246
251 auto get_rotation_euler_local() const noexcept -> math::vec3;
252
257 void set_rotation_euler_local(math::vec3 rotation) noexcept;
258
263 void rotate_by_euler_local(math::vec3 rotation) noexcept;
264
270 void rotate_axis_global(float degrees, const math::vec3& axis) noexcept;
271
272 void rotate_around_global(const math::vec3& point, const math::vec3& axis, float degrees);
273
274 void rotate_around_global(const math::vec3& point, const math::quat& rotation);
275
280 void look_at(const math::vec3& point) noexcept;
281 void look_at(const math::vec3& point, const math::vec3& up) noexcept;
282
283 //---------------------------------------------
285 //---------------------------------------------
286
291 auto get_scale_global() const noexcept -> const math::vec3&;
292
297 void set_scale_global(const math::vec3& scale) noexcept;
298
303 void scale_by_global(const math::vec3& scale) noexcept;
304
308 void reset_scale_global() noexcept;
309
314 auto get_scale_local() const noexcept -> const math::vec3&;
315
320 void set_scale_local(const math::vec3& scale) noexcept;
321
326 void scale_by_local(const math::vec3& scale) noexcept;
327
331 void reset_scale_local() noexcept;
332
333 //---------------------------------------------
335 //---------------------------------------------
336
341 auto get_skew_global() const noexcept -> const math::vec3&;
342
347 void set_skew_global(const math::vec3& s) noexcept;
348
353 auto get_skew_local() const noexcept -> const math::vec3&;
354
359 void set_skew_local(const math::vec3& s) noexcept;
360
361 //---------------------------------------------
363 //---------------------------------------------
364
369 auto get_perspective_global() const noexcept -> const math::vec4&;
370
375 void set_perspective_global(const math::vec4& p) noexcept;
376
381 auto get_perspective_local() const noexcept -> const math::vec4&;
382
387 void set_perspective_local(const math::vec4& p) noexcept;
388
389 //---------------------------------------------
391 //---------------------------------------------
392
397 auto get_x_axis_global() const noexcept -> math::vec3;
398
403 auto get_x_axis_local() const noexcept -> math::vec3;
404
409 auto get_y_axis_global() const noexcept -> math::vec3;
410
415 auto get_y_axis_local() const noexcept -> math::vec3;
416
421 auto get_z_axis_global() const noexcept -> math::vec3;
422
427 auto get_z_axis_local() const noexcept -> math::vec3;
428
429 //---------------------------------------------
431 //---------------------------------------------
432
438 auto to_local(const math::vec3& point) const noexcept -> math::vec3;
439
440 //---------------------------------------------
442 //---------------------------------------------
443
448 auto get_parent() const noexcept -> entt::handle;
449
456 auto set_parent(const entt::handle& parent, bool global_stays = true) -> bool;
457
462 auto get_children() const noexcept -> const std::vector<entt::handle>&;
463
468 void set_children(const std::vector<entt::handle>& children);
469
473 void sort_children() noexcept;
474
479 void set_active(bool active) noexcept;
480
485 auto is_active() const noexcept -> bool;
486
487 auto is_active_global() const noexcept -> bool;
488
493 void set_dirty(bool dirty) noexcept;
494
499 auto is_dirty() const noexcept -> bool;
500
506 void set_dirty(uint8_t id, bool dirty) noexcept;
507
513 auto is_dirty(uint8_t id) const noexcept -> bool;
514
520 void mark_consumers_dirty() noexcept;
521
526
527private:
532 void set_owner(entt::handle owner);
533
538 void apply_transform(const math::transform& tr) noexcept;
539
545 auto inverse_parent_transform(const entt::handle& parent) noexcept -> math::transform;
546
551 void on_dirty_transform(bool dirty) noexcept;
552 void on_dirty_flags(bool dirty) noexcept;
553 void on_flags_changed(flags_t flags);
554 auto get_flags_global() const noexcept -> flags_t;
559 auto resolve_global_value_transform() const noexcept -> math::transform;
560 auto resolve_global_value_flags() const noexcept -> flags_t;
561
567 void attach_child(const entt::handle& child, transform_component& child_transform);
568
575 auto remove_child(const entt::handle& child, transform_component& child_transform) -> bool;
576
578 entt::handle parent_{};
580 std::vector<entt::handle> children_{};
581
586 template<typename T,
587 typename Owner,
588 void (Owner::*on_dirty)(bool),
589 T (Owner::*resolve_global_value)() const,
590 bool auto_resolve>
591 struct local_global_property
592 {
599 auto set_value(Owner* owner, const T& val) noexcept -> bool
600 {
601 local = val;
602
603 set_dirty(owner, true);
604
605 return true;
606 }
607
613 auto get_value(const Owner* owner) const noexcept -> const T&
614 {
615 return local;
616 }
617
623 auto value(Owner* owner) noexcept -> T&
624 {
625 set_dirty(owner, true);
626
627 return local;
628 }
629
635 void set_dirty(Owner* owner, bool flag) const noexcept
636 {
637 if(dirty == flag)
638 {
639 return;
640 }
641 dirty = flag;
642
643 if(dirty)
644 {
645 assert(owner);
646 (owner->*on_dirty)(flag);
647 }
648 }
649
655 auto get_global_value(const Owner* owner, bool force) const noexcept -> const T&
656 {
657 if(force || auto_resolve && dirty)
658 {
659 assert(owner);
660 global = (owner->*resolve_global_value)();
661
662 set_dirty(const_cast<Owner*>(owner), false);
663 }
664
665 return global;
666 }
667
668 auto has_auto_resolve() const noexcept -> bool
669 {
670 return auto_resolve;
671 }
672
674 T local{};
676 mutable T global{};
678 mutable bool dirty{true};
679 };
680
685 using property_transform = local_global_property<math::transform,
686 transform_component,
687 &transform_component::on_dirty_transform,
688 &transform_component::resolve_global_value_transform,
689 true>;
690
691 using property_flags = local_global_property<flags_t,
692 transform_component,
693 &transform_component::on_dirty_flags,
694 &transform_component::resolve_global_value_flags,
695 true>;
696
697 enum flags_types
698 {
699 active,
700 count = 8,
701 };
702
704 property_transform transform_{};
705
706 property_flags flags_{};
708 std::bitset<32> transform_dirty_{};
709};
710
711} // 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.
void mark_consumers_dirty() noexcept
Sets every consumer dirty bit, forcing all indexed consumers (physics sync, model pose refresh,...
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.
math::vec3 position
Definition defaults.cpp:52
std::vector< render_pass_node > children
uint32_t flag
Definition bbox.cpp:5
transform_t< float > transform
Hash specialization for batch_key to enable use in std::unordered_map.
auto is_roots_order_changed() -> bool
void reset_roots_order_changed()
std::vector< float > scale
std::vector< math::quat > rotation
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
Reserved consumer slots for the indexed dirty flags (see is_dirty(id)/set_dirty(id,...
@ physics
Physics backend transform sync (see bullet_backend.cpp).
gfx::uniform_handle handle
Definition uniform.cpp:9
std::string owner