2009-03-10 01:38:39 +08:00
|
|
|
/* -*-c++-*-
|
|
|
|
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* 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) {}
|
|
|
|
void apply(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
if (_geom.valid())
|
|
|
|
return;
|
2009-03-13 19:06:12 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
osg::Geometry* getShape(const std::string& name)
|
|
|
|
{
|
|
|
|
osg::Node* shape0 = osgDB::readNodeFile(name);
|
|
|
|
GeometryFinder finder;
|
|
|
|
shape0->accept(finder);
|
|
|
|
return finder._geom.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
osg::Geometry* geom0 = getShape("morphtarget_shape0.osg");
|
|
|
|
if (!geom0) {
|
|
|
|
std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Geometry* geom1 = getShape("morphtarget_shape1.osg");
|
|
|
|
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);
|
|
|
|
morph->addMorphTarget(geom1);
|
|
|
|
|
|
|
|
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
|
|
|
|
|
|
|
|
|
|
|
|
osg::Group* scene = new osg::Group;
|
|
|
|
scene->addUpdateCallback(bam);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|