This is an old revision of the document!
Table of Contents
LAB5: Scene Graph
This lab is based on “Chapter 6: Scene Managers” in the book “Ogre 3d 1.7 Beginner's Guide”.
Discussion
Discussion thread for this lab is here: Lab 5 Discussion Thread
Goal
The primary goal of this lab is to see how the representation of geometry in a scene can affect rendering performance. In particular, a scene where everything is represented as a movable object can get super heavy. Instead, you can “bake” meshes that won't move into static batches, which get optimized for rendering performance.
Preparation
No special preparation is required.
Lab Project
Follow these steps to complete the lab project:
- Create a New Project Create a new empty project called “Lab5” in the same way you have created new projects for other lab projects.
- Create a Manual Grass Object You will use the
createManualObject()
method in Ogre to specify new geometry inside your code representing a single bundle of grass straws. This method is handy for simple geometries or when you want to procedurally generate them. The grass bundle you create is essentially a set of three quads, each at a 60 degree angle to the next, and that all crossing in the middle - producing a star with 6 spikes if viewed from the top. If viewed from the side, you always see the front of one or two quads, regardless of where you stand. You can therefore make this object hold a texture that is visible from all directions, a property that is called billboarding. To get started, create a new method in yourMyApplication
class which you should callcreateGrass()
. Inside this method construct the quads and assign grass tecture to them. You construct them by specifying all the vertices first (in model space) and then the indices of each triangle that make up the three quads. Here is the code with the initialization of the manual object and the first quad specified:Ogre::ManualObject* manual = _sceneManager->createManualObject("grass"); manual->begin("Examples/GrassBlades", Ogre::RenderOperation::OT_TRIANGLE_LIST); // Vertices for quad 1 manual->position(5,0,0); manual->textureCoord(1,1); manual->position(-5,10,0); manual->textureCoord(0,0); manual->position(-5,0,0); manual->textureCoord(0,1); manual->position(5,10,0); manual->textureCoord(1,0); // Put vertices for other 2 quads here // ... // Triangles for quad 1 manual->index(0); manual->index(1); manual->index(2); manual->index(0); manual->index(3); manual->index(1); // Put triangles for other 2 quads here // ... // And now wrap up the object and add to scene graph for display manual->end(); Ogre::SceneNode* grassNode = _sceneManager->getRootSceneNode()->createChildSceneNode("GrassNode2"); grassNode->attachObject(manual);
To add the two missing quads, use the following corner vertex positions: Quad 2 = (2.5,0,4.3),(-2.5,10,-4.3), (-2.0,0,-4.3), (2.5,10,4.3), Quad 3 = (2.5,0,-4.3),(-2.5,10,4.3),(-2.0,0,4.3),(2.5,10,-4.3). Don't forget to give the vertices texture coordinates, which are the same for all quads. Now call
createGrass()
from yourcreateScene()
method and you should see a single bundle of grass right in the middle of the scene. - Instance Grass Object To create multiple instances of the object you created, you need to first convert it into a proper Ogre Mesh. In the
createGrass()
method, replace your last two lines with the following line:manual->convertToMesh("BladesOfGrass");
This essentially creates a new model with the name “BladesOfGrass”, which you can now refer to in subsequent
createEntity(<name>)
calls. So do that next: Let's create 2500 instances of this model, spread across the ground in a 50×50 square patch! You can do this using a nested for-loop (over i=0-49 and j=0-49) and inside the loop do the following:Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); Ogre::SceneNode* node = _sceneManager->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(i*3,-10,j*3)); node->attachObject(ent);
Notice that you are not naming each entity or node when you create them! But Ogre requires unique names for each of its nodes and entities. When a name is not given, Ogre actually assigns unique names. you can take a look at these names by printing them out inside the loop:
std::cout << node->getName() << "::"<< ent->getName() << std::endl;
(Just remember to remove the print-outs for later steps). Do you notice that with 2500 grass models the rendering has slowed dramatically?
- Optimize using Static Geometry If we assume that the each grass model will stay in the same location in world space throughout your application life, we can optimize rendering by bundling them up into a packet of pre-transformed static geometry. First create a single new
StaticGeometry
instance. Add this line after theconvertToMesh
call:Ogre::StaticGeometry* field = _sceneManager->createStaticGeometry("FieldOfGrass");
And now, instead of instancing the grass entities into the scene graph directly, we add them into the static geometry object in the body of our nested for-loop. So, the body of the loop becomes:
Ogre::Entity* ent = _sceneManager->createEntity("BladesOfGrass"); field->addEntity(ent,Ogre::Vector3(i*3,-10,j*3));
Finally, after we have added all the entities to our
field
, we need to ask the static geometry to “bake”/optimize with the following call:field->build();
Now you should try running the scene again and see if there is any improved performance!
When You Are Finished
Upload your commented source files into Lab5 in MySchool (zip them up if more than one). The lab projects will not be graded, but their completion counts towards your participation grade.