Hello,

I have two question the first one about bounding box and the other one
about select object

The first one I would like to load model in node and draw bounding box for
this node, I have tried to write the code but doesn't work..

This is the code:

int main(int argc, char *argv[])
{

    osg::ref_ptr<osg::Vec3Array> wireBoxPt;
    osg::ref_ptr<osg::Vec3Array> wireBoxVert;
    osg::ref_ptr<osg::StateSet> stateset;
    osg::ref_ptr<osg::Geode> root;
    osg::ref_ptr<osg::Geometry> lineGeom;
    osg::ref_ptr<osg::Geode> lineGeode;

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

    osg::ref_ptr<osg::Group> group = new osg::Group;


    wireBoxPt= new osg::Vec3Array;
    lineGeode= new osg::Geode;
    lineGeom= new osg::Geometry;
    wireBoxVert= new osg::Vec3Array;

    float scale= 1.0;
    wireBoxPt->push_back(osg::Vec3(-scale,-scale,-scale));
    wireBoxPt->push_back(osg::Vec3(scale,-scale,-scale));
    wireBoxPt->push_back(osg::Vec3(scale,scale,-scale));
    wireBoxPt->push_back(osg::Vec3(-scale,scale,-scale));
    wireBoxPt->push_back(osg::Vec3(-scale,-scale,scale));
    wireBoxPt->push_back(osg::Vec3(scale,-scale,scale));
    wireBoxPt->push_back(osg::Vec3(scale,scale,scale));
    wireBoxPt->push_back(osg::Vec3(-scale,scale,scale));

    wireBoxVert->push_back(wireBoxPt->at(0));
    wireBoxVert->push_back(wireBoxPt->at(1));

    wireBoxVert->push_back(wireBoxPt->at(1));
    wireBoxVert->push_back(wireBoxPt->at(2));

    wireBoxVert->push_back(wireBoxPt->at(2));
    wireBoxVert->push_back(wireBoxPt->at(3));

    wireBoxVert->push_back(wireBoxPt->at(3));
    wireBoxVert->push_back(wireBoxPt->at(0));

    wireBoxVert->push_back(wireBoxPt->at(0));
    wireBoxVert->push_back(wireBoxPt->at(4));

    wireBoxVert->push_back(wireBoxPt->at(1));
    wireBoxVert->push_back(wireBoxPt->at(5));

    wireBoxVert->push_back(wireBoxPt->at(2));
    wireBoxVert->push_back(wireBoxPt->at(6));

    wireBoxVert->push_back(wireBoxPt->at(3));
    wireBoxVert->push_back(wireBoxPt->at(7));

    wireBoxVert->push_back(wireBoxPt->at(4));
    wireBoxVert->push_back(wireBoxPt->at(5));

    wireBoxVert->push_back(wireBoxPt->at(5));
    wireBoxVert->push_back(wireBoxPt->at(6));

    wireBoxVert->push_back(wireBoxPt->at(6));
    wireBoxVert->push_back(wireBoxPt->at(7));

    wireBoxVert->push_back(wireBoxPt->at(7));
    wireBoxVert->push_back(wireBoxPt->at(4));
    lineGeom->setVertexArray(wireBoxVert);
    lineGeom->setUseDisplayList(false);
    lineGeom->addPrimitiveSet(new
osg::DrawArrays(GL_LINES,0,wireBoxVert->size()));

    lineGeode->addDrawable(lineGeom);
    group->addChild(lineGeode);

    group->addChild( model1 );
    group->addChild( model2 );

    int index1;
    index1=0;

    osg::Geometry* geome =
group->getChild(index1)->asGeode()->getDrawable(0)->asGeometry();
       osg::BoundingBox bb=geome->getBound();

       wireBoxPt->at(0)=(osg::Vec3(bb.xMin(),bb.yMin(),bb.zMin()));
       wireBoxPt->at(1)=(osg::Vec3(bb.xMax(),bb.yMin(),bb.zMin()));
       wireBoxPt->at(2)=(osg::Vec3(bb.xMax(),bb.yMax(),bb.zMin()));
       wireBoxPt->at(3)=(osg::Vec3(bb.xMin(),bb.yMax(),bb.zMin()));
       wireBoxPt->at(4)=(osg::Vec3(bb.xMin(),bb.yMin(),bb.zMax()));
       wireBoxPt->at(5)=(osg::Vec3(bb.xMax(),bb.yMin(),bb.zMax()));
       wireBoxPt->at(6)=(osg::Vec3(bb.xMax(),bb.yMax(),bb.zMax()));
       wireBoxPt->at(7)=(osg::Vec3(bb.xMin(),bb.yMax(),bb.zMax()));

       wireBoxVert->at(0)=(wireBoxPt->at(0));
       wireBoxVert->at(1)=(wireBoxPt->at(1));

       wireBoxVert->at(2)=(wireBoxPt->at(1));
       wireBoxVert->at(3)=(wireBoxPt->at(2));

       wireBoxVert->at(4)=(wireBoxPt->at(2));
       wireBoxVert->at(5)=(wireBoxPt->at(3));

       wireBoxVert->at(6)=(wireBoxPt->at(3));
       wireBoxVert->at(7)=(wireBoxPt->at(0));

       wireBoxVert->at(8)=(wireBoxPt->at(0));
       wireBoxVert->at(9)=(wireBoxPt->at(4));

       wireBoxVert->at(10)=(wireBoxPt->at(1));
       wireBoxVert->at(11)=(wireBoxPt->at(5));

       wireBoxVert->at(12)=(wireBoxPt->at(2));
       wireBoxVert->at(13)=(wireBoxPt->at(6));

       wireBoxVert->at(14)=(wireBoxPt->at(3));
       wireBoxVert->at(15)=(wireBoxPt->at(7));

       wireBoxVert->at(16)=(wireBoxPt->at(4));
       wireBoxVert->at(17)=(wireBoxPt->at(5));

       wireBoxVert->at(18)=(wireBoxPt->at(5));
       wireBoxVert->at(19)=(wireBoxPt->at(6));

       wireBoxVert->at(20)=(wireBoxPt->at(6));
       wireBoxVert->at(21)=(wireBoxPt->at(7));

       wireBoxVert->at(22)=(wireBoxPt->at(7));
       wireBoxVert->at(23)=(wireBoxPt->at(4));
       wireBoxVert->dirty();

    osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
    viewer->setUpViewInWindow(0, 0, 1000, 700);
    viewer->setSceneData(group );
    viewer->run();
}

_______________________________________________________________________________________________________________________

The second question about select object I have tried to use this code from
OSG beginner guide book...

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osg/ShapeDrawable>
#include <osg/PolygonMode>
#include <osgUtil/LineSegmentIntersector>

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::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;
}


int main(int, char **)
{

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

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

}

I would like to get the index number of the object that I selected

I have tried to use these lines with the previous code but it give me wrong
index number

osgUtil::LineSegmentIntersector::Intersection
result=*(intersector->getIntersections().begin());
bb = result.drawable->getBound();
int indexNode;
osg::ref_ptr<osg::Node> newnode = result.drawable->getParent(0);
indexNode= root->getChildIndex(newnode);

Best Regards,

-- 

------------------------------
This message and its contents, including attachments are intended solely 
for the original recipient. If you are not the intended recipient or have 
received this message in error, please notify me immediately and delete 
this message from your computer system. Any unauthorized use or 
distribution is prohibited. Please consider the environment before printing 
this email.
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to