Native Haxe version of the fantastic GLM library (https://github.com/g-truc/glm). For those unaware of the GLM library, it is basically a toolset for using 2, 3, and 4 dimensional vectors and matrices, as well as quaternions. This tends to be rather useful when working with OpenGL, which is largely all about rasterizing primitives using vectors and matrices.
Issues, forks, and pull requests are gladly welcomed!
This library provides several classes (actually, abstracts over floating point arrays) which enable vector, matrix, and quaternion mathematical operations (Vec2
, Vec3
, Vec4
, Mat2
, Mat3
, Mat4
, and Quat
). The library also includes static utilities for generating transformation and projection matrices (GLM
).
The library includes static extensions and gives you the flexibility to instantiate or reuse objects as desired using destination arguments.
I decided to create this library for Haxe after seeing project after project write their own code for dealing with vectors, matrices, and quaternions. This has resulted in a lot of duplicated effort by developers who all seem to write their own classes to deal with this.
Hopefully the creation of this [fully cross-target and cross-platform] library, some of this duplicated effort can be minimized in the future.
API documentation is available here: http://fuzzywuzzie.github.io/haxe-glm/
// create a new perspective projection matrix
var P:Mat4 = GLM.perspective(
45 * Math.PI / 180, // field of view (radians)
640 / 480, // aspect ratio
0.1, 100, // near, far
new Mat4() // where to store the result
);
using glm.Quat;
// transform components
var pos:Vec3 = new Vec3(0, 0, 1.5);
var rot:Quat = new Quat().identity();
var sca:Vec3 = new Vec3(1, 1, 1);
var modelMatrix:Mat4 = new Mat4();
// ...
// update the model matrix
GLM.transform(pos, rot, sca, modelMatrix);
This library is written using abstract types over simple base object. When used on its own, the library uses basic classes containing each type's data. For example, Vec4s are implemented as:
class Vec4Base {
function new(){}
var x:Float;
var y:Float;
var z:Float;
var w:Float;
}
abstract Vec4(Vec4Base) {
// ...
}
Since there are a plethora of libraries out there that each have their own internal format, when the library detects a supported library, it changes to become an abstract over that library's type. For example, if Kha is detected, Vec4
becomes an abstract over Kha's FastVector4
type with implicit casts to and from FastVector4
s. This enables you to use the functionality in this library without taking performance hits converting to and from the types your library expects.
Implemented underlying types:
- Standalone
- Kha ('Fast' versions—(32-bit floats on cpp, 64-bit doubles everywhere else))
- Heaps
- lime
- OpenFL
- HaxeFlixel
- luxe
- ✔️—The calculation is currently available
- ❌—The calculation is planned but not yet available
Calculation | GLM | API |
---|---|---|
Lerp scalars | ✔️ | GLM.lerp(a:Float, b:Float, t:Float):Float |
Translation matrix | ✔️ | GLM.translate(translation:Vec3, dest:Mat4):Mat4 |
Rotation matrix | ✔️ | GLM.rotate(rotation:Quat, dest:Mat4):Mat4 |
Scale matrix | ✔️ | GLM.scale(scale:Quat, dest:Mat4):Mat4 |
Transformation / model matrix | ✔️ | GLM.transform(translation:Vec3, rotation:Quat, scale:Vec3, dest:Mat4):Mat4 |
Look-at matrix | ✔️ | GLM.lookAt(eye:Vec3, centre:Vec3, up:Vec3, dest:Mat4):Mat4 |
Perspective projection matrix | ✔️ | GLM.perspective(fovy:Float, aspectRatio:Float, near:Float, far:Float, dest:Mat4):Mat4 |
Orthographic projection matrix | ✔️ | GLM.orthographic(left:Float, right:Float, bottom:Float, top:Float, near:Float=-1, far:Float=1, dest:Mat4):Mat4 |
Frustum projection matrix | ✔️ | GLM.frustum(left:Float, right:Float, bottom:Float, top:Float, near:Float=-1, far:Float=1, dest:Mat4):Mat4 |
Calculation | Vec2 | Vec3 | Vec4 | API |
---|---|---|---|---|
Add vectors | ✔️ | ✔️ | ✔️ | VecX.addVec(a, b, dest) |
Add scalars | ✔️ | ✔️ | ✔️ | VecX.addScalar(a, s, dest) |
Array access | ✔️ | ✔️ | ✔️ | vec[index] |
Copy | ✔️ | ✔️ | ✔️ | VecX.copy(src, dest) |
Cross product | ✔️ | ✔️ | VecX.cross(a, b, dest) |
|
Distance | ✔️ | ✔️ | ✔️ | VecX.distance (a, b) |
Distance squared | ✔️ | ✔️ | ✔️ | VecX.distanceSquared (a, b) |
VecX × scalar op | ✔️ | ✔️ | ✔️ | v * c |
VecX ÷ scalar op | ✔️ | ✔️ | ✔️ | v / c |
VecX + scalar op | ✔️ | ✔️ | ✔️ | v + c |
VecX - scalar op | ✔️ | ✔️ | ✔️ | v - c |
Dot product | ✔️ | ✔️ | ✔️ | VecX.dot(a, b) |
Equals | ✔️ | ✔️ | ✔️ | VecX.equals(a, b) |
Length (magnitude) | ✔️ | ✔️ | ✔️ | vec.length() |
Length squared | ✔️ | ✔️ | ✔️ | vec.lengthSquared() |
Lerp | ✔️ | ✔️ | ✔️ | VecX.lerp(a, b, t, dest) |
Normalize | ✔️ | ✔️ | ✔️ | VecX.normalize(v, dest) |
String representation | ✔️ | ✔️ | ✔️ | vec.toString() |
To/from float array | ✔️ | ✔️ | ✔️ | vec.toFloatArray() , VecX.fromFloatArray(arr) |
Calculation | Quat | API |
---|---|---|
Axis-angle | ✔️ | Quat.axisAngle(axis, angle, dest) |
Copy | ✔️ | Quat.copy(src, dest) |
Conjugate | ✔️ | Quat.conjugate(q, dest) |
Dot product | ✔️ | Quat.dot(a, b) |
From Euler | ✔️ | Quat.fromEuler(x, y, z, dest) |
Get axis-angle | ❌ | |
Dot product | ✔️ | Quat.identity(dest) |
Invert | ✔️ | Quat.invert(q, dest) |
Length (magnitude) | ✔️ | quat.length() |
Length squared | ✔️ | quat.lengthSquared() |
Lerp | ✔️ | Quat.lerp(a, b, t, dest) |
SLerp | ✔️ | Quat.slerp(a, b, t, dest) |
Multiply w/ quats | ✔️ | Quat.multiplyQuats(a, b, dest) |
Multiply w/ scalar | ✔️ | Quat.multiplyScalar(a, s, dest) |
Quat × quat | ✔️ | a * b |
Quat × scalar | ✔️ | a * s |
Normalize | ✔️ | Quat.normalize(q, dest) |
String representation | ✔️ | quat.toString() |
To/from float array | ✔️ | quat.toFloatArray() , Quat.fromFloatArray(arr) |
Calculation | Mat2 | Mat3 | Mat4 | API |
---|---|---|---|---|
Add matrices | ❌ | ❌ | ❌ | |
Add scalars | ❌ | ❌ | ❌ | |
Adjugate | ❌ | ❌ | ❌ | |
Array access | ✔️ | ✔️ | ✔️ | mat[index] |
Copy | ✔️ | ✔️ | ✔️ | MatX.copy(src, dest) |
Determinant | ✔️ | ✔️ | ✔️ | MatX.determinant(src) |
Equals | ✔️ | ✔️ | ✔️ | MatX.equals(a, b) |
Identity | ✔️ | ✔️ | ✔️ | MatX.identity(dest) |
Invert | ✔️ | ✔️ | ✔️ | MatX.invert(src, dest) |
Multiply w/ matrices | ✔️ | ✔️ | ✔️ | MatX.multMat(a, b, dest) |
Multiply w/ vectors | ✔️ | ✔️ | ✔️ | MatX.multVec(m, v, dest) |
Multiply w/ scalars | ❌ | ❌ | ❌ | |
String representation | ✔️ | ✔️ | ✔️ | mat.toString() |
To/from float array | ✔️ | ✔️ | ✔️ | mat.toFloatArray() , MatX.fromFloatArray(arr) |
Transpose | ✔️ | ✔️ | ✔️ | MatX.transpose(src, dest) |