随着osg代码发生变化,Delta3d升级后,官方网站上的选取代码也不能应用,现将代码修改如下:
bool Picker:: MouseButtonPressed (const dtCore::Mouse *mouse, dtCore::Mouse::MouseButton button)
{
if (button == dtCore::Mouse::MouseButton::RightButton)
{
//is there an object picked by the mouse ?
dtCore::DeltaDrawable* picked_object = NULL;
osg::Vec3 nearPoint,farPoint;
float x=0, y=0;
mouse->GetPosition(x, y);
osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(GetCamera()->GetOSGCamera()->getRenderer());
osgUtil::SceneView* sceneView = renderer->getSceneView(0);
int xWin = sceneView->getViewport()->width()*(x+1)/2;
int yWin = sceneView->getViewport()->height()*(y+1)/2;
sceneView->projectWindowXYIntoObject(xWin,yWin,nearPoint,farPoint);
// create the isector
dtCore::RefPtr<dtCore::Isector> pickRay = new dtCore::Isector(GetScene());
pickRay->SetStartPosition(nearPoint);
pickRay->SetDirection(farPoint-nearPoint);
// shoot the ray
if(pickRay->Update())
{
osgUtil::IntersectVisitor::HitList hlist = pickRay->GetHitList();
osgUtil::Hit& hit = hlist.front();
osg::NodePath& np = hit._nodePath;
for (int i=np.size()-1; i>=0; --i)
{
osg::Node *tempNode = np[i];
std::string strName = tempNode->getName();
std::cout<<"Node name:"<<strName<<std::endl;
}
}
}
return false;
}