rayzor

annotate src/snode.cc @ 13:964f8ea5f095

missed quite a lot of things in my last commit apparently
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:37:55 +0300
parents
children a9a948809c6f
rev   line source
nuclear@13 1 #include <string.h>
nuclear@13 2 #include <assert.h>
nuclear@13 3 #include "snode.h"
nuclear@13 4
nuclear@13 5
nuclear@13 6 SceneNode::SceneNode()
nuclear@13 7 {
nuclear@13 8 parent = 0;
nuclear@13 9 name = 0;
nuclear@13 10 type = NODE_NULL;
nuclear@13 11 scale = Vector3(1, 1, 1);
nuclear@13 12 invalidate();
nuclear@13 13 }
nuclear@13 14
nuclear@13 15 SceneNode::~SceneNode()
nuclear@13 16 {
nuclear@13 17 delete [] name;
nuclear@13 18 }
nuclear@13 19
nuclear@13 20 void SceneNode::set_name(const char *name)
nuclear@13 21 {
nuclear@13 22 this->name = new char[strlen(name) + 1];
nuclear@13 23 strcpy(this->name, name);
nuclear@13 24 }
nuclear@13 25
nuclear@13 26 const char *SceneNode::get_name() const
nuclear@13 27 {
nuclear@13 28 return name ? name : "<unnamed>";
nuclear@13 29 }
nuclear@13 30
nuclear@13 31 NodeType SceneNode::get_type() const
nuclear@13 32 {
nuclear@13 33 return type;
nuclear@13 34 }
nuclear@13 35
nuclear@13 36 SceneNode *SceneNode::get_parent()
nuclear@13 37 {
nuclear@13 38 return parent;
nuclear@13 39 }
nuclear@13 40
nuclear@13 41 const SceneNode *SceneNode::get_parent() const
nuclear@13 42 {
nuclear@13 43 return parent;
nuclear@13 44 }
nuclear@13 45
nuclear@13 46 void SceneNode::add_child(SceneNode *child)
nuclear@13 47 {
nuclear@13 48 children.push_back(child);
nuclear@13 49 child->parent = this;
nuclear@13 50 invalidate();
nuclear@13 51 }
nuclear@13 52
nuclear@13 53 void SceneNode::remove_child(SceneNode *child)
nuclear@13 54 {
nuclear@13 55 // TODO
nuclear@13 56 invalidate();
nuclear@13 57 }
nuclear@13 58
nuclear@13 59 int SceneNode::get_children_count() const
nuclear@13 60 {
nuclear@13 61 return (int)children.size();
nuclear@13 62 }
nuclear@13 63
nuclear@13 64 SceneNode *SceneNode::get_child(int idx)
nuclear@13 65 {
nuclear@13 66 if(idx >= 0 && idx < get_children_count()) {
nuclear@13 67 return children[idx];
nuclear@13 68 }
nuclear@13 69 return 0;
nuclear@13 70 }
nuclear@13 71
nuclear@13 72 const SceneNode *SceneNode::get_child(int idx) const
nuclear@13 73 {
nuclear@13 74 if(idx >= 0 && idx < get_children_count()) {
nuclear@13 75 return children[idx];
nuclear@13 76 }
nuclear@13 77 return 0;
nuclear@13 78 }
nuclear@13 79
nuclear@13 80
nuclear@13 81
nuclear@13 82 void SceneNode::set_position(const Vector3 &pos)
nuclear@13 83 {
nuclear@13 84 this->pos = pos;
nuclear@13 85 invalidate();
nuclear@13 86 }
nuclear@13 87
nuclear@13 88 Vector3 SceneNode::get_node_position() const
nuclear@13 89 {
nuclear@13 90 return pos;
nuclear@13 91 }
nuclear@13 92
nuclear@13 93 void SceneNode::set_rotation(const Quat &quat)
nuclear@13 94 {
nuclear@13 95 rot = quat;
nuclear@13 96 invalidate();
nuclear@13 97 }
nuclear@13 98
nuclear@13 99 Quat SceneNode::get_node_rotation() const
nuclear@13 100 {
nuclear@13 101 return rot;
nuclear@13 102 }
nuclear@13 103
nuclear@13 104 void SceneNode::set_scaling(const Vector3 &scale)
nuclear@13 105 {
nuclear@13 106 this->scale = scale;
nuclear@13 107 invalidate();
nuclear@13 108 }
nuclear@13 109
nuclear@13 110 Vector3 SceneNode::get_node_scaling() const
nuclear@13 111 {
nuclear@13 112 return scale;
nuclear@13 113 }
nuclear@13 114
nuclear@13 115 // these take hierarchy into account
nuclear@13 116 Vector3 SceneNode::get_position() const
nuclear@13 117 {
nuclear@13 118 return transform(get_matrix(), Vector3(0, 0, 0));
nuclear@13 119 }
nuclear@13 120
nuclear@13 121 Quat SceneNode::get_rotation() const
nuclear@13 122 {
nuclear@13 123 if(parent) {
nuclear@13 124 return parent->get_rotation() * rot;
nuclear@13 125 }
nuclear@13 126 return rot;
nuclear@13 127 }
nuclear@13 128
nuclear@13 129 Vector3 SceneNode::get_scaling() const
nuclear@13 130 {
nuclear@13 131 if(parent) {
nuclear@13 132 return parent->get_scaling() * scale;
nuclear@13 133 }
nuclear@13 134 return scale;
nuclear@13 135 }
nuclear@13 136
nuclear@13 137 void SceneNode::set_pivot(const Vector3 &pivot)
nuclear@13 138 {
nuclear@13 139 this->pivot = pivot;
nuclear@13 140 invalidate();
nuclear@13 141 }
nuclear@13 142
nuclear@13 143 Vector3 SceneNode::get_pivot() const
nuclear@13 144 {
nuclear@13 145 return pivot;
nuclear@13 146 }
nuclear@13 147
nuclear@13 148 const Matrix4x4 &SceneNode::get_matrix() const
nuclear@13 149 {
nuclear@13 150 calc_matrix();
nuclear@13 151 return xform;
nuclear@13 152 }
nuclear@13 153
nuclear@13 154 const Matrix4x4 &SceneNode::get_inv_matrix() const
nuclear@13 155 {
nuclear@13 156 calc_inv_matrix();
nuclear@13 157 return inv_xform;
nuclear@13 158 }
nuclear@13 159
nuclear@13 160 void SceneNode::invalidate() const
nuclear@13 161 {
nuclear@13 162 xform_valid = inv_xform_valid = false;
nuclear@13 163 }
nuclear@13 164
nuclear@13 165 // TODO: hierarchy
nuclear@13 166 void SceneNode::calc_matrix() const
nuclear@13 167 {
nuclear@13 168 xform.set_identity();
nuclear@13 169 xform.translate(pivot.x, pivot.y, pivot.z);
nuclear@13 170 xform = xform * rot.get_matrix();
nuclear@13 171 xform.translate(pos.x, pos.y, pos.z);
nuclear@13 172 xform.scale(scale.x, scale.y, scale.z);
nuclear@13 173 xform.translate(-pivot.x, -pivot.y, -pivot.z);
nuclear@13 174
nuclear@13 175 xform_valid = true;
nuclear@13 176 }
nuclear@13 177
nuclear@13 178 void SceneNode::calc_inv_matrix() const
nuclear@13 179 {
nuclear@13 180 calc_matrix();
nuclear@13 181
nuclear@13 182 inv_xform = xform.inverse();
nuclear@13 183 inv_xform_valid = true;
nuclear@13 184 }
nuclear@13 185
nuclear@13 186 void SceneNode::draw() const
nuclear@13 187 {
nuclear@13 188 }
nuclear@13 189
nuclear@13 190 bool SceneNode::intersect(const Ray &ray, float *dist) const
nuclear@13 191 {
nuclear@13 192 return false;
nuclear@13 193 }