>
>
> Hi, Andreas.
>
>         Sorry I took so long to reply. I still haven't tried your 
> method, but that's because I didn't understand it right away. I'll 
> read a little bit more of the documentation to see the classes and 
> methods that I need, and when I do it, I'll let you know.
>
Hi Renan,

here a bit of code, which you will have to adjust to your setup (meaning 
that I use a GUI and you use a Viewer, where Functions for retrieving 
the mouse are different):

    //Here you get the mouse, you will have to do this differently
    int lastx = event.GetX();
    int lasty = event.GetY();
    //Here you get the size of your viewport, you will have to do this 
differently:
    int w=0; int h=0; GetSize(&w, &h);
    //Here I normalize the mouse to floats between -1 and 1
    float x = -1.0f + (float)(2*(lastx))/(float)w;
    float y = -1.0f + (float)(2*(h -lasty))/(float)h;
   
    //You will have to do this slightly differently, using the 
corresponding functions of your viewer:
    osg::Matrix pm = sceneView->getProjectionMatrix();
    osg::Matrix vm = sceneView->getViewMatrix();
   
    //This function is explained later
    osg::ref_ptr<osg::LineSegment> line = 
projectNormalizedXYIntoObjectCoordinates(pm, vm, x, y);
   
    //This is the plane with the normal as described going throught the 
origin
    osg::Plane p(line->start() - line->end(), 0);
   
    //Here I use a function for the intersection of a plane and a line, 
this is a good exercise for you to write yourself
    osg::Vec3 Newpos = Geo::Intersect(p, *line);
   


Now the desired position in World coordinates is in the osg::Vec3 Newpos


Here the function "project...":

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);
   
    near_point = osg::Vec3(x, y, -1.0f) * inverseVP;
   
    osg::Vec3 far_point = osg::Vec3(x, y, 1.0f) * inverseVP;

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


You can do two things: Just use it, it should work, or read the first 
chapters in RealTimeRendering, where you will learn what the view and 
the projection matrices are and why ou need to normalize your 
coordinates to [-1..1].

Regards,

Andreas

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to