| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <iostream> |
|---|
| 16 | #include <osg/Geometry> |
|---|
| 17 | #include <osg/Shape> |
|---|
| 18 | #include <osg/ShapeDrawable> |
|---|
| 19 | #include <osgViewer/Viewer> |
|---|
| 20 | #include <osgGA/TrackballManipulator> |
|---|
| 21 | #include <osg/MatrixTransform> |
|---|
| 22 | #include <osg/Material> |
|---|
| 23 | #include <osgAnimation/Sampler> |
|---|
| 24 | |
|---|
| 25 | class AnimtkUpdateCallback : public osg::NodeCallback |
|---|
| 26 | { |
|---|
| 27 | public: |
|---|
| 28 | META_Object(osgAnimation, AnimtkUpdateCallback); |
|---|
| 29 | |
|---|
| 30 | AnimtkUpdateCallback() |
|---|
| 31 | { |
|---|
| 32 | _sampler = new osgAnimation::Vec3CubicBezierSampler; |
|---|
| 33 | _playing = false; |
|---|
| 34 | _lastUpdate = 0; |
|---|
| 35 | } |
|---|
| 36 | AnimtkUpdateCallback(const AnimtkUpdateCallback& val, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY): |
|---|
| 37 | osg::Object(val, copyop), |
|---|
| 38 | osg::NodeCallback(val, copyop), |
|---|
| 39 | _sampler(val._sampler), |
|---|
| 40 | _startTime(val._startTime), |
|---|
| 41 | _currentTime(val._currentTime), |
|---|
| 42 | _playing(val._playing), |
|---|
| 43 | _lastUpdate(val._lastUpdate) |
|---|
| 44 | { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 49 | { |
|---|
| 50 | if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR && |
|---|
| 51 | nv->getFrameStamp() && |
|---|
| 52 | nv->getFrameStamp()->getFrameNumber() != _lastUpdate) { |
|---|
| 53 | |
|---|
| 54 | _lastUpdate = nv->getFrameStamp()->getFrameNumber(); |
|---|
| 55 | _currentTime = osg::Timer::instance()->tick(); |
|---|
| 56 | |
|---|
| 57 | if (_playing && _sampler.get() && _sampler->getKeyframeContainer()) { |
|---|
| 58 | osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(node); |
|---|
| 59 | if (transform) { |
|---|
| 60 | osg::Vec3 result; |
|---|
| 61 | float t = osg::Timer::instance()->delta_s(_startTime, _currentTime); |
|---|
| 62 | float duration = _sampler->getEndTime() - _sampler->getStartTime(); |
|---|
| 63 | t = fmod(t, duration); |
|---|
| 64 | t += _sampler->getStartTime(); |
|---|
| 65 | _sampler->getValueAt(t, result); |
|---|
| 66 | transform->setMatrix(osg::Matrix::translate(result)); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | traverse(node,nv); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void start() { _startTime = osg::Timer::instance()->tick(); _currentTime = _startTime; _playing = true;} |
|---|
| 77 | void stop() { _currentTime = _startTime; _playing = false;} |
|---|
| 78 | |
|---|
| 79 | osg::ref_ptr<osgAnimation::Vec3CubicBezierSampler> _sampler; |
|---|
| 80 | osg::Timer_t _startTime; |
|---|
| 81 | osg::Timer_t _currentTime; |
|---|
| 82 | bool _playing; |
|---|
| 83 | int _lastUpdate; |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | class AnimtkStateSetUpdateCallback : public osg::StateSet::Callback |
|---|
| 88 | { |
|---|
| 89 | public: |
|---|
| 90 | META_Object(osgAnimation, AnimtkStateSetUpdateCallback); |
|---|
| 91 | |
|---|
| 92 | AnimtkStateSetUpdateCallback() |
|---|
| 93 | { |
|---|
| 94 | _sampler = new osgAnimation::Vec4LinearSampler; |
|---|
| 95 | _playing = false; |
|---|
| 96 | _lastUpdate = 0; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | AnimtkStateSetUpdateCallback(const AnimtkStateSetUpdateCallback& val, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY): |
|---|
| 100 | osg::Object(val, copyop), |
|---|
| 101 | osg::StateSet::Callback(val, copyop), |
|---|
| 102 | _sampler(val._sampler), |
|---|
| 103 | _startTime(val._startTime), |
|---|
| 104 | _currentTime(val._currentTime), |
|---|
| 105 | _playing(val._playing), |
|---|
| 106 | _lastUpdate(val._lastUpdate) |
|---|
| 107 | { |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | virtual void operator()(osg::StateSet* state, osg::NodeVisitor* nv) |
|---|
| 112 | { |
|---|
| 113 | if (state && |
|---|
| 114 | nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR && |
|---|
| 115 | nv->getFrameStamp() && |
|---|
| 116 | nv->getFrameStamp()->getFrameNumber() != _lastUpdate) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | _lastUpdate = nv->getFrameStamp()->getFrameNumber(); |
|---|
| 120 | _currentTime = osg::Timer::instance()->tick(); |
|---|
| 121 | |
|---|
| 122 | if (_playing && _sampler.get() && _sampler->getKeyframeContainer()) |
|---|
| 123 | { |
|---|
| 124 | osg::Material* material = dynamic_cast<osg::Material*>(state->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 125 | if (material) |
|---|
| 126 | { |
|---|
| 127 | osg::Vec4 result; |
|---|
| 128 | float t = osg::Timer::instance()->delta_s(_startTime, _currentTime); |
|---|
| 129 | float duration = _sampler->getEndTime() - _sampler->getStartTime(); |
|---|
| 130 | t = fmod(t, duration); |
|---|
| 131 | t += _sampler->getStartTime(); |
|---|
| 132 | _sampler->getValueAt(t, result); |
|---|
| 133 | material->setDiffuse(osg::Material::FRONT_AND_BACK, result); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void start() { _startTime = osg::Timer::instance()->tick(); _currentTime = _startTime; _playing = true;} |
|---|
| 140 | void stop() { _currentTime = _startTime; _playing = false;} |
|---|
| 141 | |
|---|
| 142 | osg::ref_ptr<osgAnimation::Vec4LinearSampler> _sampler; |
|---|
| 143 | osg::Timer_t _startTime; |
|---|
| 144 | osg::Timer_t _currentTime; |
|---|
| 145 | bool _playing; |
|---|
| 146 | int _lastUpdate; |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | osg::Geode* createAxis() |
|---|
| 151 | { |
|---|
| 152 | osg::Geode* geode = new osg::Geode; |
|---|
| 153 | osg::ref_ptr<osg::Geometry> geometry (new osg::Geometry()); |
|---|
| 154 | |
|---|
| 155 | osg::ref_ptr<osg::Vec3Array> vertices (new osg::Vec3Array()); |
|---|
| 156 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 157 | vertices->push_back (osg::Vec3 ( 10.0, 0.0, 0.0)); |
|---|
| 158 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 159 | vertices->push_back (osg::Vec3 ( 0.0, 10.0, 0.0)); |
|---|
| 160 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 0.0)); |
|---|
| 161 | vertices->push_back (osg::Vec3 ( 0.0, 0.0, 10.0)); |
|---|
| 162 | geometry->setVertexArray (vertices.get()); |
|---|
| 163 | |
|---|
| 164 | osg::ref_ptr<osg::Vec4Array> colors (new osg::Vec4Array()); |
|---|
| 165 | colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f)); |
|---|
| 166 | colors->push_back (osg::Vec4 (1.0f, 0.0f, 0.0f, 1.0f)); |
|---|
| 167 | colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 168 | colors->push_back (osg::Vec4 (0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 169 | colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f)); |
|---|
| 170 | colors->push_back (osg::Vec4 (0.0f, 0.0f, 1.0f, 1.0f)); |
|---|
| 171 | geometry->setColorArray (colors.get()); |
|---|
| 172 | |
|---|
| 173 | geometry->setColorBinding (osg::Geometry::BIND_PER_VERTEX); |
|---|
| 174 | geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,6)); |
|---|
| 175 | |
|---|
| 176 | geode->addDrawable( geometry.get() ); |
|---|
| 177 | geode->getOrCreateStateSet()->setMode(GL_LIGHTING, false); |
|---|
| 178 | return geode; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | osg::StateSet* setupStateSet() |
|---|
| 182 | { |
|---|
| 183 | osg::StateSet* st = new osg::StateSet; |
|---|
| 184 | st->setAttributeAndModes(new osg::Material, true); |
|---|
| 185 | st->setMode(GL_BLEND, true); |
|---|
| 186 | AnimtkStateSetUpdateCallback* callback = new AnimtkStateSetUpdateCallback; |
|---|
| 187 | osgAnimation::Vec4KeyframeContainer* keys = callback->_sampler->getOrCreateKeyframeContainer(); |
|---|
| 188 | keys->push_back(osgAnimation::Vec4Keyframe(0, osg::Vec4(0,0,0,0))); |
|---|
| 189 | keys->push_back(osgAnimation::Vec4Keyframe(2, osg::Vec4(0.5,0,0,0.5))); |
|---|
| 190 | keys->push_back(osgAnimation::Vec4Keyframe(4, osg::Vec4(0,0.5,0,1))); |
|---|
| 191 | keys->push_back(osgAnimation::Vec4Keyframe(6, osg::Vec4(0,0,0.5,1))); |
|---|
| 192 | keys->push_back(osgAnimation::Vec4Keyframe(8, osg::Vec4(1,1,1,0.5))); |
|---|
| 193 | keys->push_back(osgAnimation::Vec4Keyframe(10, osg::Vec4(0,0,0,0))); |
|---|
| 194 | callback->start(); |
|---|
| 195 | st->setUpdateCallback(callback); |
|---|
| 196 | return st; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | osg::Node* setupCube() |
|---|
| 200 | { |
|---|
| 201 | osg::Geode* geode = new osg::Geode; |
|---|
| 202 | geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),2))); |
|---|
| 203 | geode->setStateSet(setupStateSet()); |
|---|
| 204 | return geode; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | osg::MatrixTransform* setupAnimtkNode() |
|---|
| 208 | { |
|---|
| 209 | osg::Vec3 v[4]; |
|---|
| 210 | v[0] = osg::Vec3(0,0,0); |
|---|
| 211 | v[1] = osg::Vec3(10,-50,0); |
|---|
| 212 | v[2] = osg::Vec3(30,-10,20); |
|---|
| 213 | v[3] = osg::Vec3(-10,20,-20); |
|---|
| 214 | v[4] = osg::Vec3(0,0,0); |
|---|
| 215 | osg::MatrixTransform* node = new osg::MatrixTransform; |
|---|
| 216 | AnimtkUpdateCallback* callback = new AnimtkUpdateCallback; |
|---|
| 217 | osgAnimation::Vec3CubicBezierKeyframeContainer* keys = callback->_sampler->getOrCreateKeyframeContainer(); |
|---|
| 218 | keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(0, osgAnimation::Vec3CubicBezier( |
|---|
| 219 | v[0], |
|---|
| 220 | v[0] + (v[0] - v[3]), |
|---|
| 221 | v[1] - (v[1] - v[0]) |
|---|
| 222 | ))); |
|---|
| 223 | keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(2, osgAnimation::Vec3CubicBezier( |
|---|
| 224 | v[1], |
|---|
| 225 | v[1] + (v[1] - v[0]), |
|---|
| 226 | v[2] - (v[2] - v[1]) |
|---|
| 227 | ))); |
|---|
| 228 | keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(4, osgAnimation::Vec3CubicBezier( |
|---|
| 229 | v[2], |
|---|
| 230 | v[2] + (v[2] - v[1]), |
|---|
| 231 | v[3] - (v[3] - v[2]) |
|---|
| 232 | ))); |
|---|
| 233 | keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(6, osgAnimation::Vec3CubicBezier( |
|---|
| 234 | v[3], |
|---|
| 235 | v[3] + (v[3] - v[2]), |
|---|
| 236 | v[4] - (v[4] - v[3]) |
|---|
| 237 | ))); |
|---|
| 238 | keys->push_back(osgAnimation::Vec3CubicBezierKeyframe(8, osgAnimation::Vec3CubicBezier( |
|---|
| 239 | v[4], |
|---|
| 240 | v[4] + (v[4] - v[3]), |
|---|
| 241 | v[0] - (v[0] - v[4]) |
|---|
| 242 | ))); |
|---|
| 243 | |
|---|
| 244 | callback->start(); |
|---|
| 245 | node->setUpdateCallback(callback); |
|---|
| 246 | node->addChild(setupCube()); |
|---|
| 247 | return node; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | int main (int argc, char* argv[]) |
|---|
| 251 | { |
|---|
| 252 | osgViewer::Viewer viewer; |
|---|
| 253 | osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator(); |
|---|
| 254 | viewer.setCameraManipulator(manipulator); |
|---|
| 255 | |
|---|
| 256 | osg::Group* root = new osg::Group; |
|---|
| 257 | root->setInitialBound(osg::BoundingSphere(osg::Vec3(10,0,10), 30)); |
|---|
| 258 | root->addChild(createAxis()); |
|---|
| 259 | |
|---|
| 260 | osg::MatrixTransform* node = setupAnimtkNode(); |
|---|
| 261 | node->addChild(createAxis()); |
|---|
| 262 | root->addChild(node); |
|---|
| 263 | |
|---|
| 264 | viewer.setSceneData( root ); |
|---|
| 265 | viewer.realize(); |
|---|
| 266 | |
|---|
| 267 | while (!viewer.done()) |
|---|
| 268 | { |
|---|
| 269 | viewer.frame(); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | } |
|---|