erebus
view 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 source
1 #include <assert.h>
2 #include <algorithm>
3 #include "snode.h"
5 void SceneNode::add_child(SceneNode *node)
6 {
7 if(node->parent) {
8 if(node->parent == this) {
9 return;
10 }
11 node->parent->remove_child(node);
12 }
14 children.push_back(node);
15 node->parent = this;
16 }
18 bool SceneNode::remove_child(SceneNode *node)
19 {
20 auto it = std::find(children.begin(), children.end(), node);
21 if(it != children.end()) {
22 assert(node->parent == this);
23 node->parent = 0;
24 }
25 }
27 int SceneNode::get_num_children() const
28 {
29 return (int)children.size();
30 }
32 SceneNode *SceneNode::get_child(int idx) const
33 {
34 return children[idx];
35 }
37 void SceneNode::set_position(const Vector3 &pos)
38 {
39 this->pos = pos;
40 }
42 void SceneNode::set_rotation(const Quaternion &rot)
43 {
44 this->rot = rot;
45 }
47 void SceneNode::set_scaling(const Vector3 &scale)
48 {
49 this->scale = scale;
50 }
53 const Vector3 &SceneNode::get_node_position() const
54 {
55 return pos;
56 }
58 const Quaternion &SceneNode::get_node_rotation() const
59 {
60 return rot;
61 }
63 const Vector3 &SceneNode::get_node_scaling() const
64 {
65 return scale;
66 }
69 Vector3 SceneNode::get_position() const
70 {
71 return Vector3{0, 0, 0}.transformed(xform);
72 }
74 Quaternion SceneNode::get_rotation() const
75 {
76 return rot; // TODO
77 }
79 Vector3 SceneNode::get_scaling() const
80 {
81 return scale; // TODO
82 }
85 void SceneNode::update_node(long msec)
86 {
87 node_xform.reset_identity();
88 node_xform.translate(pos);
89 node_xform.rotate(rot);
90 node_xform.scale(scale);
92 xform = parent ? parent->xform * node_xform : node_xform;
93 }
95 void SceneNode::update(long msec)
96 {
97 update_node(msec);
99 for(size_t i=0; i<children.size(); i++) {
100 children[i]->update(msec);
101 }
102 }
105 bool SceneNode::intersect(const Ray &ray, RayHit *hit) const
106 {
107 }