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

Revision 9459, 4.9 kB (checked in by robert, 5 years ago)

Fixed warnings

RevLine 
[9097]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
[9370]23#include <osgAnimation/BasicAnimationManager>
[9097]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{
[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
74    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
75    trans->setName("AnimatedNode");
76    trans->setDataVariance(osg::Object::DYNAMIC);
77    trans->setUpdateCallback(new osgAnimation::UpdateTransform("AnimatedCallback"));
78    trans->setMatrix(osg::Matrix::identity());
79    trans->addChild (geode.get());
80
81    root->addChild (axe.get());
82    root->addChild (trans.get());
83
84    // Define a scheduler for our animations
[9370]85    osg::Group* grp = new osg::Group;
86    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
87    grp->setUpdateCallback(mng);
[9097]88
[9370]89    grp->addChild(root);
[9097]90
91    // And we finaly define our channel
92    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
93    channelAnimation1->setTargetName("AnimatedCallback");
94    channelAnimation1->setName("position");
95    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
96    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
97    osgAnimation::Animation* anim1 = new osgAnimation::Animation;
98    anim1->addChannel(channelAnimation1);
99    anim1->setPlaymode(osgAnimation::Animation::PPONG);
100
101
102    osgAnimation::Vec3LinearChannel* channelAnimation2 = new osgAnimation::Vec3LinearChannel;
103    channelAnimation2->setTargetName("AnimatedCallback");
[9104]104    channelAnimation2->setName("euler");
[9097]105    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
106    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(1.5, osg::Vec3(2*osg::PI,0,0)));
107    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
108    anim2->addChannel(channelAnimation2);
109    anim2->setPlaymode(osgAnimation::Animation::LOOP);
110                                                                                     
111
112    // We register all animation inside the scheduler
113    mng->registerAnimation(anim1);
114    mng->registerAnimation(anim2);
115
116    mng->playAnimation(anim1);
117    mng->playAnimation(anim2);
118
[9370]119    viewer.setSceneData( grp );
[9097]120    return viewer.run();
121}
Note: See TracBrowser for help on using the browser.