root/OpenSceneGraph/trunk/examples/osganimationnode/osganimationnode.cpp @ 13850

Revision 13574, 10.7 kB (checked in by robert, 8 hours ago)

Added simple test script for osgUI's TabWidget?

  • 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/Shape>
18#include <osg/ShapeDrawable>
19#include <osgViewer/Viewer>
20#include <osgGA/TrackballManipulator>
21#include <osg/MatrixTransform>
22#include <osg/Material>
23#include <osgAnimation/Sampler>
24
25class AnimtkUpdateCallback : public osg::NodeCallback
26{
27public:
28    META_Object(osgAnimation, AnimtkUpdateCallback);
29
30    AnimtkUpdateCallback()
31    {
32        _sampler = new osgAnimation::Vec3CubicBezierSampler;
33        _playing = false;
34        _lastUpdate = 0;
35    }
36    AnimtkUpdateCallback(const AnimtkUpdateCallback& val, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
37        osg::Object(val, copyop),
38        osg::NodeCallback(val, copyop),
39        _sampler(val._sampler),
40        _startTime(val._startTime),
41        _currentTime(val._currentTime),
42        _playing(val._playing),
43        _lastUpdate(val._lastUpdate)
44    {
45    }
46
47    /** Callback method called by the NodeVisitor when visiting a node.*/
48    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
49    {
50        if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR &&
51            nv->getFrameStamp() &&
52            nv->getFrameStamp()->getFrameNumber() != _lastUpdate) {
53
54            _lastUpdate = nv->getFrameStamp()->getFrameNumber();
55            _currentTime = osg::Timer::instance()->tick();
56
57            if (_playing && _sampler.get() && _sampler->getKeyframeContainer()) {
58                osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node);
59                if (transform) {
60                    osg::Vec3 result;
61                    float t = osg::Timer::instance()->delta_s(_startTime, _currentTime);
62                    float duration = _sampler->getEndTime() - _sampler->getStartTime();
63                    t = fmod(t, duration);
64                    t += _sampler->getStartTime();
65                    _sampler->getValueAt(t, result);
66                    transform->setMatrix(osg::Matrix::translate(result));
67                }
68            }
69        }
70        // note, callback is responsible for scenegraph traversal so
71        // they must call traverse(node,nv) to ensure that the
72        // scene graph subtree (and associated callbacks) are traversed.
73        traverse(node,nv);
74    }
75
76    void start() { _startTime = osg::Timer::instance()->tick(); _currentTime = _startTime; _playing = true;}
77    void stop() { _currentTime = _startTime; _playing = false;}
78
79    osg::ref_ptr<osgAnimation::Vec3CubicBezierSampler> _sampler;
80    osg::Timer_t _startTime;
81    osg::Timer_t _currentTime;
82    bool _playing;
83    unsigned int _lastUpdate;
84};
85
86
87class AnimtkStateSetUpdateCallback : public osg::StateSet::Callback
88{
89public:
90    META_Object(osgAnimation, AnimtkStateSetUpdateCallback);
91
92    AnimtkStateSetUpdateCallback()
93    {
94        _sampler = new osgAnimation::Vec4LinearSampler;
95        _playing = false;
96        _lastUpdate = 0;
97    }
98
99    AnimtkStateSetUpdateCallback(const AnimtkStateSetUpdateCallback& val, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY):
100        osg::Object(val, copyop),
101        osg::StateSet::Callback(val, copyop),
102        _sampler(val._sampler),
103        _startTime(val._startTime),
104        _currentTime(val._currentTime),
105        _playing(val._playing),
106        _lastUpdate(val._lastUpdate)
107    {
108    }
109
110    /** Callback method called by the NodeVisitor when visiting a node.*/
111    virtual void operator()(osg::StateSet* state, osg::NodeVisitor* nv)
112    {
113        if (state &&
114            nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR &&
115            nv->getFrameStamp() &&
116            nv->getFrameStamp()->getFrameNumber() != _lastUpdate)
117        {
118
119            _lastUpdate = nv->getFrameStamp()->getFrameNumber();
120            _currentTime = osg::Timer::instance()->tick();
121
122            if (_playing && _sampler.get() && _sampler->getKeyframeContainer())
123            {
124                osg::Material* material = dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL));
125                if (material)
126                {
127                    osg::Vec4 result;
128                    float t = osg::Timer::instance()->delta_s(_startTime, _currentTime);
129                    float duration = _sampler->getEndTime() - _sampler->getStartTime();
130                    t = fmod(t, duration);
131                    t += _sampler->getStartTime();
132                    _sampler->getValueAt(t, result);
133                    material->setDiffuse(osg::Material::FRONT_AND_BACK, result);
134                }
135            }
136        }
137    }
138
139    void start() { _startTime = osg::Timer::instance()->tick(); _currentTime = _startTime; _playing = true;}
140    void stop() { _currentTime = _startTime; _playing = false;}
141
142    osg::ref_ptr<osgAnimation::Vec4LinearSampler> _sampler;
143    osg::Timer_t _startTime;
144    osg::Timer_t _currentTime;
145    bool _playing;
146    unsigned int _lastUpdate;
147};
148
149
150osg::Geode* createAxis()
151{
152    osg::Geode* geode  = new osg::Geode;
153    osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry());
154
155    osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array());
156    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
157    vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0));
158    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
159    vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0));
160    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0));
161    vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0));
162    geometry->setVertexArray (vertices.get());
163
164    osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array());
165    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
166    colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f));
167    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
168    colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f));
169    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
170    colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f));
171    geometry->setColorArray (colors.get(), osg::Array::BIND_PER_VERTEX);
172    geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6));
173
174    geode->addDrawable( geometry.get() );
175    geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
176    return geode;
177}
178
179osg::StateSet* setupStateSet()
180{
181    osg::StateSet* st = new osg::StateSet;
182    st->setAttributeAndModes(new osg::Material, true);
183    st->setMode(GL_BLEND, true);
184    AnimtkStateSetUpdateCallback* callback = new AnimtkStateSetUpdateCallback;
185    osgAnimation::Vec4KeyframeContainer* keys = callback->_sampler->getOrCreateKeyframeContainer();
186    keys->push_back(osgAnimation::Vec4Keyframe(0, osg::Vec4(0,0,0,0)));
187    keys->push_back(osgAnimation::Vec4Keyframe(2, osg::Vec4(0.5,0,0,0.5)));
188    keys->push_back(osgAnimation::Vec4Keyframe(4, osg::Vec4(0,0.5,0,1)));
189    keys->push_back(osgAnimation::Vec4Keyframe(6, osg::Vec4(0,0,0.5,1)));
190    keys->push_back(osgAnimation::Vec4Keyframe(8, osg::Vec4(1,1,1,0.5)));
191    keys->push_back(osgAnimation::Vec4Keyframe(10, osg::Vec4(0,0,0,0)));
192    callback->start();
193    st->setUpdateCallback(callback);
194    return st;
195}
196
197osg::Node* setupCube()
198{
199    osg::Geode* geode = new osg::Geode;
200    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),2)));
201    geode->setStateSet(setupStateSet());
202    return geode;
203}
204
205osg::MatrixTransform* setupAnimtkNode()
206{
207    osg::Vec3 v[5];
208    v[0] = osg::Vec3(0,0,0);
209    v[1] = osg::Vec3(10,-50,0);
210    v[2] = osg::Vec3(30,-10,20);
211    v[3] = osg::Vec3(-10,20,-20);
212    v[4] = osg::Vec3(0,0,0);
213    osg::MatrixTransform* node = new osg::MatrixTransform;
214    AnimtkUpdateCallback* callback = new AnimtkUpdateCallback;
215    osgAnimation::Vec3CubicBezierKeyframeContainer* keys = callback->_sampler->getOrCreateKeyframeContainer();
216    keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(0, osgAnimation::Vec3CubicBezier(
217                                                        v[0], // pos
218                                                        v[0] + (v[0] - v[3]), // p1
219                                                        v[1] - (v[1] - v[0]) // p2
220                                                        )));
221    keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(2, osgAnimation::Vec3CubicBezier(
222                                                        v[1], // pos
223                                                        v[1] + (v[1] - v[0]),
224                                                        v[2] - (v[2] - v[1])
225                                                        )));
226    keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(4, osgAnimation::Vec3CubicBezier(
227                                                        v[2], // pos
228                                                        v[2] + (v[2] - v[1]),
229                                                        v[3] - (v[3] - v[2])
230                                                        )));
231    keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(6, osgAnimation::Vec3CubicBezier(
232                                                        v[3], // pos
233                                                        v[3] + (v[3] - v[2]),
234                                                        v[4] - (v[4] - v[3])
235                                                        )));
236    keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(8, osgAnimation::Vec3CubicBezier(
237                                                        v[4], // pos
238                                                        v[4] + (v[4] - v[3]),
239                                                        v[0] - (v[0] - v[4])
240                                                        )));
241
242    callback->start();
243    node->setUpdateCallback(callback);
244    node->addChild(setupCube());
245    return node;
246}
247
248int main (int argc, char* argv[])
249{
250    osg::ArgumentParser arguments(&argc, argv);
251    osgViewer::Viewer viewer(arguments);
252
253    osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator();
254    viewer.setCameraManipulator(manipulator);
255
256    osg::Group* root = new osg::Group;
257    root->setInitialBound(osg::BoundingSphere(osg::Vec3(10,0,10), 30));
258    root->addChild(createAxis());
259
260    osg::MatrixTransform* node = setupAnimtkNode();
261    node->addChild(createAxis());
262    root->addChild(node);
263
264    viewer.setSceneData( root );
265    viewer.realize();
266
267    while (!viewer.done())
268    {
269        viewer.frame();
270    }
271
272}
Note: See TracBrowser for help on using the browser.