rayzor
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/snode.cc Sat Apr 12 23:37:55 2014 +0300 1.3 @@ -0,0 +1,193 @@ 1.4 +#include <string.h> 1.5 +#include <assert.h> 1.6 +#include "snode.h" 1.7 + 1.8 + 1.9 +SceneNode::SceneNode() 1.10 +{ 1.11 + parent = 0; 1.12 + name = 0; 1.13 + type = NODE_NULL; 1.14 + scale = Vector3(1, 1, 1); 1.15 + invalidate(); 1.16 +} 1.17 + 1.18 +SceneNode::~SceneNode() 1.19 +{ 1.20 + delete [] name; 1.21 +} 1.22 + 1.23 +void SceneNode::set_name(const char *name) 1.24 +{ 1.25 + this->name = new char[strlen(name) + 1]; 1.26 + strcpy(this->name, name); 1.27 +} 1.28 + 1.29 +const char *SceneNode::get_name() const 1.30 +{ 1.31 + return name ? name : "<unnamed>"; 1.32 +} 1.33 + 1.34 +NodeType SceneNode::get_type() const 1.35 +{ 1.36 + return type; 1.37 +} 1.38 + 1.39 +SceneNode *SceneNode::get_parent() 1.40 +{ 1.41 + return parent; 1.42 +} 1.43 + 1.44 +const SceneNode *SceneNode::get_parent() const 1.45 +{ 1.46 + return parent; 1.47 +} 1.48 + 1.49 +void SceneNode::add_child(SceneNode *child) 1.50 +{ 1.51 + children.push_back(child); 1.52 + child->parent = this; 1.53 + invalidate(); 1.54 +} 1.55 + 1.56 +void SceneNode::remove_child(SceneNode *child) 1.57 +{ 1.58 + // TODO 1.59 + invalidate(); 1.60 +} 1.61 + 1.62 +int SceneNode::get_children_count() const 1.63 +{ 1.64 + return (int)children.size(); 1.65 +} 1.66 + 1.67 +SceneNode *SceneNode::get_child(int idx) 1.68 +{ 1.69 + if(idx >= 0 && idx < get_children_count()) { 1.70 + return children[idx]; 1.71 + } 1.72 + return 0; 1.73 +} 1.74 + 1.75 +const SceneNode *SceneNode::get_child(int idx) const 1.76 +{ 1.77 + if(idx >= 0 && idx < get_children_count()) { 1.78 + return children[idx]; 1.79 + } 1.80 + return 0; 1.81 +} 1.82 + 1.83 + 1.84 + 1.85 +void SceneNode::set_position(const Vector3 &pos) 1.86 +{ 1.87 + this->pos = pos; 1.88 + invalidate(); 1.89 +} 1.90 + 1.91 +Vector3 SceneNode::get_node_position() const 1.92 +{ 1.93 + return pos; 1.94 +} 1.95 + 1.96 +void SceneNode::set_rotation(const Quat &quat) 1.97 +{ 1.98 + rot = quat; 1.99 + invalidate(); 1.100 +} 1.101 + 1.102 +Quat SceneNode::get_node_rotation() const 1.103 +{ 1.104 + return rot; 1.105 +} 1.106 + 1.107 +void SceneNode::set_scaling(const Vector3 &scale) 1.108 +{ 1.109 + this->scale = scale; 1.110 + invalidate(); 1.111 +} 1.112 + 1.113 +Vector3 SceneNode::get_node_scaling() const 1.114 +{ 1.115 + return scale; 1.116 +} 1.117 + 1.118 +// these take hierarchy into account 1.119 +Vector3 SceneNode::get_position() const 1.120 +{ 1.121 + return transform(get_matrix(), Vector3(0, 0, 0)); 1.122 +} 1.123 + 1.124 +Quat SceneNode::get_rotation() const 1.125 +{ 1.126 + if(parent) { 1.127 + return parent->get_rotation() * rot; 1.128 + } 1.129 + return rot; 1.130 +} 1.131 + 1.132 +Vector3 SceneNode::get_scaling() const 1.133 +{ 1.134 + if(parent) { 1.135 + return parent->get_scaling() * scale; 1.136 + } 1.137 + return scale; 1.138 +} 1.139 + 1.140 +void SceneNode::set_pivot(const Vector3 &pivot) 1.141 +{ 1.142 + this->pivot = pivot; 1.143 + invalidate(); 1.144 +} 1.145 + 1.146 +Vector3 SceneNode::get_pivot() const 1.147 +{ 1.148 + return pivot; 1.149 +} 1.150 + 1.151 +const Matrix4x4 &SceneNode::get_matrix() const 1.152 +{ 1.153 + calc_matrix(); 1.154 + return xform; 1.155 +} 1.156 + 1.157 +const Matrix4x4 &SceneNode::get_inv_matrix() const 1.158 +{ 1.159 + calc_inv_matrix(); 1.160 + return inv_xform; 1.161 +} 1.162 + 1.163 +void SceneNode::invalidate() const 1.164 +{ 1.165 + xform_valid = inv_xform_valid = false; 1.166 +} 1.167 + 1.168 +// TODO: hierarchy 1.169 +void SceneNode::calc_matrix() const 1.170 +{ 1.171 + xform.set_identity(); 1.172 + xform.translate(pivot.x, pivot.y, pivot.z); 1.173 + xform = xform * rot.get_matrix(); 1.174 + xform.translate(pos.x, pos.y, pos.z); 1.175 + xform.scale(scale.x, scale.y, scale.z); 1.176 + xform.translate(-pivot.x, -pivot.y, -pivot.z); 1.177 + 1.178 + xform_valid = true; 1.179 +} 1.180 + 1.181 +void SceneNode::calc_inv_matrix() const 1.182 +{ 1.183 + calc_matrix(); 1.184 + 1.185 + inv_xform = xform.inverse(); 1.186 + inv_xform_valid = true; 1.187 +} 1.188 + 1.189 +void SceneNode::draw() const 1.190 +{ 1.191 +} 1.192 + 1.193 +bool SceneNode::intersect(const Ray &ray, float *dist) const 1.194 +{ 1.195 + return false; 1.196 +}