//! //! Deletes a whole scene node tree (including attached objects). //! //! \param sceneNode The scene node containing the tree to delete. //! \param sceneManager The scene manager to use for destroying the scene node. //! \param deleteRoot Flag to control whether to delete the given scene node. //! void OgreTools::deepDeleteSceneNode ( Ogre::SceneNode *sceneNode, Ogre::SceneManager *sceneManager /* = 0 */, bool deleteRoot /* = false */ ) { // make sure the given scene node is valid if (!sceneNode) { Log::error("The given scene node is invalid.", "OgreTools::deepDeleteSceneNode"); return; } // make sure a valid scene manager is available if (!sceneManager) sceneManager = sceneNode->getCreator(); if (!sceneManager) { Log::error("No valid scene manager available.", "OgreTools::deepDeleteSceneNode"); return; } // iterate over the list of attached objects Ogre::SceneNode::ObjectIterator objectIterator = sceneNode->getAttachedObjectIterator(); while (objectIterator.hasMoreElements()) { Ogre::MovableObject *movableObject = objectIterator.getNext(); sceneNode->detachObject(movableObject); Ogre::SceneManager *movableSceneManager = movableObject->_getManager(); if (!movableSceneManager) { Log::error("The Ogre scene manager could not be obtained.", "OgreTools::deepDeleteSceneNode"); return; } Ogre::Any customData = movableObject->getUserAny(); if (!customData.isEmpty()) { OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData); if (ogreContainer) { delete ogreContainer; ogreContainer = 0; } } movableSceneManager->destroyMovableObject(movableObject); } // iterate over the list of child nodes Ogre::SceneNode::ChildNodeIterator childNodeIterator = sceneNode->getChildIterator(); while (childNodeIterator.hasMoreElements()) { Ogre::SceneNode *childSceneNode = dynamic_cast<Ogre::SceneNode *>(childNodeIterator.getNext()); if (childSceneNode) { Ogre::Any customData = childSceneNode->getUserAny(); if (!customData.isEmpty()) { OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData); if (ogreContainer) { delete ogreContainer; ogreContainer = 0; } else { CameraInfo *cameraInfo = Ogre::any_cast<CameraInfo *>(customData); if (cameraInfo) { delete cameraInfo; cameraInfo = 0; } } } deepDeleteSceneNode(childSceneNode, sceneManager); } } // destroy all child nodes of the given scene node sceneNode->removeAndDestroyAllChildren(); // check if the given scene node should be destroyed as well if (deleteRoot) { Ogre::Any customData = sceneNode->getUserAny(); if (!customData.isEmpty()) { OgreContainer *ogreContainer = Ogre::any_cast<OgreContainer *>(customData); if (ogreContainer) { delete ogreContainer; ogreContainer = 0; } else { CameraInfo *cameraInfo = Ogre::any_cast<CameraInfo *>(customData); if (cameraInfo) { delete cameraInfo; cameraInfo = 0; } } } sceneManager->destroySceneNode(sceneNode); } }
void CSaveSceneView::SceneNodeExplore(Ogre::SceneNode *SceneNode) { Ogre::Entity *Entity = NULL; Ogre::Camera *Camera = NULL; Ogre::Light *Light = NULL; Ogre::ParticleSystem *ParticleSystem = NULL; Ogre::ManualObject *ManualObject = NULL; Ogre::BillboardSet *BillboardSet = NULL; xmlTextWriterStartElement(m_XmlWriter, BAD_CAST "SceneNode"); Ogre::String SceneNodeName = SceneNode->getName(); xmlTextWriterWriteAttribute(m_XmlWriter, BAD_CAST "SceneNodeName", BAD_CAST SceneNodeName.c_str()); Ogre::SceneNode::ObjectIterator obji = SceneNode->getAttachedObjectIterator(); while (obji.hasMoreElements()) { Ogre::MovableObject* mobj = obji.getNext(); Ogre::String Type = mobj->getMovableType(); if (Type == "Entity") { Entity = (Ogre::Entity *)(mobj); Ogre::String EntityName = Entity->getName(); xmlTextWriterStartElement(m_XmlWriter, BAD_CAST "Entity"); xmlTextWriterWriteAttribute(m_XmlWriter, BAD_CAST "EntityName", BAD_CAST EntityName.c_str()); Ogre::MeshPtr Mesh = Entity->getMesh(); Ogre::String MeshName = Mesh->getName(); xmlTextWriterWriteAttribute(m_XmlWriter, BAD_CAST "MeshName", BAD_CAST MeshName.c_str()); xmlTextWriterEndElement(m_XmlWriter); } if (Type == "Camera") { Camera = (Ogre::Camera *)(mobj); Ogre::String CameraName = Camera->getName(); xmlTextWriterStartElement(m_XmlWriter, BAD_CAST "Camera"); xmlTextWriterWriteAttribute(m_XmlWriter, BAD_CAST "CameraName", BAD_CAST CameraName.c_str()); Ogre::Vector3 CameraPosition = Camera->getPosition(); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "XPosition", "%f",CameraPosition.x); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "YPosition", "%f",CameraPosition.y); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "ZPosition", "%f",CameraPosition.z); Ogre::Vector3 CameraDirection = Camera->getDirection(); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "XDirection", "%f",CameraDirection.x); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "YDirection", "%f",CameraDirection.y); xmlTextWriterWriteFormatAttribute(m_XmlWriter, BAD_CAST "ZDirection", "%f",CameraDirection.z); xmlTextWriterEndElement(m_XmlWriter); } if (Type == "Light") { Light = (Ogre::Light *)(mobj); } if (Type == "ParticleSystem") { ParticleSystem = (Ogre::ParticleSystem *)(mobj); } if (Type == "ManualObject") { ManualObject = (Ogre::ManualObject *)(mobj); } if (Type == "BillboardSet") { BillboardSet = (Ogre::BillboardSet *)(mobj); } } Ogre::Node::ChildNodeIterator nodei = SceneNode->getChildIterator(); while (nodei.hasMoreElements()) { Ogre::SceneNode* node = (Ogre::SceneNode*)(nodei.getNext()); // Add this subnode and its children... SceneNodeExplore(node); } xmlTextWriterEndElement(m_XmlWriter); //end SceneNode }