| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 33 | osg::Geode* createText(const std::string& str, const osg::Vec3& pos) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 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 | |
|---|
| 42 | osg::Geode* geode = new osg::Geode; |
|---|
| 43 | geode->addDrawable(text); |
|---|
| 44 | |
|---|
| 45 | return geode; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | osg::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 | |
|---|
| 56 | const char** t = text; |
|---|
| 57 | group->addChild(createText(*t++, pos)); |
|---|
| 58 | pos += delta; |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 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 | |
|---|
| 70 | seq->setInterval(osg::Sequence::LOOP, 0,-1); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | seq->setDuration(1.0f, -1); |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | seq->setMode(osg::Sequence::START); |
|---|
| 77 | |
|---|
| 78 | return group; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | osg::Node* createHUD(osg::Node* node) |
|---|
| 82 | { |
|---|
| 83 | |
|---|
| 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 | |
|---|
| 90 | osg::Projection* projection = new osg::Projection; |
|---|
| 91 | projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); |
|---|
| 92 | projection->addChild(modelview_abs); |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 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 | |
|---|
| 102 | osg::Node* createScaledNode(osg::Node* node, float targetScale) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 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 | |
|---|
| 114 | osg::StateSet* state = transform->getOrCreateStateSet(); |
|---|
| 115 | state->setMode(GL_NORMALIZE, osg::StateAttribute::ON); |
|---|
| 116 | |
|---|
| 117 | return transform; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | osg::Sequence* createSequence(osg::ArgumentParser& arguments) |
|---|
| 121 | { |
|---|
| 122 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 158 | seq->setInterval(osg::Sequence::LOOP, 0,-1); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | seq->setDuration(1.0f, -1); |
|---|
| 162 | |
|---|
| 163 | seq->setMode(osg::Sequence::START); |
|---|
| 164 | |
|---|
| 165 | return seq; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | class SequenceEventHandler : public osgGA::GUIEventHandler |
|---|
| 170 | { |
|---|
| 171 | public: |
|---|
| 172 | SequenceEventHandler(osg::Sequence* seq) |
|---|
| 173 | { |
|---|
| 174 | _seq = seq; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 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 | |
|---|
| 225 | private: |
|---|
| 226 | osg::ref_ptr<osg::Sequence> _seq; |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | int main( int argc, char **argv ) |
|---|
| 231 | { |
|---|
| 232 | |
|---|
| 233 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | osgViewer::Viewer viewer; |
|---|
| 237 | |
|---|
| 238 | osg::Group* rootNode = new osg::Group; |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 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 | |
|---|
| 255 | osg::Sequence* seq = createSequence(arguments); |
|---|
| 256 | rootNode->addChild(seq); |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | viewer.setSceneData(rootNode); |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | viewer.addEventHandler(new SequenceEventHandler(seq)); |
|---|
| 263 | |
|---|
| 264 | return viewer.run(); |
|---|
| 265 | } |
|---|