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.
|
|
|
|
|
|
|
|
#ifndef OSGMANIPULATOR_DRAGGER
|
|
|
|
#define OSGMANIPULATOR_DRAGGER 1
|
|
|
|
|
|
|
|
#include <osgManipulator/Selection>
|
|
|
|
|
|
|
|
#include <osg/BoundingSphere>
|
|
|
|
#include <osgUtil/SceneView>
|
|
|
|
#include <osgUtil/IntersectVisitor>
|
|
|
|
#include <osgGA/GUIEventAdapter>
|
|
|
|
#include <osgGA/GUIActionAdapter>
|
|
|
|
|
|
|
|
namespace osgManipulator
|
|
|
|
{
|
|
|
|
|
|
|
|
class CommandManager;
|
|
|
|
class CompositeDragger;
|
|
|
|
|
2007-02-26 21:01:17 +08:00
|
|
|
class OSGMANIPULATOR_EXPORT PointerInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
PointerInfo();
|
|
|
|
|
|
|
|
PointerInfo(const PointerInfo& rhs):
|
2007-09-07 23:03:56 +08:00
|
|
|
_hitList(rhs._hitList),
|
2007-05-28 18:14:29 +08:00
|
|
|
_nearPoint(rhs._nearPoint),
|
|
|
|
_farPoint(rhs._farPoint),
|
2007-09-07 23:03:56 +08:00
|
|
|
_eyeDir(rhs._eyeDir)
|
2007-02-26 21:01:17 +08:00
|
|
|
{
|
|
|
|
_hitIter = _hitList.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_hitList.clear();
|
|
|
|
_hitIter = _hitList.begin();
|
|
|
|
setCamera(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-05 22:37:55 +08:00
|
|
|
bool completed() const { return _hitIter==_hitList.end(); }
|
2007-02-26 21:01:17 +08:00
|
|
|
|
|
|
|
void next()
|
|
|
|
{
|
|
|
|
if (!completed()) ++_hitIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<osg::NodePath, osg::Vec3> NodePathIntersectionPair;
|
|
|
|
typedef std::list< NodePathIntersectionPair> IntersectionList;
|
|
|
|
|
|
|
|
|
|
|
|
osg::Vec3 getLocalIntersectPoint() const { return _hitIter->second; }
|
|
|
|
|
|
|
|
|
2007-05-28 18:14:29 +08:00
|
|
|
|
|
|
|
void setNearFarPoints (osg::Vec3 nearPoint, osg::Vec3 farPoint) {
|
|
|
|
_nearPoint = nearPoint;
|
|
|
|
_farPoint=farPoint;
|
|
|
|
_eyeDir = farPoint - nearPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
const osg::Vec3& getEyeDir() const {return _eyeDir;}
|
|
|
|
|
|
|
|
void getNearFarPoints( osg::Vec3& nearPoint, osg::Vec3& farPoint) const {
|
|
|
|
nearPoint = _nearPoint;
|
|
|
|
farPoint = _farPoint;
|
2007-02-26 21:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const osg::Node* node) const;
|
|
|
|
|
|
|
|
void setCamera(osg::Camera* camera)
|
|
|
|
{
|
2007-05-28 18:14:29 +08:00
|
|
|
|
|
|
|
if (camera)
|
2007-02-26 21:01:17 +08:00
|
|
|
{
|
2007-05-28 18:14:29 +08:00
|
|
|
_MVPW = camera->getViewMatrix() * camera->getProjectionMatrix();
|
|
|
|
if (camera->getViewport()) _MVPW.postMult(camera->getViewport()->computeWindowMatrix());
|
2007-02-26 21:01:17 +08:00
|
|
|
_inverseMVPW.invert(_MVPW);
|
2007-05-28 18:14:29 +08:00
|
|
|
osg::Vec3 eye, center, up;
|
|
|
|
camera->getViewMatrix().getLookAt(eye, center, up);
|
|
|
|
_eyeDir = eye - center;
|
|
|
|
|
2007-02-26 21:01:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_MVPW.makeIdentity();
|
|
|
|
_inverseMVPW.makeIdentity();
|
2007-05-28 18:14:29 +08:00
|
|
|
_eyeDir = osg::Vec3(0,0,1);
|
2007-02-26 21:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-02-25 22:27:57 +08:00
|
|
|
void addIntersection(const osg::NodePath& nodePath, const osg::Vec3& intersectionPoint)
|
2007-02-26 21:01:17 +08:00
|
|
|
{
|
|
|
|
bool needToResetHitIter = _hitList.empty();
|
|
|
|
_hitList.push_back(NodePathIntersectionPair(nodePath, intersectionPoint));
|
|
|
|
if (needToResetHitIter) _hitIter = _hitList.begin();
|
|
|
|
}
|
|
|
|
|
2007-03-06 20:01:55 +08:00
|
|
|
void setMousePosition(float pixel_x, float pixel_y)
|
2007-02-26 21:01:17 +08:00
|
|
|
{
|
2007-05-28 18:14:29 +08:00
|
|
|
projectWindowXYIntoObject(osg::Vec2(pixel_x, pixel_y), _nearPoint, _farPoint);
|
2007-02-26 21:01:17 +08:00
|
|
|
}
|
2007-05-28 18:14:29 +08:00
|
|
|
protected:
|
|
|
|
bool projectWindowXYIntoObject(const osg::Vec2& windowCoord, osg::Vec3& nearPoint, osg::Vec3& farPoint) const;
|
2007-02-26 21:01:17 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
IntersectionList _hitList;
|
2007-06-05 22:37:55 +08:00
|
|
|
IntersectionList::const_iterator _hitIter;
|
2007-05-28 18:14:29 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
osg::Vec3 _nearPoint,_farPoint;
|
|
|
|
osg::Vec3 _eyeDir;
|
2007-02-26 21:01:17 +08:00
|
|
|
|
|
|
|
osg::Matrix _MVPW;
|
|
|
|
osg::Matrix _inverseMVPW;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
/**
|
|
|
|
* Base class for draggers. Concrete draggers implement the pick event handler
|
|
|
|
* and generate motion commands (translate, rotate, ...) and sends these
|
|
|
|
* command to the CommandManager. The CommandManager dispatches the commands
|
|
|
|
* to all the Selections that are connected to the Dragger that generates the
|
|
|
|
* commands.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT Dragger : public Selection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** Set/Get the CommandManager. Draggers use CommandManager to dispatch commands. */
|
|
|
|
virtual void setCommandManager(CommandManager* cm) { _commandManager = cm; }
|
|
|
|
CommandManager* getCommandManager() { return _commandManager; }
|
|
|
|
const CommandManager* getCommandManager() const { return _commandManager; }
|
|
|
|
|
|
|
|
/**
|
2007-05-28 18:14:29 +08:00
|
|
|
* Set/Get parent dragger. For simple draggers parent points to itself.
|
|
|
|
* For composite draggers parent points to the parent dragger that uses
|
|
|
|
* this dragger.
|
|
|
|
*/
|
2007-02-11 18:33:59 +08:00
|
|
|
virtual void setParentDragger(Dragger* parent) { _parentDragger = parent; }
|
|
|
|
Dragger* getParentDragger() { return _parentDragger; }
|
|
|
|
const Dragger* getParentDragger() const { return _parentDragger; }
|
2007-05-28 18:14:29 +08:00
|
|
|
|
2007-02-11 18:33:59 +08:00
|
|
|
/** Returns 0 if this Dragger is not a CompositeDragger. */
|
|
|
|
virtual const CompositeDragger* getComposite() const { return 0; }
|
|
|
|
|
|
|
|
/** Returns 0 if this Dragger is not a CompositeDragger. */
|
|
|
|
virtual CompositeDragger* getComposite() { return 0; }
|
2007-02-12 05:12:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
virtual bool handle(const PointerInfo&, const osgGA::GUIEventAdapter&, osgGA::GUIActionAdapter&) { return false; }
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
Dragger();
|
|
|
|
virtual ~Dragger();
|
|
|
|
|
|
|
|
CommandManager* _commandManager;
|
|
|
|
|
2007-05-28 18:14:29 +08:00
|
|
|
Dragger* _parentDragger;
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CompositeDragger allows to create complex draggers that are composed of a
|
|
|
|
* hierarchy of Draggers.
|
|
|
|
*/
|
|
|
|
class OSGMANIPULATOR_EXPORT CompositeDragger : public Dragger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::vector< osg::ref_ptr<Dragger> > DraggerList;
|
|
|
|
|
|
|
|
virtual const CompositeDragger* getComposite() const { return this; }
|
|
|
|
virtual CompositeDragger* getComposite() { return this; }
|
|
|
|
|
|
|
|
virtual void setCommandManager(CommandManager* cm);
|
|
|
|
|
|
|
|
virtual void setParentDragger(Dragger* parent);
|
|
|
|
|
2007-02-12 05:12:34 +08:00
|
|
|
virtual bool handle(const PointerInfo& pi, const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
// Composite-specific methods below
|
|
|
|
virtual bool addDragger(Dragger* dragger);
|
|
|
|
virtual bool removeDragger(Dragger* dragger);
|
|
|
|
unsigned int getNumDraggers() const { return _draggerList.size(); }
|
|
|
|
Dragger* getDragger(unsigned int i) { return _draggerList[i].get(); }
|
|
|
|
const Dragger* getDragger(unsigned int i) const { return _draggerList[i].get(); }
|
|
|
|
bool containsDragger(const Dragger* dragger) const;
|
|
|
|
DraggerList::iterator findDragger(const Dragger* dragger);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
CompositeDragger() {}
|
|
|
|
virtual ~CompositeDragger() {}
|
|
|
|
|
|
|
|
DraggerList _draggerList;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Culls the drawable all the time. Used by draggers to have invisible geometry
|
|
|
|
* around lines and points so that they can be picked. For example, a dragger
|
|
|
|
* could have a line with an invisible cylinder around it to enable picking on
|
|
|
|
* that line.
|
|
|
|
*/
|
2007-05-28 18:14:29 +08:00
|
|
|
void OSGMANIPULATOR_EXPORT setDrawableToAlwaysCull(osg::Drawable& drawable);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function for setting the material color on a node.
|
|
|
|
*/
|
2007-05-28 18:14:29 +08:00
|
|
|
void OSGMANIPULATOR_EXPORT setMaterialColor(const osg::Vec4& color, osg::Node& node);
|
2007-02-11 18:33:59 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|