Home

Awesome

zalgebra

CI <br/> <br/> Linear algebra library for games and computer graphics.

The goal is to become as complete and useful as the Unity one. I'm currently using it for my projects and will continue to update it as new needs are coming.

If you would like to contribute, don't hesitate! :)

Note: The main branch is for the latest Zig release (0.13.x), use latest for latest master changements

Examples

Example of usage is located at example/.

const za = @import("zalgebra");
const Vec3 = za.Vec3;
const Mat4 = za.Mat4;

pub fn main () void {
  const projection = za.perspective(45.0, 800.0 / 600.0, 0.1, 100.0);
  const view = za.lookAt(Vec3.new(0.0, 0.0, -3.), Vec3.zero(), Vec3.up());
  const model = Mat4.fromTranslate(Vec3.new(0.2, 0.5, 0.0));

  const mvp = Mat4.mul(projection, view.mul(model));
  mvp.debugPrint();
}

Quick reference

Aliases

TypeDescription
Vec2Two dimensional vector for f32
Vec2_f64Two dimensional vector for f64
Vec2_i32Two dimensional vector for i32
Vec2_usizeTwo dimensional vector for usize
Vec3Three dimensional vector for f32
Vec3_f64Three dimensional vector for f64
Vec3_i32Three dimensional vector for i32
Vec3_usizeThree dimensional vector for usize
Vec4Four dimensional vector for f32
Vec4_f64Four dimensional vector for f64
Vec4_i32Four dimensional vector for i32
Vec4_usizeFour dimensional vector for usize
Mat33x3 matrix for f32
Mat3_f643x3 matrix for f64
Mat44x4 matrix for f32
Mat4_f644x4 matrix for f64
QuatQuaternion for f32
Quat_f64Quaternion for f64
perspectivePerspective function for f32 4x4 mat4
orthographicOrthographic function for f32 4x4 mat4
lookAtLookAt function for f32 4x4 mat4

Vectors

MethodsDescription
newConstruct a vector from 2 to 4 components
xReturn first component
yReturn second component
zReturn third component (only for vec3, vec4)
wReturn fourth component (only for vec4)
atReturn component from given index
setSet all components to the same given value
negateScale all components by -1
castCast a type to another type
fromSliceConstruct new vectors from slice
zeroShorthand for (0, 0, 0)
oneShorthand for (1, 1, 1)
upShorthand for (0, 1, 0)
downShorthand for (0, -1, 0)
rightShorthand for (1, 0, 0)
leftShorthand for (-1, 0, 0)
forwardShorthand for (0, 0, 1) (only for vec3 and vec4)
backShorthand for (0, 0, -1) (only for vec3 and vec4)
toArrayReturn an array of same size.
getAngleReturn angle in degrees between two vectors (only for vec2 and vec3)
rotateRotate vector by angle (in degrees)
lengthReturn the magnitude of the current vector
distanceReturn the distance between two points
normConstruct a new normalized vector based on the given one
eqlReturn true if two vectors are equals
subConstruct new vector resulting from the substraction between two vectors
addConstruct new vector resulting from the addition between two vectors
mulConstruct new vector resulting from the multiplication between two vectors
scaleConstruct new vector after multiplying each components by a given scalar
crossConstruct the cross product (as vector) from two vectors (only for vec3)
dotReturn the dot product between two vectors
lerpLinear interpolation between two vectors
minConstruct vector from the min components between two vectors
maxConstruct vector from the max components between two vectors

Matrices

Note: All matrices are column-major.

MethodsDescription
identityConstruct an identity matrix
setSet all matrix values to given value
fromSliceConstruct new matrix from given slice of data
getDataReturn a pointer to the inner data
transposeReturn the transpose matrix
negateScale all components by -1
castCast a type to another type
eqlReturn true if two matrices are equals
mulByVec4Multiply a given vec4 by matrix (only for mat4)
fromTranslateConstruct a translation matrix
translateConstruct a translation from the given matrix according to given axis (vec3)
fromRotationConstruct a rotation matrix
fromEulerAnglesConstruct a rotation matrix from pitch/yaw/roll in degrees (X _ Y _ Z)
rotateConstruct a rotation from the given matrix according to given axis (vec3)
fromScaleConstruct a scale matrix
scaleConstruct a scale from the given matrix according to given axis (vec3)
extractTranslationReturn a vector with proper translation
orthoNormalizeOrtho normalize the given matrix.
extractEulerAnglesReturn a vector with Euler angles in degrees (pitch/yaw/roll)
extractScaleReturn a vector with proper scale
perspectiveConstruct a perspective matrix from given fovy, aspect ratio, near/far inputs
perspectiveReversedZConstruct a perspective matrix with reverse Z and infinite far plane.
orthographicConstruct an orthographic matrix from given left, right, bottom, top, near/far inputs
lookAtConstruct a right-handed lookAt matrix from given position (eye) and target
mulMultiply two matrices
invInverse the given matrix
recomposeReturn mat4 matrix from given translation, rotation and scale components
decomposeReturn components translation, rotation and scale from given 4x4 matrix.
debugPrintPrint the matrix data for debug purpose

Quaternions

MethodsDescription
newConstruct new quat from given floats
identityConstruct quat as (1, 0, 0, 0)
setSet all components to the same given value
castCast a type to another type
fromSliceConstruct new quaternion from slice
fromVec3Construct quaternion from vec3
eqlReturn true if two quaternions are equal
normNormalize given quaternion
lengthReturn the magniture of the given quaternion
invConstruct inverse quaternion
subConstruct quaternion resulting from the subtraction of two given ones
addConstruct quaternion resulting from the addition of two given ones
mulConstruct quaternion resulting from the multiplication of two given ones
scaleConstruct new quaternion resulting from the multiplication of all components by a given scalar
dotReturn the dot product between two quaternions
toMat4Convert given quat to rotation 4x4 matrix
fromEulerAnglesConstruct quaternion from Euler angles
fromAxisConstruct quat from angle around specified axis
extractAxisAnglesGet the rotation angle and axis for a given quaternion
extractRotationGet euler angles from given quaternion
rotateVecRotate given vector

Utilities

MethodsDescription
toRadiansConvert degrees to radians
toDegreesConvert radians to degrees
lerpLinear interpolation between two floats

Contributing to the project

Don’t be shy about shooting any questions you may have. If you are a beginner/junior, don’t hesitate, I will always encourage you. It’s a safe place here. Also, I would be very happy to receive any kind of pull requests, you will have (at least) some feedback/guidance rapidly.

Behind screens, there are human beings, living any sort of story. So be always kind and respectful, because we all sheer to learn new things.

Thanks

This project is inspired by Handmade Math, nalgebra and Unity.