trying to figure out a way to handle meshes and transforms more elegantly
This commit is contained in:
parent
2e151b87c6
commit
74aae08122
8 changed files with 75 additions and 54 deletions
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -46,10 +46,6 @@ public:
|
|||
typedef std::shared_ptr<component> ref;
|
||||
typedef std::vector<ref> 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 <typename T>
|
||||
inline T* cast() { return static_cast<T*>(this);}
|
||||
|
||||
protected:
|
||||
|
||||
uint32_t _weight = 0;
|
||||
|
||||
node* _node;
|
||||
bool _enabled;
|
||||
|
||||
|
|
|
@ -27,12 +27,11 @@
|
|||
|
||||
#include <pw/core/globals.hpp>
|
||||
#include <pw/scene/component.hpp>
|
||||
#include <pw/scene/transform.hpp>
|
||||
|
||||
|
||||
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> ptr_array;
|
||||
|
||||
friend class component;
|
||||
|
||||
//! standard c'tor
|
||||
node();
|
||||
|
||||
|
@ -102,20 +99,17 @@ public:
|
|||
void traversal();
|
||||
|
||||
template <typename T>
|
||||
T* find()
|
||||
{
|
||||
for (auto c : _components)
|
||||
T* find_component()
|
||||
{
|
||||
for (auto c : _components) {
|
||||
T* r = static_cast<T*>(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
|
||||
|
||||
class transform* _transform;
|
||||
class transform _transform;
|
||||
|
||||
std::string _name;
|
||||
|
||||
|
|
|
@ -9,26 +9,35 @@ namespace pw {
|
|||
class transform : public component {
|
||||
public:
|
||||
|
||||
transform() = default;
|
||||
|
||||
using component::component;
|
||||
using component::ref;
|
||||
|
||||
transform();
|
||||
|
||||
inline const matrix44& local() const { return _local; }
|
||||
void set_local(const matrix44 &local);
|
||||
|
||||
inline const matrix44& global() const { return _global; }
|
||||
void set_global(const matrix44 &global);
|
||||
|
||||
void update();
|
||||
|
||||
inline void translate(const real_t &x, const real_t &y, const real_t &z) {
|
||||
_local.at(0,3) += x;_local.at(1,3) += y;_local.at(2,3) += z;
|
||||
update_global_from_local();
|
||||
}
|
||||
|
||||
void update(node &node);
|
||||
|
||||
protected:
|
||||
|
||||
matrix44 _local;
|
||||
matrix44 _global;
|
||||
|
||||
private:
|
||||
|
||||
//! updates the global transform from parent globals and local transform
|
||||
void update_global_from_local();
|
||||
|
||||
//! updates the local transform from parent and new global
|
||||
void update_local_from_global();
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -12,22 +12,12 @@
|
|||
namespace pw {
|
||||
|
||||
component::component()
|
||||
// : _node(n)
|
||||
: _node(nullptr)
|
||||
{
|
||||
// if (_node != nullptr) {
|
||||
|
||||
// for (const auto c : _node->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 {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "pw/scene/node.hpp"
|
||||
#include "pw/scene/transform.hpp"
|
||||
#include "pw/scene/node.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
@ -7,8 +7,6 @@ namespace pw {
|
|||
|
||||
node::node()
|
||||
{
|
||||
// generate node-name from cache generate
|
||||
// add_component(std::make_shared<transform>());
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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<transform>()) {
|
||||
// this->_global = _node->find<transform>()->_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()
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue