| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 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 | |
|---|
| 26 | osg::Geode* createText(const std::string& str, const osg::Vec3& pos) |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 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 | |
|---|
| 35 | osg::Geode* geode = new osg::Geode; |
|---|
| 36 | geode->addDrawable(text); |
|---|
| 37 | |
|---|
| 38 | return geode; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | osg::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 | |
|---|
| 49 | const char** t = text; |
|---|
| 50 | group->addChild(createText(*t++, pos)); |
|---|
| 51 | pos += delta; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 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 | |
|---|
| 63 | seq->setInterval(osg::Sequence::LOOP, 0,-1); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | seq->setDuration(1.0f, -1); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | seq->setMode(osg::Sequence::START); |
|---|
| 70 | |
|---|
| 71 | return group; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | osg::Node* createHUD(osg::Node* node) |
|---|
| 75 | { |
|---|
| 76 | |
|---|
| 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 | |
|---|
| 83 | osg::Projection* projection = new osg::Projection; |
|---|
| 84 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 85 | projection->addChild(modelview_abs); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 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 | |
|---|
| 95 | osg::Node* createScaledNode(osg::Node* node, float targetScale) |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 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 | |
|---|
| 107 | osg::StateSet* state = transform->getOrCreateStateSet(); |
|---|
| 108 | state->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 109 | |
|---|
| 110 | return transform; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | osg::Sequence* createSequence(osg::ArgumentParser& arguments) |
|---|
| 114 | { |
|---|
| 115 | |
|---|
| 116 | osg::Sequence* seq = new osg::Sequence; |
|---|
| 117 | for (int i = 1; i < arguments.argc(); ++i) |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 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 | |
|---|
| 129 | seq->setInterval(osg::Sequence::LOOP, 0,-1); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | seq->setDuration(1.0f, -1); |
|---|
| 133 | |
|---|
| 134 | return seq; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | class SequenceEventHandler : public osgGA::GUIEventHandler |
|---|
| 139 | { |
|---|
| 140 | public: |
|---|
| 141 | SequenceEventHandler(osg::Sequence* seq) |
|---|
| 142 | { |
|---|
| 143 | _seq = seq; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 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 | |
|---|
| 195 | virtual void accept(osgGA::GUIEventHandlerVisitor&) {} |
|---|
| 196 | |
|---|
| 197 | private: |
|---|
| 198 | osg::ref_ptr<osg::Sequence> _seq; |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | int main( int argc, char **argv ) |
|---|
| 203 | { |
|---|
| 204 | |
|---|
| 205 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 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 | |
|---|
| 213 | osgProducer::Viewer viewer(arguments); |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 223 | { |
|---|
| 224 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 225 | return 1; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 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 | |
|---|
| 245 | osg::Group* rootNode = new osg::Group; |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 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 | |
|---|
| 262 | osg::Sequence* seq = createSequence(arguments); |
|---|
| 263 | rootNode->addChild(seq); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | viewer.setSceneData(rootNode); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | viewer.getEventHandlerList().push_front(new SequenceEventHandler(seq)); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | viewer.realize(); |
|---|
| 273 | |
|---|
| 274 | while( !viewer.done() ) |
|---|
| 275 | { |
|---|
| 276 | |
|---|
| 277 | viewer.sync(); |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | viewer.update(); |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | viewer.frame(); |
|---|
| 285 | |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | viewer.sync(); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | viewer.cleanup_frame(); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | viewer.sync(); |
|---|
| 296 | |
|---|
| 297 | return 0; |
|---|
| 298 | } |
|---|