Page 1 of 2

Scene Node pointers...

Posted: Wed Jan 10, 2007 12:28 pm
by goaty
Is there a quick or simple way for validating whether or not a created scene node pointer i still in the scene manager list or draw? As in to pass a pointer to a function etc?

Thanks

Posted: Wed Jan 10, 2007 3:28 pm
by vitek
That's a bit of a weird question.

All you should need to do is check to see that the parent node is in the scene graph. Repeat that until you get to the scene managers root node or a NULL node. If you hit a NULL node, then your node is not in the graph, or it is a child of a node that is not in the graph.

Travis

Posted: Wed Jan 10, 2007 3:39 pm
by goaty
Not really.
So you're saying NO then there isn't a quick or simple function?

Posted: Wed Jan 10, 2007 3:48 pm
by vitek
Yes, really, there isn't an existing simple function that already does exactly what you want. That said, I explained exactly what you need to do to write one that does. You would need to write an entire 7 lines of code to solve your problem.

Travis

Posted: Thu Jan 11, 2007 8:48 am
by goaty
Go on then...

Posted: Thu Jan 11, 2007 4:47 pm
by vitek
Nope. I gave you enough information to be able to write the code yourself. I'll make it simpler for you... A node is in the scene graph if its ancestor [parent, grandparent, great grandparent, ...] is the root scene node.

Travis

Posted: Fri Jan 12, 2007 7:57 am
by goaty
Ha! Thanks anyway ObiWan! :roll:

Posted: Fri Jan 12, 2007 8:48 am
by hybrid
:lol:
Hey goaty, it will definitely help more if you get at least a little more involved with the internal structures of Irrlicht. Otherwise you will have to make your whole app by asking each time again. So give it a try. In case you still fail you just come back and show what you've got and we will be happily helping you out.

this may work

Posted: Fri Jan 12, 2007 5:17 pm
by harukiblue
This may work for what you are trying to do! Be nicer to each other BTW, we only have eachother. So maby dont be so rude to people who try to help goaty. 8)

EDIT * THIS CODE DOES NOT WORK (Endless loop)*

Code: Select all

scene::ISceneNode * NodeReader = smgr->addEmptySceneNode(0,-1);
bool IsInGraph = false;
NodeReader = NodeToRead->getParent ();

//look through for a specific node
while (NodeReader != NULL){
  if (NodeReader = NodeToFind)
     {
         bool IsInGraph = true;
      }
NodeReader = NodeReader->getParent();
}
EDIT * THIS CODE DOES NOT WORK (Endless loop)*

Posted: Fri Jan 12, 2007 5:57 pm
by vitek
So maby dont be so rude to people who try to help goaty
Maybe I'm the only one, but I think it is rude to expect others to write your code for you.

This is a simple exercise that he should be able to figure out given the description I provided in my previous posts. The goal is to help goaty to be self-sufficient. If he can't write a simple function to walk the parent tree, then he will have a lot of trouble when it comes to more complicated things in a rendering engine. He is not going to learn anything if we just give him the answer [and it appears that someone else already has].

Also, the code you provided will not work. IsInGraph would always be false, you always set IsInGraph to true, you create a scene node that you never use, and you continue to walk up the graph even though you are really done searching.

Travis

cont...

Posted: Fri Jan 12, 2007 7:26 pm
by harukiblue
Maybe I'm the only one, but I think it is rude to expect others to write your code for you.
I agree with you compleetly, that is why I posted that he should be more to polite to people who give him helpfull hints. BUT enough of that right, let me see if I can get this code right!

Code: Select all

int FindNode ( ISceneManager * smgr, 
                     ISceneNode * NodeToRead, 
                     ISceneNode * NodeToFind)
{
//create a pointer to a ISceneNode to temporarily hold the current
//node being read.
scene::ISceneNode * NodeReader = smgr->addEmptySceneNode(0,-1); 

//integer to store the generation
int Generation=0;

//Default is false, set to true if we find the node we are looking for.
bool IsInGraph = false; 

//store the starting pointer into the Reader
NodeReader = NodeToRead->getParent (); 

//look through the tree for a specific node 
while (NodeReader != NULL){ 
//Check to see if the curent Node in the tree is the one we are looking for.
  if (NodeReader == NodeToFind) 
     { 
        //the node is in the graph.
         bool IsInGraph = true;
       //exit the conditional loop 
         NodeReader = NULL;
      } 
NodeReader = NodeReader->getParent(); 
Generation++;
}

return Generation;
}

Posted: Fri Jan 12, 2007 8:25 pm
by vitek
Well your code would return the right result in most cases, but it is still not right. It would return 0 if the node you were searching for was the same as the node you started the search from. Also...
  1. Find is probably not a good function name, especially since this function only searches up the scene graph.
  2. You create NodeReader, but you don't need to create a node. You already have a pointer to the node to find and the node to start from. Just remove the creation of the empty scene node entirely.
  3. You create a new IsInGraph variable inside the if in your while loop. This prevents you from setting the IsInGraph variable outside the while loop to true. Actually, you don't use IsInGraph at all, so why bother?
Given the comments from my original two posts, I would hope that you would come up with this...

Code: Select all

bool ISceneNode_isInSceneGraph(ISceneNode* self, ISceneManager* smgr)
{
  ISceneNode* root = smgr->getRootSceneNode();
  while (self && (self != root))
    self = self->getParent();
  return self != 0;
}
To see if a node is in the scene graph, you would write

Code: Select all

bool isInSceneGraph = ISceneNode_isInSceneGraph(node, smgr);
Of course you could generalize that to allow you to see if a given node is an ancestor to another node...

Code: Select all

bool ISceneNode_isAncestor(ISceneNode* self, ISceneNode* ancestor)
{
  while (self && (self != ancestor))
    self = self->getParent();
  return self != 0;
}

bool ISceneNode_isInSceneGraph(ISceneNode* self, ISceneManager* smgr)
{
  return ISceneNode_isAncestor(self, smgr->getRootSceneNode());
}
Travis

I dont get it =(

Posted: Fri Jan 12, 2007 9:55 pm
by harukiblue
I dont understand what this means

Code: Select all

return self != 0;
BUT I'll take a guess . . .
if self != 0 return true else return false?
Sorry I guess I should brush up on some more C++ :(

also I think I miss understood what the purpose was because your first function does not check to see if it is inside the graph as far as I can tell (above the Node generation). I am probrally just an idiot! So I am sorry about that :wink:

woops

Posted: Fri Jan 12, 2007 9:59 pm
by harukiblue
I think that I mis read the while loop, I can see now that you do indeed cycle through the nodes, but inbetween each cycle you dont do anything? I thought that we were supposed to check to see if the current parrent of that cycle matched a specific node. Am I wrong?

Posted: Sat Jan 13, 2007 2:24 am
by vitek
A node is in the scene graph if its parent is in the scene graph or it is the root scene node. The first part of that is a recursive definition, but it is simple. Basically that says a node is in the scene graph if it has the root scene node as an ancestor.

The code loops looking for a NULL pointer or the root scene node. Every time through the loop, we look at the parent of the node that we last examined [an ancestor of the original node]. If we hit the root scene node, then the node is in the graph. If it ends with a NULL pointer, then the node is not in the graph.