| 1 | |
|---|
| 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 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | class MyEventHandler : public osgGA::GUIEventHandler { |
|---|
| 20 | public: |
|---|
| 21 | |
|---|
| 22 | MyEventHandler(std::vector<osg::Sequence*>* seq) |
|---|
| 23 | { |
|---|
| 24 | _seq = seq; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 65 | virtual void accept(osgGA::GUIEventHandlerVisitor&) {} |
|---|
| 66 | |
|---|
| 67 | private: |
|---|
| 68 | std::vector<osg::Sequence*>* _seq; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | osg::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 | |
|---|
| 78 | for (unsigned int i = 0; i < model.size(); i++) { |
|---|
| 79 | seqNode->addChild(model[i]); |
|---|
| 80 | seqNode->setTime(i, 1.0f); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | seqNode->setInterval(mode, 0, -1); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | seqNode->setDuration(speed, nreps); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | seqNode->setMode(osg::Sequence::STOP); |
|---|
| 91 | |
|---|
| 92 | return seqNode; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | int main( int argc, char **argv ) |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 98 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ..."); |
|---|
| 102 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | osgProducer::Viewer viewer(arguments); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 115 | { |
|---|
| 116 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 117 | return 1; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | if (arguments.errors()) |
|---|
| 125 | { |
|---|
| 126 | arguments.writeErrorMessages(std::cout); |
|---|
| 127 | return 1; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 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 | |
|---|
| 144 | osg::Group* rootNode = new osg::Group; |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 178 | viewer.setSceneData(rootNode); |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | viewer.getEventHandlerList().push_front(new MyEventHandler(&seq)); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | viewer.realize(Producer::CameraGroup::ThreadPerCamera); |
|---|
| 185 | |
|---|
| 186 | while( !viewer.done() ) |
|---|
| 187 | { |
|---|
| 188 | |
|---|
| 189 | viewer.sync(); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | viewer.update(); |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | viewer.frame(); |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | return 0; |
|---|
| 201 | } |
|---|