diff --git a/ogre/src/OgreNode.cc b/ogre/src/OgreNode.cc index 2a3196549..f96024c51 100644 --- a/ogre/src/OgreNode.cc +++ b/ogre/src/OgreNode.cc @@ -58,8 +58,13 @@ Ogre::SceneNode *OgreNode::Node() const void OgreNode::Destroy() { BaseNode::Destroy(); - Ogre::SceneManager *ogreSceneManager = this->scene->OgreSceneManager(); - ogreSceneManager->destroySceneNode(this->ogreNode); + + if (nullptr != this->scene) + { + Ogre::SceneManager *ogreSceneManager = this->scene->OgreSceneManager(); + if (nullptr != ogreSceneManager) + ogreSceneManager->destroySceneNode(this->ogreNode); + } this->ogreNode = nullptr; } @@ -82,24 +87,36 @@ void OgreNode::SetRawLocalPose(const math::Pose3d &_Pose3d) ////////////////////////////////////////////////// math::Vector3d OgreNode::RawLocalPosition() const { + if (nullptr == this->ogreNode) + return math::Vector3d(); + return OgreConversions::Convert(this->ogreNode->getPosition()); } ////////////////////////////////////////////////// void OgreNode::SetRawLocalPosition(const math::Vector3d &_position) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setPosition(OgreConversions::Convert(_position)); } ////////////////////////////////////////////////// math::Quaterniond OgreNode::RawLocalRotation() const { + if (nullptr == this->ogreNode) + return math::Quaterniond(); + return OgreConversions::Convert(this->ogreNode->getOrientation()); } ////////////////////////////////////////////////// void OgreNode::SetRawLocalRotation(const math::Quaterniond &_rotation) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setOrientation(OgreConversions::Convert(_rotation)); } @@ -117,12 +134,29 @@ void OgreNode::Load() ////////////////////////////////////////////////// void OgreNode::Init() { - Ogre::SceneManager *sceneManager; - sceneManager = this->scene->OgreSceneManager(); + if (nullptr == this->scene) + { + ignerr << "Failed to initialize node: scene is NULL" << std::endl; + return; + } + + auto sceneManager = this->scene->OgreSceneManager(); + if (nullptr == sceneManager) + { + ignerr << "Failed to initialize node: scene manager is NULL" << std::endl; + return; + } + this->ogreNode = sceneManager->createSceneNode(this->name); + if (nullptr == this->ogreNode) + { + ignerr << "Failed to create Ogre node" << std::endl; + return; + } this->ogreNode->setInheritScale(true); this->children = OgreNodeStorePtr(new OgreNodeStore); } + ////////////////////////////////////////////////// NodeStorePtr OgreNode::Children() const { @@ -132,6 +166,9 @@ NodeStorePtr OgreNode::Children() const ////////////////////////////////////////////////// bool OgreNode::AttachChild(NodePtr _child) { + if (nullptr == this->ogreNode) + return false; + OgreNodePtr derived = std::dynamic_pointer_cast(_child); if (!derived) @@ -149,6 +186,9 @@ bool OgreNode::AttachChild(NodePtr _child) ////////////////////////////////////////////////// bool OgreNode::DetachChild(NodePtr _child) { + if (nullptr == this->ogreNode) + return false; + OgreNodePtr derived = std::dynamic_pointer_cast(_child); if (!derived) @@ -159,6 +199,7 @@ bool OgreNode::DetachChild(NodePtr _child) } this->ogreNode->removeChild(derived->Node()); + return true; } @@ -172,24 +213,36 @@ OgreNodePtr OgreNode::SharedThis() ////////////////////////////////////////////////// math::Vector3d OgreNode::LocalScale() const { + if (nullptr == this->ogreNode) + return math::Vector3d(); + return OgreConversions::Convert(this->ogreNode->getScale()); } ////////////////////////////////////////////////// bool OgreNode::InheritScale() const { + if (nullptr == this->ogreNode) + return false; + return this->ogreNode->getInheritScale(); } ////////////////////////////////////////////////// void OgreNode::SetInheritScale(bool _inherit) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setInheritScale(_inherit); } ////////////////////////////////////////////////// void OgreNode::SetLocalScaleImpl(const math::Vector3d &_scale) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setScale(OgreConversions::Convert(_scale)); } diff --git a/ogre2/src/Ogre2Node.cc b/ogre2/src/Ogre2Node.cc index 63a43a936..88adc9fdc 100644 --- a/ogre2/src/Ogre2Node.cc +++ b/ogre2/src/Ogre2Node.cc @@ -65,8 +65,13 @@ Ogre::SceneNode *Ogre2Node::Node() const void Ogre2Node::Destroy() { BaseNode::Destroy(); - Ogre::SceneManager *ogreSceneManager = this->scene->OgreSceneManager(); - ogreSceneManager->destroySceneNode(this->ogreNode); + + if (nullptr != this->scene) + { + Ogre::SceneManager *ogreSceneManager = this->scene->OgreSceneManager(); + if (nullptr != ogreSceneManager) + ogreSceneManager->destroySceneNode(this->ogreNode); + } this->ogreNode = nullptr; } @@ -89,24 +94,36 @@ void Ogre2Node::SetRawLocalPose(const math::Pose3d &_Pose3d) ////////////////////////////////////////////////// math::Vector3d Ogre2Node::RawLocalPosition() const { + if (nullptr == this->ogreNode) + return math::Vector3d(); + return Ogre2Conversions::Convert(this->ogreNode->getPosition()); } ////////////////////////////////////////////////// void Ogre2Node::SetRawLocalPosition(const math::Vector3d &_position) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setPosition(Ogre2Conversions::Convert(_position)); } ////////////////////////////////////////////////// math::Quaterniond Ogre2Node::RawLocalRotation() const { + if (nullptr == this->ogreNode) + return math::Quaterniond(); + return Ogre2Conversions::Convert(this->ogreNode->getOrientation()); } ////////////////////////////////////////////////// void Ogre2Node::SetRawLocalRotation(const math::Quaterniond &_rotation) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setOrientation(Ogre2Conversions::Convert(_rotation)); } @@ -124,9 +141,25 @@ void Ogre2Node::Load() ////////////////////////////////////////////////// void Ogre2Node::Init() { - Ogre::SceneManager *sceneManager; - sceneManager = this->scene->OgreSceneManager(); + if (nullptr == this->scene) + { + ignerr << "Failed to initialize node: scene is NULL" << std::endl; + return; + } + + auto sceneManager = this->scene->OgreSceneManager(); + if (nullptr == sceneManager) + { + ignerr << "Failed to initialize node: scene manager is NULL" << std::endl; + return; + } + this->ogreNode = sceneManager->createSceneNode(); + if (nullptr == this->ogreNode) + { + ignerr << "Failed to create Ogre node" << std::endl; + return; + } this->ogreNode->setInheritScale(true); this->children = Ogre2NodeStorePtr(new Ogre2NodeStore); } @@ -140,6 +173,9 @@ NodeStorePtr Ogre2Node::Children() const ////////////////////////////////////////////////// bool Ogre2Node::AttachChild(NodePtr _child) { + if (nullptr == this->ogreNode) + return false; + Ogre2NodePtr derived = std::dynamic_pointer_cast(_child); if (!derived) @@ -184,6 +220,7 @@ bool Ogre2Node::DetachChild(NodePtr _child) } this->ogreNode->removeChild(derived->Node()); + return true; } @@ -197,24 +234,36 @@ Ogre2NodePtr Ogre2Node::SharedThis() ////////////////////////////////////////////////// math::Vector3d Ogre2Node::LocalScale() const { + if (nullptr == this->ogreNode) + return math::Vector3d(); + return Ogre2Conversions::Convert(this->ogreNode->getScale()); } ////////////////////////////////////////////////// bool Ogre2Node::InheritScale() const { + if (nullptr == this->ogreNode) + return false; + return this->ogreNode->getInheritScale(); } ////////////////////////////////////////////////// void Ogre2Node::SetInheritScale(bool _inherit) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setInheritScale(_inherit); } ////////////////////////////////////////////////// void Ogre2Node::SetLocalScaleImpl(const math::Vector3d &_scale) { + if (nullptr == this->ogreNode) + return; + this->ogreNode->setScale(Ogre2Conversions::Convert(_scale)); }