root/OpenSceneGraph/trunk/examples/osganimationsolid/osganimationsolid.cpp @ 12292

Revision 12292, 6.6 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
RevLine 
[9097]1/*  -*-c++-*-
[11009]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9097]3 *
4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15#include <iostream>
16#include <osg/Geometry>
17#include <osg/Shape>
18#include <osg/ShapeDrawable>
19#include <osgViewer/Viewer>
20#include <osgGA/TrackballManipulator>
21#include <osg/MatrixTransform>
22
[9370]23#include <osgAnimation/BasicAnimationManager>
[9097]24#include <osgAnimation/Channel>
[11009]25#include <osgAnimation/UpdateMatrixTransform>
26#include <osgAnimation/StackedTranslateElement>
[11890]27#include <osgAnimation/StackedRotateAxisElement>
[9097]28
29using namespace osgAnimation;
30
31osg::ref_ptr<osg::Geode> createAxis()
32{
33    osg::ref_ptr<osg::Geode> geode (new osg::Geode()); 
34    osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry());
35
36    osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array());
37    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
38    vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0));
39    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
40    vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0));
41    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
42    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0));
43    geometry->setVertexArray (vertices.get());
44
45    osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array());
46    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
47    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
48    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
49    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
50    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
51    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
52    geometry->setColorArray (colors.get());
53
54    geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX);   
55    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
56
57    geode->addDrawable( geometry.get() );
58    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
59    return geode;
60}
61
62
63int main (int argc, char* argv[])
64{
[9459]65    osg::ArgumentParser arguments(&argc, argv);
66    osgViewer::Viewer viewer(arguments);
67
[9097]68    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
69
70    osg::Group* root = new osg::Group;
71
72    osg::ref_ptr<osg::Geode> axe = createAxis();
73    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
74    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5)));
75
[11890]76    //Tranformation to be manipulated by the animation
[9097]77    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
78    trans->setName("AnimatedNode");
[11890]79    //Dynamic object, has to be updated during update traversal
[9097]80    trans->setDataVariance(osg::Object::DYNAMIC);
[11890]81    //Animation callback for Matrix transforms, name is targetName for Channels
[11009]82    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
[11890]83    //add manipulator Stack, names must match with channel names
84    //elements are applied in LIFO order
85    //The first element modifies the position component of the matrix
86    //The second element modifies the rotation around x-axis
[11009]87    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
[11890]88    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0));
89    //connect the UpdateMatrixTransform callback to the MatrixTRanform
[11009]90    trans->setUpdateCallback(updatecb);
[11890]91    //initialize MatrixTranform
[9097]92    trans->setMatrix(osg::Matrix::identity());
[11890]93    //append geometry node
[9097]94    trans->addChild (geode.get());
95
96    root->addChild (axe.get());
97    root->addChild (trans.get());
98
99    // Define a scheduler for our animations
[9370]100    osg::Group* grp = new osg::Group;
[11890]101    //add the animation manager to the scene graph to get it called during update traversals
[9370]102    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
103    grp->setUpdateCallback(mng);
[11890]104    //add the rest of the scene to the grp node
[9370]105    grp->addChild(root);
[9097]106
[11890]107    // And we finaly define our channel for linear Vector interpolation
[9097]108    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
[11890]109    //name of the AnimationUpdateCallback
[9097]110    channelAnimation1->setTargetName("AnimatedCallback");
[11890]111    //name of the StackedElementTransform for position modification
[9097]112    channelAnimation1->setName("position");
[11890]113    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
[9097]114    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
115    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
116    osgAnimation::Animation* anim1 = new osgAnimation::Animation;
[11890]117    anim1->addChannel(channelAnimation1);   
[11338]118    anim1->setPlayMode(osgAnimation::Animation::PPONG);
[9097]119
120
[11890]121    //define the channel for interpolation of a float angle value
122    osgAnimation::FloatLinearChannel* channelAnimation2 = new osgAnimation::FloatLinearChannel;
123    //name of the AnimationUpdateCallback
[9097]124    channelAnimation2->setTargetName("AnimatedCallback");
[11890]125    //name of the StackedElementTransform for position modification
[9104]126    channelAnimation2->setName("euler");
[11890]127    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
128    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0, 0));
129    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.5, 2*osg::PI));
[9097]130    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
131    anim2->addChannel(channelAnimation2);
[11338]132    anim2->setPlayMode(osgAnimation::Animation::LOOP);
[9097]133                                                                                     
134
135    // We register all animation inside the scheduler
136    mng->registerAnimation(anim1);
137    mng->registerAnimation(anim2);
[11890]138   
139    //start the animation
[9097]140    mng->playAnimation(anim1);
141    mng->playAnimation(anim2);
142
[11890]143    //set the grp-Group with the scene and the AnimationManager as viewer's scene data
[9370]144    viewer.setSceneData( grp );
[9097]145    return viewer.run();
146}
Note: See TracBrowser for help on using the browser.