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

Revision 1832, 6.0 kB (checked in by robert, 11 years ago)

Changed the Viewer::realize() calls across to not using the threading paramter
leaving it up to the Viewer to specify the mode (which by default is MultiThreaded?).
Added a check for the presence of osgParticle systems so that threading is
disabled in this case.

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