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

Revision 12529, 7.8 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgsequence.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgText/Text>
20#include <osg/Geode>
21#include <osg/Group>
22#include <osg/Projection>
23#include <osg/Sequence>
24#include <osg/MatrixTransform>
25
26#include <osgDB/ReadFile>
27
28#include <osgViewer/Viewer>
29
30#include <iostream>
31
32// create text drawable at 'pos'
33osg::Geode* createText(const std::string& str, const osg::Vec3& pos)
34{
35    // text drawable
36    osgText::Text* text = new osgText::Text;
37    text->setFont(std::string("fonts/arial.ttf"));
38    text->setPosition(pos);
39    text->setText(str);
40
41    // geode
42    osg::Geode* geode = new osg::Geode;
43    geode->addDrawable(text);
44
45    return geode;
46}
47
48osg::Node* createTextGroup(const char** text)
49{
50    osg::Group* group = new osg::Group;
51
52    osg::Vec3 pos(120.0f, 800.0f, 0.0f);
53    const osg::Vec3 delta(0.0f, -60.0f, 0.0f);
54
55    // header
56    const char** t = text;
57    group->addChild(createText(*t++, pos));
58    pos += delta;
59
60    // remainder of text under sequence
61    osg::Sequence* seq = new osg::Sequence;
62    group->addChild(seq);
63    while (*t) {
64        seq->addChild(createText(*t++, pos));
65        seq->setTime(seq->getNumChildren()-1, 2.0f);
66        pos += delta;
67    }
68
69    // loop through all children
70    seq->setInterval(osg::Sequence::LOOP, 0,-1);
71
72    // real-time playback, repeat indefinitively
73    seq->setDuration(1.0f, -1);
74
75    // must be started explicitly
76    seq->setMode(osg::Sequence::START);
77
78    return group;
79}
80
81osg::Node* createHUD(osg::Node* node)
82{
83    // absolute transform
84    osg::MatrixTransform* modelview_abs = new osg::MatrixTransform;
85    modelview_abs->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
86    modelview_abs->setMatrix(osg::Matrix::identity());
87    modelview_abs->addChild(node);
88
89    // 2D projection node
90    osg::Projection* projection = new osg::Projection;
91    projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
92    projection->addChild(modelview_abs);
93
94    // turn off lighting and depth test
95    osg::StateSet* state = modelview_abs->getOrCreateStateSet();
96    state->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
97    state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
98
99    return projection;
100}
101
102osg::Node* createScaledNode(osg::Node* node, float targetScale)
103{
104    // create scale matrix
105    osg::MatrixTransform* transform = new osg::MatrixTransform;
106
107    const osg::BoundingSphere& bsphere = node->getBound();
108    float scale = targetScale / bsphere._radius;
109    transform->setMatrix(osg::Matrix::scale(scale,scale,scale));
110    transform->setDataVariance(osg::Object::STATIC);
111    transform->addChild(node);
112
113    // rescale normals
114    osg::StateSet* state = transform->getOrCreateStateSet();
115    state->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
116
117    return transform;
118}
119
120osg::Sequence* createSequence(osg::ArgumentParser& arguments)
121{
122    // assumes any remaining parameters are models
123    osg::Sequence* seq = new osg::Sequence;
124
125    typedef std::vector<std::string> Filenames;
126    Filenames filenames;
127   
128    if (arguments.argc() > 1)
129    {
130        for (int i = 1; i < arguments.argc(); ++i)
131        {
132            filenames.push_back(arguments[i]);
133        }
134    }
135    else
136    {
137        filenames.push_back("cow.osgt");
138        filenames.push_back("dumptruck.osgt");
139        filenames.push_back("cessna.osgt");
140        filenames.push_back("glider.osgt");
141    }
142   
143    for(Filenames::iterator itr = filenames.begin();
144        itr != filenames.end();
145        ++itr)       
146    {
147        // load model
148        osg::Node* node = osgDB::readNodeFile(*itr);
149
150        if (node)
151        {
152            seq->addChild(createScaledNode(node, 100.0f));
153            seq->setTime(seq->getNumChildren()-1, 1.0f);
154        }
155    }
156
157    // loop through all children
158    seq->setInterval(osg::Sequence::LOOP, 0,-1);
159
160    // real-time playback, repeat indefinitively
161    seq->setDuration(1.0f, -1);
162
163    seq->setMode(osg::Sequence::START);
164
165    return seq;
166}
167
168// event handler to control sequence
169class SequenceEventHandler : public osgGA::GUIEventHandler
170{
171public:
172    SequenceEventHandler(osg::Sequence* seq)
173    {
174        _seq = seq;
175    }
176
177    // handle keydown events
178    virtual bool handle(const osgGA::GUIEventAdapter& ea,
179                        osgGA::GUIActionAdapter&)
180    {
181        if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) {
182            switch (ea.getKey()) {
183            case 's':
184                {
185                    osg::Sequence::SequenceMode mode = _seq->getMode();
186                    if (mode == osg::Sequence::STOP) {
187                        mode = osg::Sequence::START;
188                        std::cerr << "Start" << std::endl;
189                    }
190                    else if (mode == osg::Sequence::PAUSE) {
191                        mode = osg::Sequence::RESUME;
192                        std::cerr << "Resume" << std::endl;
193                    }
194                    else {
195                        mode = osg::Sequence::PAUSE;
196                        std::cerr << "Pause" << std::endl;
197                    }
198                    _seq->setMode(mode);
199                }
200                break;
201            case 'l':
202                {
203                    osg::Sequence::LoopMode mode;
204                    int begin, end;
205                    _seq->getInterval(mode, begin, end);
206                    if (mode == osg::Sequence::LOOP) {
207                        mode = osg::Sequence::SWING;
208                        std::cerr << "Swing" << std::endl;
209                    }
210                    else {
211                        mode = osg::Sequence::LOOP;
212                        std::cerr << "Loop" << std::endl;
213                    }
214                    _seq->setInterval(mode, begin, end);
215                }
216                break;
217            default:
218                break;
219            }
220        }
221
222        return false;
223    }
224
225private:
226    osg::ref_ptr<osg::Sequence> _seq;
227};
228
229
230int main( int argc, char **argv )
231{
232    // use an ArgumentParser object to manage the program arguments.
233    osg::ArgumentParser arguments(&argc,argv);
234   
235    // construct the viewer.
236    osgViewer::Viewer viewer;
237    // root
238    osg::Group* rootNode = new osg::Group;
239
240    // create info display
241    const char* text[] = {
242        "osg::Sequence Mini-Howto",
243        "- can be used for simple flip-book-style animation",
244        "- is subclassed from osg::Switch",
245        "- assigns a display duration to each child",
246        "- can loop or swing through an interval of it's children",
247        "- can repeat the interval a number of times or indefinitively",
248        "- press 's' to start/pause/resume",
249        "- press 'l' to toggle loop/swing mode",
250        NULL
251    };
252    rootNode->addChild(createHUD(createTextGroup(text)));
253
254    // add sequence of models from command line
255    osg::Sequence* seq = createSequence(arguments);
256    rootNode->addChild(seq);
257
258    // add model to viewer.
259    viewer.setSceneData(rootNode);
260
261    // add event handler to control sequence
262    viewer.addEventHandler(new SequenceEventHandler(seq));
263
264    return viewer.run();
265}
Note: See TracBrowser for help on using the browser.