Add Matrix4.createPerspective() (#1681)

* add createPerspective

I didn't find a createPerspective function so I tried to make one, but I don't really know if that will work.

* forgot something

* add doc

* I hope that's correct 

added zero to one mode, and -one to one mode variants

* fix missing import

* I keep forgetting shit

* change to exceptions

* Update Matrix4.hx

* remove `new`

* remove period

* final changes

* Follow formatting conventions.

* Avoid using `Math.pow()`.

Haxe is unable to optimize this function away. Also, it seems to be unable to return numbers smaller than 1e-7, so that's what I used in its place. Smaller constants can work, you just can't get them from `Math.pow()`.

* Fill in missing entries.

As per all the other "create" functions.

* Use guard clause for better readability.

* Fix duplicate assignment.

* Make `createPerspective()` public.

---------

Co-authored-by: player-03 <player3.14@gmail.com>
This commit is contained in:
Beshoy Kamel
2024-05-27 22:36:44 +03:00
committed by GitHub
parent d0b1f6b0df
commit eabb10efbf

View File

@@ -378,6 +378,46 @@ abstract Matrix4(Float32Array) from Float32Array to Float32Array
this[14] = -(zNear + zFar) * sz;
this[15] = 1;
}
/**
Initializes this matrix with values for a perspective projection
@param fov The field of view
@param aspect The aspect ratio
@param zNear The near depth-clipping plane position
@param zFar The far depth-clipping plane position
**/
public function createPerspective(fov:Float, aspect:Float, zNear:Float, zFar:Float):Void
{
if (aspect > -0.0000001 && aspect < 0.0000001)
{
throw "Aspect ratio may not be 0";
}
var top = fov * zNear;
var bottom = -top;
var right = top * aspect;
var left = -right;
this[0] = 2.0 * zNear / (right - left);
this[1] = 0;
this[2] = 0;
this[3] = 0;
this[4] = 0;
this[5] = 2.0 * zNear / (top - bottom);
this[6] = 0;
this[7] = 0;
this[8] = (right + left) / (right - left);
this[9] = (top + bottom) / (top - bottom);
this[10] = -(zFar + zNear) / (zFar - zNear);
this[11] = -1.0;
this[12] = 0;
this[13] = 0;
this[14] = -2 * zFar * zNear / (zFar - zNear);
this[15] = 1;
}
/**
* Returns the transformation matrix's translation, rotation, and scale settings as a Vector of three Vector4 objects.