erebus

diff liberebus/src/snode.cc @ 1:59a72293f9bd

continuing
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Apr 2014 05:58:22 +0300
parents
children a932848de652
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/liberebus/src/snode.cc	Mon Apr 28 05:58:22 2014 +0300
     1.3 @@ -0,0 +1,107 @@
     1.4 +#include <assert.h>
     1.5 +#include <algorithm>
     1.6 +#include "snode.h"
     1.7 +
     1.8 +void SceneNode::add_child(SceneNode *node)
     1.9 +{
    1.10 +	if(node->parent) {
    1.11 +		if(node->parent == this) {
    1.12 +			return;
    1.13 +		}
    1.14 +		node->parent->remove_child(node);
    1.15 +	}
    1.16 +
    1.17 +	children.push_back(node);
    1.18 +	node->parent = this;
    1.19 +}
    1.20 +
    1.21 +bool SceneNode::remove_child(SceneNode *node)
    1.22 +{
    1.23 +	auto it = std::find(children.begin(), children.end(), node);
    1.24 +	if(it != children.end()) {
    1.25 +		assert(node->parent == this);
    1.26 +		node->parent = 0;
    1.27 +	}
    1.28 +}
    1.29 +
    1.30 +int SceneNode::get_num_children() const
    1.31 +{
    1.32 +	return (int)children.size();
    1.33 +}
    1.34 +
    1.35 +SceneNode *SceneNode::get_child(int idx) const
    1.36 +{
    1.37 +	return children[idx];
    1.38 +}
    1.39 +
    1.40 +void SceneNode::set_position(const Vector3 &pos)
    1.41 +{
    1.42 +	this->pos = pos;
    1.43 +}
    1.44 +
    1.45 +void SceneNode::set_rotation(const Quaternion &rot)
    1.46 +{
    1.47 +	this->rot = rot;
    1.48 +}
    1.49 +
    1.50 +void SceneNode::set_scaling(const Vector3 &scale)
    1.51 +{
    1.52 +	this->scale = scale;
    1.53 +}
    1.54 +
    1.55 +
    1.56 +const Vector3 &SceneNode::get_node_position() const
    1.57 +{
    1.58 +	return pos;
    1.59 +}
    1.60 +
    1.61 +const Quaternion &SceneNode::get_node_rotation() const
    1.62 +{
    1.63 +	return rot;
    1.64 +}
    1.65 +
    1.66 +const Vector3 &SceneNode::get_node_scaling() const
    1.67 +{
    1.68 +	return scale;
    1.69 +}
    1.70 +
    1.71 +
    1.72 +Vector3 SceneNode::get_position() const
    1.73 +{
    1.74 +	return Vector3{0, 0, 0}.transformed(xform);
    1.75 +}
    1.76 +
    1.77 +Quaternion SceneNode::get_rotation() const
    1.78 +{
    1.79 +	return rot;	// TODO
    1.80 +}
    1.81 +
    1.82 +Vector3 SceneNode::get_scaling() const
    1.83 +{
    1.84 +	return scale;	// TODO
    1.85 +}
    1.86 +
    1.87 +
    1.88 +void SceneNode::update_node(long msec)
    1.89 +{
    1.90 +	node_xform.reset_identity();
    1.91 +	node_xform.translate(pos);
    1.92 +	node_xform.rotate(rot);
    1.93 +	node_xform.scale(scale);
    1.94 +
    1.95 +	xform = parent ? parent->xform * node_xform : node_xform;
    1.96 +}
    1.97 +
    1.98 +void SceneNode::update(long msec)
    1.99 +{
   1.100 +	update_node(msec);
   1.101 +
   1.102 +	for(size_t i=0; i<children.size(); i++) {
   1.103 +		children[i]->update(msec);
   1.104 +	}
   1.105 +}
   1.106 +
   1.107 +
   1.108 +bool SceneNode::intersect(const Ray &ray, RayHit *hit) const
   1.109 +{
   1.110 +}
   1.111 \ No newline at end of file