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