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

Revision 12292, 4.0 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
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::ref_ptr<osg::Geometry> getShape(const std::string& name)
52{
53    osg::ref_ptr<osg::Node> shape0 = osgDB::readNodeFile(name);
54    if (shape0)
55    {
56        GeometryFinder finder;
57        shape0->accept(finder);
58        return finder._geom;
59    }
60    else
61    {
62        return NULL;
63    }
64}
65
66
67int main (int argc, char* argv[])
68{
69    osg::ArgumentParser arguments(&argc, argv);
70    osgViewer::Viewer viewer(arguments);
71
72    osgAnimation::Animation* animation = new osgAnimation::Animation;
73    osgAnimation::FloatLinearChannel* channel0 = new osgAnimation::FloatLinearChannel;
74    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
75    channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,1.0));
76    channel0->setTargetName("MorphNodeCallback");
77    channel0->setName("0");
78
79    animation->addChannel(channel0);
80    animation->setName("Morph");
81    animation->computeDuration();
82    animation->setPlayMode(osgAnimation::Animation::PPONG);
83    osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
84    bam->registerAnimation(animation);
85
86    osg::ref_ptr<osg::Geometry> geom0 = getShape("morphtarget_shape0.osg");
87    if (!geom0) {
88        std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
89        return 0;
90    }
91
92    osg::ref_ptr<osg::Geometry> geom1 = getShape("morphtarget_shape1.osg");
93    if (!geom1) {
94        std::cerr << "can't read morphtarget_shape1.osg" << std::endl;
95        return 0;
96    }
97
98    // initialize with the first shape
99    osgAnimation::MorphGeometry* morph = new osgAnimation::MorphGeometry(*geom0);
100    morph->addMorphTarget(geom1.get());
101
102    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
103
104
105    osg::Group* scene = new osg::Group;
106    scene->addUpdateCallback(bam);
107   
108    osg::Geode* geode = new osg::Geode;
109    geode->addDrawable(morph);
110    geode->addUpdateCallback(new osgAnimation::UpdateMorph("MorphNodeCallback"));
111    scene->addChild(geode);
112
113    viewer.addEventHandler(new osgViewer::StatsHandler());
114    viewer.addEventHandler(new osgViewer::WindowSizeHandler());
115    viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
116
117    // let's run !
118    viewer.setSceneData( scene );
119    viewer.realize();
120
121    bam->playAnimation(animation);
122
123
124    while (!viewer.done())
125    {
126        viewer.frame();
127    }
128
129    osgDB::writeNodeFile(*scene, "morph_scene.osg");
130
131    return 0;
132}
133
Note: See TracBrowser for help on using the browser.