osg fbx模型点击节点,对应节点染色

class CPickHandler :public osgGA::GUIEventHandler
{
public:
    CPickHandler(osgViewer::Viewer *viewer) :mViewer(viewer) {}
    virtual bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa)
    {
        switch (ea.getEventType())
        {
        //case osgGA::GUIEventAdapter::PUSH:
        case osgGA::GUIEventAdapter::DOUBLECLICK:
            if (ea.getButton() == 1)
            {
                Pick(ea.getX(), ea.getY());//可通过事件ea获得鼠标点击的坐标  
            }
            return true;
        }
        return false;
    }
protected:
    void Pick(float x, float y)
    {
        osgUtil::LineSegmentIntersector::Intersections intersections;//声明一个相交测试的结果集  
        if (mViewer->computeIntersections(x, y, intersections))//利用view的computerIntersection函数来测试屏幕与场景相交结果存入到结果集中  
        {
            osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
            for (; hitr != intersections.end(); hitr++)
            {
                if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty()))
                {
                    
                    const osg::NodePath& np = hitr->nodePath;
                    //for (int i = np.size() - 1; i >= 0; --i)
                    //{
                        osg::Node *sc = hitr->nodePath.at(np.size() - 1);
                        //将场景中的模型动态转换为Scribe类型,如果场景的模型中有Scribe节点则返回的是实际的模型Scribe对象  
                        //osgFX::Scribe *sc = dynamic_cast<osgFX::Scribe*>(np[i]);
                        //osg::Node *sc = dynamic_cast<osg::Node*>(np[i]);
                        if (sc != NULL)//如果找到相应的sc,则隐藏起来  
                        {
                            //if (sc->getNodeMask() != 0)
                            //{
                            //    sc->setNodeMask(0);
                            //}
                            setNodeStateset(sc);
                        }
                    //}
                }
            }
        }
    }
    osgViewer::Viewer *mViewer;
};

原文地址:https://www.cnblogs.com/herd/p/11148008.html