02-14-2017, 10:54 AM
When using cameras from Ab3d.PowerToys library, you can easily get the 2D point on the screen from the 3D position with using the Point3DTo2D method on a Ab3d.PowerToys camera.
You can also convert a 2D point on the screen into a 3D ray (3D position and 3D vector) - in this case you cannot get only one 3D point but a ray. You can get this with calling CreateMouseRay3D method.
If you are not using Ab3d.PowerToys cameras, you can get the 2D screen position with first calculating the view-projection matrix (see viewProj in my previous post).
Then you need to need to multiply the 3D position (converted into Point4D) with viewProjection and divide by w:
To calculate the ray from 2D position to 3D space, you can use the Ab3d.Cameras.BaseCamera.CreateMouseRay3D static method - the following is the method signature:
public static bool CreateMouseRay3D(Point mousePosition, Size viewportSize, ref Matrix3D viewMatrix, ref Matrix3D projectionMatrix, out Point3D rayOrigin, out Vector3D rayDirection)
You can also convert a 2D point on the screen into a 3D ray (3D position and 3D vector) - in this case you cannot get only one 3D point but a ray. You can get this with calling CreateMouseRay3D method.
If you are not using Ab3d.PowerToys cameras, you can get the 2D screen position with first calculating the view-projection matrix (see viewProj in my previous post).
Then you need to need to multiply the 3D position (converted into Point4D) with viewProjection and divide by w:
Code:
Point4D point4d = ((Point4D)point3D) * viewProj;
if (point4d.W != 0)
screenPosition = new Point(point4d.X / point4d.W, point4d.Y / point4d.W);
else
screenPosition = new Point(double.NaN, double.NaN);To calculate the ray from 2D position to 3D space, you can use the Ab3d.Cameras.BaseCamera.CreateMouseRay3D static method - the following is the method signature:
public static bool CreateMouseRay3D(Point mousePosition, Size viewportSize, ref Matrix3D viewMatrix, ref Matrix3D projectionMatrix, out Point3D rayOrigin, out Vector3D rayDirection)
Andrej Benedik

