erebus
diff liberebus/src/snode.cc @ 4:93894c232d65
more changes across the board
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 29 Apr 2014 07:38:40 +0300 |
parents | a932848de652 |
children | e2d9bf168a41 |
line diff
1.1 --- a/liberebus/src/snode.cc Mon Apr 28 15:44:59 2014 +0300 1.2 +++ b/liberebus/src/snode.cc Tue Apr 29 07:38:40 2014 +0300 1.3 @@ -1,111 +1,168 @@ 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 - xform.reset_identity(); 1.91 - xform.translate(pos); 1.92 - xform.rotate(rot); 1.93 - xform.scale(scale); 1.94 - 1.95 - if(parent) { 1.96 - xform = parent->xform * xform; 1.97 - } 1.98 - inv_xform = xform.inverse(); 1.99 -} 1.100 - 1.101 -void SceneNode::update(long msec) 1.102 -{ 1.103 - update_node(msec); 1.104 - 1.105 - for(size_t i=0; i<children.size(); i++) { 1.106 - children[i]->update(msec); 1.107 - } 1.108 -} 1.109 - 1.110 - 1.111 -bool SceneNode::intersect(const Ray &ray, RayHit *hit) const 1.112 -{ 1.113 - Ray local_ray = ray.transformed(inv_xform); 1.114 -} 1.115 \ No newline at end of file 1.116 +#include <float.h> 1.117 +#include <assert.h> 1.118 +#include <algorithm> 1.119 +#include "snode.h" 1.120 + 1.121 +SceneNode::SceneNode() 1.122 + : scale(1, 1, 1) 1.123 +{ 1.124 + parent = 0; 1.125 +} 1.126 + 1.127 +SceneNode::SceneNode(Object *obj) 1.128 + : scale(1, 1, 1) 1.129 +{ 1.130 + parent = 0; 1.131 + add_object(obj); 1.132 +} 1.133 + 1.134 +void SceneNode::add_child(SceneNode *node) 1.135 +{ 1.136 + if(node->parent) { 1.137 + if(node->parent == this) { 1.138 + return; 1.139 + } 1.140 + node->parent->remove_child(node); 1.141 + } 1.142 + 1.143 + children.push_back(node); 1.144 + node->parent = this; 1.145 +} 1.146 + 1.147 +bool SceneNode::remove_child(SceneNode *node) 1.148 +{ 1.149 + auto it = std::find(children.begin(), children.end(), node); 1.150 + if(it != children.end()) { 1.151 + assert(node->parent == this); 1.152 + node->parent = 0; 1.153 + return true; 1.154 + } 1.155 + return false; 1.156 +} 1.157 + 1.158 +int SceneNode::get_num_children() const 1.159 +{ 1.160 + return (int)children.size(); 1.161 +} 1.162 + 1.163 +SceneNode *SceneNode::get_child(int idx) const 1.164 +{ 1.165 + return children[idx]; 1.166 +} 1.167 + 1.168 +SceneNode *SceneNode::get_parent() const 1.169 +{ 1.170 + return parent; 1.171 +} 1.172 + 1.173 +void SceneNode::add_object(Object *obj) 1.174 +{ 1.175 + if(std::find(this->obj.begin(), this->obj.end(), obj) == this->obj.end()) { 1.176 + this->obj.push_back(obj); 1.177 + } 1.178 +} 1.179 + 1.180 +int SceneNode::get_num_objects() const 1.181 +{ 1.182 + return (int)obj.size(); 1.183 +} 1.184 + 1.185 +Object *SceneNode::get_object(int idx) const 1.186 +{ 1.187 + return obj[idx]; 1.188 +} 1.189 + 1.190 +void SceneNode::set_position(const Vector3 &pos) 1.191 +{ 1.192 + this->pos = pos; 1.193 +} 1.194 + 1.195 +void SceneNode::set_rotation(const Quaternion &rot) 1.196 +{ 1.197 + this->rot = rot; 1.198 +} 1.199 + 1.200 +void SceneNode::set_scaling(const Vector3 &scale) 1.201 +{ 1.202 + this->scale = scale; 1.203 +} 1.204 + 1.205 + 1.206 +const Vector3 &SceneNode::get_node_position() const 1.207 +{ 1.208 + return pos; 1.209 +} 1.210 + 1.211 +const Quaternion &SceneNode::get_node_rotation() const 1.212 +{ 1.213 + return rot; 1.214 +} 1.215 + 1.216 +const Vector3 &SceneNode::get_node_scaling() const 1.217 +{ 1.218 + return scale; 1.219 +} 1.220 + 1.221 + 1.222 +Vector3 SceneNode::get_position() const 1.223 +{ 1.224 + return Vector3{0, 0, 0}.transformed(xform); 1.225 +} 1.226 + 1.227 +Quaternion SceneNode::get_rotation() const 1.228 +{ 1.229 + return rot; // TODO 1.230 +} 1.231 + 1.232 +Vector3 SceneNode::get_scaling() const 1.233 +{ 1.234 + return scale; // TODO 1.235 +} 1.236 + 1.237 + 1.238 +void SceneNode::update_node(long msec) 1.239 +{ 1.240 + xform.reset_identity(); 1.241 + xform.translate(pos); 1.242 + xform.rotate(rot); 1.243 + xform.scale(scale); 1.244 + 1.245 + if(parent) { 1.246 + xform = parent->xform * xform; 1.247 + } 1.248 + inv_xform = xform.inverse(); 1.249 +} 1.250 + 1.251 +void SceneNode::update(long msec) 1.252 +{ 1.253 + update_node(msec); 1.254 + 1.255 + for(size_t i=0; i<children.size(); i++) { 1.256 + children[i]->update(msec); 1.257 + } 1.258 +} 1.259 + 1.260 + 1.261 +bool SceneNode::intersect(const Ray &ray, RayHit *hit) const 1.262 +{ 1.263 + Ray local_ray = ray.transformed(inv_xform); 1.264 + 1.265 + RayHit nearest; 1.266 + nearest.dist = FLT_MAX; 1.267 + for(size_t i=0; i<obj.size(); i++) { 1.268 + if(obj[i]->intersect(local_ray, hit)) { 1.269 + if(!hit) return true; 1.270 + if(hit->dist < nearest.dist) { 1.271 + nearest = *hit; 1.272 + } 1.273 + } 1.274 + } 1.275 + 1.276 + if(nearest.dist < FLT_MAX) { 1.277 + *hit = nearest; 1.278 + hit->local_ray = local_ray; 1.279 + hit->world_ray = ray; 1.280 + return true; 1.281 + } 1.282 + return false; 1.283 +}