鼠标选择物体

随着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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值