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

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

Fixed rendering of Popups

  • Property svn:eol-style set to native
RevLine 
[13576]1/*  -*-c++-*-
[11009]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9097]3 *
[13576]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
[9097]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.
[13576]8 *
[9097]9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[13576]11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
[9097]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{
[13576]33    osg::ref_ptr<osg::Geode> geode (new osg::Geode());
[9097]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));
[13576]52    geometry->setColorArray (colors.get(), osg::Array::BIND_PER_VERTEX);
[9097]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{
[9459]63    osg::ArgumentParser arguments(&argc, argv);
64    osgViewer::Viewer viewer(arguments);
65
[9097]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
[11890]74    //Tranformation to be manipulated by the animation
[9097]75    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
76    trans->setName("AnimatedNode");
[11890]77    //Dynamic object, has to be updated during update traversal
[9097]78    trans->setDataVariance(osg::Object::DYNAMIC);
[11890]79    //Animation callback for Matrix transforms, name is targetName for Channels
[11009]80    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
[11890]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
[11009]85    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
[11890]86    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0));
87    //connect the UpdateMatrixTransform callback to the MatrixTRanform
[11009]88    trans->setUpdateCallback(updatecb);
[11890]89    //initialize MatrixTranform
[9097]90    trans->setMatrix(osg::Matrix::identity());
[11890]91    //append geometry node
[9097]92    trans->addChild (geode.get());
93
94    root->addChild (axe.get());
95    root->addChild (trans.get());
96
97    // Define a scheduler for our animations
[9370]98    osg::Group* grp = new osg::Group;
[11890]99    //add the animation manager to the scene graph to get it called during update traversals
[9370]100    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
101    grp->setUpdateCallback(mng);
[11890]102    //add the rest of the scene to the grp node
[9370]103    grp->addChild(root);
[9097]104
[11890]105    // And we finaly define our channel for linear Vector interpolation
[9097]106    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
[11890]107    //name of the AnimationUpdateCallback
[9097]108    channelAnimation1->setTargetName("AnimatedCallback");
[11890]109    //name of the StackedElementTransform for position modification
[9097]110    channelAnimation1->setName("position");
[11890]111    //Create keyframes for (in this case linear) interpolation of a osg::Vec3
[9097]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;
[13576]115    anim1->addChannel(channelAnimation1);
[11338]116    anim1->setPlayMode(osgAnimation::Animation::PPONG);
[9097]117
118
[11890]119    //define the channel for interpolation of a float angle value
120    osgAnimation::FloatLinearChannel* channelAnimation2 = new osgAnimation::FloatLinearChannel;
121    //name of the AnimationUpdateCallback
[9097]122    channelAnimation2->setTargetName("AnimatedCallback");
[11890]123    //name of the StackedElementTransform for position modification
[9104]124    channelAnimation2->setName("euler");
[11890]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));
[9097]128    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
129    anim2->addChannel(channelAnimation2);
[11338]130    anim2->setPlayMode(osgAnimation::Animation::LOOP);
[9097]131
[13576]132
[9097]133    // We register all animation inside the scheduler
134    mng->registerAnimation(anim1);
135    mng->registerAnimation(anim2);
[13576]136
[11890]137    //start the animation
[9097]138    mng->playAnimation(anim1);
139    mng->playAnimation(anim2);
140
[11890]141    //set the grp-Group with the scene and the AnimationManager as viewer's scene data
[9370]142    viewer.setSceneData( grp );
[9097]143    return viewer.run();
144}
Note: See TracBrowser for help on using the browser.