0

我在 Android 上使用 OSG...

我在 main 中初始化了 indexNode= 0

int main(int, char **)
{

    osg::ref_ptr<osg::Node> model1 = osgDB::readNodeFile( "cessna.osg");
    osg::ref_ptr<osg::Node> model2 = osgDB::readNodeFile( "cow.osg" );

    unsigned int indexNode= 0;

    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->addChild( model1.get() );
    root->addChild( model2.get() );

    osg::ref_ptr<PickHandler> picker = new PickHandler;
    root->addChild( picker->getOrCreateSelectionBox() );

    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    viewer.addEventHandler( picker.get() );
    return viewer.run();

}

这是带有打印语句的部分

 class PickHandler : public osgGA::GUIEventHandler
{
  public:
         osg::Node* getOrCreateSelectionBox();
  virtual bool handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa );

  protected: osg::ref_ptr<osg::MatrixTransform> _selectionBox;
};


osg::Node* PickHandler::getOrCreateSelectionBox()
{
  if ( !_selectionBox )
     {
       osg::ref_ptr<osg::Geode> geode = new osg::Geode;
       geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(),1.0f)) );
       _selectionBox = new osg::MatrixTransform;
       _selectionBox->setNodeMask( 0x1 );
       _selectionBox->addChild( geode.get() );
       osg::StateSet* ss = _selectionBox->getOrCreateStateSet();
       ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
       ss->setAttributeAndModes(new osg::PolygonMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE));
     }
  return _selectionBox.get();
}


bool PickHandler::handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa )
{
  if ( ea.getEventType()!=osgGA::GUIEventAdapter::RELEASE || ea.getButton()!=osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
      return false;

osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);

if ( viewer )
   {
     osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, ea.getX(), ea.getY());
     osgUtil::IntersectionVisitor iv( intersector.get() );
     iv.setTraversalMask( ~0x1 );
     viewer->getCamera()->accept( iv );
     if ( intersector->containsIntersections() )
        {
          osgUtil::LineSegmentIntersector::Intersection result=*(intersector->getIntersections().begin());
          osg::BoundingBox bb = result.drawable->getBound();


          osg::ref_ptr newnode = result.drawable->getParent(0);
          indexNode= mTransform->getChildIndex(newnode);

          __android_log_print(ANDROID_LOG_INFO,"index","%d\n",indexNode);


          osg::Vec3 worldCenter = bb.center() * osg::computeLocalToWorld(result.nodePath);
          _selectionBox->setMatrix(osg::Matrix::scale(bb.xMax()-bb.xMin(), bb.yMax()-bb.yMin(), bb.zMax()-bb.zMin()) *osg::Matrix::translate(worldCenter) );
        }
   }
   return false;
}

这些行:

osg::ref_ptr newnode = result.drawable->getParent(0);
indexNode= mTransform->getChildIndex(newnode);       
__android_log_print(ANDROID_LOG_INFO,"index","%d\n",indexNode);

当我选择任何节点(牛或飞机)时,我想获取节点名称或索引节点,我需要返回任何可以帮助我知道我选择了哪个对象的东西?

我不知道我所做的是否正确,但是当我单击任何对象时,它会给我数字 3

4

2 回答 2

0

这是添加节点名称和标识符的方式:

int main()
{
    ....
    osg::ref_ptr<osg::Group> root = new osg::Group;
    root->setName("Node Name");
    root->setUserValue("id", idNumber);
    ...
}

拾取对象时,您可以通过以下方式检索其名称和标识符:

osg::Node *node;
node->getUserValue("id", returnedString);
std::string name = node->getName();

希望这可以消除您的疑问。

于 2013-05-08T17:41:47.067 回答
0

乍一看,不清楚它在哪里“给”你数字 3。我没有看到任何打印的内容,也没有看到任何可能包含 3 的 int。我看到了很多边界和矩阵。

您需要更好地解释这种情况,也许使用更简单的演示代码。

于 2013-02-22T01:30:17.627 回答