Click or drag to resize
AB4D logo

BaseCameraGetWorldToViewportMatrix Method

Gets Matrix3D that can be used to convert 3D coordinates to 2D viewport (screen) coordinates. If the method returns true, than the worldToViewportMatrix was set; if false is returned than the matrix cannot be calculated.

Namespace: Ab3d.Cameras
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.2.9104.2045
Syntax
C#
public bool GetWorldToViewportMatrix(
	ref Matrix3D worldToViewportMatrix,
	bool forceMatrixRefresh
)

Parameters

worldToViewportMatrix  Matrix3D
reference to Matrix3D
forceMatrixRefresh  Boolean
if true than the worldToViewportMatrix is always recalculated; if false the recalculation is done only when the main camera properties were changed

Return Value

Boolean
true if worldToViewportMatrix was set; false if the matrix cannot be calculated
Remarks

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:

C#
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);
}
See Also