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

Revision 11338, 5.2 kB (checked in by robert, 5 years ago)

From Wang Rui, "Attached is the osgAnimation wrappers for serialize IO operations. A
few headers and the osgAnimation sources are also modified to make
everything goes well, including:

A new REGISTER_OBJECT_WRAPPER2 macro to wrap classes like
Skeleton::UpdateSkeleton?.
A bug fix in the Seralizer header which avoids setting default values
to objects.
Naming style fixes in osgAnimation headers and sources, also in the
deprecated dotosg wrappers.
A bug fix for the XML support, to write char values correctly.
A small change in the osg::Geometry wrapper to ignore the
InternalGeometry? property, which is used by the MorphGeometry? and
should not be set by user applications.

The avatar.osg, nathan.osg and robot.osg data files all work fine with
serializers, with some 'unsupported wrapper' warnings when converting.
I'm thinking of removing these warnings by disabling related property
serializers (ComputeBoundingBoxCallback? and Drawable::UpdateCallback?),
which are seldom recorded by users.

By the way, I still wonder how would we handle the C4121 problem,
discussed some days before. The /Zp compile option is set to 16 in the
attached cmake script file. And is there a better solution now?"

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
28using namespace osgAnimation;
29
30osg::ref_ptr<osg::Geode> createAxis()
31{
32    osg::ref_ptr<osg::Geode> geode (new osg::Geode()); 
33    osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry());
34
35    osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array());
36    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
37    vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0));
38    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
39    vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0));
40    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
41    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0));
42    geometry->setVertexArray (vertices.get());
43
44    osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array());
45    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
46    colors->push_back (osg::Vec4 (1.0f, 0.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, 1.0f, 0.0f, 1.0f));
49    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
50    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
51    geometry->setColorArray (colors.get());
52
53    geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX);   
54    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
55
56    geode->addDrawable( geometry.get() );
57    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
58    return geode;
59}
60
61
62int main (int argc, char* argv[])
63{
64    osg::ArgumentParser arguments(&argc, argv);
65    osgViewer::Viewer viewer(arguments);
66
67    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
68
69    osg::Group* root = new osg::Group;
70
71    osg::ref_ptr<osg::Geode> axe = createAxis();
72    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
73    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5)));
74
75    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform();
76    trans->setName("AnimatedNode");
77    trans->setDataVariance(osg::Object::DYNAMIC);
78    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
79    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
80    trans->setUpdateCallback(updatecb);
81    trans->setMatrix(osg::Matrix::identity());
82    trans->addChild (geode.get());
83
84    root->addChild (axe.get());
85    root->addChild (trans.get());
86
87    // Define a scheduler for our animations
88    osg::Group* grp = new osg::Group;
89    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
90    grp->setUpdateCallback(mng);
91
92    grp->addChild(root);
93
94    // And we finaly define our channel
95    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
96    channelAnimation1->setTargetName("AnimatedCallback");
97    channelAnimation1->setName("position");
98    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
99    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
100    osgAnimation::Animation* anim1 = new osgAnimation::Animation;
101    anim1->addChannel(channelAnimation1);
102    anim1->setPlayMode(osgAnimation::Animation::PPONG);
103
104
105    osgAnimation::Vec3LinearChannel* channelAnimation2 = new osgAnimation::Vec3LinearChannel;
106    channelAnimation2->setTargetName("AnimatedCallback");
107    channelAnimation2->setName("euler");
108    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0)));
109    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(1.5, osg::Vec3(2*osg::PI,0,0)));
110    osgAnimation::Animation* anim2 = new osgAnimation::Animation;
111    anim2->addChannel(channelAnimation2);
112    anim2->setPlayMode(osgAnimation::Animation::LOOP);
113                                                                                     
114
115    // We register all animation inside the scheduler
116    mng->registerAnimation(anim1);
117    mng->registerAnimation(anim2);
118
119    mng->playAnimation(anim1);
120    mng->playAnimation(anim2);
121
122    viewer.setSceneData( grp );
123    return viewer.run();
124}
Note: See TracBrowser for help on using the browser.