#gamedev #math #matrix #vector #quaternion

glam

A simple and fast 3D math library for games and graphics

22 releases

✓ Uses Rust 2018 edition

new 0.9.2 Jul 9, 2020
0.9.0 Jun 28, 2020
0.8.6 Feb 18, 2020
0.8.4 Dec 17, 2019
0.7.1 Jul 8, 2019

#2 in Game dev

Download history 1395/week @ 2020-03-16 1783/week @ 2020-03-23 1667/week @ 2020-03-30 1936/week @ 2020-04-06 2287/week @ 2020-04-13 1522/week @ 2020-04-20 2950/week @ 2020-04-27 4133/week @ 2020-05-04 3246/week @ 2020-05-11 2422/week @ 2020-05-18 3350/week @ 2020-05-25 2758/week @ 2020-06-01 2890/week @ 2020-06-08 1803/week @ 2020-06-15 2392/week @ 2020-06-22 3487/week @ 2020-06-29

11,857 downloads per month
Used in 18 crates (11 directly)

MIT/Apache

260KB
6.5K SLoC

glam

Build Status Coverage Status Latest Version docs

A simple and fast 3D math library for games and graphics.

Development status

glam is in alpha stage. Minimal base functionality has been implemented and the look and feel of the API has solidified.

Features

  • Only single precision floating point (f32) arithmetic is supported
  • vectors: Vec2, Vec3, Vec3A Vec4
  • square matrices: Mat2, Mat3, Mat4
  • a quaternion type: Quat

SIMD

The Vec3A, Vec4 and Quat types use SSE2 on x86/x86_64 architectures. Mat2, Mat3 and Mat4 also use SSE2 for some functionality. Not everything has a SIMD implementation yet.

Note that this does result in some wasted space in the case of Vec3A as the SIMD vector type is 16 bytes large and 16 byte aligned.

glam outperforms similar Rust libraries such as cgmath, nalgebra-glm and others for common operations as tested by the mathbench project.

Due to the use of SIMD, vector elements may only be get and set via accessor methods, e.g. Vec4::x() and Vec4::x_mut() or Vec4::set_x(). If getting or setting more than one element it is more efficient to convert from tuples or arrays:

let (x, y, z, w) = v.into();
let [x, y, z, w]: [f32; 4] = v.into();

Optional features

  • mint - for interoperating with other 3D math libraries
  • rand - implementations of Distribution trait for all glam types. This is primarily used for unit testing
  • serde - implementations of Serialize and Deserialize for all glam types. Note that serialization should work between builds of glam with and without SIMD enabled

Feature gates

  • scalar-math - compiles with SIMD support disabled
  • debug-glam-assert - adds assertions in debug builds which check the validity of parameters passed to glam to help catch runtime errors
  • glam-assert - adds validation assertions to all builds

Conventions

Column vectors

glam interprets vectors as column matrices (also known as "column vectors") meaning when transforming a vector with a matrix the matrix goes on the left, e.g. v' = Mv. DirectX uses row vectors, OpenGL uses column vectors. There are pros and cons to both.

Column-major order

Matrices are stored in column major format. Each column vector is stored in contiguous memory.

Co-ordinate system

glam is co-ordinate system agnostic and intends to support both right handed and left handed conventions.

Rotations follow the left-hand rule.

Design Philosophy

The design of this library is guided by a desire for simplicity and good performance.

  • No traits or generics for simplicity of implementation and usage
  • Only single precision floating point (f32) arithmetic is supported
  • All dependencies are optional (e.g. mint, rand and serde)
  • Follows the Rust API Guidelines where possible
  • Aiming for 100% test coverage
  • Common functionality is benchmarked using Criterion.rs

Future work

  • Experiment with a using a 4x3 matrix as a 3D transform type that can be more efficient than Mat4 for certain operations like inverse and multiplies
  • no-std support
  • wasm support

Inspirations

There were many inspirations for the interface and internals of glam from the Rust and C++ worlds. In particular:

  • How to write a maths library in 2016 inspired the initial Vec3 implementation
  • Realtime Math - header only C++11 with SSE and NEON SIMD intrinsic support
  • DirectXMath - header only SIMD C++ linear algebra library for use in games and graphics apps
  • glam is a play on the name of the popular C++ library glm

License

Licensed under either of

at your option.

Contribution

Contributions in any form (issues, pull requests, etc.) to this project must adhere to Rust's Code of Conduct.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Thank you to all of the glam contributors!

Support

If you are interested in contributing or have a request or suggestion create an issue on github.

Dependencies

~0–285KB