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

Revision 5381, 8.9 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// -*-c++-*-
2
3/*
4 * This application is open source and may be redistributed and/or modified   
5 * freely and without restriction, both in commericial and non commericial
6 * applications,as long as this copyright notice is maintained.
7 *
8 * This application is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*/
12
13#include <osgText/Text>
14#include <osg/Geode>
15#include <osg/Group>
16#include <osg/Projection>
17#include <osg/Sequence>
18#include <osg/MatrixTransform>
19
20#include <osgDB/ReadFile>
21
22#include <osgProducer/Viewer>
23
24
25// create text drawable at 'pos'
26osg::Geode* createText(const std::string& str, const osg::Vec3& pos)
27{
28    // text drawable
29    osgText::Text* text = new osgText::Text;
30    text->setFont(std::string("fonts/arial.ttf"));
31    text->setPosition(pos);
32    text->setText(str);
33
34    // geode
35    osg::Geode* geode = new osg::Geode;
36    geode->addDrawable(text);
37
38    return geode;
39}
40
41osg::Node* createTextGroup(const char** text)
42{
43    osg::Group* group = new osg::Group;
44
45    osg::Vec3 pos(120.0f, 800.0f, 0.0f);
46    const osg::Vec3 delta(0.0f, -60.0f, 0.0f);
47
48    // header
49    const char** t = text;
50    group->addChild(createText(*t++, pos));
51    pos += delta;
52
53    // remainder of text under sequence
54    osg::Sequence* seq = new osg::Sequence;
55    group->addChild(seq);
56    while (*t) {
57        seq->addChild(createText(*t++, pos));
58        seq->setTime(seq->getNumChildren()-1, 2.0f);
59        pos += delta;
60    }
61
62    // loop through all children
63    seq->setInterval(osg::Sequence::LOOP, 0,-1);
64
65    // real-time playback, repeat indefinitively
66    seq->setDuration(1.0f, -1);
67
68    // must be started explicitly
69    seq->setMode(osg::Sequence::START);
70
71    return group;
72}
73
74osg::Node* createHUD(osg::Node* node)
75{
76    // absolute transform
77    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
78    modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
79    modelview_abs->setMatrix(osg::Matrix::identity());
80    modelview_abs->addChild(node);
81
82    // 2D projection node
83    osg::Projection* projection = new osg::Projection;
84    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
85    projection->addChild(modelview_abs);
86
87    // turn off lighting and depth test
88    osg::StateSet* state = modelview_abs->getOrCreateStateSet();
89    state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
90    state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
91
92    return projection;
93}
94
95osg::Node* createScaledNode(osg::Node* node, float targetScale)
96{
97    // create scale matrix
98    osg::MatrixTransform* transform = new osg::MatrixTransform;
99
100    const osg::BoundingSphere& bsphere = node->getBound();
101    float scale = targetScale / bsphere._radius;
102    transform->setMatrix(osg::Matrix::scale(scale,scale,scale));
103    transform->setDataVariance(osg::Object::STATIC);
104    transform->addChild(node);
105
106    // rescale normals
107    osg::StateSet* state = transform->getOrCreateStateSet();
108    state->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
109
110    return transform;
111}
112
113osg::Sequence* createSequence(osg::ArgumentParser& arguments)
114{
115    // assumes any remaining parameters are models
116    osg::Sequence* seq = new osg::Sequence;
117    for (int i = 1; i < arguments.argc(); ++i)
118    {
119        // load model
120        osg::Node* node = osgDB::readNodeFile(arguments[i]);
121        if (!node) {
122            continue;
123        }
124        seq->addChild(createScaledNode(node, 100.0f));
125        seq->setTime(seq->getNumChildren()-1, 1.0f);
126    }
127
128    // loop through all children
129    seq->setInterval(osg::Sequence::LOOP, 0,-1);
130
131    // real-time playback, repeat indefinitively
132    seq->setDuration(1.0f, -1);
133
134    return seq;
135}
136
137// event handler to control sequence
138class SequenceEventHandler : public osgGA::GUIEventHandler
139{
140public:
141    SequenceEventHandler(osg::Sequence* seq)
142    {
143        _seq = seq;
144    }
145
146    // handle keydown events
147    virtual bool handle(const osgGA::GUIEventAdapter& ea,
148                        osgGA::GUIActionAdapter&)
149    {
150        if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) {
151            switch (ea.getKey()) {
152            case 'S':
153                {
154                    osg::Sequence::SequenceMode mode = _seq->getMode();
155                    if (mode == osg::Sequence::STOP) {
156                        mode = osg::Sequence::START;
157                        std::cerr << "Start" << std::endl;
158                    }
159                    else if (mode == osg::Sequence::PAUSE) {
160                        mode = osg::Sequence::RESUME;
161                        std::cerr << "Resume" << std::endl;
162                    }
163                    else {
164                        mode = osg::Sequence::PAUSE;
165                        std::cerr << "Pause" << std::endl;
166                    }
167                    _seq->setMode(mode);
168                }
169                break;
170            case 'L':
171                {
172                    osg::Sequence::LoopMode mode;
173                    int begin, end;
174                    _seq->getInterval(mode, begin, end);
175                    if (mode == osg::Sequence::LOOP) {
176                        mode = osg::Sequence::SWING;
177                        std::cerr << "Swing" << std::endl;
178                    }
179                    else {
180                        mode = osg::Sequence::LOOP;
181                        std::cerr << "Loop" << std::endl;
182                    }
183                    _seq->setInterval(mode, begin, end);
184                }
185                break;
186            default:
187                break;
188            }
189        }
190
191        return false;
192    }
193
194    // accept visits
195    virtual void accept(osgGA::GUIEventHandlerVisitor&) {}
196
197private:
198    osg::ref_ptr<osg::Sequence> _seq;
199};
200
201
202int main( int argc, char **argv )
203{
204    // use an ArgumentParser object to manage the program arguments.
205    osg::ArgumentParser arguments(&argc,argv);
206
207    // set up the usage document, in case we need to print out how to use this program.
208    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::Sequence.");
209    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
210    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
211   
212    // construct the viewer.
213    osgProducer::Viewer viewer(arguments);
214
215    // set up the value with sensible default event handlers.
216    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
217
218    // get details on keyboard and mouse bindings used by the viewer.
219    viewer.getUsage(*arguments.getApplicationUsage());
220
221    // if user request help write it out to cout.
222    if (arguments.read("-h") || arguments.read("--help"))
223    {
224        arguments.getApplicationUsage()->write(std::cout);
225        return 1;
226    }
227
228    // any option left unread are converted into errors to write out later.
229    arguments.reportRemainingOptionsAsUnrecognized();
230
231    // report any errors if they have occured when parsing the program aguments.
232    if (arguments.errors())
233    {
234        arguments.writeErrorMessages(std::cout);
235        return 1;
236    }
237   
238    if (arguments.argc()<=1)
239    {
240        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
241        return 1;
242    }
243   
244    // root
245    osg::Group* rootNode = new osg::Group;
246
247    // create info display
248    const char* text[] = {
249        "osg::Sequence Mini-Howto",
250        "- can be used for simple flip-book-style animation",
251        "- is subclassed from osg::Switch",
252        "- assigns a display duration to each child",
253        "- can loop or swing through an interval of it's children",
254        "- can repeat the interval a number of times or indefinitively",
255        "- press 'Shift-S' to start/pause/resume",
256        "- press 'Shift-L' to toggle loop/swing mode",
257        NULL
258    };
259    rootNode->addChild(createHUD(createTextGroup(text)));
260
261    // add sequence of models from command line
262    osg::Sequence* seq = createSequence(arguments);
263    rootNode->addChild(seq);
264
265    // add model to viewer.
266    viewer.setSceneData(rootNode);
267
268    // add event handler to control sequence
269    viewer.getEventHandlerList().push_front(new SequenceEventHandler(seq));
270
271    // create the windows and run the threads.
272    viewer.realize();
273
274    while( !viewer.done() )
275    {
276        // wait for all cull and draw threads to complete.
277        viewer.sync();
278
279        // update the scene by traversing it with the the update visitor which will
280        // call all node update callbacks and animations.
281        viewer.update();
282         
283        // fire off the cull and draw traversals of the scene.
284        viewer.frame();
285       
286    }
287   
288    // wait for all cull and draw threads to complete.
289    viewer.sync();
290
291    // run a clean up frame to delete all OpenGL objects.
292    viewer.cleanup_frame();
293
294    // wait for all the clean up frame to complete.
295    viewer.sync();
296
297    return 0;
298}
Note: See TracBrowser for help on using the browser.