查看: 1421|回复: 4

拾取到osg::Drawable对象后移动它到另一个位置,再也无法拾取到此对象了,这是为什么

[复制链接]

该用户从未签到

发表于 2013-6-8 11:15:08 | 显示全部楼层 |阅读模式
新建了三个osg::ShapeDrawable对象,并且使用osg::Sphere对其进行了初始化
void ClipBoard::SetFourPointAndInitClipBoard( const osg::Vec3& p1, const osg::Vec3& p2, const osg::Vec3& p3, const osg::Vec3& p4 )
{
        static bool bFirst = true;
        if(bFirst)
        {
                m_v3P1 = p1;
                m_v3P2 = p2;
                m_v3P3 = p3;
                m_v3P4 = p4;
                bFirst = false;
        }
        if(m_pSpatialRef == 0)
                return;
        float fCtrlSphereRadius = 10000;
        m_Vec3Array->clear();
        osg::Vec3d v3Wp1, v3Wp2, v3Wp3, v3Wp4, v3CtrlSphereCenter;
//转换经维度坐标为球面坐标
        m_pSpatialRef->transformToECEF(p1, v3Wp1);
        m_pSpatialRef->transformToECEF(p2, v3Wp2);
        m_pSpatialRef->transformToECEF(p3, v3Wp3);
        m_pSpatialRef->transformToECEF(p4, v3Wp4);

        m_Vec3Array->push_back(v3Wp1);
        m_Vec3Array->push_back(v3Wp2);
        m_Vec3Array->push_back(v3Wp3);
        m_Vec3Array->push_back(v3Wp4);

        SetPlaneColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
        SetBorderlineColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
        m_PlaneGeometry->setVertexArray(m_Vec3Array);
        m_PlaneGeometry->addPrimitiveSet(m_PlanePrimitiveset);

        m_BorderlineGeometry->setVertexArray(m_Vec3Array);
        m_BorderlineGeometry->addPrimitiveSet(m_Borderline);

                ////////////////////////////创建ShapeDrawable控制球/////////////////////////////////////
        m_Contrl_Sphere1 = new osg::Sphere(v3Wp3, fCtrlSphereRadius);
        m_Contrl_Sphere2 = new osg::Sphere(v3Wp4, fCtrlSphereRadius);

        osg::Vec3 controlSphere3Center;
        controlSphere3Center.set(p3.x() - abs(p3.x() - p4.x()) / 2, p3.y() - abs(p3.y() - p4.y()) / 2, p3.z());
        m_pSpatialRef->transformToECEF(controlSphere3Center, v3CtrlSphereCenter);
        m_Contrl_Sphere3 = new osg::Sphere(v3CtrlSphereCenter, fCtrlSphereRadius);
        osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
        hints->setDetailRatio(0.5f);

        //if(m_ShapeDrawable1) bool bR = m_ControlGeode1->removeDrawable(m_ShapeDrawable1);
        m_ShapeDrawable1 = new osg::ShapeDrawable(m_Contrl_Sphere1, hints);
        m_ShapeDrawable1->setName("ControlShape1");
        m_ShapeDrawable1->setColor(osg::Vec4(1, 1, 0, 1));
        m_ShapeDrawable1->setUserData(this);
        //m_ControlGeode1->setDrawable(0, m_ShapeDrawable1);
        m_ControlGeode1->addDrawable(m_ShapeDrawable1);

        //if(m_ShapeDrawable2) m_ControlGeode1->removeDrawable(m_ShapeDrawable2);
        m_ShapeDrawable2 = new osg::ShapeDrawable(m_Contrl_Sphere2, hints);
        m_ShapeDrawable2->setName("ControlShape2");
        m_ShapeDrawable2->setColor(osg::Vec4(1, 1, 0, 1));
        m_ShapeDrawable2->setUserData(this);
        //m_ControlGeode1->setDrawable(1, m_ShapeDrawable2);
        m_ControlGeode1->addDrawable(m_ShapeDrawable2);
       
        //if(m_ShapeDrawable3) m_ControlGeode1->removeDrawable(m_ShapeDrawable3);
        m_ShapeDrawable3 = new osg::ShapeDrawable(m_Contrl_Sphere3, hints);
        m_ShapeDrawable3->setName("ControlShape3");
        m_ShapeDrawable3->setColor(osg::Vec4(1, 0, 0, 1));
        m_ShapeDrawable3->setUserData(this);
        //m_ControlGeode1->setDrawable(2, m_ShapeDrawable3);
        m_ControlGeode1->addDrawable(m_ShapeDrawable3);

        /*pDofTransform->addChild(m_ControlGeode1);
        pDofTransform->addChild(m_ControlGeode2);
        pDofTransform->addChild(m_ControlGeode3);*/
       
        addChild(m_ControlGeode1);
        //addChild(m_ControlGeode2);
        //addChild(m_ControlGeode3);

        m_ShapeDrawable1->dirtyDisplayList();
        m_ShapeDrawable2->dirtyDisplayList();
        m_ShapeDrawable3->dirtyDisplayList();

        m_PlaneGeometry->dirtyDisplayList();
        m_BorderlineGeometry->dirtyDisplayList();
}
拾取代码如下:
osg:rawable* idpEarthManipulator::pickDrawable( osgViewer::View* view, const osgGA::GUIEventAdapter& ea )
{
        if(!view) return 0;
        osg::MatrixTransform* pMT = 0;
        static osg::Vec4 color1(1, 0.5, 0.5, 0.5), color2(0.5, 0.5, 1, 0.5);

        osgUtil:ineSegmentIntersector::Intersections intersections;

        std::string gdlist="";
        float x = ea.getX();
        float y = ea.getY();

        if (view->computeIntersections(x,y,intersections))
        {
                for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
                        hitr != intersections.end();
                        ++hitr)
                {
                        std::string strClassName = hitr->drawable->className();

                        if(strClassName == "ShapeDrawable")
                        {
                                return hitr->drawable;
                        }
                }
        }
        return 0;
}

移动代码如下:
void ClipBoard::MoveClipBoard(int iPosID, double dXDist, double dYDist )
{
        switch(iPosID)
        {
                case 0:
                        m_v3P1.set(m_v3P1.x()+dXDist, m_v3P1.y() + dYDist, m_v3P1.z());
                        m_v3P2.set(m_v3P2.x(), m_v3P2.y(), m_v3P2.z());
                        m_v3P3.set(m_v3P3.x(), m_v3P3.y(), m_v3P3.z());
                        m_v3P4.set(m_v3P4.x()+dXDist, m_v3P4.y() + dYDist, m_v3P4.z());
                        break;
                case 1:
                        m_v3P1.set(m_v3P1.x()+dXDist, m_v3P1.y() + dYDist, m_v3P1.z());
                        m_v3P2.set(m_v3P2.x()+dXDist, m_v3P2.y() + dYDist, m_v3P2.z());
                        m_v3P3.set(m_v3P3.x()+dXDist, m_v3P3.y() + dYDist, m_v3P3.z());
                        m_v3P4.set(m_v3P4.x()+dXDist, m_v3P4.y() + dYDist, m_v3P4.z());
                        break;
                case 2:
                        m_v3P1.set(m_v3P1.x(), m_v3P1.y(), m_v3P1.z());
                        m_v3P2.set(m_v3P2.x()+dXDist, m_v3P2.y() + dYDist, m_v3P2.z());
                        m_v3P3.set(m_v3P3.x()+dXDist, m_v3P3.y() + dYDist, m_v3P3.z());
                        m_v3P4.set(m_v3P4.x(), m_v3P4.y(), m_v3P4.z());
                        break;
                default:
                        return;
        }
       
        if(m_pSpatialRef == 0)
                return;
        float fCtrlSphereRadius = 10000;
        m_Vec3Array->clear();
        osg::Vec3d v3Wp1, v3Wp2, v3Wp3, v3Wp4, v3CtrlSphereCenter;
//转换经维度坐标为球面坐标
        m_pSpatialRef->transformToECEF(m_v3P1, v3Wp1);
        m_pSpatialRef->transformToECEF(m_v3P2, v3Wp2);
        m_pSpatialRef->transformToECEF(m_v3P3, v3Wp3);
        m_pSpatialRef->transformToECEF(m_v3P4, v3Wp4);

        m_Vec3Array->push_back(v3Wp1);
        m_Vec3Array->push_back(v3Wp2);
        m_Vec3Array->push_back(v3Wp3);
        m_Vec3Array->push_back(v3Wp4);

        SetPlaneColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
        SetBorderlineColor(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
        m_PlaneGeometry->setVertexArray(m_Vec3Array);
        m_PlaneGeometry->addPrimitiveSet(m_PlanePrimitiveset);

        m_BorderlineGeometry->setVertexArray(m_Vec3Array);
        m_BorderlineGeometry->addPrimitiveSet(m_Borderline);

        ////////////////////////////控制球/////////////////////////////////////
        m_Contrl_Sphere1->set(v3Wp3, fCtrlSphereRadius);
        m_Contrl_Sphere2->set(v3Wp4, fCtrlSphereRadius);

        osg::Vec3 controlSphere3Center;
        controlSphere3Center.set(m_v3P3.x() - abs(m_v3P3.x() - m_v3P4.x()) / 2, m_v3P3.y() - abs(m_v3P3.y() - m_v3P4.y()) / 2, m_v3P3.z());
        m_pSpatialRef->transformToECEF(controlSphere3Center, v3CtrlSphereCenter);
        m_Contrl_Sphere3->set(v3CtrlSphereCenter, fCtrlSphereRadius);
        //SetFourPointAndInitClipBoard(m_v3P1, m_v3P2, m_v3P3, m_v3P4);
       
        m_ShapeDrawable1->dirtyDisplayList();
        m_ShapeDrawable2->dirtyDisplayList();
        m_ShapeDrawable3->dirtyDisplayList();

        m_PlaneGeometry->dirtyDisplayList();
        m_BorderlineGeometry->dirtyDisplayList();
}

第一次拾取到是可以移动的,移动以后,再也无法拾取到这些osg::ShapeDrawable对象了。
哪位高手指点一下迷津,谢谢了。

该用户从未签到

发表于 2013-6-8 11:20:47 | 显示全部楼层
好像看到过dirty Bound  之类的东东,看你只dirtyDisplayList,是不是这个问题阿。。。

该用户从未签到

 楼主| 发表于 2013-6-8 11:37:08 | 显示全部楼层
2楼说的还 真对啊,真没想到啊,居然标记了显示列为dirty,重绘以后,图形的Bound不跟着变,也不知道OSG的开发者是怎么想的。

该用户从未签到

发表于 2013-6-8 13:55:24 | 显示全部楼层
灵活和简单的权衡,无可厚非,osg还是极其给力的,也经过了这么长时间的考研,很强大

该用户从未签到

发表于 2013-6-8 14:45:34 | 显示全部楼层
完全是自己的疏忽 跟OSG有什么关系
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

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

联系我们

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