[osg-users] How to create a 3D ray from cursor position?
Jad Killian
m.hasani at outlook.com
Wed Aug 15 00:09:45 PDT 2018
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
More information about the osg-users
mailing list