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

Revision 13576, 6.4 kB (checked in by robert, 15 hours ago)

Removed GL header as it's already included via the ${OPENSCENEGRAPH_OPENGL_HEADER} entry.

  • 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(), osg::Array::BIND_PER_VERTEX);
53    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
54
55    geode->addDrawable( geometry.get() );
56    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
57    return geode;
58}
59
60
61int main (int argc, char* argv[])
62{
63    osg::ArgumentParser arguments(&argc, argv);
64    osgViewer::Viewer viewer(arguments);
65
66    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
67
68    osg::Group* root = new osg::Group;
69
70    osg::ref_ptr<osg::Geode> axe = createAxis();
71    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
72    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5)));
73
74    //Tranformation to be manipulated by the animation
75    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
76    trans->setName("AnimatedNode");
77    //Dynamic object, has to be updated during update traversal
78    trans->setDataVariance(osg::Object::DYNAMIC);
79    //Animation callback for Matrix transforms, name is targetName for Channels
80    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
81    //add manipulator Stack, names must match with channel names
82    //elements are applied in LIFO order
83    //The first element modifies the position component of the matrix
84    //The second element modifies the rotation around x-axis
85    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
86    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0));
87    //connect the UpdateMatrixTransform callback to the MatrixTRanform
88    trans->setUpdateCallback(updatecb);
89    //initialize MatrixTranform
90    trans->setMatrix(osg::Matrix::identity());
91    //append geometry node
92    trans->addChild (geode.get());
93
94    root->addChild (axe.get());
95    root->addChild (trans.get());
96
97    // Define a scheduler for our animations
98    osg::Group* grp = new osg::Group;
99    //add the animation manager to the scene graph to get it called during update traversals
100    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
101    grp->setUpdateCallback(mng);
102    //add the rest of the scene to the grp node
103    grp->addChild(root);
104
105    // And we finaly define our channel for linear Vector interpolation
106    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
107    //name of the AnimationUpdateCallback
108    channelAnimation1->setTargetName("AnimatedCallback");
109    //name of the StackedElementTransform for position modification
110    channelAnimation1->setName("position");
111    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
112    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
113    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
114    osgAnimation::Animation* anim1 = new osgAnimation::Animation;
115    anim1->addChannel(channelAnimation1);
116    anim1->setPlayMode(osgAnimation::Animation::PPONG);
117
118
119    //define the channel for interpolation of a float angle value
120    osgAnimation::FloatLinearChannel* channelAnimation2 = new osgAnimation::FloatLinearChannel;
121    //name of the AnimationUpdateCallback
122    channelAnimation2->setTargetName("AnimatedCallback");
123    //name of the StackedElementTransform for position modification
124    channelAnimation2->setName("euler");
125    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
126    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0, 0));
127    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.5, 2*osg::PI));
128    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
129    anim2->addChannel(channelAnimation2);
130    anim2->setPlayMode(osgAnimation::Animation::LOOP);
131
132
133    // We register all animation inside the scheduler
134    mng->registerAnimation(anim1);
135    mng->registerAnimation(anim2);
136
137    //start the animation
138    mng->playAnimation(anim1);
139    mng->playAnimation(anim2);
140
141    //set the grp-Group with the scene and the AnimationManager as viewer's scene data
142    viewer.setSceneData( grp );
143    return viewer.run();
144}
Note: See TracBrowser for help on using the browser.