### 18 releases (4 breaking)

✓ Uses Rust 2018 edition

new 0.8.6 | Feb 18, 2020 |
---|---|

0.8.4 | Dec 17, 2019 |

0.8.3 | Nov 27, 2019 |

0.7.1 | Jul 8, 2019 |

#**5** in Game development

**4,620** downloads per month

Used in **6** crates

**MIT/Apache**

235KB

5.5K
SLoC

# glam

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

## Development status

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

## Features

- Only single precision floating point (

) arithmetic is supported`f32` - vectors:

,`Vec2`

,`Vec3``Vec4` - square matrices:

,`Mat2`

,`Mat3``Mat4` - a quaternion type:
`Quat`

### SIMD

The

, `Vec3`

and `Vec4`

types use SSE2 on x86/x86_64 architectures.
`Quat`

, `Mat2`

and `Mat3`

also use SSE2 for some functionality. Not everything
has a SIMD implementation yet.`Mat4`

Note that this does result in some wasted space in the case of

and `Vec3`

as the SIMD vector type is 16 bytes large and 16 byte aligned.`Mat3`

It is possible to opt out of using SIMD types for Vec3 and Mat3 storage with the

feature.`packed-vec3`

outperforms similar Rust libraries such as `glam`

,
`cgmath`

and others for common operations as tested by the
`nalgebra-glm`

project.`mathbench`

If you are more concerned with size than speed you can build glam with the
feature

enabled to disable SIMD usage.`scalar-math`

Due to the use of SIMD, vector elements may only be get and set via accessor
methods, e.g.

and `Vec3 ::`x

`(`

`)`

`Vec3``::`x_mut`(``)`

or `Vec3``::`set_x`(``)`

. If getting or
setting more than one element it is more efficient to convert from tuples or
arrays:`let` `(`x`,` y`,` z`)` `=` v`.``into``(``)``;`
`let` `[`x`,` y`,` z`]``:` `[``f32``;` `3``]` `=` v`.``into``(``)``;`

### Optional features

- for interoperating with other 3D math libraries`mint`

- implementations of`rand`

trait for all`Distribution`

types. This is primarily used for unit testing`glam`

- implementations of`serde`

and`Serialize`

for all`Deserialize`

types. Note that serialization should work between builds of`glam`

with and without SIMD enabled`glam`

### Feature gates

- disable using SIMD types for`packed-vec3`

and`Vec3`

storage. This avoids wasting space due to 16 byte alignment at the cost of some performance.`Mat3`

- compiles with SIMD support disabled`scalar-math`

- adds assertions which check the validity of parameters passed to`glam-assert`

to help catch runtime errors`glam`

## Conventions

### Column vectors

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. `glam`

. DirectX uses row vectors, OpenGL uses column vectors. There
are pros and cons to both.`v ' = Mv`

### Column-major order

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

### Co-ordinate system

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

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 (

) arithmetic is supported`f32` - All dependencies are optional (e.g.

,`mint`

and`rand`

)`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

for certain operations like inverse and multiplies`Mat4`

support`no-std`

support`wasm`

## 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

implementation`Vec3` - 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

is a play on the name of the popular C++ library`glam``glm`

## License

Licensed under either of

- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

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

contributors!`glam`

## Support

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

#### Dependencies

~2–305KB