2
votes

I was trying to place a sphere on the 3D space from the user selected point on 2d screen space. For this iam trying to calculate 3d point from 2d point using the below technique and this technique not giving the correct solution.

mousePosition.x = ((clickPos.clientX - window.left) / control.width) * 2 - 1;
    mousePosition.y = -((clickPos.clientY - window.top) / control.height) * 2 + 1;

then Iam multiplying the mousePositionwith Inverse of MVP matrix. But getting random number at result.

for calculating MVP Matrix :

 osg::Matrix mvp =   _camera->getViewMatrix() * _camera->getProjectionMatrix();

How can I proceed? Thanks.

1
How will you calculate the depth coordinate from a mouse click? - vincent
I'm having a plane placed at xz axis right now. @vincent - S.Frank Richarrd
In that case you're sort of doing a clipping plane. Typically one uses dot product for this kind of calculation. - vincent
Not a kind of clipping place. Iam Restricting the user to place the sphere on that plane. - S.Frank Richarrd
You can't multiply a 2d-vector with a 4x4 matrix. Please show the exact code you tried including relevant declarations. - Nico Schertler

1 Answers

3
votes

Under the assumption that the mouse position is normalized in the range [-1, 1] for x and y, the following code will give you 2 points in world coordinates projected from your mouse coords: nearPoint is the point in 3D lying on the camera frustum near plane, farPointon the frustum far plane.
Than you can compute a line passing by these points and intersecting that with your plane.

  // compute the matrix to unproject the mouse coords (in homogeneous space)    
  osg::Matrix VP = _camera->getViewMatrix() * _camera->getProjectionMatrix();

  osg::Matrix inverseVP;
  inverseVP.invert(VP);

  // compute world near far
  osg::Vec3 nearPoint(mousePosition.x, mousePosition.x, -1.0f);
  osg::Vec3 farPoint(mousePosition.x, mousePosition.x, 1.0f);
  osg::Vec3 nearPointWorld = nearPoint * inverseVP;
  osg::Vec3 farPointWorld = farPoint * inverseVP;