Hi,

this is the original post (below the dotted line). Be aware that there might be functions nowadays that might make your life easier. Maybe someone else could comment on this.

And: I don´t mind sending this, but there _is_ a mailing-list archive :-)

Regards,

Andreas


---------------------------------------------------
Hi,

I can´t find a mistake in your code, but I don´t know what you have copied from producer.

I solved that problem in a similar situation, only that I use wxWidgets as windowing system.

As I did not understand the producer-code I used osg-classes only, with a little help from the list archives:

This is the code, you will have to modify the wx-dependent stuff, which is very little, only at the beginning. Sorry I don´t know why your´s doesnt work, but this definitely works:


//Get mouse coordinates:

      lastx = event.GetX();
      lasty = event.GetY();
//Get Window width and height:
      int w=0; int h=0; GetSize(&w, &h);
      int px=0; int py =0; GetPosition(&px, &py);
     //Normalize:
      float x = -1.0f + (float)(2*(lastx))/(float)w;
      float y = -1.0f + (float)(2*(h -lasty))/(float)h;
osg::ref_ptr<osgUtil::IntersectVisitor> visit = new osgUtil::IntersectVisitor(); //Make the visitor visit the scene, I don´t know if all this is necessary, I just tried for some hours until it worked:
      visit->apply(*(sceneView->getSceneData()));
      sceneView->getSceneData()->accept(*visit);
//Now the stuff for calculating back into the scene:
      osg::Matrix pm = sceneView->getProjectionMatrix();
      osg::Matrix vm = sceneView->getViewMatrix();
//Now I use that fancy function from the list:
osg::ref_ptr<osg::LineSegment> line = projectNormalizedXYIntoObjectCoordinates(pm, vm, x, y);
      visit->addLineSegment(line.get());
      visit->setEyePoint(viewPos2);
      visit->apply(*(sceneView->getSceneData()));
//And that´s where I get the hits:
osgUtil::IntersectVisitor::HitList list = visit->getHitList(line.get()); //Now that function:
//I just improved it by using ref_ptr:

osg::ref_ptr<osg::LineSegment> TestGLCanvas::projectNormalizedXYIntoObjectCoordinates(
  osg::Matrix
  &projectionMatrix,
  osg::Matrix &viewMatrix,
  float x,
  float y ){
    osg::Matrix matrix = viewMatrix * projectionMatrix;
  osg::Matrix inverseVP;
  inverseVP.invert(matrix);


  osg::Vec3 near_point = osg::Vec3(x, y, 0.0f) * inverseVP;
  osg::Vec3 far_point = osg::Vec3(x, y, 1.0f) * inverseVP;
//Only for testing-purposes:
  wxString bla;
bla<< near_point[0] << " " << near_point[1] << " " << near_point[2] << "\n" <<
      far_point[0] << " " << far_point[1] << " " << far_point[2];
  //wxMessageBox(bla);
  osg::Vec3 xv = near_point - far_point;
//Here I push the near point more nearer and the far point farther
//because I didn´t get all hits
//This has the disadvantage, that if your´e inside an object, you will get hits "from behind":

  near_point = near_point + xv;
  far_point = far_point - xv;
osg::ref_ptr<osg::LineSegment> rueck =new osg::LineSegment( near_point, far_point );
    return rueck;
}



Regards,


Andreas
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/



_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to