<div dir="ltr">Hi, all,<div><br></div><div>I noticed in osgpick.cpp that  osgText::Text and osg::Geometry could be picked, but when I add an osg::Geode with osg::ShapeDrawable into the HUD camera, the picking does not work anymore.</div><div><br></div><div>I list the code as follows. It will be appreciated if anyone can point out where the problem is or provide the correct way.  Thanks in advance.</div><div><br></div><div><div>// g++ osgpick.cpp -Wall -O3 -W   -I/usr/include  -losgDB -losgGA -losgViewer -losg -losgUtil -losgText -lOpenThreads  -losgWidget  -o osgpick.out </div><div>#include <osgViewer/Viewer></div><div><br></div><div>#include <osg/Geode></div><div>#include <osg/MatrixTransform></div><div>#include <osg/Camera></div><div>#include <osg/io_utils></div><div>#include <osg/ShapeDrawable></div><div><br></div><div>#include <osgText/Text></div><div><br></div><div>#include <sstream></div><div><br></div><div>// class to handle events with a pick</div><div>class PickHandler : public osgGA::GUIEventHandler {</div><div>public:</div><div><br></div><div>    PickHandler(osgText::Text* updateText):</div><div>        _updateText(updateText) {}</div><div>    ~PickHandler() {}</div><div>    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);</div><div>    virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea);</div><div>    void setLabel(const std::string& name)</div><div>    {</div><div>        if (_updateText.get()) _updateText->setText(name);</div><div>    }</div><div>protected:</div><div>    osg::ref_ptr<osgText::Text>  _updateText;</div><div>};</div><div><br></div><div>bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)</div><div>{</div><div>    switch(ea.getEventType())</div><div>    {</div><div>        case(osgGA::GUIEventAdapter::PUSH):</div><div>        {</div><div>            osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);</div><div>            if (view) pick(view,ea);</div><div>            return false;</div><div>        }</div><div>        case(osgGA::GUIEventAdapter::KEYDOWN):</div><div>        {</div><div>            if (ea.getKey()=='c')</div><div>            {</div><div>                osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);</div><div>                osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter(ea);</div><div>                event->setX((ea.getXmin()+ea.getXmax())*0.5);</div><div>                event->setY((ea.getYmin()+ea.getYmax())*0.5);</div><div>                if (view) pick(view,*event);</div><div>            }</div><div>            return false;</div><div>        }</div><div>        default:</div><div>            return false;</div><div>    }</div><div>}</div><div><br></div><div>void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea)</div><div>{</div><div>    osgUtil::LineSegmentIntersector::Intersections intersections;</div><div><br></div><div>    std::string gdlist="";</div><div><br></div><div>    if (view->computeIntersections(ea,intersections))</div><div>    {</div><div>        for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();</div><div>            hitr != intersections.end();</div><div>            ++hitr)</div><div>        {</div><div>            std::ostringstream os;</div><div>            if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty()))</div><div>            {</div><div>                // the geodes are identified by name.</div><div>                os<<"Object \""<<hitr->nodePath.back()->getName()<<"\""<<std::endl;</div><div>            }</div><div>            else if (hitr->drawable.valid())</div><div>            {</div><div>                os<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl;</div><div>            }</div><div><br></div><div>            os<<"        local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<"  normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;</div><div>            os<<"        world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<"  normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;</div><div>            const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil = hitr->indexList;</div><div>            for(unsigned int i=0;i<vil.size();++i)</div><div>            {</div><div>                os<<"        vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;</div><div>            }</div><div><br></div><div>            gdlist += os.str();</div><div>        }</div><div>    }</div><div>    setLabel(gdlist);</div><div>}</div><div><br></div><div>osg::Camera* createHUD(osgText::Text* updateText)</div><div>{</div><div>    osg::Camera* hudCamera = new osg::Camera;</div><div>    hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);</div><div>    hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);</div><div>    hudCamera->setViewMatrix(osg::Matrix::identity());</div><div>    hudCamera->setRenderOrder(osg::Camera::POST_RENDER);</div><div>    hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);</div><div><br></div><div>    std::string timesFont("fonts/times.ttf");</div><div><br></div><div>    osg::Vec3 position(150.0f,800.0f,0.0f);</div><div>    osg::Vec3 delta(0.0f,-60.0f,0.0f);</div><div><br></div><div>    {</div><div>        osg::Geode* geode = new osg::Geode();</div><div>        osg::StateSet* stateset = geode->getOrCreateStateSet();</div><div>        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);</div><div>        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);</div><div>        geode->setName("simple");</div><div>        hudCamera->addChild(geode);</div><div><br></div><div>        osgText::Text* text = new  osgText::Text;</div><div>        geode->addDrawable( text );</div><div><br></div><div>        text->setFont(timesFont);</div><div>        text->setText("Picking in Head Up Displays is simple!");</div><div>        text->setPosition(position);</div><div><br></div><div>        position += delta;</div><div>    }</div><div><br></div><div><br></div><div>    for (int i=0; i<5; i++) {</div><div>        osg::Vec3 dy(0.0f,-30.0f,0.0f);</div><div>        osg::Vec3 dx(120.0f,0.0f,0.0f);</div><div>        osg::Geode* geode = new osg::Geode();</div><div>        osg::StateSet* stateset = geode->getOrCreateStateSet();</div><div>        const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"};</div><div>        osg::Geometry *quad=new osg::Geometry;</div><div>        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);</div><div>        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);</div><div>        std::string name="subOption";</div><div>        name += " ";</div><div>        name += std::string(opts[i]);</div><div>        geode->setName(name);</div><div>        osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad</div><div>        osg::Vec4Array* colors = new osg::Vec4Array;</div><div>        colors = new osg::Vec4Array;</div><div>        colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0));</div><div>        quad->setColorArray(colors, osg::Array::BIND_OVERALL);</div><div>        (*vertices)[0]=position;</div><div>        (*vertices)[1]=position+dx;</div><div>        (*vertices)[2]=position+dx+dy;</div><div>        (*vertices)[3]=position+dy;</div><div>        quad->setVertexArray(vertices);</div><div>        quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));</div><div>        geode->addDrawable(quad);</div><div>        hudCamera->addChild(geode);</div><div><br></div><div>        position += delta;</div><div>    }</div><div><br></div><div>    { // this displays what has been selected</div><div>        osg::Geode* geode = new osg::Geode();</div><div>        osg::StateSet* stateset = geode->getOrCreateStateSet();</div><div>        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);</div><div>        stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);</div><div>        geode->setName("The text label");</div><div>        geode->addDrawable( updateText );</div><div>        hudCamera->addChild(geode);</div><div><br></div><div>        updateText->setCharacterSize(20.0f);</div><div>        updateText->setFont(timesFont);</div><div>        updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));</div><div>        updateText->setText("");</div><div>        updateText->setPosition(position);</div><div>        updateText->setDataVariance(osg::Object::DYNAMIC);</div><div><br></div><div>        position += delta;</div><div>    }</div><div>    return hudCamera;</div><div>}</div><div><br></div><div>osg::Node* createScene() {</div><div><span class="" style="white-space:pre">    </span>osg::ref_ptr<osg::Geode> node = new osg::Geode;</div><div><span class="" style="white-space:pre">      </span>osg::ref_ptr<osg::ShapeDrawable> sphere = new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(500,300,0), 100));</div><div><span class="" style="white-space:pre">        </span>node->addDrawable(sphere.get());</div><div><span class="" style="white-space:pre">        </span>return node.release();</div><div>}</div><div><br></div><div>int main( int argc, char **argv )</div><div>{</div><div>    osg::ref_ptr<osg::Node> scene = createScene();</div><div>    osg::ref_ptr<osg::Group> group = new osg::Group;</div><div>    osg::ref_ptr<osgText::Text> updateText = new osgText::Text;</div><div><span class="" style="white-space:pre">  </span>osg::ref_ptr<osg::Camera> hud = createHUD(updateText.get());</div><div><span class="" style="white-space:pre"> </span>hud->addChild(scene.get());</div><div><span class="" style="white-space:pre">     </span>//group->addChild(scene.get());</div><div>    group->addChild(hud.get());</div><div><span class="" style="white-space:pre">      </span>osgViewer::Viewer viewer;</div><div><span class="" style="white-space:pre">  </span>viewer.addEventHandler(new PickHandler(updateText.get()));</div><div><span class="" style="white-space:pre"> </span>viewer.setSceneData(group.get());</div><div><span class="" style="white-space:pre">  </span>return viewer.run();</div><div>}</div><div><br></div><div><div><br></div>-- <br><div class="gmail_signature">Martin</div>
</div></div></div>