Added support for gather information on which vertices have been intersected with.
This commit is contained in:
parent
84fb15717d
commit
89b1365b2b
@ -28,6 +28,7 @@
|
||||
|
||||
#include <osgText/Text>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
// class to handle events with a pick
|
||||
class PickHandler : public osgGA::GUIEventHandler {
|
||||
@ -80,21 +81,26 @@ void PickHandler::pick(const osgGA::GUIEventAdapter& ea)
|
||||
hitr!=hlist.end();
|
||||
++hitr)
|
||||
{
|
||||
//osg::Vec3 ip = hitr->getLocalIntersectPoint();
|
||||
//osg::Vec3 in = hitr->getLocalIntersectNormal();
|
||||
osg::Geode* geode = hitr->_geode.get();
|
||||
std::ostringstream os;
|
||||
if (hitr->_geode.valid() && !hitr->_geode->getName().empty())
|
||||
{
|
||||
// the geodes are identified by name.
|
||||
if (geode)
|
||||
{
|
||||
if (!geode->getName().empty())
|
||||
{
|
||||
gdlist=gdlist+" "+geode->getName();
|
||||
os<<"Object \""<<hitr->_geode->getName()<<"\""<<std::endl;
|
||||
}
|
||||
else
|
||||
else if (hitr->_drawable.valid())
|
||||
{
|
||||
gdlist=gdlist+" geode";
|
||||
os<<"Object \""<<hitr->_drawable->className()<<"\""<<std::endl;
|
||||
}
|
||||
|
||||
os<<" local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<" normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;
|
||||
os<<" world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<" normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;
|
||||
osgUtil::Hit::VecIndexList& vil = hitr->_vecIndexList;
|
||||
for(unsigned int i=0;i<vil.size();++i)
|
||||
{
|
||||
os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;
|
||||
}
|
||||
|
||||
gdlist += os.str();
|
||||
}
|
||||
}
|
||||
setLabel(gdlist);
|
||||
@ -177,12 +183,14 @@ osg::Node* createHUD(osgText::Text* updateText)
|
||||
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
||||
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
||||
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
|
||||
geode->setName("whatis");
|
||||
geode->setName("The text label");
|
||||
geode->addDrawable( updateText );
|
||||
modelview_abs->addChild(geode);
|
||||
|
||||
updateText->setCharacterSize(20.0f);
|
||||
updateText->setFont(timesFont);
|
||||
updateText->setText("whatis will tell you what is under the mouse");
|
||||
updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
||||
updateText->setText("");
|
||||
updateText->setPosition(position);
|
||||
|
||||
position += delta;
|
||||
|
@ -39,8 +39,8 @@ public:
|
||||
_lineSegment = new osg::LineSegment;
|
||||
_lineSegment->set(nr,fr); // make a line segment
|
||||
|
||||
std::cout<<"near "<<nr<<std::endl;
|
||||
std::cout<<"far "<<fr<<std::endl;
|
||||
//std::cout<<"near "<<nr<<std::endl;
|
||||
//std::cout<<"far "<<fr<<std::endl;
|
||||
|
||||
addLineSegment(_lineSegment.get());
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <osg/Billboard>
|
||||
#include <osg/Notify>
|
||||
#include <osg/TriangleFunctor>
|
||||
#include <osg/Geometry>
|
||||
|
||||
#include <float.h>
|
||||
#include <algorithm>
|
||||
@ -304,6 +305,29 @@ void IntersectVisitor::apply(Node& node)
|
||||
}
|
||||
|
||||
|
||||
struct TriangleHit
|
||||
{
|
||||
TriangleHit(unsigned int index, const osg::Vec3& normal, float r1, const osg::Vec3* v1, float r2, const osg::Vec3* v2, float r3, const osg::Vec3* v3):
|
||||
_index(index),
|
||||
_normal(normal),
|
||||
_r1(r1),
|
||||
_v1(v1),
|
||||
_r2(r2),
|
||||
_v2(v2),
|
||||
_r3(r3),
|
||||
_v3(v3) {}
|
||||
|
||||
unsigned int _index;
|
||||
const osg::Vec3 _normal;
|
||||
float _r1;
|
||||
const osg::Vec3* _v1;
|
||||
float _r2;
|
||||
const osg::Vec3* _v2;
|
||||
float _r3;
|
||||
const osg::Vec3* _v3;
|
||||
};
|
||||
|
||||
|
||||
struct TriangleIntersect
|
||||
{
|
||||
osg::ref_ptr<LineSegment> _seg;
|
||||
@ -316,7 +340,10 @@ struct TriangleIntersect
|
||||
float _ratio;
|
||||
bool _hit;
|
||||
|
||||
typedef std::multimap<float,std::pair<int,osg::Vec3> > TriangleHitList;
|
||||
|
||||
|
||||
typedef std::multimap<float,TriangleHit> TriangleHitList;
|
||||
|
||||
TriangleHitList _thl;
|
||||
|
||||
TriangleIntersect()
|
||||
@ -439,7 +466,7 @@ struct TriangleIntersect
|
||||
return;
|
||||
}
|
||||
|
||||
_thl.insert(std::pair<const float,std::pair<int,osg::Vec3> > (r,std::pair<int,osg::Vec3>(_index-1,normal)));
|
||||
_thl.insert(std::pair<const float,TriangleHit>(r,TriangleHit(_index-1,normal,r1,&v1,r2,&v2,r3,&v3)));
|
||||
_hit = true;
|
||||
|
||||
}
|
||||
@ -467,10 +494,14 @@ bool IntersectVisitor::intersect(Drawable& drawable)
|
||||
if (ti._hit)
|
||||
{
|
||||
|
||||
osg::Geometry* geometry = drawable.asGeometry();
|
||||
|
||||
|
||||
for(TriangleIntersect::TriangleHitList::iterator thitr=ti._thl.begin();
|
||||
thitr!=ti._thl.end();
|
||||
++thitr)
|
||||
{
|
||||
|
||||
Hit hit;
|
||||
hit._nodePath = _nodePath;
|
||||
hit._matrix = cis->_matrix;
|
||||
@ -479,15 +510,30 @@ bool IntersectVisitor::intersect(Drawable& drawable)
|
||||
if (_nodePath.empty()) hit._geode = NULL;
|
||||
else hit._geode = dynamic_cast<Geode*>(_nodePath.back());
|
||||
|
||||
TriangleHit& triHit = thitr->second;
|
||||
|
||||
hit._ratio = thitr->first;
|
||||
hit._primitiveIndex = thitr->second.first;
|
||||
hit._primitiveIndex = triHit._index;
|
||||
hit._originalLineSegment = sitr->first;
|
||||
hit._localLineSegment = sitr->second;
|
||||
|
||||
hit._intersectPoint = sitr->second->start()*(1.0f-hit._ratio)+
|
||||
sitr->second->end()*hit._ratio;
|
||||
|
||||
hit._intersectNormal = thitr->second.second;
|
||||
hit._intersectNormal = triHit._normal;
|
||||
|
||||
if (geometry)
|
||||
{
|
||||
osg::Vec3Array* vertices = geometry->getVertexArray();
|
||||
if (vertices)
|
||||
{
|
||||
osg::Vec3* first = &(vertices->front());
|
||||
if (triHit._v1) hit._vecIndexList.push_back(triHit._v1-first);
|
||||
if (triHit._v2) hit._vecIndexList.push_back(triHit._v2-first);
|
||||
if (triHit._v2) hit._vecIndexList.push_back(triHit._v3-first);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_segHitList[sitr->first.get()].push_back(hit);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user