Based on the introduction in the previous chapter, we assume that you already know how to use the Ogre Project Wizard to initially create a executable 3DProgramThe 3D program displays an ogre avatar. Then we can learn the following things. Note: In view myArticleBefore, it is best to go to The http://ogre3d.cn to see the basic tutorial of the engine, although I are the basic tutorial, but, I do not have the site of the tutorial said in detail, I only take some notes during the learning process. Those are basic lessons and must be learned.
Before I start, I also want to declare that I will not repeat it in the future: many of myCodeThey are added at the process level and will rarely be added on the basis of the original, unless the added content is closely related to the original code. In this way, we will have a clear division of each part of the Code. In fact, this is also my habit of writing code. I hope everyone will develop a good habit.
Question:
Defines a 10-second animation that contains an animation track (up/down flip ). Apply the animation to the current camera. When the program is running, I (First Person camera) Should flip up and down. 1. First define an animation object, animationstate * manimstate; write this code in front of all the Code so that it can be used in this unit. 2. The program wizard has automatically created a camera for US (in the virtual void createcamera (void) process). 3. Create a process using the following code: (create an animation)
Void Createanimation ()
... {
/**/ /*First, we should consider how to apply the animation to the current camera.
An animation can be created because it can be applied to a node.
One node and attach the current camera to this node*/
Scenenode * Camnode = Mscenemgr -> Getrootscenenode () -> Createchildscenenode ();
Camnode -> Attachobject (mcamera );
// The animation, animation track, and key frame are defined as follows:
// Define the animation and specify the animation name and length (10 seconds here)
Animation * Anim = Mscenemgr -> Createanimation ( " Cameratrack " , 10 );
// Specifies the interpolation method (including linear interpolation and Spline Interpolation) between animation key frames)
Anim -> Setinterpolationmode (Animation: im_spline );
// Define an animation track, and specify that the track is applied to the camnode node.
Nodeanimationtrack * Track = Anim -> Createnodetrack ( 0 , Camnode );
// Defines the key frames included in the animation trajectory. The following defines four key frames, plus the start frame.
// Five key frames form a flip animation.
Transformkeyframe * Key = Track -> Createnodekeyframe ( 0 ); // Startposition
Key = Track -> Createnodekeyframe ( 2.5 );
Key -> Settranslate (vector3 ( 500 , 500 , - 1000 ));
Key = Track -> Createnodekeyframe ( 5 );
Key -> Settranslate (vector3 ( - 1500 , 1000 , - 600 ));
Key = Track -> Createnodekeyframe ( 7.5 );
Key -> Settranslate (vector3 ( 0 , - 100 , 0 ));
Key = Track -> Createnodekeyframe ( 10 );
Key -> Settranslate (vector3 ( 0 , 0 , 0 ));
// Then define the object of the animationstate class, which corresponds to the animation class just defined. Set the animation status to enable:
Manimstate = Mscenemgr -> Createanimationstate ( " Cameratrack " );
Manimstate -> Setenabled ( True ); // Enable this animation
// At this point, the initialization is complete.
}
After the initialization process of the animation is created, we can execute it where we want it. As an example, we use the virtual void createscene (void) you can execute it in the process.
We must push these animations forward somewhere, which involves time. Just now, ogre will automatically perform rendering based on the passage of time. We don't need to care about how they are rendered, because here we are concerned about how our animation is played based on time, therefore, we can define a class that inherits from exampleframelistener in the Ogre Project Wizard. We can use the bool framestarted (const frameevent & EVT) of this class) add this code in the process: manimstate-> addtime (EVT. timesincelastframe );
The rest is just to run our program. (Note: Do not comment out the code written by the program wizard. Otherwise, we will not see anything .)
If we want to create a plane, copy the following code:
Void Createplane ()
... {
Plane plane; // Define a horizontal plane Parameter
Plane. Normal = Vector3: unit_y; // Set the normal of the horizontal plane, perpendicular to the Y axis
Plane. d = 100 ; // Set the D parameter of the horizontal plane: the distance to the origin
// The following figure shows how to create a Plane Based on the horizontal plane parameters just defined.
Meshmanager: getsingleton (). createplane ( " Myplane " ,
Resourcegroupmanager: default_resource_group_name, plane,
1500 , 1500 , 20 , 20 , True , 1 , 60 , 60 , Vector3: unit_z );
// After the plane is created, an object must be created. With the entity, the scenario can be placed in the node.
Entity * Pplaneent = Mscenemgr -> Createentity ( " Plane " , " Myplane " );
Pplaneent -> Setmaterialname ( " Examples/Rockwall " );
Pplaneent -> Setcastshadows ( False );
// Attach a plane instance to the scenario manager to automatically render it.
Mscenemgr -> Getrootscenenode () -> Createchildscenenode (vector3 ( 0 , 99 , 0 )) -> Attachobject (pplaneent );
}
When will this code be executed? You can execute it whenever you want, but if you really want to say it, I have to tell you that it should be written in the virtual void createscene (void) process, I can't think of where to execute it. Don't be dizzy during execution. Run the command to check the effect. This is just a small example of my learning process. Thank you for your support.
Original Works are from hard work, please describe them for reprintingArticle Source:Http://blog.csdn.net/kfarvidOrHttp://www.cnblogs.com/kfarvid/