File: osganimationmorph.cpp

package info (click to toggle)
openscenegraph 3.2.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,820 kB
  • ctags: 34,610
  • sloc: cpp: 370,040; ansic: 9,071; java: 1,020; yacc: 548; objc: 288; makefile: 285; xml: 155; lex: 151
file content (134 lines) | stat: -rw-r--r-- 4,147 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*  -*-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;
        for (unsigned int i = 0; i < geode.getNumDrawables(); i++) 
        {
            osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
            if (geom) {
                _geom = geom;
                return;
            }
        }
    }
};

osg::ref_ptr<osg::Geometry> getShape(const std::string& name)
{
    osg::ref_ptr<osg::Node> shape0 = osgDB::readNodeFile(name);
    if (shape0)
    {
        GeometryFinder finder;
        shape0->accept(finder);
        return finder._geom;
    }
    else
    {
        return NULL;
    }
}


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();
    animation->setPlayMode(osgAnimation::Animation::PPONG);
    osgAnimation::BasicAnimationManager* bam = new osgAnimation::BasicAnimationManager;
    bam->registerAnimation(animation);

    osg::ref_ptr<osg::Geometry> geom0 = getShape("morphtarget_shape0.osg");
    if (!geom0) {
        std::cerr << "can't read morphtarget_shape0.osg" << std::endl;
        return 0;
    }

    osg::ref_ptr<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.get());

    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;
}