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::unique_ptr;
|
||||||
using std::weak_ptr;
|
using std::weak_ptr;
|
||||||
|
|
||||||
|
using std::make_unique;
|
||||||
|
using std::make_shared;
|
||||||
|
|
||||||
typedef float real_t;
|
typedef float real_t;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,10 +46,6 @@ public:
|
||||||
typedef std::shared_ptr<component> ref;
|
typedef std::shared_ptr<component> ref;
|
||||||
typedef std::vector<ref> array;
|
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();
|
||||||
component(const component& other);
|
component(const component& other);
|
||||||
virtual ~component();
|
virtual ~component();
|
||||||
|
@ -57,8 +53,17 @@ public:
|
||||||
bool enabled() const;
|
bool enabled() const;
|
||||||
void set_enabled(bool enabled);
|
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:
|
protected:
|
||||||
|
|
||||||
|
uint32_t _weight = 0;
|
||||||
|
|
||||||
node* _node;
|
node* _node;
|
||||||
bool _enabled;
|
bool _enabled;
|
||||||
|
|
||||||
|
|
|
@ -27,12 +27,11 @@
|
||||||
|
|
||||||
#include <pw/core/globals.hpp>
|
#include <pw/core/globals.hpp>
|
||||||
#include <pw/scene/component.hpp>
|
#include <pw/scene/component.hpp>
|
||||||
|
#include <pw/scene/transform.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
class transform;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief nodes represent the structure for the scene graph
|
* @brief nodes represent the structure for the scene graph
|
||||||
*
|
*
|
||||||
|
@ -49,8 +48,6 @@ public:
|
||||||
typedef node *ptr;
|
typedef node *ptr;
|
||||||
typedef std::vector<ptr> ptr_array;
|
typedef std::vector<ptr> ptr_array;
|
||||||
|
|
||||||
friend class component;
|
|
||||||
|
|
||||||
//! standard c'tor
|
//! standard c'tor
|
||||||
node();
|
node();
|
||||||
|
|
||||||
|
@ -102,20 +99,17 @@ public:
|
||||||
void traversal();
|
void traversal();
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* find()
|
T* find_component()
|
||||||
{
|
{
|
||||||
for (auto c : _components)
|
for (auto c : _components) {
|
||||||
{
|
|
||||||
T* r = static_cast<T*>(c.get());
|
T* r = static_cast<T*>(c.get());
|
||||||
if (r != nullptr) return r;
|
if (r != nullptr) return r;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const class transform& transform() const { return _transform; }
|
||||||
const transform* transform() const { return _transform; }
|
class transform& transform() { return _transform; }
|
||||||
class transform* transform() { return _transform; }
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
@ -124,7 +118,7 @@ protected:
|
||||||
|
|
||||||
component::array _components; //<! components
|
component::array _components; //<! components
|
||||||
|
|
||||||
class transform* _transform;
|
class transform _transform;
|
||||||
|
|
||||||
std::string _name;
|
std::string _name;
|
||||||
|
|
||||||
|
|
|
@ -9,26 +9,35 @@ namespace pw {
|
||||||
class transform : public component {
|
class transform : public component {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
transform() = default;
|
using component::component;
|
||||||
|
|
||||||
using component::component;
|
transform();
|
||||||
using component::ref;
|
|
||||||
|
|
||||||
inline const matrix44& local() const { return _local; }
|
inline const matrix44& local() const { return _local; }
|
||||||
void set_local(const matrix44 &local);
|
void set_local(const matrix44 &local);
|
||||||
|
|
||||||
|
inline const matrix44& global() const { return _global; }
|
||||||
void set_global(const matrix44 &global);
|
void set_global(const matrix44 &global);
|
||||||
|
|
||||||
void update();
|
|
||||||
|
|
||||||
inline void translate(const real_t &x, const real_t &y, const real_t &z) {
|
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;
|
_local.at(0,3) += x;_local.at(1,3) += y;_local.at(2,3) += z;
|
||||||
|
update_global_from_local();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update(node &node);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
matrix44 _local;
|
matrix44 _local;
|
||||||
matrix44 _global;
|
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 {
|
namespace pw {
|
||||||
|
|
||||||
component::component()
|
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)
|
component::component(const component &other)
|
||||||
// : _node(other._node)
|
: _node(other._node)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +36,16 @@ void component::set_enabled(bool enabled)
|
||||||
_enabled = enabled;
|
_enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint32_t component::weight() const
|
||||||
|
{
|
||||||
|
return _weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
void component::set_weight(const uint32_t &weight)
|
||||||
|
{
|
||||||
|
_weight = weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//class trs : public transform {
|
//class trs : public transform {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#include "pw/scene/node.hpp"
|
|
||||||
#include "pw/scene/transform.hpp"
|
#include "pw/scene/transform.hpp"
|
||||||
|
#include "pw/scene/node.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -7,8 +7,6 @@ namespace pw {
|
||||||
|
|
||||||
node::node()
|
node::node()
|
||||||
{
|
{
|
||||||
// generate node-name from cache generate
|
|
||||||
// add_component(std::make_shared<transform>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
node::node(const node &node)
|
node::node(const node &node)
|
||||||
|
@ -70,16 +68,7 @@ node::~node()
|
||||||
//
|
//
|
||||||
void node::add_component(component::ref new_component)
|
void node::add_component(component::ref new_component)
|
||||||
{
|
{
|
||||||
if (new_component->singular()) {
|
// assumption is that only transform is a "singular" node
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_components.push_back(new_component);
|
_components.push_back(new_component);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +89,7 @@ const node::ptr_array &node::path() const
|
||||||
|
|
||||||
void node::traversal()
|
void node::traversal()
|
||||||
{
|
{
|
||||||
for (auto c : _components) c->visit(this);
|
// for (auto c : _components) c->visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
const component::array& node::components() const
|
const component::array& node::components() const
|
||||||
|
|
|
@ -3,15 +3,17 @@
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
|
transform::transform()
|
||||||
|
{
|
||||||
|
_local.set_identity();
|
||||||
|
_global.set_identity();
|
||||||
|
}
|
||||||
|
|
||||||
void transform::set_local(const matrix44 &local)
|
void transform::set_local(const matrix44 &local)
|
||||||
{
|
{
|
||||||
// TODO need to rebuild the transforms: both -> global down and global up
|
// TODO need to rebuild the transforms: both -> global down and global up
|
||||||
_local = local;
|
_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)
|
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));
|
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