root/OpenSceneGraph/trunk/examples/osganimationeasemotion/osganimationeasemotion.cpp @ 11115

Revision 11115, 12.0 kB (checked in by robert, 4 years ago)

From Jeremy Moles, submitted by Cedric Pinson "Here an new example from Jeremy Moles that demonstrate EaseMotion from
osgAnimation, i have updated EaseMotion with new type, so for users of
EaseMotion just have look, it's a great example."

Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2010 Jeremy Moles <cubicool@gmail.com>
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 <osg/Geode>
16#include <osg/MatrixTransform>
17#include <osg/ShapeDrawable>
18#include <osgGA/TrackballManipulator>
19#include <osgViewer/Viewer>
20#include <osgViewer/ViewerEventHandlers>
21#include <osgAnimation/EaseMotion>
22#include <osgWidget/WindowManager>
23#include <osgWidget/Box>
24#include <osgWidget/Table>
25#include <osgWidget/Label>
26
27class EaseMotionSampler;
28
29const unsigned int WINDOW_WIDTH  = 800;
30const unsigned int WINDOW_HEIGHT = 600;
31const unsigned int MASK_2D       = 0xF0000000;
32const unsigned int MASK_3D       = 0x0F000000;
33const float        M_START       = 0.0f;
34const float        M_DURATION    = 2.0f;
35const float        M_CHANGE      = 1.0f;
36
37EaseMotionSampler* EASE_MOTION_SAMPLER = 0;
38osg::Geode*        EASE_MOTION_GEODE   = 0;
39
40osg::Geometry* createEaseMotionGeometry(osgAnimation::Motion* motion) {
41    osg::Geometry*  geom = new osg::Geometry();
42    osg::Vec4Array* cols = new osg::Vec4Array();
43    osg::Vec3Array* v    = new osg::Vec3Array();
44
45    for(float i = 0.0f; i < M_DURATION; i += M_DURATION / 256.0f) v->push_back(
46        osg::Vec3(i * 30.0f, motion->getValueAt(i) * 30.0f, 0.0f)
47        );
48
49    cols->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
50
51    geom->setUseDisplayList(false);
52    geom->setVertexArray(v);
53    geom->setColorArray(cols);
54    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
55    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, v->size()));
56
57    return geom;
58}
59
60struct EaseMotionSampler: public osg::NodeCallback {
61    float     _previous;
62    osg::Vec3 _pos;
63
64    osg::ref_ptr<osgAnimation::Motion> _motion;
65
66    EaseMotionSampler(const osg::Vec3& pos):
67        _previous (0.0f),
68        _pos      (pos) {
69    }
70
71    void operator()(osg::Node* node, osg::NodeVisitor* nv) {
72        if(!_motion.valid()) return;
73
74        osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(node);
75
76        if(!mt) return;
77
78        double t = nv->getFrameStamp()->getSimulationTime();
79
80        // This avoids a little glitch when the animation doesn't start right away
81        // when the application is launched.
82        if(_previous == 0.0f) _previous = t;
83
84        _motion->update(t - _previous);
85
86        _previous = t;
87
88        mt->setMatrix(osg::Matrix::translate(_pos * _motion->getValue()));
89    }
90
91    template<typename T>
92    void setMotion() {
93        _motion = new T(M_START, M_DURATION, M_CHANGE, osgAnimation::Motion::LOOP);
94
95        EASE_MOTION_GEODE->removeDrawables(0, EASE_MOTION_GEODE->getNumDrawables());
96        EASE_MOTION_GEODE->addDrawable(createEaseMotionGeometry(_motion.get()));
97    }
98};
99
100struct ColorLabel: public osgWidget::Label {
101    ColorLabel(const char* label):
102        osgWidget::Label(label, "") {
103        setFont("fonts/VeraMono.ttf");
104        setFontSize(14);
105        setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
106               
107        setColor(0.3f, 0.3f, 0.3f, 1.0f);
108        setPadding(2.0f);
109        setCanFill(true);
110               
111        addSize(150.0f, 25.0f);
112
113        setLabel(label);
114        setEventMask(osgWidget::EVENT_MOUSE_PUSH | osgWidget::EVENT_MASK_MOUSE_MOVE);
115    }
116
117    bool mousePush(double, double, const osgWidget::WindowManager*) {
118        osgWidget::Table* p = dynamic_cast<osgWidget::Table*>(_parent);
119       
120        if(!p) return false;
121               
122        p->hide();
123
124        const std::string& name = getName();
125
126        if(!name.compare("OutQuadMotion"))
127            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutQuadMotion>()
128                ;
129
130        else if(!name.compare("InQuadMotion"))
131            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InQuadMotion>()
132                ;
133
134        else if(!name.compare("InOutQuadMotion"))
135            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutQuadMotion>()
136                ;
137
138        else if(!name.compare("OutCubicMotion"))
139            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutCubicMotion>()
140                ;
141
142        else if(!name.compare("InCubicMotion"))
143            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InCubicMotion>()
144                ;
145
146        else if(!name.compare("InOutCubicMotion"))
147            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutCubicMotion>()
148                ;
149
150        else if(!name.compare("OutQuartMotion"))
151            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutQuartMotion>()
152                ;
153
154        else if(!name.compare("InQuartMotion"))
155            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InQuartMotion>()
156                ;
157
158        else if(!name.compare("InOutQuartMotion"))
159            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutQuartMotion>()
160                ;
161
162        else if(!name.compare("OutBounceMotion"))
163            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutBounceMotion>()
164                ;
165
166        else if(!name.compare("InBounceMotion"))
167            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InBounceMotion>()
168                ;
169
170        else if(!name.compare("InOutBounceMotion"))
171            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutBounceMotion>()
172                ;
173
174        else if(!name.compare("OutElasticMotion"))
175            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutElasticMotion>()
176                ;
177
178        else if(!name.compare("InElasticMotion"))
179            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InElasticMotion>()
180                ;
181
182        else if(!name.compare("InOutElasticMotion"))
183            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutElasticMotion>()
184                ;
185
186        else if(!name.compare("OutSineMotion"))
187            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutSineMotion>()
188                ;
189
190        else if(!name.compare("InSineMotion"))
191            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InSineMotion>()
192                ;
193
194        else if(!name.compare("InOutSineMotion"))
195            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutSineMotion>()
196                ;
197
198        else if(!name.compare("OutBackMotion"))
199            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutBackMotion>()
200                ;
201
202        else if(!name.compare("InBackMotion"))
203            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InBackMotion>()
204                ;
205
206        else if(!name.compare("InOutBackMotion"))
207            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutBackMotion>()
208                ;
209
210        else if(!name.compare("OutCircMotion"))
211            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutCircMotion>()
212                ;
213
214        else if(!name.compare("InCircMotion"))
215            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InCircMotion>()
216                ;
217
218        else if(!name.compare("InOutCircMotion"))
219            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutCircMotion>()
220                ;
221
222        else if(!name.compare("OutExpoMotion"))
223            EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutExpoMotion>()
224                ;
225
226        else if(!name.compare("InExpoMotion"))
227            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InExpoMotion>()
228                ;
229
230        else if(!name.compare("InOutExpoMotion"))
231            EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutExpoMotion>()
232                ;
233               
234        else EASE_MOTION_SAMPLER->setMotion<osgAnimation::LinearMotion>();
235
236        return true;
237    }
238
239    bool mouseEnter(double, double, const osgWidget::WindowManager*) {
240        setColor(0.9f, 0.6f, 0.1f, 1.0f);
241               
242        return true;
243    }
244
245    bool mouseLeave(double, double, const osgWidget::WindowManager*) {
246        setColor(0.3f, 0.3f, 0.3f, 1.0f);
247               
248        return true;
249    }
250};
251
252class ColorLabelMenu: public ColorLabel {
253    osg::ref_ptr<osgWidget::Table> _window;
254
255public:
256    ColorLabelMenu(const char* label):
257        ColorLabel(label) {
258        _window = new osgWidget::Table(std::string("Menu_") + label, 6, 5);
259
260        _window->addWidget(new ColorLabel("OutQuadMotion"), 0, 0);
261        _window->addWidget(new ColorLabel("InQuadMotion"), 1, 0);
262        _window->addWidget(new ColorLabel("InOutQuadMotion"), 2, 0);
263        _window->addWidget(new ColorLabel("OutCubicMotion"), 3, 0);
264        _window->addWidget(new ColorLabel("InCubicMotion"), 4, 0);
265        _window->addWidget(new ColorLabel("InOutCubicMotion"), 5, 0);
266
267        _window->addWidget(new ColorLabel("OutQuartMotion"), 0, 1);
268        _window->addWidget(new ColorLabel("InQuartMotion"), 1, 1);
269        _window->addWidget(new ColorLabel("InOutQuartMotion"), 2, 1);
270        _window->addWidget(new ColorLabel("OutBounceMotion"), 3, 1);
271        _window->addWidget(new ColorLabel("InBounceMotion"), 4, 1);
272        _window->addWidget(new ColorLabel("InOutBounceMotion"), 5, 1);
273
274        _window->addWidget(new ColorLabel("OutElasticMotion"), 0, 2);
275        _window->addWidget(new ColorLabel("InElasticMotion"), 1, 2);
276        _window->addWidget(new ColorLabel("InOutElasticMotion"), 2, 2);
277        _window->addWidget(new ColorLabel("OutSineMotion"), 3, 2);
278        _window->addWidget(new ColorLabel("InSineMotion"), 4, 2);
279        _window->addWidget(new ColorLabel("InOutSineMotion"), 5, 2);
280
281        _window->addWidget(new ColorLabel("OutBackMotion"), 0, 3);
282        _window->addWidget(new ColorLabel("InBackMotion"), 1, 3);
283        _window->addWidget(new ColorLabel("InOutBackMotion"), 2, 3);
284        _window->addWidget(new ColorLabel("OutCircMotion"), 3, 3);
285        _window->addWidget(new ColorLabel("InCircMotion"), 4, 3);
286        _window->addWidget(new ColorLabel("InOutCircMotion"), 5, 3);
287               
288        _window->addWidget(new ColorLabel("OutExpoMotion"), 0, 4);
289        _window->addWidget(new ColorLabel("InExpoMotion"), 1, 4);
290        _window->addWidget(new ColorLabel("InOutExpoMotion"), 2, 4);
291        _window->addWidget(new ColorLabel("Linear"), 3, 4);
292
293        _window->resize();
294    }
295
296    void managed(osgWidget::WindowManager* wm) {
297        osgWidget::Label::managed(wm);
298
299        wm->addChild(_window.get());
300
301        _window->hide();
302    }
303
304    void positioned() {
305        osgWidget::Label::positioned();
306
307        _window->setOrigin(_parent->getX(), _parent->getY() +  _parent->getHeight());
308    }
309
310    bool mousePush(double, double, const osgWidget::WindowManager*) {
311        if(!_window->isVisible()) _window->show();
312
313        else _window->hide();
314
315        return true;
316    }
317};
318
319int main(int argc, char** argv) {
320    osgViewer::Viewer viewer;
321
322    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
323        &viewer,
324        WINDOW_WIDTH,
325        WINDOW_HEIGHT,
326        MASK_2D
327        );
328
329    osgWidget::Window* menu = new osgWidget::Box("menu", osgWidget::Box::HORIZONTAL);
330
331    menu->addWidget(new ColorLabelMenu("Choose EaseMotion"));
332    menu->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f);
333    menu->setPosition(15.0f, 15.0f, 0.0f);
334
335    wm->addChild(menu);
336
337    osg::Group*           group = new osg::Group();
338    osg::Geode*           geode = new osg::Geode();
339    osg::MatrixTransform* mt    = new osg::MatrixTransform();
340
341    geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(), 4.0f)));
342
343    EASE_MOTION_SAMPLER = new EaseMotionSampler(osg::Vec3(50.0f, 0.0f, 0.0f));
344    EASE_MOTION_GEODE   = new osg::Geode();
345
346    mt->addChild(geode);
347    mt->setUpdateCallback(EASE_MOTION_SAMPLER);
348    mt->setNodeMask(MASK_3D);
349
350    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
351    viewer.getCameraManipulator()->setHomePosition(
352        osg::Vec3d(0.0f, 0.0f, 200.0f),
353        osg::Vec3d(20.0f, 0.0f, 0.0f),
354        osg::Vec3d(0.0f, 1.0f, 0.0f)
355        );
356    viewer.home();
357
358    group->addChild(mt);
359    group->addChild(EASE_MOTION_GEODE);
360
361    return osgWidget::createExample(viewer, wm, group);
362}
Note: See TracBrowser for help on using the browser.