OSG Example parsing osganimationsolid (1)

Source: Internet
Author: User
Introduction

The Osganimationsolid example uses the animation manager in Osganimation to manage multiple animation animtion, which involves some other concepts, such as the animation contains one or more channel channels, Channel set Action object Settargetname and so on. Create a coordinate system

Osg::ref_ptr<osg::geode> Createaxis () {osg::ref_ptr<osg::geode> Geode (new Osg::geode ());

    Osg::ref_ptr<osg::geometry> Geometry (New Osg::geometry ());
    Set the coordinate values of three coordinate axes osg::ref_ptr<osg::vec3array> vertices (new Osg::vec3array ());
    Vertices->push_back (OSG::VEC3 (0.0, 0.0, 0.0));
    Vertices->push_back (OSG::VEC3 (10.0, 0.0, 0.0));
    Vertices->push_back (OSG::VEC3 (0.0, 0.0, 0.0));
    Vertices->push_back (OSG::VEC3 (0.0, 10.0, 0.0));
    Vertices->push_back (OSG::VEC3 (0.0, 0.0, 0.0));
    Vertices->push_back (OSG::VEC3 (0.0, 0.0, 10.0));

    Geometry->setvertexarray (Vertices.get ());
    Set the color of the three coordinate axes osg::ref_ptr<osg::vec4array> colors (new Osg::vec4array ());
    Colors->push_back (OSG::VEC4 (1.0f, 0.0f, 0.0f, 1.0f));
    Colors->push_back (OSG::VEC4 (1.0f, 0.0f, 0.0f, 1.0f));
    Colors->push_back (OSG::VEC4 (0.0f, 1.0f, 0.0f, 1.0f));
  Colors->push_back (OSG::VEC4 (0.0f, 1.0f, 0.0f, 1.0f));  Colors->push_back (OSG::VEC4 (0.0f, 0.0f, 1.0f, 1.0f));
	
    Colors->push_back (OSG::VEC4 (0.0f, 0.0f, 1.0f, 1.0f));
    Geometry->setcolorarray (Colors.get (), Osg::array::bind_per_vertex);
    Geometry->addprimitiveset (new OSG::D rawarrays (OSG::P rimitiveset::lines,0,6));
	Geode->adddrawable (Geometry.get ());
    Turn off Light geode->getorcreatestateset ()->setmode (gl_lighting, false);
return geode; }
The code to create the coordinate system is simple, set the vertex, color array, and draw as a segment (Gl_lines)

creating a Role Object

    osganimation::updatematrixtransform* UPDATECB = new Osganimation::updatematrixtransform ("AnimatedCallback");
	Set two transformations to pan and rotate
    updatecb->getstackedtransforms (). push_back (New Osganimation::stackedtranslateelement (" Osition "));
    Updatecb->getstackedtransforms (). push_back (New Osganimation::stackedrotateaxiselement ("Euler", OSG::VEC3 ( 1,0,0), 0));
 
    Trans->setupdatecallback (UPDATECB);
Create the Action object, and set its name to correspond to the name of the channel in animation, which is the name of the Settargetname and updatematrixtransform that set the channel. The names in the channel are set in the same name as the array elements in the updatematrixtransform, that is, the name of the channel is set to "Osition" and "Euler" so that the execution object is created correctly.

Create an animated channel

    osganimation::vec3linearchannel* channelAnimation1 = new Osganimation::vec3linearchannel;
    Channelanimation1->settargetname ("Animatedcallback");
    Channelanimation1->setname ("Osition"); Channelanimation1->getorcreatesampler ()->getorcreatekeyframecontainer ()->push_back (osgAnimation::
    Vec3keyframe (0, OSG::VEC3 (0,0,0))); Channelanimation1->getorcreatesampler ()->getorcreatekeyframecontainer ()->push_back (osgAnimation::
    Vec3keyframe (2, OSG::VEC3 (1,1,0)));
    osganimation::animation* anim1 = new Osganimation::animation;
    Anim1->addchannel (ChannelAnimation1);

    Anim1->setplaymode (osganimation::animation::P pong);
    osganimation::floatlinearchannel* channelAnimation2 = new Osganimation::floatlinearchannel;
    Channelanimation2->settargetname ("Animatedcallback");
    Channelanimation2->setname ("Euler"); Channelanimation2->getorcreatesampler ()->getorcreatekeyframecontainer ()->push_back (osgAnimation::
   Floatkeyframe (0, 0)); Channelanimation2->getorcreatesampler ()->getorcreatekeyframecontainer ()->push_back (osgAnimation::
    Floatkeyframe (1.5, 2*OSG::P i));
    osganimation::animation* anim2 = new Osganimation::animation;
    Anim2->addchannel (ChannelAnimation2); Anim2->setplaymode (Osganimation::animation::loop);
The animation channel creates its targetname settings that are consistent with the action object, and its name also needs to match the names of the elements in the Action object array

Create manager

    osg::group* grp = new Osg::group;
	Basicanimationmanager must be set to the parent of the animation sub-scene (in this case, the cube)
	//Because Basicanimationmanager will traverse its child nodes and set the execution object in the child node target
    osganimation::basicanimationmanager* mng = new Osganimation::basicanimationmanager ();
    Grp->setupdatecallback (MNG);
    Grp->addchild (root);
The manager must be set to the updatecallback of the parent node that needs to animate the scene

Finally, register the animation, play the animation, the code is as follows:

    Mng->registeranimation (ANIM1);
    Mng->registeranimation (ANIM2);

    Mng->playanimation (ANIM1);
    Mng->playanimation (ANIM2);

Think: What happens when we change the position of the two elements that create the action object. That is, modify the source code to:

    Updatecb->getstackedtransforms (). push_back (New Osganimation::stackedrotateaxiselement ("Euler", OSG::VEC3 ( 1,0,0), 0));
    Updatecb->getstackedtransforms (). push_back (New Osganimation::stackedtranslateelement ("Osition"));
After running, we find that the trajectory of the cube is completely transformed, which is why. The literal meaning of stackedtransform, which is similar to the stack of LIFO (LIFO) mode, so if the example is translated and rotated, then the update matrix is equal to M = Mr (rotation) * MT (translation), and after the change becomes the first translation after the rotation, M=MT (translation) * MR (rotation), the matrix has no Exchange law, so the results of the calculations are very different.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.