2005-04-15 05:41:28 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSGUTIL_INTERSECTVISITOR
|
|
|
|
#define OSGUTIL_INTERSECTVISITOR 1
|
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/LineSegment>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Matrix>
|
2005-12-17 00:27:17 +08:00
|
|
|
#include <osg/Transform>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace osgUtil {
|
|
|
|
|
|
|
|
|
2002-01-30 20:09:18 +08:00
|
|
|
class OSGUTIL_EXPORT Hit
|
2004-09-27 02:39:34 +08:00
|
|
|
{
|
|
|
|
/** Describes a point in space produced by an intersection of a line with a scene.
|
|
|
|
* A Hit is always on a surface as rendered by the Open Scene Graph scene (usually
|
|
|
|
* a triangle or other primitive, but a special hit handler could return a
|
|
|
|
* different value perhaps: a sphere shape might return a Hit on the true sphere
|
|
|
|
* rather than the approximate tesselated sphere rendered.
|
|
|
|
*/
|
2001-01-11 00:32:10 +08:00
|
|
|
public:
|
2002-01-30 20:09:18 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
Hit();
|
|
|
|
Hit(const Hit& hit);
|
|
|
|
~Hit();
|
|
|
|
|
|
|
|
Hit& operator = (const Hit& hit);
|
|
|
|
|
|
|
|
typedef std::vector<int> VecIndexList;
|
|
|
|
|
|
|
|
bool operator < (const Hit& hit) const
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_originalLineSegment<hit._originalLineSegment) return true;
|
|
|
|
if (_originalLineSegment>hit._originalLineSegment) return false;
|
2001-01-11 00:32:10 +08:00
|
|
|
return _ratio<hit._ratio;
|
|
|
|
}
|
|
|
|
|
2002-01-30 20:09:18 +08:00
|
|
|
|
|
|
|
const osg::Vec3& getLocalIntersectPoint() const { return _intersectPoint; }
|
|
|
|
const osg::Vec3& getLocalIntersectNormal() const { return _intersectNormal; }
|
|
|
|
|
|
|
|
const osg::Vec3 getWorldIntersectPoint() const { if (_matrix.valid()) return _intersectPoint*(*_matrix); else return _intersectPoint; }
|
|
|
|
const osg::Vec3 getWorldIntersectNormal() const ;
|
|
|
|
|
2005-01-22 03:30:35 +08:00
|
|
|
float getRatio() const { return _ratio; }
|
|
|
|
const osg::LineSegment* getOriginalLineSegment() const { return _originalLineSegment.get(); }
|
|
|
|
const osg::LineSegment* getLocalLineSegment() const { return _localLineSegment.get(); }
|
2005-04-04 15:54:52 +08:00
|
|
|
osg::NodePath& getNodePath() { return _nodePath; }
|
2005-01-22 03:30:35 +08:00
|
|
|
const osg::NodePath& getNodePath() const { return _nodePath; }
|
2005-04-04 15:54:52 +08:00
|
|
|
osg::Geode* getGeode() { return _geode.get(); }
|
2005-01-22 03:30:35 +08:00
|
|
|
const osg::Geode* getGeode() const { return _geode.get(); }
|
2005-04-04 15:54:52 +08:00
|
|
|
osg::Drawable* getDrawable() { return _drawable.get(); }
|
2005-01-22 03:30:35 +08:00
|
|
|
const osg::Drawable* getDrawable() const { return _drawable.get(); }
|
|
|
|
const osg::RefMatrix* getMatrix() const { return _matrix.get(); }
|
|
|
|
const osg::RefMatrix* getInverseMatrix() const { return _inverse.get(); }
|
|
|
|
const VecIndexList& getVecIndexList() const { return _vecIndexList; }
|
|
|
|
int getPrimitiveIndex() const { return _primitiveIndex; }
|
|
|
|
|
|
|
|
|
2002-01-30 20:09:18 +08:00
|
|
|
float _ratio;
|
|
|
|
osg::ref_ptr<osg::LineSegment> _originalLineSegment;
|
|
|
|
osg::ref_ptr<osg::LineSegment> _localLineSegment;
|
|
|
|
osg::NodePath _nodePath;
|
|
|
|
osg::ref_ptr<osg::Geode> _geode;
|
2002-06-26 04:27:51 +08:00
|
|
|
osg::ref_ptr<osg::Drawable> _drawable;
|
2003-01-10 17:25:42 +08:00
|
|
|
osg::ref_ptr<osg::RefMatrix> _matrix;
|
|
|
|
osg::ref_ptr<osg::RefMatrix> _inverse;
|
2002-01-30 20:09:18 +08:00
|
|
|
|
|
|
|
VecIndexList _vecIndexList;
|
|
|
|
int _primitiveIndex;
|
|
|
|
osg::Vec3 _intersectPoint;
|
|
|
|
osg::Vec3 _intersectNormal;
|
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-05-28 18:24:43 +08:00
|
|
|
/** Basic visitor for ray based collisions of a scene.*/
|
2001-01-11 00:32:10 +08:00
|
|
|
class OSGUTIL_EXPORT IntersectVisitor : public osg::NodeVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
IntersectVisitor();
|
|
|
|
virtual ~IntersectVisitor();
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
|
2005-10-27 19:15:25 +08:00
|
|
|
/** Add a line segment to use for intersection testing during scene traversal.
|
|
|
|
* Note, a maximum of 32 line segments can be added to a IntersectVistor,
|
|
|
|
* adding more than this will result in warning being emitted to the console
|
|
|
|
* and the excess segments being ignored.*/
|
2001-09-20 05:08:56 +08:00
|
|
|
void addLineSegment(osg::LineSegment* seg);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
typedef std::vector<Hit> HitList;
|
2005-05-16 22:27:03 +08:00
|
|
|
typedef std::map<const osg::LineSegment*,HitList > LineSegmentHitListMap;
|
|
|
|
|
|
|
|
HitList& getHitList(const osg::LineSegment* seg) { return _segHitList[seg]; }
|
|
|
|
|
|
|
|
int getNumHits(const osg::LineSegment* seg) { return _segHitList[seg].size(); }
|
2005-12-07 05:59:43 +08:00
|
|
|
|
|
|
|
LineSegmentHitListMap& getSegHitList() { return _segHitList; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
bool hits();
|
|
|
|
|
2005-02-11 05:18:04 +08:00
|
|
|
enum LODSelectionMode
|
|
|
|
{
|
|
|
|
USE_HEIGHEST_LEVEL_OF_DETAIL,
|
|
|
|
USE_SEGMENT_START_POINT_AS_EYE_POINT_FOR_LOD_LEVEL_SELECTION
|
|
|
|
};
|
|
|
|
|
|
|
|
void setLODSelectionMode(LODSelectionMode mode) { _lodSelectionMode = mode; }
|
|
|
|
LODSelectionMode getLODSelectionMode() const { return _lodSelectionMode; }
|
|
|
|
|
|
|
|
/** Set the eye point in local coordinates.
|
|
|
|
* This is a pseudo-EyePoint for billboarding and LOD purposes.
|
|
|
|
* It is copied from the Start point of the most-recently-added segment
|
|
|
|
* of the intersection ray set (IntersectState::_segList). */
|
|
|
|
void setEyePoint(const osg::Vec3& eye) { _pseudoEyePoint = eye; }
|
|
|
|
|
2005-12-19 21:57:04 +08:00
|
|
|
virtual osg::Vec3 getEyePoint() const;
|
2005-02-11 05:18:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** Get the distance from a point to the eye point, distance value in local coordinate system.
|
|
|
|
* This is calculated using the pseudo-EyePoint (above) when doing LOD calculcations. */
|
|
|
|
virtual float getDistanceToEyePoint(const osg::Vec3& pos, bool withLODScale) const;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
virtual void apply(osg::Node&);
|
|
|
|
virtual void apply(osg::Geode& node);
|
|
|
|
virtual void apply(osg::Billboard& node);
|
|
|
|
|
|
|
|
virtual void apply(osg::Group& node);
|
2001-09-20 05:08:56 +08:00
|
|
|
virtual void apply(osg::Transform& node);
|
2001-01-11 00:32:10 +08:00
|
|
|
virtual void apply(osg::Switch& node);
|
|
|
|
virtual void apply(osg::LOD& node);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2002-01-30 20:09:18 +08:00
|
|
|
class IntersectState : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
IntersectState();
|
|
|
|
|
2005-12-22 22:06:33 +08:00
|
|
|
osg::ref_ptr<osg::RefMatrix> _view_matrix;
|
|
|
|
osg::ref_ptr<osg::RefMatrix> _view_inverse;
|
|
|
|
osg::ref_ptr<osg::RefMatrix> _model_matrix;
|
|
|
|
osg::ref_ptr<osg::RefMatrix> _model_inverse;
|
2002-01-30 20:09:18 +08:00
|
|
|
|
|
|
|
typedef std::pair<osg::ref_ptr<osg::LineSegment>,osg::ref_ptr<osg::LineSegment> > LineSegmentPair;
|
|
|
|
typedef std::vector< LineSegmentPair > LineSegmentList;
|
|
|
|
LineSegmentList _segList;
|
|
|
|
|
2002-11-08 19:00:16 +08:00
|
|
|
typedef unsigned int LineSegmentMask;
|
|
|
|
typedef std::vector<LineSegmentMask> LineSegmentMaskStack;
|
|
|
|
LineSegmentMaskStack _segmentMaskStack;
|
2002-01-30 20:09:18 +08:00
|
|
|
|
2002-11-08 19:00:16 +08:00
|
|
|
bool isCulled(const osg::BoundingSphere& bs,LineSegmentMask& segMaskOut);
|
|
|
|
bool isCulled(const osg::BoundingBox& bb,LineSegmentMask& segMaskOut);
|
2002-01-30 20:09:18 +08:00
|
|
|
|
2005-12-22 22:06:33 +08:00
|
|
|
void addLineSegment(osg::LineSegment* seg);
|
2002-01-30 20:09:18 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
~IntersectState();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2002-06-26 04:27:51 +08:00
|
|
|
bool intersect(osg::Drawable& gset);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2005-12-17 00:27:17 +08:00
|
|
|
void pushMatrix(osg::RefMatrix* matrix, osg::Transform::ReferenceFrame rf);
|
2001-01-11 00:32:10 +08:00
|
|
|
void popMatrix();
|
|
|
|
|
|
|
|
bool enterNode(osg::Node& node);
|
|
|
|
void leaveNode();
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
typedef std::vector<osg::ref_ptr<IntersectState> > IntersectStateStack;
|
2002-01-30 20:09:18 +08:00
|
|
|
|
|
|
|
IntersectStateStack _intersectStateStack;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
LineSegmentHitListMap _segHitList;
|
2005-02-11 05:18:04 +08:00
|
|
|
|
|
|
|
LODSelectionMode _lodSelectionMode;
|
|
|
|
osg::Vec3 _pseudoEyePoint;
|
2001-01-11 00:32:10 +08:00
|
|
|
};
|
|
|
|
|
2005-12-07 18:25:50 +08:00
|
|
|
/** Picking intersection visitor specialises the IntersectVistor to allow more convinient handling of mouse picking.*/
|
|
|
|
class OSGUTIL_EXPORT PickVisitor : public osgUtil::IntersectVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
PickVisitor(const osg::Viewport* viewport, const osg::Matrixd& proj, const osg::Matrixd& view, float mx, float my);
|
|
|
|
|
|
|
|
void runNestedPickVisitor(osg::Node& node, const osg::Viewport* viewport, const osg::Matrix& proj, const osg::Matrix& view, float mx, float my);
|
|
|
|
|
|
|
|
void apply(osg::Projection& projection);
|
|
|
|
|
|
|
|
void apply(osg::CameraNode& camera);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
float _mx;
|
|
|
|
float _my;
|
|
|
|
|
|
|
|
osg::ref_ptr<const osg::Viewport> _lastViewport;
|
|
|
|
osg::Matrixd _lastProjectionMatrix;
|
|
|
|
osg::Matrixd _lastViewMatrix;
|
|
|
|
};
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|