查看: 2929|回复: 2

鼠标选择物体

[复制链接]

该用户从未签到

发表于 2009-7-31 09:12:22 | 显示全部楼层 |阅读模式
随着osg代码发生变化,Delta3d升级后,官方网站上的选取代码也不能应用,现将代码修改如下:
bool  Picker::MBPressed (const dtCore::Mouse *mouse, dtCore::Mouse::MouseButton button)
{
        if (button == dtCore::Mouse::MouseButton::RightButton)
        {
                //is there an object picked by the mouse ?
                dtCore:eltaDrawable* 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;
                                std::string strName  = tempNode->getName();
                                std::cout<<"Node name:"<<strName<<std::endl;
                        }

                }
        }
        return false;
}
本文来自新浪博客,转载请标明出处:http://blog.sina.com.cn/s/blog_613d5bdc0100em09.html

该用户从未签到

发表于 2009-7-31 19:06:55 | 显示全部楼层
支持一个~~~~····

该用户从未签到

发表于 2010-3-20 23:35:37 | 显示全部楼层
支持了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表