|
新建了三个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对象了。
哪位高手指点一下迷津,谢谢了。 |
|