Tutorial 22 - LevelsIntroduction In this tutorial we will demonstrate one way to add multiple levels to your game engine, each containing different game objects. Concepts
Revisiting osg::Group Revisiting osg::Group We must remember when a child is attached to an osg::Group two links are created: One going from the child to the parent, and one going from the parent to the child. Adding children is easy, but what happens when we remove a child from an osg::Group?
As we would expect, the osg::Group::removeChild() function breaks the
link between both parent and child: Multiple Scenes How does this help us? Remember that Scene contains an osg::Group that acts as the root for the scene. This root is then attached directly to the osg::SceneView in Window via the osg::SceneView::setSceneData() function. In reality, the osg::Group is actually attached to an osg::Camera contained by the osg::SceneView. The reason for explaining that detail is that osg::Camera is derived class of osg::Group... meaning it can have children! Meaning we can add and remove any children via the process described in the previous section. Thus to change levels (scenes), the current scene (osg::Group) must be removed from the game (osg::Camera), a new scene (osg::Group) must be created and then attached to the game (osg::SceneView). Demonstration:
Note that for the tutorial code, the two classes of importance are Scene and System_Controller. Controls The tutorial code can be manipulated via the following keyboard input:
References
osg::SceneView Conclusion As can be noted, changing 'levels' is as easy as removing old objects from the scene and adding new ones. However, one must be sure that the old objects are removed properly, so careful use of ref_ptrs is well advised. |