2013-02-21 112 views
0

我用OSG在Android ...獲取索引節點或名稱節點時,挑對象

我初始化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

回答

0

一目瞭然,它是「給」這是不明確的,你的數字3.我沒有看到任何打印,我沒有看到任何可能包含3的int。我看到很多邊界和矩陣。

您需要更好地解釋這種情況,或許使用更簡單的演示代碼。

+0

好的,我編輯我的問題... – user1585783 2013-02-22 08:26:45

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(); 

希望這將清除您的疑問。