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

Revision 1747, 5.9 kB (checked in by robert, 12 years ago)

Added a viewer.sync to the end of all the demos.

Removed the Win32 remapping of keycodes from the osgProducer::EventAdapter?.

  • 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.getProgramName()+" [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    // assumes any remaining parameters are models
129    std::vector<osg::Node*> model;
130    for (int i = 1; i < arguments.argc(); i++)
131    {
132        std::cerr << "Loading " << arguments[i] << std::endl;
133        osg::Node* node = osgDB::readNodeFile(arguments[i]);
134        if (node)
135            model.push_back(node);
136    }
137    if (model.empty()) {
138        return -1;
139    }
140
141    // root
142    osg::Group* rootNode = new osg::Group;
143
144    // create sequences
145    std::vector<osg::Sequence*> seq;
146
147    const osg::Sequence::LoopMode mode[] = { osg::Sequence::LOOP,
148                                             osg::Sequence::SWING,
149                                             osg::Sequence::LOOP };
150    const float speed[] = { 0.5f, 1.0f, 1.5f };
151    const int nreps[] = { -1, 5, 1 };
152
153    float x = 0.0f;
154    for (unsigned int j = 0; j < (sizeof(speed) / sizeof(speed[0])); j++) {
155        osg::Sequence* seqNode = generateSeq(mode[j], speed[j], nreps[j],
156                                             model);
157        if (!seqNode)
158            continue;
159        seq.push_back(seqNode);
160
161        // position sequence
162        osg::Matrix matrix;
163        matrix.makeTranslate(x, 0.0, 0.0);
164
165        osg::MatrixTransform* xform = new osg::MatrixTransform;
166        xform->setMatrix(matrix);
167        xform->addChild(seqNode);
168
169        rootNode->addChild(xform);
170
171        x += seqNode->getBound()._radius * 1.5f;
172    }
173
174
175    // add model to viewer.
176    viewer.setSceneData(rootNode);
177
178    // register additional event handler
179    viewer.getEventHandlerList().push_front(new MyEventHandler(&seq));
180
181    // create the windows and run the threads.
182    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
183
184    while( !viewer.done() )
185    {
186        // wait for all cull and draw threads to complete.
187        viewer.sync();
188
189        // update the scene by traversing it with the the update visitor which will
190        // call all node update callbacks and animations.
191        viewer.update();
192         
193        // fire off the cull and draw traversals of the scene.
194        viewer.frame();
195       
196    }
197   
198    // wait for all cull and draw threads to complete before exit.
199    viewer.sync();
200
201    return 0;
202}
Note: See TracBrowser for help on using the browser.