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

Revision 11009, 5.2 kB (checked in by robert, 4 years ago)

From Cedric Pinson, "Here a list of changes:
Bone now inherit from MatrixTransform?. It simplify a lot the update of
Bone matrix. It helps to have the bone system more generic. eg it's now
possible to have animation data with precomputed bind matrix. The other
benefit, is now the collada plugin will be able to use osgAnimation to
display skinned mesh. Michael Plating did a great work to improve this
aspect, he is working on the collada plugin and should be able to submit
a new version soon.
The RigGeometry? has been refactored so now it works when you save and
reload RigGeometry? because the source is not touched anymore. The
benefit with this update is that it should be now possible to use a
MorphGeometry? as source for a RigGeometry?.

The bad news is that the format has changed, so i have rebuild osg-data
related to osgAnimation data, updated the blender exporter to export to
the new format.
The fbx plugin could be touched about this commit, i dont compile it so
i can't give more information about it.
The bvh plugin has been updated by Wang rui so this one is fixed with
the new code of osgAnimation.
The examples has been updated to work with the new code too...

The example osg-data/example.osg should be remove, it's an old example
that does not work.

For people using blender the blender exporter up to date is here:
http://hg.plopbyte.net/osgexport2/
it will be merge to http://hg.plopbyte.net/osgexport/ as soon as the
modification will be push in the trunk.
"

RevLine 
[9097]1/*  -*-c++-*-
[11009]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9097]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>
[11009]25#include <osgAnimation/UpdateMatrixTransform>
26#include <osgAnimation/StackedTranslateElement>
[9097]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{
[9459]64    osg::ArgumentParser arguments(&argc, argv);
65    osgViewer::Viewer viewer(arguments);
66
[9097]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);
[11009]78    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback");
79    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
80    trans->setUpdateCallback(updatecb);
[9097]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
[9370]88    osg::Group* grp = new osg::Group;
89    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager();
90    grp->setUpdateCallback(mng);
[9097]91
[9370]92    grp->addChild(root);
[9097]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");
[9104]107    channelAnimation2->setName("euler");
[9097]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
[9370]122    viewer.setSceneData( grp );
[9097]123    return viewer.run();
124}
Note: See TracBrowser for help on using the browser.