root/OpenSceneGraph/trunk/examples/osganimationmorph/osganimationmorph.cpp @ 9951

Revision 9951, 3.9 kB (checked in by robert, 6 years ago)

From Roland Smeenk, "Here's a small simplification of the osganimationmorph example. Only one morphtarget needs to be added to the MorphGeometry? since it already has a base geometry. The animation will morph between the base geometry and the first target.

"

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/MatrixTransform>
18#include <osg/Geode>
19#include <osgViewer/Viewer>
20#include <osgViewer/ViewerEventHandlers>
21#include <osgGA/TrackballManipulator>
22#include <osgGA/StateSetManipulator>
23#include <osgUtil/SmoothingVisitor>
24#include <osg/io_utils>
25
26#include <osgAnimation/MorphGeometry>
27#include <osgAnimation/BasicAnimationManager>
28
29#include <osgDB/ReadFile>
30#include <osgDB/WriteFile>
31
32struct GeometryFinder : public osg::NodeVisitor
33{
34    osg::ref_ptr<osg::Geometry> _geom;
35    GeometryFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
36    void apply(osg::Geode& geode)
37    {
38        if (_geom.valid())
39            return;
40        for (unsigned int i = 0; i < geode.getNumDrawables(); i++)
41        {
42            osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
43            if (geom) {
44                _geom = geom;
45                return;
46            }
47        }
48    }
49};
50
51osg::Geometry* getShape(const std::string& name)
52{
53    osg::Node* shape0 = osgDB::readNodeFile(name);
54    GeometryFinder finder;
55    shape0->accept(finder);
56    return finder._geom.get();
57}
58
59
60int main (int argc, char* argv[])
61{
62    osg::ArgumentParser arguments(&argc, argv);
63    osgViewer::Viewer viewer(arguments);
64
65    osgAnimation::Animation* animation = new osgAnimation::Animation;
66    osgAnimation::FloatLinearChannel* channel0 = new osgAnimation::FloatLinearChannel;
67    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
68    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,1.0));
69    channel0->setTargetName("MorphNodeCallback");
70    channel0->setName("0");
71
72    animation->addChannel(channel0);
73    animation->setName("Morph");
74    animation->computeDuration();
75    animation->setPlaymode(osgAnimation::Animation::PPONG);
76    osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
77    bam->registerAnimation(animation);
78
79    osg::Geometry* geom0 = getShape("morphtarget_shape0.osg");
80    if (!geom0) {
81        std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
82        return 0;
83    }
84
85    osg::Geometry* geom1 = getShape("morphtarget_shape1.osg");
86    if (!geom1) {
87        std::cerr << "can't read morphtarget_shape1.osg" << std::endl;
88        return 0;
89    }
90
91    // initialize with the first shape
92    osgAnimation::MorphGeometry* morph = new osgAnimation::MorphGeometry(*geom0);
93    morph->addMorphTarget(geom1);
94
95    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
96
97
98    osg::Group* scene = new osg::Group;
99    scene->addUpdateCallback(bam);
100   
101    osg::Geode* geode = new osg::Geode;
102    geode->addDrawable(morph);
103    geode->addUpdateCallback(new osgAnimation::UpdateMorph("MorphNodeCallback"));
104    scene->addChild(geode);
105
106    viewer.addEventHandler(new osgViewer::StatsHandler());
107    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
108    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
109
110    // let's run !
111    viewer.setSceneData( scene );
112    viewer.realize();
113
114    bam->playAnimation(animation);
115
116
117    while (!viewer.done())
118    {
119        viewer.frame();
120    }
121
122    osgDB::writeNodeFile(*scene, "morph_scene.osg");
123
124    return 0;
125}
126
Note: See TracBrowser for help on using the browser.