2007-02-11 18:33:59 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
//osgManipulator - Copyright (C) 2007 Fugro-Jason B.V.
|
|
|
|
|
|
|
|
#include <osgManipulator/RotateSphereDragger>
|
|
|
|
#include <osgManipulator/Command>
|
|
|
|
|
|
|
|
#include <osg/ShapeDrawable>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Material>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace osgManipulator;
|
|
|
|
|
|
|
|
RotateSphereDragger::RotateSphereDragger() : _prevPtOnSphere(true)
|
|
|
|
{
|
|
|
|
_projector = new SpherePlaneProjector();
|
|
|
|
setColor(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
|
|
|
|
setPickColor(osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
RotateSphereDragger::~RotateSphereDragger()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-02-12 05:12:34 +08:00
|
|
|
bool RotateSphereDragger::handle(const PointerInfo& pointer, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
|
2007-02-11 18:33:59 +08:00
|
|
|
{
|
|
|
|
// Check if the dragger node is in the nodepath.
|
2007-02-12 05:12:34 +08:00
|
|
|
if (!pointer.contains(this)) return false;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
switch (ea.getEventType())
|
|
|
|
{
|
|
|
|
// Pick start.
|
|
|
|
case (osgGA::GUIEventAdapter::PUSH):
|
|
|
|
{
|
|
|
|
// Get the LocalToWorld matrix for this node and set it for the projector.
|
|
|
|
osg::NodePath nodePathToRoot;
|
|
|
|
computeNodePathToRoot(*this,nodePathToRoot);
|
|
|
|
osg::Matrix localToWorld = osg::computeLocalToWorld(nodePathToRoot);
|
|
|
|
_projector->setLocalToWorld(localToWorld);
|
|
|
|
|
|
|
|
_startLocalToWorld = _projector->getLocalToWorld();
|
|
|
|
_startWorldToLocal = _projector->getWorldToLocal();
|
|
|
|
|
2007-02-12 05:12:34 +08:00
|
|
|
if (_projector->isPointInFront(pointer, _startLocalToWorld))
|
2007-02-11 18:33:59 +08:00
|
|
|
_projector->setFront(true);
|
|
|
|
else
|
|
|
|
_projector->setFront(false);
|
|
|
|
|
2008-11-21 19:23:21 +08:00
|
|
|
osg::Vec3d projectedPoint;
|
2007-02-12 05:12:34 +08:00
|
|
|
if (_projector->project(pointer, projectedPoint))
|
2007-02-11 18:33:59 +08:00
|
|
|
{
|
|
|
|
// Generate the motion command.
|
|
|
|
osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
|
|
|
|
cmd->setStage(MotionCommand::START);
|
|
|
|
cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
|
|
|
|
|
|
|
|
// Dispatch command.
|
2009-06-30 19:39:39 +08:00
|
|
|
dispatch(*cmd);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
// Set color to pick color.
|
|
|
|
setMaterialColor(_pickColor,*this);
|
|
|
|
|
|
|
|
_prevRotation = osg::Quat();
|
|
|
|
_prevWorldProjPt = projectedPoint * _projector->getLocalToWorld();
|
|
|
|
_prevPtOnSphere = _projector->isProjectionOnSphere();
|
|
|
|
|
|
|
|
aa.requestRedraw();
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
return true;
|
2007-02-11 18:33:59 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
// Pick move.
|
|
|
|
case (osgGA::GUIEventAdapter::DRAG):
|
|
|
|
{
|
|
|
|
// Get the LocalToWorld matrix for this node and set it for the projector.
|
|
|
|
osg::Matrix localToWorld = osg::Matrix(_prevRotation) * _startLocalToWorld;
|
|
|
|
_projector->setLocalToWorld(localToWorld);
|
|
|
|
|
2008-11-21 19:23:21 +08:00
|
|
|
osg::Vec3d projectedPoint;
|
2007-02-12 05:12:34 +08:00
|
|
|
if (_projector->project(pointer, projectedPoint))
|
2007-02-11 18:33:59 +08:00
|
|
|
{
|
2008-11-21 19:23:21 +08:00
|
|
|
osg::Vec3d prevProjectedPoint = _prevWorldProjPt * _projector->getWorldToLocal();
|
|
|
|
osg::Quat deltaRotation = _projector->getRotation(prevProjectedPoint, _prevPtOnSphere,
|
2007-02-11 18:33:59 +08:00
|
|
|
projectedPoint, _projector->isProjectionOnSphere(),1.0f);
|
|
|
|
osg::Quat rotation = deltaRotation * _prevRotation;
|
|
|
|
|
|
|
|
// Generate the motion command.
|
|
|
|
osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
|
|
|
|
cmd->setStage(MotionCommand::MOVE);
|
|
|
|
cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
|
|
|
|
cmd->setRotation(rotation);
|
|
|
|
|
|
|
|
// Dispatch command.
|
2009-06-30 19:39:39 +08:00
|
|
|
dispatch(*cmd);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
_prevWorldProjPt = projectedPoint * _projector->getLocalToWorld();
|
|
|
|
_prevRotation = rotation;
|
|
|
|
_prevPtOnSphere = _projector->isProjectionOnSphere();
|
|
|
|
aa.requestRedraw();
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
return true;
|
2007-02-11 18:33:59 +08:00
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
// Pick finish.
|
|
|
|
case (osgGA::GUIEventAdapter::RELEASE):
|
|
|
|
{
|
|
|
|
osg::ref_ptr<Rotate3DCommand> cmd = new Rotate3DCommand();
|
|
|
|
|
|
|
|
cmd->setStage(MotionCommand::FINISH);
|
|
|
|
cmd->setLocalToWorldAndWorldToLocal(_startLocalToWorld,_startWorldToLocal);
|
2009-06-30 19:39:39 +08:00
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
// Dispatch command.
|
2009-06-30 19:39:39 +08:00
|
|
|
dispatch(*cmd);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
// Reset color.
|
|
|
|
setMaterialColor(_color,*this);
|
2009-06-30 19:39:39 +08:00
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
aa.requestRedraw();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateSphereDragger::setupDefaultGeometry()
|
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
2007-02-11 20:22:58 +08:00
|
|
|
geode->addDrawable(new osg::ShapeDrawable(const_cast<osg::Sphere*>(_projector->getSphere())));
|
2007-02-11 18:33:59 +08:00
|
|
|
addChild(geode);
|
|
|
|
}
|