diff --git a/src/core/include/pw/core/globals.hpp b/src/core/include/pw/core/globals.hpp index e831e87..765e6f4 100644 --- a/src/core/include/pw/core/globals.hpp +++ b/src/core/include/pw/core/globals.hpp @@ -12,6 +12,9 @@ using std::shared_ptr; using std::unique_ptr; using std::weak_ptr; +using std::make_unique; +using std::make_shared; + typedef float real_t; } diff --git a/src/scene/include/pw/scene/component.hpp b/src/scene/include/pw/scene/component.hpp index dfa3d43..1be7791 100644 --- a/src/scene/include/pw/scene/component.hpp +++ b/src/scene/include/pw/scene/component.hpp @@ -46,10 +46,6 @@ public: typedef std::shared_ptr ref; typedef std::vector array; - //! only very few components can be attached multiple times - virtual bool singular() const { return true; } - virtual void visit(node*) {} - component(); component(const component& other); virtual ~component(); @@ -57,8 +53,17 @@ public: bool enabled() const; void set_enabled(bool enabled); + uint32_t weight() const; + void set_weight(const uint32_t &weight); + + + template + inline T* cast() { return static_cast(this);} + protected: + uint32_t _weight = 0; + node* _node; bool _enabled; diff --git a/src/scene/include/pw/scene/node.hpp b/src/scene/include/pw/scene/node.hpp index 9d44f1d..d4a853b 100644 --- a/src/scene/include/pw/scene/node.hpp +++ b/src/scene/include/pw/scene/node.hpp @@ -27,12 +27,11 @@ #include #include +#include namespace pw { -class transform; - /** * @brief nodes represent the structure for the scene graph * @@ -49,8 +48,6 @@ public: typedef node *ptr; typedef std::vector ptr_array; - friend class component; - //! standard c'tor node(); @@ -102,20 +99,17 @@ public: void traversal(); template - T* find() + T* find_component() { - for (auto c : _components) - { + for (auto c : _components) { T* r = static_cast(c.get()); if (r != nullptr) return r; } return nullptr; } - - const transform* transform() const { return _transform; } - class transform* transform() { return _transform; } - + const class transform& transform() const { return _transform; } + class transform& transform() { return _transform; } protected: @@ -124,7 +118,7 @@ protected: component::array _components; //components()) { -// if (typeid (c.get()).name() == typeid (this).name()) { -// // error -// return; -// } -// } -//// _node->register_component(this); -// } } component::component(const component &other) -// : _node(other._node) + : _node(other._node) { } @@ -46,6 +36,16 @@ void component::set_enabled(bool enabled) _enabled = enabled; } +uint32_t component::weight() const +{ + return _weight; +} + +void component::set_weight(const uint32_t &weight) +{ + _weight = weight; +} + //class trs : public transform { diff --git a/src/scene/src/node.cpp b/src/scene/src/node.cpp index 1c564a8..c28bff3 100644 --- a/src/scene/src/node.cpp +++ b/src/scene/src/node.cpp @@ -1,5 +1,5 @@ -#include "pw/scene/node.hpp" #include "pw/scene/transform.hpp" +#include "pw/scene/node.hpp" #include @@ -7,8 +7,6 @@ namespace pw { node::node() { - // generate node-name from cache generate -// add_component(std::make_shared()); } node::node(const node &node) @@ -70,16 +68,7 @@ node::~node() // void node::add_component(component::ref new_component) { - if (new_component->singular()) { - for (auto c : _components) { - if (typeid(c.get()) == typeid (new_component.get())) { - // warn and/or override? - c = new_component; - std::cout << "Meh!" << std::endl; - return; - } - } - } + // assumption is that only transform is a "singular" node _components.push_back(new_component); } @@ -100,7 +89,7 @@ const node::ptr_array &node::path() const void node::traversal() { - for (auto c : _components) c->visit(this); +// for (auto c : _components) c->visit(this); } const component::array& node::components() const diff --git a/src/scene/src/transform.cpp b/src/scene/src/transform.cpp index 526ebda..2ff7a0e 100644 --- a/src/scene/src/transform.cpp +++ b/src/scene/src/transform.cpp @@ -3,15 +3,17 @@ namespace pw { +transform::transform() +{ + _local.set_identity(); + _global.set_identity(); +} + void transform::set_local(const matrix44 &local) { // TODO need to rebuild the transforms: both -> global down and global up _local = local; - // update the global transform - if (_node && _node->parent() && _node->parent()->find()) { -// this->_global = _node->find()->_global * _local; - } } void transform::set_global(const matrix44 &global) @@ -21,8 +23,23 @@ void transform::set_global(const matrix44 &global) } -void transform::update() +void transform::update(node& node) { } +void transform::update_global_from_local() +{ + // update the global transform + if (_node && _node->parent()) + { + this->_global = _node->transform()._global * _local; + } else + { + this->_global = _local; + } +} + +void transform::update_local_from_global() +{ +// _local = this->global() } diff --git a/src/scene/tests/pwscene_test_node.cpp b/src/scene/tests/pwscene_test_node.cpp index e9aee35..4fb15a3 100644 --- a/src/scene/tests/pwscene_test_node.cpp +++ b/src/scene/tests/pwscene_test_node.cpp @@ -50,9 +50,13 @@ int main(int argc,char **argv) { print_node_path((n)); - auto t = n->transform(); +// auto t = n->transform(); - if (t) std::cout << t->local().at(0,0) << std::endl; +// if (t) + std::cout << n->transform().local().at(0,0) << std::endl; +// else { +// std::cerr << "no transform?" << std::endl; +// }