| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgManipulator/RotateSphereDragger> |
|---|
| 16 | #include <osgManipulator/Command> |
|---|
| 17 | |
|---|
| 18 | #include <osg/ShapeDrawable> |
|---|
| 19 | #include <osg/Geometry> |
|---|
| 20 | #include <osg/Material> |
|---|
| 21 | |
|---|
| 22 | #include <iostream> |
|---|
| 23 | |
|---|
| 24 | using namespace osgManipulator; |
|---|
| 25 | |
|---|
| 26 | RotateSphereDragger::RotateSphereDragger() : _prevPtOnSphere(true) |
|---|
| 27 | { |
|---|
| 28 | _projector = new SpherePlaneProjector(); |
|---|
| 29 | setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 30 | setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f)); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | RotateSphereDragger::~RotateSphereDragger() |
|---|
| 34 | { |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | bool RotateSphereDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 38 | { |
|---|
| 39 | |
|---|
| 40 | if (!pointer.contains(this)) return false; |
|---|
| 41 | |
|---|
| 42 | switch (ea.getEventType()) |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | case (osgGA::GUIEventAdapter::PUSH): |
|---|
| 46 | { |
|---|
| 47 | |
|---|
| 48 | osg::NodePath nodePathToRoot; |
|---|
| 49 | computeNodePathToRoot(*this,nodePathToRoot); |
|---|
| 50 | osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot); |
|---|
| 51 | _projector->setLocalToWorld(localToWorld); |
|---|
| 52 | |
|---|
| 53 | _startLocalToWorld = _projector->getLocalToWorld(); |
|---|
| 54 | _startWorldToLocal = _projector->getWorldToLocal(); |
|---|
| 55 | |
|---|
| 56 | if (_projector->isPointInFront(pointer, _startLocalToWorld)) |
|---|
| 57 | _projector->setFront(true); |
|---|
| 58 | else |
|---|
| 59 | _projector->setFront(false); |
|---|
| 60 | |
|---|
| 61 | osg::Vec3d projectedPoint; |
|---|
| 62 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 63 | { |
|---|
| 64 | |
|---|
| 65 | osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand(); |
|---|
| 66 | cmd->setStage(MotionCommand::START); |
|---|
| 67 | cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | dispatch(*cmd); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | setMaterialColor(_pickColor,*this); |
|---|
| 74 | |
|---|
| 75 | _prevRotation = osg::Quat(); |
|---|
| 76 | _prevWorldProjPt = projectedPoint * _projector->getLocalToWorld(); |
|---|
| 77 | _prevPtOnSphere = _projector->isProjectionOnSphere(); |
|---|
| 78 | |
|---|
| 79 | aa.requestRedraw(); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return true; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | case (osgGA::GUIEventAdapter::DRAG): |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 89 | osg::Matrix localToWorld = osg::Matrix(_prevRotation) * _startLocalToWorld; |
|---|
| 90 | _projector->setLocalToWorld(localToWorld); |
|---|
| 91 | |
|---|
| 92 | osg::Vec3d projectedPoint; |
|---|
| 93 | if (_projector->project(pointer, projectedPoint)) |
|---|
| 94 | { |
|---|
| 95 | osg::Vec3d prevProjectedPoint = _prevWorldProjPt * _projector->getWorldToLocal(); |
|---|
| 96 | osg::Quat deltaRotation = _projector->getRotation(prevProjectedPoint, _prevPtOnSphere, |
|---|
| 97 | projectedPoint, _projector->isProjectionOnSphere(),1.0f); |
|---|
| 98 | osg::Quat rotation = deltaRotation * _prevRotation; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand(); |
|---|
| 102 | cmd->setStage(MotionCommand::MOVE); |
|---|
| 103 | cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal); |
|---|
| 104 | cmd->setRotation(rotation); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | dispatch(*cmd); |
|---|
| 108 | |
|---|
| 109 | _prevWorldProjPt = projectedPoint * _projector->getLocalToWorld(); |
|---|
| 110 | _prevRotation = rotation; |
|---|
| 111 | _prevPtOnSphere = _projector->isProjectionOnSphere(); |
|---|
| 112 | aa.requestRedraw(); |
|---|
| 113 | } |
|---|
| 114 | return true; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | case (osgGA::GUIEventAdapter::RELEASE): |
|---|
| 119 | { |
|---|
| 120 | osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand(); |
|---|
| 121 | |
|---|
| 122 | cmd->setStage(MotionCommand::FINISH); |
|---|
| 123 | cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | dispatch(*cmd); |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | setMaterialColor(_color,*this); |
|---|
| 130 | |
|---|
| 131 | aa.requestRedraw(); |
|---|
| 132 | |
|---|
| 133 | return true; |
|---|
| 134 | } |
|---|
| 135 | default: |
|---|
| 136 | return false; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void RotateSphereDragger::setupDefaultGeometry() |
|---|
| 141 | { |
|---|
| 142 | osg::Geode* geode = new osg::Geode; |
|---|
| 143 | geode->addDrawable(new osg::ShapeDrawable(const_cast<osg::Sphere*>(_projector->getSphere()))); |
|---|
| 144 | addChild(geode); |
|---|
| 145 | } |
|---|