title: picking object using mouse click in direct3d (managed directx)
body: <p>basically I want to pick an object using mouse click in direct3d (managed directx) C#.
  I need to transform 2D point to 3D point. After googling, I have found that, I can use picking 
  or ray. here is an example of using ray
  <a href="http://msdn.microsoft.com/en-us/library/bb203905.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb203905.aspx</a></p>
  
  <p>my problem if I implement ray is I can't find GraphicsDevice.Viewport.Unproject in direct3d.
  If there is another solution please tell me.</p>
  
  <p>Update:</p>
  
  <p>here is what I have done:</p>
  
  <pre><code>protected override void OnMouseClick(MouseEventArgs e)
      {
          base.OnMouseClick(e);
          int x = e.X;
          int y = e.Y;
          Vector3 nearsource = new Vector3((float)x, (float)y, 0f);
          Vector3 farsource = new Vector3((float)x, (float)y, 1f);
  
          nearsource.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View,device.Transform.World);
          farsource.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, device.Transform.World);
          Vector3 direction=nearsource-farsource;
          direction.Normalize();
          }
  </code></pre>
  
  <p>The problem is the farsouce matrix has NaN value</p>