root/OpenSceneGraph/branches/OpenSceneGraph-osgWidget-dev/examples/osganimationsolid/osganimationsolid.cpp @ 9104

Revision 9104, 4.8 kB (checked in by cedricpinson, 6 years ago)

remove old timer
update example without initTimer
update the UpdateCallback? of AnimationManager? to use the FrameStamp?
split osganimationnode example in two piece
fix the -DOSGANIMATION_LIBRARY in the CMakeLists.txt of osgAnimation

Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <mornifle@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/AnimationManager>
24#include <osgAnimation/Channel>
25#include <osgAnimation/UpdateCallback>
26
27using namespace osgAnimation;
28
29osg::ref_ptr<osg::Geode> createAxis()
30{
31    osg::ref_ptr<osg::Geode> geode (new osg::Geode()); 
32    osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry());
33
34    osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array());
35    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
36    vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0));
37    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
38    vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0));
39    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
40    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0));
41    geometry->setVertexArray (vertices.get());
42
43    osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array());
44    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
45    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
46    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
47    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
48    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
49    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
50    geometry->setColorArray (colors.get());
51
52    geometry->setColorBinding (osg::Geometry::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    osgViewer::Viewer viewer;
64    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
65
66    osg::Group* root = new osg::Group;
67
68    osg::ref_ptr<osg::Geode> axe = createAxis();
69    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
70    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5)));
71
72    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
73    trans->setName("AnimatedNode");
74    trans->setDataVariance(osg::Object::DYNAMIC);
75    trans->setUpdateCallback(new osgAnimation::UpdateTransform("AnimatedCallback"));
76    trans->setMatrix(osg::Matrix::identity());
77    trans->addChild (geode.get());
78
79    root->addChild (axe.get());
80    root->addChild (trans.get());
81
82    // Define a scheduler for our animations
83    osgAnimation::AnimationManager* mng = new osgAnimation::AnimationManager();
84
85
86    mng->addChild(root);
87
88    // And we finaly define our channel
89    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
90    channelAnimation1->setTargetName("AnimatedCallback");
91    channelAnimation1->setName("position");
92    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
93    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
94    osgAnimation::Animation* anim1 = new osgAnimation::Animation;
95    anim1->addChannel(channelAnimation1);
96    anim1->setPlaymode(osgAnimation::Animation::PPONG);
97
98
99    osgAnimation::Vec3LinearChannel* channelAnimation2 = new osgAnimation::Vec3LinearChannel;
100    channelAnimation2->setTargetName("AnimatedCallback");
101    channelAnimation2->setName("euler");
102    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
103    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(1.5, osg::Vec3(2*osg::PI,0,0)));
104    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
105    anim2->addChannel(channelAnimation2);
106    anim2->setPlaymode(osgAnimation::Animation::LOOP);
107                                                                                     
108
109    // We register all animation inside the scheduler
110    mng->registerAnimation(anim1);
111    mng->registerAnimation(anim2);
112
113    mng->playAnimation(anim1);
114    mng->playAnimation(anim2);
115
116    viewer.setSceneData( mng );
117    return viewer.run();
118}
Note: See TracBrowser for help on using the browser.