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

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
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
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
23#include <osgAnimation/BasicAnimationManager>
24#include <osgAnimation/Channel>
25#include <osgAnimation/UpdateMatrixTransform>
26#include <osgAnimation/StackedTranslateElement>
27#include <osgAnimation/StackedRotateAxisElement>
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{
65    osg::ArgumentParser arguments(&argc, argv);
66    osgViewer::Viewer viewer(arguments);
67
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
76    //Tranformation to be manipulated by the animation
77    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
78    trans->setName("AnimatedNode");
79    //Dynamic object, has to be updated during update traversal
80    trans->setDataVariance(osg::Object::DYNAMIC);
81    //Animation callback for Matrix transforms, name is targetName for Channels
82    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
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
87    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
88    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0));
89    //connect the UpdateMatrixTransform callback to the MatrixTRanform
90    trans->setUpdateCallback(updatecb);
91    //initialize MatrixTranform
92    trans->setMatrix(osg::Matrix::identity());
93    //append geometry node
94    trans->addChild (geode.get());
95
96    root->addChild (axe.get());
97    root->addChild (trans.get());
98
99    // Define a scheduler for our animations
100    osg::Group* grp = new osg::Group;
101    //add the animation manager to the scene graph to get it called during update traversals
102    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
103    grp->setUpdateCallback(mng);
104    //add the rest of the scene to the grp node
105    grp->addChild(root);
106
107    // And we finaly define our channel for linear Vector interpolation
108    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
109    //name of the AnimationUpdateCallback
110    channelAnimation1->setTargetName("AnimatedCallback");
111    //name of the StackedElementTransform for position modification
112    channelAnimation1->setName("position");
113    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
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;
117    anim1->addChannel(channelAnimation1);   
118    anim1->setPlayMode(osgAnimation::Animation::PPONG);
119
120
121    //define the channel for interpolation of a float angle value
122    osgAnimation::FloatLinearChannel* channelAnimation2 = new osgAnimation::FloatLinearChannel;
123    //name of the AnimationUpdateCallback
124    channelAnimation2->setTargetName("AnimatedCallback");
125    //name of the StackedElementTransform for position modification
126    channelAnimation2->setName("euler");
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));
130    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
131    anim2->addChannel(channelAnimation2);
132    anim2->setPlayMode(osgAnimation::Animation::LOOP);
133                                                                                     
134
135    // We register all animation inside the scheduler
136    mng->registerAnimation(anim1);
137    mng->registerAnimation(anim2);
138   
139    //start the animation
140    mng->playAnimation(anim1);
141    mng->playAnimation(anim2);
142
143    //set the grp-Group with the scene and the AnimationManager as viewer's scene data
144    viewer.setSceneData( grp );
145    return viewer.run();
146}
Note: See TracBrowser for help on using the browser.