A scene graph is essentially a tree hierarchy which contains all of the nodes and entities in your scene. At the very top is the root node, which is a parent of everything else in the scene. Underneath the root node will be all of its children, attached like branches. Each node can contain an entity, which can be a mesh or light or locator etc. The great thing about these nodes is that they each contain information about their respective entity, and keep everything organized.
A node contains information such as its parent, child, and all data pertaining to its attached entity. The entity will have values such as position, orientation, etc.
Ogre::Entity* Sun = mSceneMgr->createEntity("Sun","Sun.mesh");
Ogre::SceneNode* node = mSceneMgr->createSceneNode("RootNode");
mSceneMgr->getRootSceneNode()->addChild(SunNode);
SunNode->attachObject(Sun);
The code above will create an entity, the Sun, and a root node. It then adds a child to the
root node and attaches the entity to it. Since there were no translations involved, the entity should be placed at the origin, with no scaling or rotations from its original orientation. For the solar system question, this would be a good place to add the Sun, since all planets (and therefore moons) will rotate around it. This will work because entities will rotate relative to their parent node's entity.
Source: http://www.packtpub.com/article/ogre-scene-graph |
SunNode->addChild(EarthNode);
EarthNode->setPosition(100,0,0);EarthNode->attachObject(Earth);
Next we'll add a planet such as Earth to the scene graph tree, and offset it from the Sun's position. Now we have a child to the root node which is offset by 100 units in the x-axis. This new node also contains an entity, this time being the Earth mesh. It can be rotated on its own, but we want it to rotate based on the Sun's orientation. To achieve this revolving motion we will rotate the Sun itself.
SunNode->pitch(Ogre::Radian(Ogre::Math::HALF_PI));
Now the Sun has rotated 90 degrees, and the Earth will revolve around the sun, since it is the child node. Also make sure to keep in mind what coordinate space you are doing transformations in. A child note that has been rotated will translate differently, such as local space vs world space. Since not all planets are the same size (obviously!), the following function will also be needed if you have not pre-built every planet to scale.
EarthNode->scale(0.1f,0.1f,0.1f);
Using these simple functions, you can easily build the entire solar system, moons included, starting from the sun at the centre. Aside from creating a tree of nodes, the Ogre scene graph has many additional functions that can be taken advantage of, to create more complex scenes.