2015-10-22 22:14:53 +08:00
|
|
|
/* -*-c++-*-
|
2009-03-10 01:38:39 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
|
|
|
|
*
|
2015-10-22 22:14:53 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2009-03-10 01:38:39 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2015-10-22 22:14:53 +08:00
|
|
|
*
|
2009-03-10 01:38:39 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-10-22 22:14:53 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-03-10 01:38:39 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
#include <osgUtil/SmoothingVisitor>
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
|
|
|
#include <osgAnimation/MorphGeometry>
|
|
|
|
#include <osgAnimation/BasicAnimationManager>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
|
|
|
|
struct GeometryFinder : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Geometry> _geom;
|
|
|
|
GeometryFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
|
2015-10-22 22:14:53 +08:00
|
|
|
void apply(osg::Geode& geode)
|
2009-03-10 01:38:39 +08:00
|
|
|
{
|
|
|
|
if (_geom.valid())
|
|
|
|
return;
|
2015-10-22 22:14:53 +08:00
|
|
|
for (unsigned int i = 0; i < geode.getNumDrawables(); i++)
|
2009-03-10 01:38:39 +08:00
|
|
|
{
|
|
|
|
osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
|
|
|
|
if (geom) {
|
|
|
|
_geom = geom;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-20 20:03:56 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> getShape(const std::string& name)
|
2009-03-10 01:38:39 +08:00
|
|
|
{
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> shape0 = osgDB::readRefNodeFile(name);
|
2010-12-20 20:02:50 +08:00
|
|
|
if (shape0)
|
|
|
|
{
|
2010-12-20 20:03:56 +08:00
|
|
|
GeometryFinder finder;
|
2010-12-20 20:02:50 +08:00
|
|
|
shape0->accept(finder);
|
2010-12-20 20:03:56 +08:00
|
|
|
return finder._geom;
|
2010-12-20 20:02:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-10 01:38:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
osgViewer::Viewer viewer(arguments);
|
|
|
|
|
|
|
|
osgAnimation::Animation* animation = new osgAnimation::Animation;
|
|
|
|
osgAnimation::FloatLinearChannel* channel0 = new osgAnimation::FloatLinearChannel;
|
|
|
|
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0,0.0));
|
|
|
|
channel0->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1,1.0));
|
|
|
|
channel0->setTargetName("MorphNodeCallback");
|
|
|
|
channel0->setName("0");
|
|
|
|
|
|
|
|
animation->addChannel(channel0);
|
|
|
|
animation->setName("Morph");
|
|
|
|
animation->computeDuration();
|
From Wang Rui, "Attached is the osgAnimation wrappers for serialize IO operations. A
few headers and the osgAnimation sources are also modified to make
everything goes well, including:
A new REGISTER_OBJECT_WRAPPER2 macro to wrap classes like
Skeleton::UpdateSkeleton.
A bug fix in the Seralizer header which avoids setting default values
to objects.
Naming style fixes in osgAnimation headers and sources, also in the
deprecated dotosg wrappers.
A bug fix for the XML support, to write char values correctly.
A small change in the osg::Geometry wrapper to ignore the
InternalGeometry property, which is used by the MorphGeometry and
should not be set by user applications.
The avatar.osg, nathan.osg and robot.osg data files all work fine with
serializers, with some 'unsupported wrapper' warnings when converting.
I'm thinking of removing these warnings by disabling related property
serializers (ComputeBoundingBoxCallback and Drawable::UpdateCallback),
which are seldom recorded by users.
By the way, I still wonder how would we handle the C4121 problem,
discussed some days before. The /Zp compile option is set to 16 in the
attached cmake script file. And is there a better solution now?"
2010-04-19 18:35:18 +08:00
|
|
|
animation->setPlayMode(osgAnimation::Animation::PPONG);
|
2009-03-10 01:38:39 +08:00
|
|
|
osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
|
|
|
|
bam->registerAnimation(animation);
|
|
|
|
|
2010-12-20 20:03:56 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> geom0 = getShape("morphtarget_shape0.osg");
|
2009-03-10 01:38:39 +08:00
|
|
|
if (!geom0) {
|
|
|
|
std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-20 20:03:56 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> geom1 = getShape("morphtarget_shape1.osg");
|
2009-03-10 01:38:39 +08:00
|
|
|
if (!geom1) {
|
|
|
|
std::cerr << "can't read morphtarget_shape1.osg" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize with the first shape
|
|
|
|
osgAnimation::MorphGeometry* morph = new osgAnimation::MorphGeometry(*geom0);
|
2010-12-20 20:03:56 +08:00
|
|
|
morph->addMorphTarget(geom1.get());
|
2009-03-10 01:38:39 +08:00
|
|
|
|
|
|
|
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
|
|
|
|
|
|
|
|
|
|
|
|
osg::Group* scene = new osg::Group;
|
|
|
|
scene->addUpdateCallback(bam);
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2009-03-10 01:38:39 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(morph);
|
|
|
|
geode->addUpdateCallback(new osgAnimation::UpdateMorph("MorphNodeCallback"));
|
|
|
|
scene->addChild(geode);
|
|
|
|
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler());
|
|
|
|
viewer.addEventHandler(new osgViewer::WindowSizeHandler());
|
|
|
|
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
|
|
|
|
|
|
|
|
// let's run !
|
|
|
|
viewer.setSceneData( scene );
|
|
|
|
viewer.realize();
|
|
|
|
|
|
|
|
bam->playAnimation(animation);
|
|
|
|
|
|
|
|
|
|
|
|
while (!viewer.done())
|
|
|
|
{
|
|
|
|
viewer.frame();
|
|
|
|
}
|
|
|
|
|
|
|
|
osgDB::writeNodeFile(*scene, "morph_scene.osg");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|