root/OpenSceneGraph/trunk/examples/osgsequence/osgsequence.cpp @ 1697

Revision 1697, 5.9 kB (checked in by robert, 11 years ago)

Ported osgGLUT based src/Demos across to being osgProducer based, and placed
them in the new examples/ directory.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// -*-c++-*-
2
3#include <osg/Group>
4#include <osg/Sequence>
5#include <osg/MatrixTransform>
6
7#include <osgDB/ReadFile>
8
9#include <osgGA/TrackballManipulator>
10
11#include <osgProducer/Viewer>
12
13
14//
15// A simple demo demonstrating usage of osg::Sequence.
16//
17
18// simple event handler to start/stop sequences
19class MyEventHandler : public osgGA::GUIEventHandler {
20public:
21    /// Constructor.
22    MyEventHandler(std::vector<osg::Sequence*>* seq)
23    {
24        _seq = seq;
25    }
26
27    /// Handle events.
28    virtual bool handle(const osgGA::GUIEventAdapter& ea,
29                        osgGA::GUIActionAdapter&)
30    {
31        bool handled = false;
32
33        if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN)
34        {
35            const char keys[] = "!@#$%^&*()";
36            for (unsigned int i = 0; i < (sizeof(keys) / sizeof(keys[0])); i++) {
37                if (i < _seq->size() && ea.getKey() == keys[i])
38                {
39                    // toggle sequence
40                    osg::Sequence* seq = (*_seq)[i];
41                    osg::Sequence::SequenceMode mode = seq->getMode();
42                    switch (mode) {
43                    case osg::Sequence::START:
44                        seq->setMode(osg::Sequence::PAUSE);
45                        break;
46                    case osg::Sequence::STOP:
47                        seq->setMode(osg::Sequence::START);
48                        break;
49                    case osg::Sequence::PAUSE:
50                        seq->setMode(osg::Sequence::RESUME);
51                        break;
52                    default:
53                        break;
54                    }
55                    std::cerr << "Toggled sequence " << i << std::endl;
56                    handled = true;
57                }
58            }
59        }
60
61        return handled;
62    }
63
64    /// accept visits.
65    virtual void accept(osgGA::GUIEventHandlerVisitor&) {}
66
67private:
68    std::vector<osg::Sequence*>* _seq;
69};
70
71osg::Sequence* generateSeq(osg::Sequence::LoopMode mode,
72                           float speed, int nreps,
73                           std::vector<osg::Node*>& model)
74{
75    osg::Sequence* seqNode = new osg::Sequence;
76
77    // add children, show each child for 1.0 seconds
78    for (unsigned int i = 0; i < model.size(); i++) {
79        seqNode->addChild(model[i]);
80        seqNode->setTime(i, 1.0f);
81    }
82
83    // interval
84    seqNode->setInterval(mode, 0, -1);
85
86    // speed-up factor and number of repeats for entire sequence
87    seqNode->setDuration(speed, nreps);
88
89    // stopped
90    seqNode->setMode(osg::Sequence::STOP);
91
92    return seqNode;
93}
94
95int main( int argc, char **argv )
96{
97    // use an ArgumentParser object to manage the program arguments.
98    osg::ArgumentParser arguments(&argc,argv);
99
100    // set up the usage document, in case we need to print out how to use this program.
101    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
102    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
103   
104    // construct the viewer.
105    osgProducer::Viewer viewer(arguments);
106
107    // set up the value with sensible default event handlers.
108    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
109
110    // get details on keyboard and mouse bindings used by the viewer.
111    viewer.getUsage(*arguments.getApplicationUsage());
112
113    // if user request help write it out to cout.
114    if (arguments.read("-h") || arguments.read("--help"))
115    {
116        arguments.getApplicationUsage()->write(std::cout);
117        return 1;
118    }
119
120    // any option left unread are converted into errors to write out later.
121    arguments.reportRemainingOptionsAsUnrecognized();
122
123    // report any errors if they have occured when parsing the program aguments.
124    if (arguments.errors())
125    {
126        arguments.writeErrorMessages(std::cout);
127        return 1;
128    }
129   
130    // assumes any remaining parameters are models
131    std::vector<osg::Node*> model;
132    for (int i = 1; i < arguments.argc(); i++)
133    {
134        std::cerr << "Loading " << arguments[i] << std::endl;
135        osg::Node* node = osgDB::readNodeFile(arguments[i]);
136        if (node)
137            model.push_back(node);
138    }
139    if (model.empty()) {
140        return -1;
141    }
142
143    // root
144    osg::Group* rootNode = new osg::Group;
145
146    // create sequences
147    std::vector<osg::Sequence*> seq;
148
149    const osg::Sequence::LoopMode mode[] = { osg::Sequence::LOOP,
150                                             osg::Sequence::SWING,
151                                             osg::Sequence::LOOP };
152    const float speed[] = { 0.5f, 1.0f, 1.5f };
153    const int nreps[] = { -1, 5, 1 };
154
155    float x = 0.0f;
156    for (unsigned int j = 0; j < (sizeof(speed) / sizeof(speed[0])); j++) {
157        osg::Sequence* seqNode = generateSeq(mode[j], speed[j], nreps[j],
158                                             model);
159        if (!seqNode)
160            continue;
161        seq.push_back(seqNode);
162
163        // position sequence
164        osg::Matrix matrix;
165        matrix.makeTranslate(x, 0.0, 0.0);
166
167        osg::MatrixTransform* xform = new osg::MatrixTransform;
168        xform->setMatrix(matrix);
169        xform->addChild(seqNode);
170
171        rootNode->addChild(xform);
172
173        x += seqNode->getBound()._radius * 1.5f;
174    }
175
176
177    // add model to viewer.
178    viewer.setSceneData(rootNode);
179
180    // register additional event handler
181    viewer.getEventHandlerList().push_front(new MyEventHandler(&seq));
182
183    // create the windows and run the threads.
184    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
185
186    while( !viewer.done() )
187    {
188        // wait for all cull and draw threads to complete.
189        viewer.sync();
190
191        // update the scene by traversing it with the the update visitor which will
192        // call all node update callbacks and animations.
193        viewer.update();
194         
195        // fire off the cull and draw traversals of the scene.
196        viewer.frame();
197       
198    }
199
200    return 0;
201}
Note: See TracBrowser for help on using the browser.