| [11115] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osg/Geode> |
|---|
| 16 | #include <osg/MatrixTransform> |
|---|
| 17 | #include <osg/ShapeDrawable> |
|---|
| 18 | #include <osgGA/TrackballManipulator> |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 21 | #include <osgAnimation/EaseMotion> |
|---|
| 22 | #include <osgWidget/WindowManager> |
|---|
| 23 | #include <osgWidget/Box> |
|---|
| 24 | #include <osgWidget/Table> |
|---|
| 25 | #include <osgWidget/Label> |
|---|
| 26 | |
|---|
| 27 | class EaseMotionSampler; |
|---|
| 28 | |
|---|
| 29 | const unsigned int WINDOW_WIDTH = 800; |
|---|
| 30 | const unsigned int WINDOW_HEIGHT = 600; |
|---|
| 31 | const unsigned int MASK_2D = 0xF0000000; |
|---|
| 32 | const unsigned int MASK_3D = 0x0F000000; |
|---|
| 33 | const float M_START = 0.0f; |
|---|
| 34 | const float M_DURATION = 2.0f; |
|---|
| 35 | const float M_CHANGE = 1.0f; |
|---|
| 36 | |
|---|
| 37 | EaseMotionSampler* EASE_MOTION_SAMPLER = 0; |
|---|
| 38 | osg::Geode* EASE_MOTION_GEODE = 0; |
|---|
| 39 | |
|---|
| 40 | osg::Geometry* createEaseMotionGeometry(osgAnimation::Motion* motion) { |
|---|
| 41 | osg::Geometry* geom = new osg::Geometry(); |
|---|
| 42 | osg::Vec4Array* cols = new osg::Vec4Array(); |
|---|
| 43 | osg::Vec3Array* v = new osg::Vec3Array(); |
|---|
| 44 | |
|---|
| 45 | for(float i = 0.0f; i < M_DURATION; i += M_DURATION / 256.0f) v->push_back( |
|---|
| 46 | osg::Vec3(i * 30.0f, motion->getValueAt(i) * 30.0f, 0.0f) |
|---|
| 47 | ); |
|---|
| 48 | |
|---|
| 49 | cols->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); |
|---|
| 50 | |
|---|
| 51 | geom->setUseDisplayList(false); |
|---|
| 52 | geom->setVertexArray(v); |
|---|
| 53 | geom->setColorArray(cols); |
|---|
| 54 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 55 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, v->size())); |
|---|
| 56 | |
|---|
| 57 | return geom; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| [11288] | 60 | class EaseMotionSampler: public osg::NodeCallback |
|---|
| 61 | { |
|---|
| 62 | public: |
|---|
| [11115] | 63 | float _previous; |
|---|
| 64 | osg::Vec3 _pos; |
|---|
| 65 | |
|---|
| 66 | osg::ref_ptr<osgAnimation::Motion> _motion; |
|---|
| 67 | |
|---|
| 68 | EaseMotionSampler(const osg::Vec3& pos): |
|---|
| 69 | _previous (0.0f), |
|---|
| 70 | _pos (pos) { |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void operator()(osg::Node* node, osg::NodeVisitor* nv) { |
|---|
| 74 | if(!_motion.valid()) return; |
|---|
| 75 | |
|---|
| 76 | osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(node); |
|---|
| 77 | |
|---|
| 78 | if(!mt) return; |
|---|
| 79 | |
|---|
| 80 | double t = nv->getFrameStamp()->getSimulationTime(); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | if(_previous == 0.0f) _previous = t; |
|---|
| 85 | |
|---|
| 86 | _motion->update(t - _previous); |
|---|
| 87 | |
|---|
| 88 | _previous = t; |
|---|
| 89 | |
|---|
| 90 | mt->setMatrix(osg::Matrix::translate(_pos * _motion->getValue())); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | template<typename T> |
|---|
| 94 | void setMotion() { |
|---|
| 95 | _motion = new T(M_START, M_DURATION, M_CHANGE, osgAnimation::Motion::LOOP); |
|---|
| 96 | |
|---|
| 97 | EASE_MOTION_GEODE->removeDrawables(0, EASE_MOTION_GEODE->getNumDrawables()); |
|---|
| 98 | EASE_MOTION_GEODE->addDrawable(createEaseMotionGeometry(_motion.get())); |
|---|
| 99 | } |
|---|
| 100 | }; |
|---|
| 101 | |
|---|
| 102 | struct ColorLabel: public osgWidget::Label { |
|---|
| 103 | ColorLabel(const char* label): |
|---|
| 104 | osgWidget::Label(label, "") { |
|---|
| 105 | setFont("fonts/VeraMono.ttf"); |
|---|
| 106 | setFontSize(14); |
|---|
| 107 | setFontColor(1.0f, 1.0f, 1.0f, 1.0f); |
|---|
| 108 | |
|---|
| 109 | setColor(0.3f, 0.3f, 0.3f, 1.0f); |
|---|
| 110 | setPadding(2.0f); |
|---|
| 111 | setCanFill(true); |
|---|
| 112 | |
|---|
| 113 | addSize(150.0f, 25.0f); |
|---|
| 114 | |
|---|
| 115 | setLabel(label); |
|---|
| 116 | setEventMask(osgWidget::EVENT_MOUSE_PUSH | osgWidget::EVENT_MASK_MOUSE_MOVE); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | bool mousePush(double, double, const osgWidget::WindowManager*) { |
|---|
| 120 | osgWidget::Table* p = dynamic_cast<osgWidget::Table*>(_parent); |
|---|
| 121 | |
|---|
| 122 | if(!p) return false; |
|---|
| 123 | |
|---|
| 124 | p->hide(); |
|---|
| 125 | |
|---|
| 126 | const std::string& name = getName(); |
|---|
| 127 | |
|---|
| 128 | if(!name.compare("OutQuadMotion")) |
|---|
| 129 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutQuadMotion>() |
|---|
| 130 | ; |
|---|
| 131 | |
|---|
| 132 | else if(!name.compare("InQuadMotion")) |
|---|
| 133 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InQuadMotion>() |
|---|
| 134 | ; |
|---|
| 135 | |
|---|
| 136 | else if(!name.compare("InOutQuadMotion")) |
|---|
| 137 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutQuadMotion>() |
|---|
| 138 | ; |
|---|
| 139 | |
|---|
| 140 | else if(!name.compare("OutCubicMotion")) |
|---|
| 141 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutCubicMotion>() |
|---|
| 142 | ; |
|---|
| 143 | |
|---|
| 144 | else if(!name.compare("InCubicMotion")) |
|---|
| 145 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InCubicMotion>() |
|---|
| 146 | ; |
|---|
| 147 | |
|---|
| 148 | else if(!name.compare("InOutCubicMotion")) |
|---|
| 149 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutCubicMotion>() |
|---|
| 150 | ; |
|---|
| 151 | |
|---|
| 152 | else if(!name.compare("OutQuartMotion")) |
|---|
| 153 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutQuartMotion>() |
|---|
| 154 | ; |
|---|
| 155 | |
|---|
| 156 | else if(!name.compare("InQuartMotion")) |
|---|
| 157 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InQuartMotion>() |
|---|
| 158 | ; |
|---|
| 159 | |
|---|
| 160 | else if(!name.compare("InOutQuartMotion")) |
|---|
| 161 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutQuartMotion>() |
|---|
| 162 | ; |
|---|
| 163 | |
|---|
| 164 | else if(!name.compare("OutBounceMotion")) |
|---|
| 165 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutBounceMotion>() |
|---|
| 166 | ; |
|---|
| 167 | |
|---|
| 168 | else if(!name.compare("InBounceMotion")) |
|---|
| 169 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InBounceMotion>() |
|---|
| 170 | ; |
|---|
| 171 | |
|---|
| 172 | else if(!name.compare("InOutBounceMotion")) |
|---|
| 173 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutBounceMotion>() |
|---|
| 174 | ; |
|---|
| 175 | |
|---|
| 176 | else if(!name.compare("OutElasticMotion")) |
|---|
| 177 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutElasticMotion>() |
|---|
| 178 | ; |
|---|
| 179 | |
|---|
| 180 | else if(!name.compare("InElasticMotion")) |
|---|
| 181 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InElasticMotion>() |
|---|
| 182 | ; |
|---|
| 183 | |
|---|
| 184 | else if(!name.compare("InOutElasticMotion")) |
|---|
| 185 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutElasticMotion>() |
|---|
| 186 | ; |
|---|
| 187 | |
|---|
| 188 | else if(!name.compare("OutSineMotion")) |
|---|
| 189 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutSineMotion>() |
|---|
| 190 | ; |
|---|
| 191 | |
|---|
| 192 | else if(!name.compare("InSineMotion")) |
|---|
| 193 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InSineMotion>() |
|---|
| 194 | ; |
|---|
| 195 | |
|---|
| 196 | else if(!name.compare("InOutSineMotion")) |
|---|
| 197 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutSineMotion>() |
|---|
| 198 | ; |
|---|
| 199 | |
|---|
| 200 | else if(!name.compare("OutBackMotion")) |
|---|
| 201 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutBackMotion>() |
|---|
| 202 | ; |
|---|
| 203 | |
|---|
| 204 | else if(!name.compare("InBackMotion")) |
|---|
| 205 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InBackMotion>() |
|---|
| 206 | ; |
|---|
| 207 | |
|---|
| 208 | else if(!name.compare("InOutBackMotion")) |
|---|
| 209 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutBackMotion>() |
|---|
| 210 | ; |
|---|
| 211 | |
|---|
| 212 | else if(!name.compare("OutCircMotion")) |
|---|
| 213 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutCircMotion>() |
|---|
| 214 | ; |
|---|
| 215 | |
|---|
| 216 | else if(!name.compare("InCircMotion")) |
|---|
| 217 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InCircMotion>() |
|---|
| 218 | ; |
|---|
| 219 | |
|---|
| 220 | else if(!name.compare("InOutCircMotion")) |
|---|
| 221 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutCircMotion>() |
|---|
| 222 | ; |
|---|
| 223 | |
|---|
| 224 | else if(!name.compare("OutExpoMotion")) |
|---|
| 225 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::OutExpoMotion>() |
|---|
| 226 | ; |
|---|
| 227 | |
|---|
| 228 | else if(!name.compare("InExpoMotion")) |
|---|
| 229 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InExpoMotion>() |
|---|
| 230 | ; |
|---|
| 231 | |
|---|
| 232 | else if(!name.compare("InOutExpoMotion")) |
|---|
| 233 | EASE_MOTION_SAMPLER->setMotion<osgAnimation::InOutExpoMotion>() |
|---|
| 234 | ; |
|---|
| 235 | |
|---|
| 236 | else EASE_MOTION_SAMPLER->setMotion<osgAnimation::LinearMotion>(); |
|---|
| 237 | |
|---|
| 238 | return true; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | bool mouseEnter(double, double, const osgWidget::WindowManager*) { |
|---|
| 242 | setColor(0.9f, 0.6f, 0.1f, 1.0f); |
|---|
| 243 | |
|---|
| 244 | return true; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | bool mouseLeave(double, double, const osgWidget::WindowManager*) { |
|---|
| 248 | setColor(0.3f, 0.3f, 0.3f, 1.0f); |
|---|
| 249 | |
|---|
| 250 | return true; |
|---|
| 251 | } |
|---|
| 252 | }; |
|---|
| 253 | |
|---|
| 254 | class ColorLabelMenu: public ColorLabel { |
|---|
| 255 | osg::ref_ptr<osgWidget::Table> _window; |
|---|
| 256 | |
|---|
| 257 | public: |
|---|
| 258 | ColorLabelMenu(const char* label): |
|---|
| 259 | ColorLabel(label) { |
|---|
| 260 | _window = new osgWidget::Table(std::string("Menu_") + label, 6, 5); |
|---|
| 261 | |
|---|
| 262 | _window->addWidget(new ColorLabel("OutQuadMotion"), 0, 0); |
|---|
| 263 | _window->addWidget(new ColorLabel("InQuadMotion"), 1, 0); |
|---|
| 264 | _window->addWidget(new ColorLabel("InOutQuadMotion"), 2, 0); |
|---|
| 265 | _window->addWidget(new ColorLabel("OutCubicMotion"), 3, 0); |
|---|
| 266 | _window->addWidget(new ColorLabel("InCubicMotion"), 4, 0); |
|---|
| 267 | _window->addWidget(new ColorLabel("InOutCubicMotion"), 5, 0); |
|---|
| 268 | |
|---|
| 269 | _window->addWidget(new ColorLabel("OutQuartMotion"), 0, 1); |
|---|
| 270 | _window->addWidget(new ColorLabel("InQuartMotion"), 1, 1); |
|---|
| 271 | _window->addWidget(new ColorLabel("InOutQuartMotion"), 2, 1); |
|---|
| 272 | _window->addWidget(new ColorLabel("OutBounceMotion"), 3, 1); |
|---|
| 273 | _window->addWidget(new ColorLabel("InBounceMotion"), 4, 1); |
|---|
| 274 | _window->addWidget(new ColorLabel("InOutBounceMotion"), 5, 1); |
|---|
| 275 | |
|---|
| 276 | _window->addWidget(new ColorLabel("OutElasticMotion"), 0, 2); |
|---|
| 277 | _window->addWidget(new ColorLabel("InElasticMotion"), 1, 2); |
|---|
| 278 | _window->addWidget(new ColorLabel("InOutElasticMotion"), 2, 2); |
|---|
| 279 | _window->addWidget(new ColorLabel("OutSineMotion"), 3, 2); |
|---|
| 280 | _window->addWidget(new ColorLabel("InSineMotion"), 4, 2); |
|---|
| 281 | _window->addWidget(new ColorLabel("InOutSineMotion"), 5, 2); |
|---|
| 282 | |
|---|
| 283 | _window->addWidget(new ColorLabel("OutBackMotion"), 0, 3); |
|---|
| 284 | _window->addWidget(new ColorLabel("InBackMotion"), 1, 3); |
|---|
| 285 | _window->addWidget(new ColorLabel("InOutBackMotion"), 2, 3); |
|---|
| 286 | _window->addWidget(new ColorLabel("OutCircMotion"), 3, 3); |
|---|
| 287 | _window->addWidget(new ColorLabel("InCircMotion"), 4, 3); |
|---|
| 288 | _window->addWidget(new ColorLabel("InOutCircMotion"), 5, 3); |
|---|
| 289 | |
|---|
| 290 | _window->addWidget(new ColorLabel("OutExpoMotion"), 0, 4); |
|---|
| 291 | _window->addWidget(new ColorLabel("InExpoMotion"), 1, 4); |
|---|
| 292 | _window->addWidget(new ColorLabel("InOutExpoMotion"), 2, 4); |
|---|
| 293 | _window->addWidget(new ColorLabel("Linear"), 3, 4); |
|---|
| 294 | |
|---|
| 295 | _window->resize(); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | void managed(osgWidget::WindowManager* wm) { |
|---|
| 299 | osgWidget::Label::managed(wm); |
|---|
| 300 | |
|---|
| 301 | wm->addChild(_window.get()); |
|---|
| 302 | |
|---|
| 303 | _window->hide(); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | void positioned() { |
|---|
| 307 | osgWidget::Label::positioned(); |
|---|
| 308 | |
|---|
| 309 | _window->setOrigin(_parent->getX(), _parent->getY() + _parent->getHeight()); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | bool mousePush(double, double, const osgWidget::WindowManager*) { |
|---|
| 313 | if(!_window->isVisible()) _window->show(); |
|---|
| 314 | |
|---|
| 315 | else _window->hide(); |
|---|
| 316 | |
|---|
| 317 | return true; |
|---|
| 318 | } |
|---|
| 319 | }; |
|---|
| 320 | |
|---|
| 321 | int main(int argc, char** argv) { |
|---|
| 322 | osgViewer::Viewer viewer; |
|---|
| 323 | |
|---|
| 324 | osgWidget::WindowManager* wm = new osgWidget::WindowManager( |
|---|
| 325 | &viewer, |
|---|
| 326 | WINDOW_WIDTH, |
|---|
| 327 | WINDOW_HEIGHT, |
|---|
| 328 | MASK_2D |
|---|
| 329 | ); |
|---|
| 330 | |
|---|
| 331 | osgWidget::Window* menu = new osgWidget::Box("menu", osgWidget::Box::HORIZONTAL); |
|---|
| 332 | |
|---|
| 333 | menu->addWidget(new ColorLabelMenu("Choose EaseMotion")); |
|---|
| 334 | menu->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f); |
|---|
| 335 | menu->setPosition(15.0f, 15.0f, 0.0f); |
|---|
| 336 | |
|---|
| 337 | wm->addChild(menu); |
|---|
| 338 | |
|---|
| 339 | osg::Group* group = new osg::Group(); |
|---|
| 340 | osg::Geode* geode = new osg::Geode(); |
|---|
| 341 | osg::MatrixTransform* mt = new osg::MatrixTransform(); |
|---|
| 342 | |
|---|
| 343 | geode->addDrawable(new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(), 4.0f))); |
|---|
| 344 | |
|---|
| 345 | EASE_MOTION_SAMPLER = new EaseMotionSampler(osg::Vec3(50.0f, 0.0f, 0.0f)); |
|---|
| 346 | EASE_MOTION_GEODE = new osg::Geode(); |
|---|
| 347 | |
|---|
| 348 | mt->addChild(geode); |
|---|
| 349 | mt->setUpdateCallback(EASE_MOTION_SAMPLER); |
|---|
| 350 | mt->setNodeMask(MASK_3D); |
|---|
| 351 | |
|---|
| 352 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 353 | viewer.getCameraManipulator()->setHomePosition( |
|---|
| 354 | osg::Vec3d(0.0f, 0.0f, 200.0f), |
|---|
| 355 | osg::Vec3d(20.0f, 0.0f, 0.0f), |
|---|
| 356 | osg::Vec3d(0.0f, 1.0f, 0.0f) |
|---|
| 357 | ); |
|---|
| 358 | viewer.home(); |
|---|
| 359 | |
|---|
| 360 | group->addChild(mt); |
|---|
| 361 | group->addChild(EASE_MOTION_GEODE); |
|---|
| 362 | |
|---|
| 363 | return osgWidget::createExample(viewer, wm, group); |
|---|
| 364 | } |
|---|