| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of OGRE |
|---|
| 4 | (Object-oriented Graphics Rendering Engine) |
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
|---|
| 8 | Also see acknowledgements in Readme.html |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | |
|---|
| 24 | You may alternatively use this source under the terms of a specific version of |
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
|---|
| 26 | Torus Knot Software Ltd. |
|---|
| 27 | ----------------------------------------------------------------------------- |
|---|
| 28 | */ |
|---|
| 29 | #include "OgreStableHeaders.h" |
|---|
| 30 | #include "OgreNode.h" |
|---|
| 31 | |
|---|
| 32 | #include "OgreException.h" |
|---|
| 33 | #include "OgreMath.h" |
|---|
| 34 | |
|---|
| 35 | // Dependencies on render-related types due to ability to render node |
|---|
| 36 | #include "OgreMaterialManager.h" |
|---|
| 37 | #include "OgreMeshManager.h" |
|---|
| 38 | #include "OgreMesh.h" |
|---|
| 39 | #include "OgreSubMesh.h" |
|---|
| 40 | #include "OgreCamera.h" |
|---|
| 41 | |
|---|
| 42 | namespace Ogre { |
|---|
| 43 | |
|---|
| 44 | unsigned long Node::msNextGeneratedNameExt = 1; |
|---|
| 45 | Node::QueuedUpdates Node::msQueuedUpdates; |
|---|
| 46 | //----------------------------------------------------------------------- |
|---|
| 47 | Node::Node() |
|---|
| 48 | :mParent(0), |
|---|
| 49 | mNeedParentUpdate(false), |
|---|
| 50 | mNeedChildUpdate(false), |
|---|
| 51 | mParentNotified(false), |
|---|
| 52 | mQueuedForUpdate(false), |
|---|
| 53 | mOrientation(Quaternion::IDENTITY), |
|---|
| 54 | mPosition(Vector3::ZERO), |
|---|
| 55 | mScale(Vector3::UNIT_SCALE), |
|---|
| 56 | mInheritOrientation(true), |
|---|
| 57 | mInheritScale(true), |
|---|
| 58 | mDerivedOrientation(Quaternion::IDENTITY), |
|---|
| 59 | mDerivedPosition(Vector3::ZERO), |
|---|
| 60 | mDerivedScale(Vector3::UNIT_SCALE), |
|---|
| 61 | mInitialPosition(Vector3::ZERO), |
|---|
| 62 | mInitialOrientation(Quaternion::IDENTITY), |
|---|
| 63 | mInitialScale(Vector3::UNIT_SCALE), |
|---|
| 64 | mCachedTransformOutOfDate(true), |
|---|
| 65 | mListener(0) |
|---|
| 66 | { |
|---|
| 67 | // Generate a name |
|---|
| 68 | StringUtil::StrStreamType str; |
|---|
| 69 | str << "Unnamed_" << msNextGeneratedNameExt++; |
|---|
| 70 | mName = str.str(); |
|---|
| 71 | |
|---|
| 72 | needUpdate(); |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | //----------------------------------------------------------------------- |
|---|
| 76 | Node::Node(const String& name) : Renderable(), |
|---|
| 77 | mParent(0), |
|---|
| 78 | mNeedParentUpdate(false), |
|---|
| 79 | mNeedChildUpdate(false), |
|---|
| 80 | mParentNotified(false), |
|---|
| 81 | mQueuedForUpdate(false), |
|---|
| 82 | mName(name), |
|---|
| 83 | mOrientation(Quaternion::IDENTITY), |
|---|
| 84 | mPosition(Vector3::ZERO), |
|---|
| 85 | mScale(Vector3::UNIT_SCALE), |
|---|
| 86 | mInheritOrientation(true), |
|---|
| 87 | mInheritScale(true), |
|---|
| 88 | mDerivedOrientation(Quaternion::IDENTITY), |
|---|
| 89 | mDerivedPosition(Vector3::ZERO), |
|---|
| 90 | mDerivedScale(Vector3::UNIT_SCALE), |
|---|
| 91 | mInitialPosition(Vector3::ZERO), |
|---|
| 92 | mInitialOrientation(Quaternion::IDENTITY), |
|---|
| 93 | mInitialScale(Vector3::UNIT_SCALE), |
|---|
| 94 | mCachedTransformOutOfDate(true), |
|---|
| 95 | mListener(0) |
|---|
| 96 | |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | needUpdate(); |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | //----------------------------------------------------------------------- |
|---|
| 104 | Node::~Node() |
|---|
| 105 | { |
|---|
| 106 | // Call listener (note, only called if there's something to do) |
|---|
| 107 | if (mListener) |
|---|
| 108 | { |
|---|
| 109 | mListener->nodeDestroyed(this); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | removeAllChildren(); |
|---|
| 113 | if(mParent) |
|---|
| 114 | mParent->removeChild(this); |
|---|
| 115 | |
|---|
| 116 | if (mQueuedForUpdate) |
|---|
| 117 | { |
|---|
| 118 | // Erase from queued updates |
|---|
| 119 | QueuedUpdates::iterator it = |
|---|
| 120 | std::find(msQueuedUpdates.begin(), msQueuedUpdates.end(), this); |
|---|
| 121 | assert(it != msQueuedUpdates.end()); |
|---|
| 122 | if (it != msQueuedUpdates.end()) |
|---|
| 123 | { |
|---|
| 124 | // Optimised algorithm to erase an element from unordered vector. |
|---|
| 125 | *it = msQueuedUpdates.back(); |
|---|
| 126 | msQueuedUpdates.pop_back(); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | //----------------------------------------------------------------------- |
|---|
| 131 | Node* Node::getParent(void) const |
|---|
| 132 | { |
|---|
| 133 | return mParent; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | //----------------------------------------------------------------------- |
|---|
| 137 | void Node::setParent(Node* parent) |
|---|
| 138 | { |
|---|
| 139 | bool different = (parent != mParent); |
|---|
| 140 | |
|---|
| 141 | mParent = parent; |
|---|
| 142 | // Request update from parent |
|---|
| 143 | mParentNotified = false ; |
|---|
| 144 | needUpdate(); |
|---|
| 145 | |
|---|
| 146 | // Call listener (note, only called if there's something to do) |
|---|
| 147 | if (mListener && different) |
|---|
| 148 | { |
|---|
| 149 | if (mParent) |
|---|
| 150 | mListener->nodeAttached(this); |
|---|
| 151 | else |
|---|
| 152 | mListener->nodeDetached(this); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | //----------------------------------------------------------------------- |
|---|
| 158 | const Matrix4& Node::_getFullTransform(void) const |
|---|
| 159 | { |
|---|
| 160 | if (mCachedTransformOutOfDate) |
|---|
| 161 | { |
|---|
| 162 | // Use derived values |
|---|
| 163 | mCachedTransform.makeTransform( |
|---|
| 164 | _getDerivedPosition(), |
|---|
| 165 | _getDerivedScale(), |
|---|
| 166 | _getDerivedOrientation()); |
|---|
| 167 | mCachedTransformOutOfDate = false; |
|---|
| 168 | } |
|---|
| 169 | return mCachedTransform; |
|---|
| 170 | } |
|---|
| 171 | //----------------------------------------------------------------------- |
|---|
| 172 | void Node::_update(bool updateChildren, bool parentHasChanged) |
|---|
| 173 | { |
|---|
| 174 | // always clear information about parent notification |
|---|
| 175 | mParentNotified = false ; |
|---|
| 176 | |
|---|
| 177 | // Short circuit the off case |
|---|
| 178 | if (!updateChildren && !mNeedParentUpdate && !mNeedChildUpdate && !parentHasChanged ) |
|---|
| 179 | { |
|---|
| 180 | return; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | // See if we should process everyone |
|---|
| 185 | if (mNeedParentUpdate || parentHasChanged) |
|---|
| 186 | { |
|---|
| 187 | // Update transforms from parent |
|---|
| 188 | _updateFromParent(); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | if (mNeedChildUpdate || parentHasChanged) |
|---|
| 192 | { |
|---|
| 193 | |
|---|
| 194 | ChildNodeMap::iterator it, itend; |
|---|
| 195 | itend = mChildren.end(); |
|---|
| 196 | for (it = mChildren.begin(); it != itend; ++it) |
|---|
| 197 | { |
|---|
| 198 | Node* child = it->second; |
|---|
| 199 | child->_update(true, true); |
|---|
| 200 | } |
|---|
| 201 | mChildrenToUpdate.clear(); |
|---|
| 202 | } |
|---|
| 203 | else |
|---|
| 204 | { |
|---|
| 205 | // Just update selected children |
|---|
| 206 | |
|---|
| 207 | ChildUpdateSet::iterator it, itend; |
|---|
| 208 | itend = mChildrenToUpdate.end(); |
|---|
| 209 | for(it = mChildrenToUpdate.begin(); it != itend; ++it) |
|---|
| 210 | { |
|---|
| 211 | Node* child = *it; |
|---|
| 212 | child->_update(true, false); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | mChildrenToUpdate.clear(); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | mNeedChildUpdate = false; |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | //----------------------------------------------------------------------- |
|---|
| 222 | void Node::_updateFromParent(void) const |
|---|
| 223 | { |
|---|
| 224 | updateFromParentImpl(); |
|---|
| 225 | |
|---|
| 226 | // Call listener (note, this method only called if there's something to do) |
|---|
| 227 | if (mListener) |
|---|
| 228 | { |
|---|
| 229 | mListener->nodeUpdated(this); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | //----------------------------------------------------------------------- |
|---|
| 233 | void Node::updateFromParentImpl(void) const |
|---|
| 234 | { |
|---|
| 235 | if (mParent) |
|---|
| 236 | { |
|---|
| 237 | // Update orientation |
|---|
| 238 | const Quaternion& parentOrientation = mParent->_getDerivedOrientation(); |
|---|
| 239 | if (mInheritOrientation) |
|---|
| 240 | { |
|---|
| 241 | // Combine orientation with that of parent |
|---|
| 242 | mDerivedOrientation = parentOrientation * mOrientation; |
|---|
| 243 | } |
|---|
| 244 | else |
|---|
| 245 | { |
|---|
| 246 | // No inheritence |
|---|
| 247 | mDerivedOrientation = mOrientation; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // Update scale |
|---|
| 251 | const Vector3& parentScale = mParent->_getDerivedScale(); |
|---|
| 252 | if (mInheritScale) |
|---|
| 253 | { |
|---|
| 254 | // Scale own position by parent scale, NB just combine |
|---|
| 255 | // as equivalent axes, no shearing |
|---|
| 256 | mDerivedScale = parentScale * mScale; |
|---|
| 257 | } |
|---|
| 258 | else |
|---|
| 259 | { |
|---|
| 260 | // No inheritence |
|---|
| 261 | mDerivedScale = mScale; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | // Change position vector based on parent's orientation & scale |
|---|
| 265 | mDerivedPosition = parentOrientation * (parentScale * mPosition); |
|---|
| 266 | |
|---|
| 267 | // Add altered position vector to parents |
|---|
| 268 | mDerivedPosition += mParent->_getDerivedPosition(); |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | // Root node, no parent |
|---|
| 273 | mDerivedOrientation = mOrientation; |
|---|
| 274 | mDerivedPosition = mPosition; |
|---|
| 275 | mDerivedScale = mScale; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | mCachedTransformOutOfDate = true; |
|---|
| 279 | mNeedParentUpdate = false; |
|---|
| 280 | |
|---|
| 281 | } |
|---|
| 282 | //----------------------------------------------------------------------- |
|---|
| 283 | Node* Node::createChild(const Vector3& translate, const Quaternion& rotate) |
|---|
| 284 | { |
|---|
| 285 | Node* newNode = createChildImpl(); |
|---|
| 286 | newNode->translate(translate); |
|---|
| 287 | newNode->rotate(rotate); |
|---|
| 288 | this->addChild(newNode); |
|---|
| 289 | |
|---|
| 290 | return newNode; |
|---|
| 291 | } |
|---|
| 292 | //----------------------------------------------------------------------- |
|---|
| 293 | Node* Node::createChild(const String& name, const Vector3& translate, const Quaternion& rotate) |
|---|
| 294 | { |
|---|
| 295 | Node* newNode = createChildImpl(name); |
|---|
| 296 | newNode->translate(translate); |
|---|
| 297 | newNode->rotate(rotate); |
|---|
| 298 | this->addChild(newNode); |
|---|
| 299 | |
|---|
| 300 | return newNode; |
|---|
| 301 | } |
|---|
| 302 | //----------------------------------------------------------------------- |
|---|
| 303 | void Node::addChild(Node* child) |
|---|
| 304 | { |
|---|
| 305 | if (child->mParent) |
|---|
| 306 | { |
|---|
| 307 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
|---|
| 308 | "Node '" + child->getName() + "' already was a child of '" + |
|---|
| 309 | child->mParent->getName() + "'.", |
|---|
| 310 | "Node::addChild"); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | mChildren.insert(ChildNodeMap::value_type(child->getName(), child)); |
|---|
| 314 | child->setParent(this); |
|---|
| 315 | |
|---|
| 316 | } |
|---|
| 317 | //----------------------------------------------------------------------- |
|---|
| 318 | unsigned short Node::numChildren(void) const |
|---|
| 319 | { |
|---|
| 320 | return static_cast< unsigned short >( mChildren.size() ); |
|---|
| 321 | } |
|---|
| 322 | //----------------------------------------------------------------------- |
|---|
| 323 | Node* Node::getChild(unsigned short index) const |
|---|
| 324 | { |
|---|
| 325 | if( index < mChildren.size() ) |
|---|
| 326 | { |
|---|
| 327 | ChildNodeMap::const_iterator i = mChildren.begin(); |
|---|
| 328 | while (index--) ++i; |
|---|
| 329 | return i->second; |
|---|
| 330 | } |
|---|
| 331 | else |
|---|
| 332 | return NULL; |
|---|
| 333 | } |
|---|
| 334 | //----------------------------------------------------------------------- |
|---|
| 335 | Node* Node::removeChild(unsigned short index) |
|---|
| 336 | { |
|---|
| 337 | Node* ret; |
|---|
| 338 | if (index < mChildren.size()) |
|---|
| 339 | { |
|---|
| 340 | ChildNodeMap::iterator i = mChildren.begin(); |
|---|
| 341 | while (index--) ++i; |
|---|
| 342 | ret = i->second; |
|---|
| 343 | // cancel any pending update |
|---|
| 344 | cancelUpdate(ret); |
|---|
| 345 | |
|---|
| 346 | mChildren.erase(i); |
|---|
| 347 | ret->setParent(NULL); |
|---|
| 348 | return ret; |
|---|
| 349 | } |
|---|
| 350 | else |
|---|
| 351 | { |
|---|
| 352 | OGRE_EXCEPT( |
|---|
| 353 | Exception::ERR_INVALIDPARAMS, |
|---|
| 354 | "Child index out of bounds.", |
|---|
| 355 | "Node::getChild" ); |
|---|
| 356 | } |
|---|
| 357 | return 0; |
|---|
| 358 | } |
|---|
| 359 | //----------------------------------------------------------------------- |
|---|
| 360 | Node* Node::removeChild(Node* child) |
|---|
| 361 | { |
|---|
| 362 | if (child) |
|---|
| 363 | { |
|---|
| 364 | ChildNodeMap::iterator i = mChildren.find(child->getName()); |
|---|
| 365 | // ensure it's our child |
|---|
| 366 | if (i != mChildren.end() && i->second == child) |
|---|
| 367 | { |
|---|
| 368 | // cancel any pending update |
|---|
| 369 | cancelUpdate(child); |
|---|
| 370 | |
|---|
| 371 | mChildren.erase(i); |
|---|
| 372 | child->setParent(NULL); |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | return child; |
|---|
| 376 | } |
|---|
| 377 | //----------------------------------------------------------------------- |
|---|
| 378 | const Quaternion& Node::getOrientation() const |
|---|
| 379 | { |
|---|
| 380 | return mOrientation; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | //----------------------------------------------------------------------- |
|---|
| 384 | void Node::setOrientation( const Quaternion & q ) |
|---|
| 385 | { |
|---|
| 386 | mOrientation = q; |
|---|
| 387 | needUpdate(); |
|---|
| 388 | } |
|---|
| 389 | //----------------------------------------------------------------------- |
|---|
| 390 | void Node::setOrientation( Real w, Real x, Real y, Real z) |
|---|
| 391 | { |
|---|
| 392 | mOrientation.w = w; |
|---|
| 393 | mOrientation.x = x; |
|---|
| 394 | mOrientation.y = y; |
|---|
| 395 | mOrientation.z = z; |
|---|
| 396 | needUpdate(); |
|---|
| 397 | } |
|---|
| 398 | //----------------------------------------------------------------------- |
|---|
| 399 | void Node::resetOrientation(void) |
|---|
| 400 | { |
|---|
| 401 | mOrientation = Quaternion::IDENTITY; |
|---|
| 402 | needUpdate(); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | //----------------------------------------------------------------------- |
|---|
| 406 | void Node::setPosition(const Vector3& pos) |
|---|
| 407 | { |
|---|
| 408 | mPosition = pos; |
|---|
| 409 | needUpdate(); |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | //----------------------------------------------------------------------- |
|---|
| 414 | void Node::setPosition(Real x, Real y, Real z) |
|---|
| 415 | { |
|---|
| 416 | Vector3 v(x,y,z); |
|---|
| 417 | setPosition(v); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | //----------------------------------------------------------------------- |
|---|
| 421 | const Vector3 & Node::getPosition(void) const |
|---|
| 422 | { |
|---|
| 423 | return mPosition; |
|---|
| 424 | } |
|---|
| 425 | //----------------------------------------------------------------------- |
|---|
| 426 | Matrix3 Node::getLocalAxes(void) const |
|---|
| 427 | { |
|---|
| 428 | Vector3 axisX = Vector3::UNIT_X; |
|---|
| 429 | Vector3 axisY = Vector3::UNIT_Y; |
|---|
| 430 | Vector3 axisZ = Vector3::UNIT_Z; |
|---|
| 431 | |
|---|
| 432 | axisX = mOrientation * axisX; |
|---|
| 433 | axisY = mOrientation * axisY; |
|---|
| 434 | axisZ = mOrientation * axisZ; |
|---|
| 435 | |
|---|
| 436 | return Matrix3(axisX.x, axisY.x, axisZ.x, |
|---|
| 437 | axisX.y, axisY.y, axisZ.y, |
|---|
| 438 | axisX.z, axisY.z, axisZ.z); |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | //----------------------------------------------------------------------- |
|---|
| 442 | void Node::translate(const Vector3& d, TransformSpace relativeTo) |
|---|
| 443 | { |
|---|
| 444 | switch(relativeTo) |
|---|
| 445 | { |
|---|
| 446 | case TS_LOCAL: |
|---|
| 447 | // position is relative to parent so transform downwards |
|---|
| 448 | mPosition += mOrientation * d; |
|---|
| 449 | break; |
|---|
| 450 | case TS_WORLD: |
|---|
| 451 | // position is relative to parent so transform upwards |
|---|
| 452 | if (mParent) |
|---|
| 453 | { |
|---|
| 454 | mPosition += (mParent->_getDerivedOrientation().Inverse() * d) |
|---|
| 455 | / mParent->_getDerivedScale(); |
|---|
| 456 | } |
|---|
| 457 | else |
|---|
| 458 | { |
|---|
| 459 | mPosition += d; |
|---|
| 460 | } |
|---|
| 461 | break; |
|---|
| 462 | case TS_PARENT: |
|---|
| 463 | mPosition += d; |
|---|
| 464 | break; |
|---|
| 465 | } |
|---|
| 466 | needUpdate(); |
|---|
| 467 | |
|---|
| 468 | } |
|---|
| 469 | //----------------------------------------------------------------------- |
|---|
| 470 | void Node::translate(Real x, Real y, Real z, TransformSpace relativeTo) |
|---|
| 471 | { |
|---|
| 472 | Vector3 v(x,y,z); |
|---|
| 473 | translate(v, relativeTo); |
|---|
| 474 | } |
|---|
| 475 | //----------------------------------------------------------------------- |
|---|
| 476 | void Node::translate(const Matrix3& axes, const Vector3& move, TransformSpace relativeTo) |
|---|
| 477 | { |
|---|
| 478 | Vector3 derived = axes * move; |
|---|
| 479 | translate(derived, relativeTo); |
|---|
| 480 | } |
|---|
| 481 | //----------------------------------------------------------------------- |
|---|
| 482 | void Node::translate(const Matrix3& axes, Real x, Real y, Real z, TransformSpace relativeTo) |
|---|
| 483 | { |
|---|
| 484 | Vector3 d(x,y,z); |
|---|
| 485 | translate(axes,d,relativeTo); |
|---|
| 486 | } |
|---|
| 487 | //----------------------------------------------------------------------- |
|---|
| 488 | void Node::roll(const Radian& angle, TransformSpace relativeTo) |
|---|
| 489 | { |
|---|
| 490 | rotate(Vector3::UNIT_Z, angle, relativeTo); |
|---|
| 491 | } |
|---|
| 492 | //----------------------------------------------------------------------- |
|---|
| 493 | void Node::pitch(const Radian& angle, TransformSpace relativeTo) |
|---|
| 494 | { |
|---|
| 495 | rotate(Vector3::UNIT_X, angle, relativeTo); |
|---|
| 496 | } |
|---|
| 497 | //----------------------------------------------------------------------- |
|---|
| 498 | void Node::yaw(const Radian& angle, TransformSpace relativeTo) |
|---|
| 499 | { |
|---|
| 500 | rotate(Vector3::UNIT_Y, angle, relativeTo); |
|---|
| 501 | |
|---|
| 502 | } |
|---|
| 503 | //----------------------------------------------------------------------- |
|---|
| 504 | void Node::rotate(const Vector3& axis, const Radian& angle, TransformSpace relativeTo) |
|---|
| 505 | { |
|---|
| 506 | Quaternion q; |
|---|
| 507 | q.FromAngleAxis(angle,axis); |
|---|
| 508 | rotate(q, relativeTo); |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | //----------------------------------------------------------------------- |
|---|
| 512 | void Node::rotate(const Quaternion& q, TransformSpace relativeTo) |
|---|
| 513 | { |
|---|
| 514 | switch(relativeTo) |
|---|
| 515 | { |
|---|
| 516 | case TS_PARENT: |
|---|
| 517 | // Rotations are normally relative to local axes, transform up |
|---|
| 518 | mOrientation = q * mOrientation; |
|---|
| 519 | break; |
|---|
| 520 | case TS_WORLD: |
|---|
| 521 | // Rotations are normally relative to local axes, transform up |
|---|
| 522 | mOrientation = mOrientation * _getDerivedOrientation().Inverse() |
|---|
| 523 | * q * _getDerivedOrientation(); |
|---|
| 524 | break; |
|---|
| 525 | case TS_LOCAL: |
|---|
| 526 | // Note the order of the mult, i.e. q comes after |
|---|
| 527 | mOrientation = mOrientation * q; |
|---|
| 528 | break; |
|---|
| 529 | } |
|---|
| 530 | needUpdate(); |
|---|
| 531 | } |
|---|
| 532 | //----------------------------------------------------------------------- |
|---|
| 533 | const Quaternion & Node::_getDerivedOrientation(void) const |
|---|
| 534 | { |
|---|
| 535 | if (mNeedParentUpdate) |
|---|
| 536 | { |
|---|
| 537 | _updateFromParent(); |
|---|
| 538 | } |
|---|
| 539 | return mDerivedOrientation; |
|---|
| 540 | } |
|---|
| 541 | //----------------------------------------------------------------------- |
|---|
| 542 | const Vector3 & Node::_getDerivedPosition(void) const |
|---|
| 543 | { |
|---|
| 544 | if (mNeedParentUpdate) |
|---|
| 545 | { |
|---|
| 546 | _updateFromParent(); |
|---|
| 547 | } |
|---|
| 548 | return mDerivedPosition; |
|---|
| 549 | } |
|---|
| 550 | //----------------------------------------------------------------------- |
|---|
| 551 | const Vector3 & Node::_getDerivedScale(void) const |
|---|
| 552 | { |
|---|
| 553 | if (mNeedParentUpdate) |
|---|
| 554 | { |
|---|
| 555 | _updateFromParent(); |
|---|
| 556 | } |
|---|
| 557 | return mDerivedScale; |
|---|
| 558 | } |
|---|
| 559 | //----------------------------------------------------------------------- |
|---|
| 560 | void Node::removeAllChildren(void) |
|---|
| 561 | { |
|---|
| 562 | ChildNodeMap::iterator i, iend; |
|---|
| 563 | iend = mChildren.end(); |
|---|
| 564 | for (i = mChildren.begin(); i != iend; ++i) |
|---|
| 565 | { |
|---|
| 566 | i->second->setParent(0); |
|---|
| 567 | } |
|---|
| 568 | mChildren.clear(); |
|---|
| 569 | mChildrenToUpdate.clear(); |
|---|
| 570 | } |
|---|
| 571 | //----------------------------------------------------------------------- |
|---|
| 572 | void Node::setScale(const Vector3& scale) |
|---|
| 573 | { |
|---|
| 574 | mScale = scale; |
|---|
| 575 | needUpdate(); |
|---|
| 576 | } |
|---|
| 577 | //----------------------------------------------------------------------- |
|---|
| 578 | void Node::setScale(Real x, Real y, Real z) |
|---|
| 579 | { |
|---|
| 580 | mScale.x = x; |
|---|
| 581 | mScale.y = y; |
|---|
| 582 | mScale.z = z; |
|---|
| 583 | needUpdate(); |
|---|
| 584 | } |
|---|
| 585 | //----------------------------------------------------------------------- |
|---|
| 586 | const Vector3 & Node::getScale(void) const |
|---|
| 587 | { |
|---|
| 588 | return mScale; |
|---|
| 589 | } |
|---|
| 590 | //----------------------------------------------------------------------- |
|---|
| 591 | void Node::setInheritOrientation(bool inherit) |
|---|
| 592 | { |
|---|
| 593 | mInheritOrientation = inherit; |
|---|
| 594 | needUpdate(); |
|---|
| 595 | } |
|---|
| 596 | //----------------------------------------------------------------------- |
|---|
| 597 | bool Node::getInheritOrientation(void) const |
|---|
| 598 | { |
|---|
| 599 | return mInheritOrientation; |
|---|
| 600 | } |
|---|
| 601 | //----------------------------------------------------------------------- |
|---|
| 602 | void Node::setInheritScale(bool inherit) |
|---|
| 603 | { |
|---|
| 604 | mInheritScale = inherit; |
|---|
| 605 | needUpdate(); |
|---|
| 606 | } |
|---|
| 607 | //----------------------------------------------------------------------- |
|---|
| 608 | bool Node::getInheritScale(void) const |
|---|
| 609 | { |
|---|
| 610 | return mInheritScale; |
|---|
| 611 | } |
|---|
| 612 | //----------------------------------------------------------------------- |
|---|
| 613 | void Node::scale(const Vector3& scale) |
|---|
| 614 | { |
|---|
| 615 | mScale = mScale * scale; |
|---|
| 616 | needUpdate(); |
|---|
| 617 | |
|---|
| 618 | } |
|---|
| 619 | //----------------------------------------------------------------------- |
|---|
| 620 | void Node::scale(Real x, Real y, Real z) |
|---|
| 621 | { |
|---|
| 622 | mScale.x *= x; |
|---|
| 623 | mScale.y *= y; |
|---|
| 624 | mScale.z *= z; |
|---|
| 625 | needUpdate(); |
|---|
| 626 | |
|---|
| 627 | } |
|---|
| 628 | //----------------------------------------------------------------------- |
|---|
| 629 | const String& Node::getName(void) const |
|---|
| 630 | { |
|---|
| 631 | return mName; |
|---|
| 632 | } |
|---|
| 633 | //----------------------------------------------------------------------- |
|---|
| 634 | const MaterialPtr& Node::getMaterial(void) const |
|---|
| 635 | { |
|---|
| 636 | if (mpMaterial.isNull()) |
|---|
| 637 | { |
|---|
| 638 | mpMaterial = MaterialManager::getSingleton().getByName("Core/NodeMaterial"); |
|---|
| 639 | if (mpMaterial.isNull()) |
|---|
| 640 | OGRE_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, "Could not find material Core/NodeMaterial", |
|---|
| 641 | "Node::getMaterial" ); |
|---|
| 642 | mpMaterial->load(); |
|---|
| 643 | } |
|---|
| 644 | return mpMaterial; |
|---|
| 645 | |
|---|
| 646 | } |
|---|
| 647 | //----------------------------------------------------------------------- |
|---|
| 648 | void Node::getRenderOperation(RenderOperation& op) |
|---|
| 649 | { |
|---|
| 650 | static SubMesh* pSubMesh = 0; |
|---|
| 651 | if (!pSubMesh) |
|---|
| 652 | { |
|---|
| 653 | MeshPtr pMesh = MeshManager::getSingleton().load("axes.mesh", |
|---|
| 654 | ResourceGroupManager::BOOTSTRAP_RESOURCE_GROUP_NAME); |
|---|
| 655 | pSubMesh = pMesh->getSubMesh(0); |
|---|
| 656 | } |
|---|
| 657 | pSubMesh->_getRenderOperation(op); |
|---|
| 658 | } |
|---|
| 659 | //----------------------------------------------------------------------- |
|---|
| 660 | void Node::getWorldTransforms(Matrix4* xform) const |
|---|
| 661 | { |
|---|
| 662 | // Assumes up to date |
|---|
| 663 | *xform = this->_getFullTransform(); |
|---|
| 664 | } |
|---|
| 665 | //----------------------------------------------------------------------- |
|---|
| 666 | const Quaternion& Node::getWorldOrientation(void) const |
|---|
| 667 | { |
|---|
| 668 | return _getDerivedOrientation(); |
|---|
| 669 | } |
|---|
| 670 | //----------------------------------------------------------------------- |
|---|
| 671 | const Vector3& Node::getWorldPosition(void) const |
|---|
| 672 | { |
|---|
| 673 | return _getDerivedPosition(); |
|---|
| 674 | } |
|---|
| 675 | //----------------------------------------------------------------------- |
|---|
| 676 | void Node::setInitialState(void) |
|---|
| 677 | { |
|---|
| 678 | mInitialPosition = mPosition; |
|---|
| 679 | mInitialOrientation = mOrientation; |
|---|
| 680 | mInitialScale = mScale; |
|---|
| 681 | } |
|---|
| 682 | //----------------------------------------------------------------------- |
|---|
| 683 | void Node::resetToInitialState(void) |
|---|
| 684 | { |
|---|
| 685 | mPosition = mInitialPosition; |
|---|
| 686 | mOrientation = mInitialOrientation; |
|---|
| 687 | mScale = mInitialScale; |
|---|
| 688 | |
|---|
| 689 | needUpdate(); |
|---|
| 690 | } |
|---|
| 691 | //----------------------------------------------------------------------- |
|---|
| 692 | const Vector3& Node::getInitialPosition(void) const |
|---|
| 693 | { |
|---|
| 694 | return mInitialPosition; |
|---|
| 695 | } |
|---|
| 696 | //----------------------------------------------------------------------- |
|---|
| 697 | const Quaternion& Node::getInitialOrientation(void) const |
|---|
| 698 | { |
|---|
| 699 | return mInitialOrientation; |
|---|
| 700 | |
|---|
| 701 | } |
|---|
| 702 | //----------------------------------------------------------------------- |
|---|
| 703 | const Vector3& Node::getInitialScale(void) const |
|---|
| 704 | { |
|---|
| 705 | return mInitialScale; |
|---|
| 706 | } |
|---|
| 707 | //----------------------------------------------------------------------- |
|---|
| 708 | Node* Node::getChild(const String& name) const |
|---|
| 709 | { |
|---|
| 710 | ChildNodeMap::const_iterator i = mChildren.find(name); |
|---|
| 711 | |
|---|
| 712 | if (i == mChildren.end()) |
|---|
| 713 | { |
|---|
| 714 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Child node named " + name + |
|---|
| 715 | " does not exist.", "Node::getChild"); |
|---|
| 716 | } |
|---|
| 717 | return i->second; |
|---|
| 718 | |
|---|
| 719 | } |
|---|
| 720 | //----------------------------------------------------------------------- |
|---|
| 721 | Node* Node::removeChild(const String& name) |
|---|
| 722 | { |
|---|
| 723 | ChildNodeMap::iterator i = mChildren.find(name); |
|---|
| 724 | |
|---|
| 725 | if (i == mChildren.end()) |
|---|
| 726 | { |
|---|
| 727 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Child node named " + name + |
|---|
| 728 | " does not exist.", "Node::removeChild"); |
|---|
| 729 | } |
|---|
| 730 | |
|---|
| 731 | Node* ret = i->second; |
|---|
| 732 | // Cancel any pending update |
|---|
| 733 | cancelUpdate(ret); |
|---|
| 734 | |
|---|
| 735 | mChildren.erase(i); |
|---|
| 736 | ret->setParent(NULL); |
|---|
| 737 | |
|---|
| 738 | return ret; |
|---|
| 739 | |
|---|
| 740 | |
|---|
| 741 | } |
|---|
| 742 | //----------------------------------------------------------------------- |
|---|
| 743 | Node::ChildNodeIterator Node::getChildIterator(void) |
|---|
| 744 | { |
|---|
| 745 | return ChildNodeIterator(mChildren.begin(), mChildren.end()); |
|---|
| 746 | } |
|---|
| 747 | //----------------------------------------------------------------------- |
|---|
| 748 | Node::ConstChildNodeIterator Node::getChildIterator(void) const |
|---|
| 749 | { |
|---|
| 750 | return ConstChildNodeIterator(mChildren.begin(), mChildren.end()); |
|---|
| 751 | } |
|---|
| 752 | //----------------------------------------------------------------------- |
|---|
| 753 | Real Node::getSquaredViewDepth(const Camera* cam) const |
|---|
| 754 | { |
|---|
| 755 | Vector3 diff = _getDerivedPosition() - cam->getDerivedPosition(); |
|---|
| 756 | |
|---|
| 757 | // NB use squared length rather than real depth to avoid square root |
|---|
| 758 | return diff.squaredLength(); |
|---|
| 759 | } |
|---|
| 760 | //----------------------------------------------------------------------- |
|---|
| 761 | void Node::needUpdate(bool forceParentUpdate) |
|---|
| 762 | { |
|---|
| 763 | |
|---|
| 764 | mNeedParentUpdate = true; |
|---|
| 765 | mNeedChildUpdate = true; |
|---|
| 766 | mCachedTransformOutOfDate = true; |
|---|
| 767 | |
|---|
| 768 | // Make sure we're not root and parent hasn't been notified before |
|---|
| 769 | if (mParent && (!mParentNotified || forceParentUpdate)) |
|---|
| 770 | { |
|---|
| 771 | mParent->requestUpdate(this, forceParentUpdate); |
|---|
| 772 | mParentNotified = true ; |
|---|
| 773 | } |
|---|
| 774 | |
|---|
| 775 | // all children will be updated |
|---|
| 776 | mChildrenToUpdate.clear(); |
|---|
| 777 | } |
|---|
| 778 | //----------------------------------------------------------------------- |
|---|
| 779 | void Node::requestUpdate(Node* child, bool forceParentUpdate) |
|---|
| 780 | { |
|---|
| 781 | // If we're already going to update everything this doesn't matter |
|---|
| 782 | if (mNeedChildUpdate) |
|---|
| 783 | { |
|---|
| 784 | return; |
|---|
| 785 | } |
|---|
| 786 | |
|---|
| 787 | mChildrenToUpdate.insert(child); |
|---|
| 788 | // Request selective update of me, if we didn't do it before |
|---|
| 789 | if (mParent && (!mParentNotified || forceParentUpdate)) |
|---|
| 790 | { |
|---|
| 791 | mParent->requestUpdate(this, forceParentUpdate); |
|---|
| 792 | mParentNotified = true ; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | } |
|---|
| 796 | //----------------------------------------------------------------------- |
|---|
| 797 | void Node::cancelUpdate(Node* child) |
|---|
| 798 | { |
|---|
| 799 | mChildrenToUpdate.erase(child); |
|---|
| 800 | |
|---|
| 801 | // Propogate this up if we're done |
|---|
| 802 | if (mChildrenToUpdate.empty() && mParent && !mNeedChildUpdate) |
|---|
| 803 | { |
|---|
| 804 | mParent->cancelUpdate(this); |
|---|
| 805 | mParentNotified = false ; |
|---|
| 806 | } |
|---|
| 807 | } |
|---|
| 808 | //----------------------------------------------------------------------- |
|---|
| 809 | void Node::queueNeedUpdate(Node* n) |
|---|
| 810 | { |
|---|
| 811 | // Don't queue the node more than once |
|---|
| 812 | if (!n->mQueuedForUpdate) |
|---|
| 813 | { |
|---|
| 814 | n->mQueuedForUpdate = true; |
|---|
| 815 | msQueuedUpdates.push_back(n); |
|---|
| 816 | } |
|---|
| 817 | } |
|---|
| 818 | //----------------------------------------------------------------------- |
|---|
| 819 | void Node::processQueuedUpdates(void) |
|---|
| 820 | { |
|---|
| 821 | for (QueuedUpdates::iterator i = msQueuedUpdates.begin(); |
|---|
| 822 | i != msQueuedUpdates.end(); ++i) |
|---|
| 823 | { |
|---|
| 824 | // Update, and force parent update since chances are we've ended |
|---|
| 825 | // up with some mixed state in there due to re-entrancy |
|---|
| 826 | Node* n = *i; |
|---|
| 827 | n->mQueuedForUpdate = false; |
|---|
| 828 | n->needUpdate(true); |
|---|
| 829 | } |
|---|
| 830 | msQueuedUpdates.clear(); |
|---|
| 831 | } |
|---|
| 832 | //----------------------------------------------------------------------- |
|---|
| 833 | const LightList& Node::getLights(void) const |
|---|
| 834 | { |
|---|
| 835 | // Nodes should not be lit by the scene, this will not get called |
|---|
| 836 | static LightList ll; |
|---|
| 837 | return ll; |
|---|
| 838 | } |
|---|
| 839 | } |
|---|
| 840 | |
|---|