Ogre SceneNode AttachedObject 内存释放方法

我的评论:removeAndDestroyAllChildren 这个函数待考察是否只释放了直接孩子节点

转载自:  https://forums.ogre3d.org/viewtopic.php?t=53647


Destroy SceneNode and everything attached to it?

Post Reply 
redeyerulk
Halfling
Posts:  40
Joined: Wed Sep 23, 2009 7:20 pm

Destroy SceneNode and everything attached to it?

Post by redeyerulk » Tue Nov 10, 2009 9:49 pm

So the question is: Is there in ogre some function which will completely remove SceneNode and all objects/other nodes attached to it? Or I just have to iterate through child nodes/attached objects and delete them one by one?
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Tue Nov 10, 2009 10:34 pm

SceneNode::removeAndDestroyAllChildren();

followed by

SceneManager::destroySceneNode( .. );
0 x
Image
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Tue Nov 10, 2009 10:37 pm

Whoops, that only destroys child SceneNodes, not attached objects.
I don't think there's a built in way to do this. I have a function like this:

CODE: SELECT ALL

void Scene::DestroyAllAttachedMovableObjects( SceneNode* i_pSceneNode )
{
   if ( !i_pSceneNode )
   {
      ASSERT( false );
      return;
   }

   // Destroy all the attached objects
   SceneNode::ObjectIterator itObject = i_pSceneNode->getAttachedObjectIterator();

   while ( itObject.hasMoreElements() )
   {
      MovableObject* pObject = static_cast<MovableObject*>(itObject.getNext());
      i_pSceneNode->getCreator()->destroyMovableObject( pObject );
   }

   // Recurse to child SceneNodes
   SceneNode::ChildNodeIterator itChild = i_pSceneNode->getChildIterator();

   while ( itChild.hasMoreElements() )
   {
      SceneNode* pChildNode = static_cast<SceneNode*>(itChild.getNext());
      DestroyAllAttachedMovableObjects( pChildNode );
   }
}
Scene is a specific class in my game, but you should be able to adapt this function to your code.
0 x
Image
Top
redeyerulk
Halfling
Posts:  40
Joined: Wed Sep 23, 2009 7:20 pm

Re: Destroy SceneNode and everything attached to it?

Post by redeyerulk » Wed Nov 11, 2009 5:10 pm

thx for the answer! I actually have BasicScene  ;). I think such function is worth including in OGRE.
0 x
Top
Ident
Gremlin
Posts:  155
Joined: Thu Sep 17, 2009 8:43 pm
Location: Austria
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Ident » Thu Nov 26, 2009 1:31 pm

thanks a lot, such a method is exactly what i was looking for ! this should really be implemented into the ogre engine, it's kinda essential and something most people will need in their projects at some point.

now i do this:

CODE: SELECT ALL

destroyAllAttachedMovableObjects(itemNode);
itemNode->removeAndDestroyAllChildren();
m_pSceneManager->destroySceneNode(itemNode);
that should detach and delete everything, right?

thanks again, and i hope this will resolve any memory leaking problems regarding scenenodes and their entities that could occur in my project  :D
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Thu Nov 26, 2009 6:45 pm

Ident wrote:now i do this:

CODE: SELECT ALL

destroyAllAttachedMovableObjects(itemNode);
itemNode->removeAndDestroyAllChildren();
m_pSceneManager->destroySceneNode(itemNode);
that should detach and delete everything, right?
Yep, that's the idea!
0 x
Image
Top
Ident
Gremlin
Posts:  155
Joined: Thu Sep 17, 2009 8:43 pm
Location: Austria
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Ident » Thu Nov 26, 2009 9:00 pm

thanks! saved me quite some time, good snippet

btw. i changed

CODE: SELECT ALL

 
if ( !i_pSceneNode )
   {
      ASSERT( false );
      return;
 }
to

CODE: SELECT ALL

if (i_pSceneNode )
{
because my IDE (VS2008) didn't know "ASSERT", just for general info
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Thu Nov 26, 2009 9:35 pm

Ident wrote:because my IDE (VS2008) didn't know "ASSERT", just for general info
Ahh right, thanks for pointing that out.
ASSERT is a custom macro I've defined for my game, so I can change the behavior between debug and release.

Ident's change works. Alternatively, you should also be able to just change it to lower-case "assert" as well.
0 x
Image
Top
lansglenn
Gnoblar
Posts:  3
Joined: Fri Jul 24, 2009 3:32 am

Re: Destroy SceneNode and everything attached to it?

Post by lansglenn » Fri Mar 05, 2010 11:20 am

I'm a bit confused as to why we need to execute all of the above code to full clear the scene?

I'm currently just doing:
mSceneMgr->clearScene();
mSceneMgr->destroyAllCameras();

To destroy all nodes & remove their links.

As stated in the documentation:
http://www.ogre3d.org/docs/api/html/cla ... d57f2dba7e

SceneManager::clearScene() is supposed to do all that right?
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Fri Mar 05, 2010 12:50 pm

The code above is just for nuking a specific SceneNode (and the stuff attached to it), not the whole scene.
You're right, if you're clearing the whole scene, this would be overkill.
0 x
Image
Top
JDX_John
Gnome
Posts:  397
Joined: Sat Nov 08, 2008 1:59 pm

Re: Destroy SceneNode and everything attached to it?

Post by JDX_John » Thu Nov 24, 2011 12:17 pm

I found this thread looking for the same thing... just wanted to check nothing changed in the 1.5 years since that thread? Such functionality still doesn't exist in Ogre itself?

btw in this line I found the cast is not needed:

CODE: SELECT ALL

MovableObject* pObject = static_cast<MovableObject*>(itObject.getNext());
So I simplified slightly to

CODE: SELECT ALL

while ( itObject.hasMoreElements() )
  node->getCreator()->destroyMovableObject(itObject.getNext());
And I now have these two handy functions you are all free to copy (since I copied them myself...)

CODE: SELECT ALL

void OgreUtil::destroyAllAttachedMovableObjects( SceneNode* node )
{
	if(!node) return;

	// Destroy all the attached objects
	SceneNode::ObjectIterator itObject = node->getAttachedObjectIterator();

	while ( itObject.hasMoreElements() )
		node->getCreator()->destroyMovableObject(itObject.getNext());

	// Recurse to child SceneNodes
	SceneNode::ChildNodeIterator itChild = node->getChildIterator();

	while ( itChild.hasMoreElements() )
	{
		SceneNode* pChildNode = static_cast<SceneNode*>(itChild.getNext());
		destroyAllAttachedMovableObjects( pChildNode );
	}
}

void OgreUtil::destroySceneNode( Ogre::SceneNode* node )
{
	if(!node) return;
	destroyAllAttachedMovableObjects(node);
	node->removeAndDestroyAllChildren();
	node->getCreator()->destroySceneNode(node);
}
OgreUtil is just a class I have with static methods.
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Thu Nov 24, 2011 3:34 pm

See? That's the great thing about posting code. People clean it up and make it prettier.  :)

I'm not aware of anything like this being added directly into Ogre. Although I haven't kept up on all the developments for release 1.8
0 x
Image
Top
JDX_John
Gnome
Posts:  397
Joined: Sat Nov 08, 2008 1:59 pm

Re: Destroy SceneNode and everything attached to it?

Post by JDX_John » Thu Nov 24, 2011 3:58 pm

BTW - I don't know if the forum rules implicitly cover this... Jabberwocky I assume you're happy for us using your code freely, since I gave permissions for people to use by tweaked version of your work?!?
0 x
Top
User avatar
Jabberwocky
OGRE Moderator
OGRE Moderator
Posts:  2819
Joined: Mon Mar 05, 2007 11:17 pm
Location: Canada
Contact: 

Re: Destroy SceneNode and everything attached to it?

Post by Jabberwocky » Thu Nov 24, 2011 4:40 pm

Yep, it's all cool. Glad it's been useful to some folks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值