Hi Jad, this is how I do it:
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; - Werner - Am 16.07.2018 um 17:34 schrieb Jad Killian: > 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: > http://forum.openscenegraph.org/viewtopic.php?p=74349#74349 > > > > > > _______________________________________________ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org -- *TEXION Software Solutions, Rotter Bruch 26a, D-52068 Aachen* Phone: +49 241 475757-0 Fax: +49 241 475757-29 Web: http://texion.eu eMail: i...@texion.eu
_______________________________________________ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org