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

Revision 12046, 4.1 kB (checked in by robert, 4 years ago)

From Kim Seokhwan, "Error fix in example/osganimationmorph/osganimationmorph.cpp

in this file,
function,
osg::Geometry* getShape(const std::string& name)
(line 51)

tries to use null pointer when model is not provided.
I added simple comment in attached file."

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