Mat4 - GL Math¶
PHP-GLFW comes with a built-in math library this is a quick reference for the Mat4 class.
The Mat4 represents a matrix 4x4 Matrix with 16 values.
The values hold by the Mat4 object are stored internally as real float values, and can be accessed in an array like fashion:
Methods¶
__construct¶
Constructucts a new Mat4 matrix Does not take any arguments and always returns an identity matrix.
aka:
fromArray¶
Constructs and returns a new matrix based on the given array of values
- arguments
-
array$valuesThe values to use for the matrix. (flat)
- returns
-
\Mat4The new matrix.
inverted¶
Constructs and returns an inverse of the given matrix
- arguments
-
\Mat4$matrixThe matrix to invert.
- returns
-
\Mat4The inverted matrix.
multiplyQuat¶
Mat4 * Quat
Multiplies the left matrix by the right quaternion
Note: This method only exists because there is a bug with the order of operation in PHP.
copy¶
Copys the current matrix
Returns a new instance of the current matrix with the same values. This means that any modifications made to the returned matrix will not affect the current matrix.
- returns
-
\Mat4The copy of the current matrix.
row¶
Returns the row at the given index.
Example usage:
$matrix = Mat4::fromArray([
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]
]);
// Get the first row
$row = $matrix->row(0);
// Expected result: Vec4(1.0, 2.0, 3.0, 4.0)
- arguments
-
int$indexThe index of the row to return.
- returns
-
\Vec4The row at the given index.
setRow¶
Sets the row at the given index to the given row
Example usage:
- arguments
-
int$indexThe index of the row to set.\Vec4$rowThe row to set.
- returns
-
void
col¶
Returns the column at the given index.
This method retrieves a column from the matrix at the specified index and returns it as a Vec4 object.
Example:
$matrix = Mat4::fromArray([
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0]
]);
$col = $matrix->col(0);
// Returns Vec4(1.0, 5.0, 9.0, 13.0)
- arguments
-
int$indexThe index of the column to return.
- returns
-
\Vec4The column at the given index.
setCol¶
Sets the column at the given index to the given column
// Create a new matrix and set the second column
$matrix = new Mat4();
$col = new Vec4(1, 2, 3, 4);
$matrix->setCol(1, $col);
- arguments
-
int$indexThe index of the column to set.\Vec4$colThe column to set.
- returns
-
void
lookAt¶
Sets the matrix to a lookAt matrix
The lookAt() method sets the matrix to a lookAt matrix based on the camera's position and orientation.
It takes three parameters: $eye, $center, and $up, which are all instances of the Vec3 class.
The $eye parameter indicates the position of the camera, while $center indicates the point
in space to look at. The $up parameter specifies the up vector, which determines the orientation of the camera.
Example usage:
// Create a new matrix
$matrix = new Mat4();
// Set the matrix to a lookAt matrix
$matrix->lookAt(
new Vec3(0, 0, 5), // eye
new Vec3(0, 0, 0), // center
new Vec3(0, 1, 0) // up
);
This will create a new matrix and set it to a lookAt matrix with the camera positioned at (0, 0, 5),
looking at the origin (0, 0, 0), with the up vector pointing in the positive y direction.
- arguments
-
\Vec3$eyeThe position of the camera.\Vec3$centerThe position to look at.\Vec3$upThe up vector.
- returns
-
void
perspective¶
Sets the matrix to a perspective matrix
- arguments
-
float$fovThe field of view.float$aspectThe aspect ratio.float$nearThe near plane.float$farThe far plane.
- returns
-
void
ortho¶
Sets the matrix to an orthographic matrix
function ortho(float $left, float $right, float $bottom, float $top, float $near, float $far) : void
- arguments
-
float$leftThe left plane.float$rightThe right plane.float$bottomThe bottom plane.float$topThe top plane.float$nearThe near plane.float$farThe far plane.
- returns
-
void
transpose¶
Tranposes the matrix
- returns
-
void
inverse¶
Inverts the current matrix
- returns
-
void
scale¶
Scale the matrix by the given vector
translate¶
Translates the matrix by the given vector
- arguments
-
\Vec3$vectorThe vector to translate by.
- returns
-
void
rotate¶
Rotates the matrix by the given angle around the given axis
- arguments
-
float$angleThe angle to rotate by.\Vec3$axisThe axis to rotate around.
- returns
-
void
determinant¶
Retruns the determinant of the matrix
- returns
-
floatThe determinant of the matrix.