<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Hi Jad,<br>
    <br>
    this is how I do it:<br>
    <br>
    <pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><font color="#330033"> double x = mEv->x();
 double y = mEv->y();
 double w = view->getGraphicsWidth();
 double h = view->getGraphicsHeight();
 x = x/w;
 y = y/h;
 // Normalize to range -1.0 - 1.0
 x = (2.0 * _x) - 1.0;
 y = -((2.0 * _y) - 1.0); // -1.0 is at bottom!!!
 osg::Matrix MVPW( view->getCamera()->getViewMatrix() *
                          view->getCamera()->getProjectionMatrix());
 osg::Matrixd inverseMVPW = osg::Matrixd::inverse(MVPW);
 osg::Vec3 near_point = osg::Vec3(_x,_y,-1.0f) * inverseMVPW;
 osg::Vec3 far_point  = osg::Vec3(_x,_y, 1.0f) * inverseMVPW;
</font>
</pre>
    - Werner -<br>
    <br>
    <div class="moz-cite-prefix">Am 16.07.2018 um 17:34 schrieb Jad
      Killian:<br>
    </div>
    <blockquote type="cite"
      cite="mid:1531755260.m2f.74349@forum.openscenegraph.org">
      <pre wrap="">Given a 2D cursor position from a mouse event, how do I compute a 3D ray into the scene?

This is my attempt so far but it's not giving sensible results:


Code:
float width = viewer->getCamera()->getViewport()->width();
float height = viewer->getCamera()->getViewport()->width();

float x = (2.0f * ea.getX()) / width - 1.0f;
float y = 1.0f - (2.0f * ea.getY()) / height;
float z = 1.0f;

osg::Vec3 ray_nds(x, y, z);

osg::Vec4 ray_clip(ray_nds.x(), ray_nds.y(), -1.0, 1.0);

osg::Matrixd proj_inv = viewer->getCamera()->getProjectionMatrix();
proj_inv = proj_inv.inverse(proj_inv);

osg::Vec4 ray_eye = proj_inv * ray_clip;
ray_eye.z() = -1.0;
ray_eye.w() = 0.0;

osg::Vec4 ray_wor4 = viewer->getCamera()->getInverseViewMatrix() * ray_eye;
osg::Vec3 ray_wor(ray_wor4.x(), ray_wor4.y(), ray_wor4.z());



P.S: I need such a ray so I can drag a 3D object around the scene like this:
new_position = camera_position + ray_direction * depth_value;
Or is there an easier way to accomplish this?

Thanks
[/code]

------------------
Read this topic online here:
<a class="moz-txt-link-freetext" href="http://forum.openscenegraph.org/viewtopic.php?p=74349#74349">http://forum.openscenegraph.org/viewtopic.php?p=74349#74349</a>





_______________________________________________
osg-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:osg-users@lists.openscenegraph.org">osg-users@lists.openscenegraph.org</a>
<a class="moz-txt-link-freetext" href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a>
</pre>
    </blockquote>
    <br>
    <div class="moz-signature">-- <br>
      <b>TE<span style="color:red;">X</span>ION Software Solutions,
        Rotter Bruch 26a, D-52068 Aachen</b><br>
      Phone: +49 241 475757-0<br>
      Fax: +49 241 475757-29<br>
      Web: <a class="moz-txt-link-freetext" href="http://texion.eu">http://texion.eu</a><br>
      eMail: <a class="moz-txt-link-abbreviated" href="mailto:info@texion.eu">info@texion.eu</a><br>
    </div>
  </body>
</html>