Time for action — adding swords

As always, we are using the code from our previous excercises:

  1. At the end of the createScene() function, create two instances of the sword model and name them Sword1 and Sword2:
    Ogre::Entity* sword1 = mSceneMgr->createEntity("Sword1", "Sword.mesh");
    Ogre::Entity* sword2 = mSceneMgr->createEntity("Sword2", "Sword.mesh");
    
  2. Now attach the sword to the model using a bone name:
    _SinbadEnt->attachObjectToBone("Handle.L", sword1);
    _SinbadEnt->attachObjectToBone("Handle.R", sword2);
    
  3. Compile and run the application. There should be two swords in the hands of Sinbad.
    Time for action — adding swords

What just happened?

We created two instances of the sword model and attached them to bones. The creation of the instances shouldn't be too difficult to understand. The difficult and interesting part is the function called attachObjectToBone(). To understand what this function does, we need to discuss how animations are saved and played.

Animations

For animations, we are using so-called skeletons and bones. The system is inspired by nature; in nature, almost everything has a skeleton to support it. With the help of this skeleton and some muscles, animals and humans can move parts of their body in certain directions. As an example, we can make a fist with our fingers using the joints in our fingers. Animations in computer graphics work in a similar manner. The artist defines the 3D model and for this model a skeleton is created so it can be animated. The skeleton consists of bones and joints. The joints connect two bones and define in which direction the bones can move. Here is a really simplified drawing of such a skeleton; normally, they have a lot more bones.

Animations

The bones are assigned which triangle they affect when moved. Each bone only affects a part of the model: the bone for the left arm only modifies the triangles of the model which represent the left arm; all other triangles aren't modified.

Animations

With joints, bones, and the effect radius of the bones, an artist can create complex animations like the animations for the Sinbad model we are using. Like animations, the bones have names too. Ogre 3D lets us use these bones as a point to attach other entities to. This has the huge advantage that, when attached, the entity gets transformed just like the bone, meaning if we had an attachment point in the hands of Sinbad and attached the swords, they would always be in his hands because when the hands get transformed for an animation, the swords get the same transformation. If this function didn't exist, it would be almost impossible to give models something in their hands or attach things to their backs, like we just did with the swords.

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

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