Scene Graph Root Node

Started by
8 comments, last by Shannon Barber 11 years, 4 months ago
Hi,
When a scene graph is implemented, the most of time there is a root node.
Is the root node really needed ?
Is it bad to have an array of node with parent to NULL who store children array pointer ?

Thanks
Advertisement
The good of a root node is you can apply any algorithm on a node to the root, you can not do it for an array.
For example, assume you have a function to find a special child node, you can call it on the root.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.



Is it bad to have an array of node with parent to NULL who store children array pointer ?



how is this different from having a root node that, incidentally, already has an array to children nodes?

anyway.. ya,it can be done if you really like duplicating code and functionality around for no good reason whatsoever.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

The difference is merely a conceptual issue... the only difference is that your application would be the "root", instead of having a well defined struct/class..

programatically there would be no difference, but it's cleaner and more intuitive for other programmers to have a root node from which everything "spawns"...

most of the time you will have to take desisions between two options which both work ok (and there's no performance gain/loss between them), but with the question of which way would you leave a be better experience to the people who might be using your code.. ;)
"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
This depends on what kind of behaviour you want to get out of your scene graph.
In a lot of cases where a hierarchical scene system is implemented the word 'graph' is actually a misnomer, as most of those implementations are tree structures (since it doesn't really make sense in most cases to have cycles in a scene graph unless you're doing some really funky stuff), and to build a tree structure you need a root node.

Of course you could store multiple trees, which is basically what you're presenting. If you store parentless nodes with children in an array you're basically building a forest, and each of these nodes will become roots for their own trees in that forest.
So if you want your scene to be divided into different independent trees, then it isn't necessarily 'bad' to store your different tree roots in an array like you said.

However, the fact that you're trying to do something like this should trigger an alarm-bell. You're trying to 'break the rules' of your chosen data structure to get some kind of different behaviour, which basically tells me that the data structure you've initially chosen (being a scene tree) isn't a good choice for the problem you're trying to solve.

Is there any reason why you need hierarchy within your scene as a default rule, rather than as an exceptional case?
I think a lot of people on these forums will agree with me that the concept of using just a scene tree/graph is starting to get outdated, as the concept of having a hierarchy between objects just plain doesn't make sense in a lot of cases.

Different systems will need different representations of your scene, a physics engine might need to have some sort of partitioned scene structure containing shape and transformation data so it can optimize its collision detection algorithms, while a rendering engine just needs a list of visual data and transformations to crunch. Culling algorithms might need another partitioned structure containing bounding volumes, which again is completely different from what the rendering and physics engine need.
I could go on like this for a while, but I think my point should be somewhat clear by now.

Basically what I'm trying to say is that it doesn't take all too long for the "stick it all in a tree" approach to start breaking down, so if you're at the point where you're trying to somewhat break free from this approach you should maybe try to rethink how you want to handle your scene entirely instead of trying to bend and twist your current approach in ways it wasn't designed for.

I gets all your texture budgets!

It's a conceptual difference, but there's also subtle but important details:

1. A node that is not part of the root node (an orphan node) is not in the scene and hence should not be rendered (this is important when Parent nodes don't own the memory of their children, but rather just have a pointer to it).


2. Consistency. If you create a new node you should call sceneGraph->createNode( pos, rot )->attachObject( myObj );
But if you're creating a child, you should call parent->createChildNode()->attachObject( myObj );
or call parent->addChild( myChild ); after creating it with sceneGraph::createNode (two ways of doing the same, and in both ways the code for creating a child is different from creating a parent-less node)

However, if there is a root node, your code is always the same:
parent->createChildNode()->attachObject( myObj );

Just feed the proper parent (which could be the root node) and you get a consistent behavior.


3. Many implementations are tree-based, like Radikalizm said. And trees start with a root node.

Having this said, you're not forced to have one root node. But I'm just answering your question (why so many implementations start from a root node). It's not bad to have an array of objects like you said. Particularly, I think reason #2 is a very good reason to use a root node for writing clean code. Reason #1 is just a convention & conceptual. Reason #3 depends on the implementation, it also depends on the literature being consulted (Which is often about having a root node, because the problem to solve is abstract, not specific towards graphics & scene management).

Cheers
Dark Sylinc
Using a tree, recursive function is used to render the scene and find an object by name.
Is it bad to use recursive function for these operations ?

Thanks

Using a tree, recursive function is used to render the scene and find an object by name.
Is it bad to use recursive function for these operations ?

Thanks

Trees lend themselves well to recursive algorithms, which makes implementation easier, however going recursive has a few disadvantages:
- it is often a bit slower than an iterative algorithm where you maintain your own stack
- because it is stack-based, it may not be easily implemented on platforms where a stack isn't readily available (e.g. GPU's)

On the other hand, non-recursive approaches can be a real pain to get right.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

in my case, I have implemented dynamic scene with world register, a tree. If you have all root node, you can implement special exeptional logic to it, for example, if you scan register as what nodes to remove from game scene based on distance, you will not examine the root, for if player get distant from root too much, it would have to disappear and remove whole subtree (everything). You should take care as what you implement , but I would advice you to use unique root, for you can get catching your hair if you implement multiple nodes, as you develop aplication further, but I cannot think of how you could get screwed if you implement an all root.
That's the start of a "b-tree". You still have a root-node, it's just an array instead of one node.
If you make all your nodes arrays then it's a b-tree.
For many applications b-trees are a superior design. They offer the recursive power of trees and the cache-coherency of arrays.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement