Home

Awesome

<img src="docs/banner.png">

VMath - 2D and 3D vector math.

nimble install vmath

Github Actions

API reference

This library has no dependencies other than the Nim standard library.

Supports c, cpp and js backend.

About

Your one stop shop for vector math routines for 2d and 3d graphics.

Has vector functions for GLSL types:

TypeConstructorDescription
BVec#bvec#a vector of booleans
IVec#ivec#a vector of signed integers
UVec#uvec#a vector of unsigned integers
Vec#vec#a vector of single-precision floating-point numbers
DVec#dvec#a vector of double-precision floating-point numbers

You can use these constructors to make them:

NIMGLSL2349164
boolboolBVec2BVec3BVec4
int32intIVec2IVec3IVec4
uint32uintUVec2UVec3UVec4
float32floatVec2Vec3Vec4Mat3Mat4Quat
float64doubleDVec2DVec3DVec4DMat3DMat4DQuat

2D & 3D matrix math

You can combine and create 2d and 3d matrices by passing 2d or 3d vectors to matrix functions:

let mat2d = translate(vec2(10, 20)) * rotate(45.toRadians) * scale(vec2(2))
let mat3d = translate(vec3(10, 20, 0)) * rotateZ(45.toRadians) * scale(vec3(2))

Almost equal operator

Easily check if floating point numbers are close, very useful for tests:

1.0 ~= 1.0
vec2(1.0, 2.0) ~= vec2(1.0, 2.0)
dvec2(1) ~= dvec2(1)
quat(1.0, 2.0, 3.0, 4.0) ~= quat(1.0, 2.0, 3.0, 4.0)

Number functions

Angle functions

Vector and matrix representation and benchmarks.

C compilers seem to optimize different representations differently. This is very surprising for us and vmath has 3 different implementations:

name ............................... min time      avg time    std dv   runs
vmathObjBased ..................... 74.061 ms     74.297 ms    ±0.347   x100
vmathArrayBased ................... 89.498 ms     89.911 ms    ±1.019   x100
vmathObjArrayBased ................ 73.968 ms     74.292 ms    ±0.631   x100

Zmod - GLSL mod

GLSL uses a different type of float point mod. Because mod is a Nim keyword please use zmod when you need GLSL mod behavior.

Coordinate System

Right-hand z-forward coordinate system

This is the same system used in the GLTF file format.

glTF uses a right-handed coordinate system. glTF defines +Y as up, +Z as forward, and -X as right; the front of a glTF asset faces +Z.

glTF Spec 2.0

OpenGL matrix column-major notation.

9.005 For programming purposes, OpenGL matrices are 16-value arrays with base vectors laid out contiguously in memory. The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix, where indices are numbered from 1 to 16 as described in section 2.11.2 of the OpenGL 2.1 Specification.

Sadly, the use of column-major format in the spec and blue book has resulted in endless confusion in the OpenGL programming community. Column-major notation suggests that matrices are not laid out in memory as a programmer would expect.

OpenGL/GLSL/vmath vs Math/Specification notation:

        mat4([
          a, b, c, 0,              | a d g x |
          d, e, f, 0,              | b e h y |
          g, h, i, 0,              | c f i z |
          x, y, z, 1               | 0 0 0 1 |
        ])

1.x.x to 2.0.0 vmath breaking changes:

0.x.x to 1.0.0 vmath breaking changes: