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