Time for action — building a field of grass

  1. We need several instances of our blades of grass, so convert the manual object into a mesh:
    manual->convertToMesh("BladesOfGrass");
    
  2. We want a field of grass which consists of 50 x 50 instances of our grass. So we need two for loops:
    for(int i=0;i<50;i++)
    {
    for(int j=0;j<50;j++)
    {
    
  3. Inside the for loops, create a nameless entity and a nameless scene node:
    Ogre::Entity * ent = mSceneMgr->createEntity("BladesOfGrass");
    Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(i*3,-10,j*3));
    node->attachObject(ent);
    
  4. Don't forget to close the for loops:
    }
    }
    
  5. Compile and run the application, and you should see a field of grass. Depending on your computer, this application might be pretty slow.
    Time for action — building a field of grass

What just happened?

In step 1, we used a new function from a manual object which converts the manual object into a mesh we can instantiate using the createEntity() function of the scene manager. To be able to use the new entity, we need a name that will be used later as a parameter for the createEntity() function. We used BladesOfGrass as a descriptive name. We want several instances of our grass, so we created two for loops in step 2, each running 50 times. Step 3 added the body of the for loop. In the body we first created a new entity using the mesh name we just created. An observant reader might notice that we didn't use the createEntity()function with two parameters one for the entity type and one for the name we want this entity to have. This time, we only gave the entity type as a parameter, not a name. But didn't we have to always give a name to an entity because each entity needs a unique name? This is still true; the function we called is just a helper function, which only needs an entity type name because it generates a unique name and then calls the function we always called. It just saves us the trouble of appending the for loop variables to a generic name like BladesOfGrassEntity. We used the same kind of function for scene node creation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.75