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

Revision 9928, 4.4 kB (checked in by robert, 6 years ago)

Warning fixes

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
66    osgAnimation::Animation* animation = new osgAnimation::Animation;
67    osgAnimation::FloatLinearChannel* channel0 = new osgAnimation::FloatLinearChannel;
68    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
69    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,1.0));
70    channel0->setTargetName("MorphNodeCallback");
71    channel0->setName("0");
72
73    osgAnimation::FloatLinearChannel* channel1 = new osgAnimation::FloatLinearChannel;
74    channel1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,1.0));
75    channel1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,0.0));
76    channel1->setTargetName("MorphNodeCallback");
77    channel1->setName("1");
78
79    animation->addChannel(channel0);
80    animation->addChannel(channel1);
81    animation->setName("Morph");
82    animation->computeDuration();
83    animation->setPlaymode(osgAnimation::Animation::PPONG);
84    osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
85    bam->registerAnimation(animation);
86
87    osg::Geometry* geom0 = getShape("morphtarget_shape0.osg");
88    if (!geom0) {
89        std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
90        return 0;
91    }
92
93    osg::Geometry* geom1 = getShape("morphtarget_shape1.osg");
94    if (!geom1) {
95        std::cerr << "can't read morphtarget_shape1.osg" << std::endl;
96        return 0;
97    }
98
99    // initialize with the first shape
100    osgAnimation::MorphGeometry* morph = new osgAnimation::MorphGeometry(*geom0);
101    morph->addMorphTarget(geom0);
102    morph->addMorphTarget(geom1);
103
104    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
105
106
107    osg::Group* scene = new osg::Group;
108    scene->addUpdateCallback(bam);
109   
110    osg::Geode* geode = new osg::Geode;
111    geode->addDrawable(morph);
112    geode->addUpdateCallback(new osgAnimation::UpdateMorph("MorphNodeCallback"));
113    scene->addChild(geode);
114
115    viewer.addEventHandler(new osgViewer::StatsHandler());
116    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
117    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
118
119    // let's run !
120    viewer.setSceneData( scene );
121    viewer.realize();
122
123    bam->playAnimation(animation);
124
125
126    while (!viewer.done())
127    {
128        viewer.frame();
129    }
130
131    osgDB::writeNodeFile(*scene, "morph_scene.osg");
132
133    return 0;
134}
135
Note: See TracBrowser for help on using the browser.