Base |
public bool GetWorldToViewportMatrix( ref Matrix3D worldToViewportMatrix, bool forceMatrixRefresh )
GetWorldToViewportMatrix gets Matrix3D that can be used to convert 3D coordinates to 2D viewport coordinates.
If the method returns true, than the worldToViewportMatrix was set; if false is returned than the matrix cannot be calculated (for example when MatrixCamera is used and the matrix cannot be inverted).
The cameras in Ab3d.PowerToys use protected field isWorldToViewportMatrixDirty that is used to determine if the cached worldToViewportMatrix must be recalculated when its value is required. The isWorldToViewportMatrixDirty is set to true when main camera properties are changed. But if some other property is changed (for example FarPlaneDistance), than forceMatrixRefresh parameter in this method need to be set to true to force recalculation of worldToViewportMatrix.
Important:
To calculate the screen coordinates of a 3D point you need to convert the Point3D to Point4D, multiply it with worldToViewportMatrix and than divide the X and Y coordinate with W value from Point4D struct.
The following code shows that:
Point3D inputPoint3D; Point screenPoint; Matrix3D worldToViewportMatrix; bool isworldToViewportMatrixValid = myCamera.GetWorldToViewportMatrix(ref worldToViewportMatrix, false); if (isworldToViewportMatrixValid) { Point4D point4d = ((Point4D)inputPoint3D) * worldToViewportMatrix; if (point4d.W != 0) screenPoint = new Point(point4d.X / point4d.W, point4d.Y / point4d.W); else screenPoint = new Point(double.NaN, double.NaN); } else { screenPoint = new Point(double.NaN, double.NaN); }