#include "SceneNode.h" #include "Scene.h" #include "util/Exception.h" #include #include //============================================================================== // Statics = //============================================================================== uint SceneNode::uid = 0; //============================================================================== // Constructor = //============================================================================== SceneNode::SceneNode(SceneNodeType type_, Scene& scene_, ulong flags_, SceneNode* parent_) : type(type_), flags(flags_), parent(parent_), scene(scene_) { ++uid; getWorldTransform().setIdentity(); getLocalTransform().setIdentity(); name = boost::lexical_cast(uid); enableFlag(SNF_ACTIVE); if(parent != NULL) { parent->addChild(*this); } // This goes last scene.registerNode(this); } //============================================================================== // Destructor = //============================================================================== SceneNode::~SceneNode() { scene.unregisterNode(this); if(parent != NULL) { parent->removeChild(*this); } if(isFlagEnabled(SNF_ON_DELETE_DELETE_CHILDREN)) { BOOST_REVERSE_FOREACH(SceneNode* child, children) { delete child; } } else { BOOST_REVERSE_FOREACH(SceneNode* child, children) { child->parent = NULL; } } } //============================================================================== // updateWorldTransform = //============================================================================== void SceneNode::updateWorldTransform() { prevWTrf = wTrf; if(parent) { if(isFlagEnabled(SNF_INHERIT_PARENT_TRANSFORM)) { wTrf = parent->getWorldTransform(); } else { wTrf = Transform::combineTransformations( parent->getWorldTransform(), lTrf); } } else // else copy { wTrf = lTrf; } } //============================================================================== // addChild = //============================================================================== void SceneNode::addChild(SceneNode& child) { ASSERT(child.parent == NULL); // Child already has parent child.parent = this; children.push_back(&child); } //============================================================================== // removeChild = //============================================================================== void SceneNode::removeChild(SceneNode& child) { Vec::iterator it = std::find(children.begin(), children.end(), &child); if(it == children.end()) { throw EXCEPTION("Child not found"); } children.erase(it); child.parent = NULL; } //============================================================================== // Move(s) = //============================================================================== void SceneNode::moveLocalX(float distance) { Vec3 x_axis = lTrf.getRotation().getColumn(0); getLocalTransform().getOrigin() += x_axis * distance; } void SceneNode::moveLocalY(float distance) { Vec3 y_axis = lTrf.getRotation().getColumn(1); getLocalTransform().getOrigin() += y_axis * distance; } void SceneNode::moveLocalZ(float distance) { Vec3 z_axis = lTrf.getRotation().getColumn(2); getLocalTransform().getOrigin() += z_axis * distance; }