Crate directx_math

Source
Expand description

§DirectX Math for Rust

A rust translation of DirectXMath, a SIMD linear algebra library for use in games and graphics apps.

Most functions and structs are exported at the crate root. Modules are organized according to the DirectXMath reference documentation. Additional DirectXMath documentation may be found in the previous version reference.

§Matrix multiplication order and memory layout

DirectXMath uses “row-major” matrices, row-vectors, and pre-multiplication. Handedness is determined by which function version is used (RH vs. LH). Multiplication order is the same as transformation order.

Row-major multiplication order:

MVP = Model * View * Projection;

Column-major multiplication order:

MVP = Projection * View * Model;

When laid out in memory, the data elements 0 through 15 of a “row-major” matrix with “row-vectors” are in the same order as an OpenGL “column-major” matrix with “column-vectors”. The translation components of a transformation matrix occupy elements 12, 13, and 14.

For example, XMMatrixTranslation(12.0, 13.0, 14.0) results in the following contiguous memory (e.g. [f32; 16]):

[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 12.0, 13.0, 14.0, 1.0]

§Example

use directx_math::*;

let eye = XMVectorSet(10.0, 10.0, 10.0, 0.0);
let focus = XMVectorSet(0.0, 0.0, 0.0, 0.0);
let up = XMVectorSet(0.0, 1.0, 0.0, 0.0);
let view = XMMatrixLookAtRH(eye, focus, up);

let fov_y = XMConvertToRadians(65.0);
let aspect_ratio = 1024.0 / 768.0;
let near_z = 0.1;
let far_z = 1000.0;
let projection = XMMatrixPerspectiveFovRH(fov_y, aspect_ratio, near_z, far_z);

let model = XMMatrixIdentity();

let mvp = XMMatrixMultiply(XMMatrixMultiply(model, &view), &projection);

// or use the unit struct with operator overloads
let model = XMMatrix(model);
let view = XMMatrix(view);
let projection = XMMatrix(projection);

// row-major multiplication order is the same as the transformation order
assert_eq!(XMMatrix(mvp), model * view * projection);

Modules§

Structs§

Enums§

Constants§

Traits§

Functions§

Type Aliases§

Unions§