Rust Logoglam

glam is a Rust-native, high-performance, and SIMD-accelerated linear algebra library specifically designed for game development, graphics, and other applications requiring efficient 2D, 3D, and 4D mathematical operations. It provides a comprehensive set of types including vectors (`Vec2`, `Vec3`, `Vec4`), matrices (`Mat2`, `Mat3`, `Mat4`), and quaternions (`Quat`), along with a rich collection of methods for common linear algebra operations.

A key feature of glam is its focus on performance through SIMD (Single Instruction, Multiple Data) acceleration. It leverages CPU extensions like SSE2/AVX2 on x86/x64 architectures and NEON on ARM, allowing it to perform operations on multiple data points simultaneously, leading to significant speedups for computationally intensive tasks. Despite its performance focus, glam also offers `no-std` compatibility, making it suitable for embedded systems or environments without the standard library.

glam is highly ergonomic and idiomatic Rust, providing a clean and intuitive API for tasks like vector arithmetic (addition, subtraction, multiplication, division), dot and cross products, normalization, matrix transformations (translation, rotation, scaling, perspective), and quaternion-based rotations and interpolations. Its design makes it an excellent choice for low-level graphics programming, physics simulations, and any Rust project where efficient vector and matrix math is crucial.

Example Code

# Cargo.toml
# [dependencies]
# glam = { version = "0.28", features = ["std"] } # Use the latest version available

// main.rs
use glam::{Vec3, Mat4, Quat};

fn main() {
    println!("--- glam Demonstration ---");

    // 1. Vector Operations
    let v1 = Vec3::new(1.0, 2.0, 3.0);
    let v2 = Vec3::new(4.0, 5.0, 6.0);

    println!("\nVectors:");
    println!("v1: {:?}", v1);
    println!("v2: {:?}", v2);

    let v_sum = v1 + v2;
    println!("v1 + v2: {:?}", v_sum);

    let v_dot = v1.dot(v2);
    println!("v1 . v2 (dot product): {}", v_dot);

    let v_cross = v1.cross(v2);
    println!("v1 x v2 (cross product): {:?}", v_cross);

    let v_normalized = v1.normalize();
    println!("v1 normalized: {:?}", v_normalized);
    println!("v1 normalized length: {}", v_normalized.length());

    // 2. Matrix Transformations
    let mut model_matrix = Mat4::IDENTITY; // Start with an identity matrix

    println!("\nMatrix Transformations:");
    println!("Initial Model Matrix (Identity): {:?}", model_matrix);

    // Translate along X-axis
    let translation = Vec3::new(5.0, 0.0, 0.0);
    model_matrix = model_matrix * Mat4::from_translation(translation);
    println!("After translating by {:?}: {:?}", translation, model_matrix);

    // Rotate around Y-axis by 90 degrees (PI/2 radians)
    let rotation_quat = Quat::from_rotation_y(std::f32::consts::FRAC_PI_2);
    model_matrix = model_matrix * Mat4::from_quat(rotation_quat);
    println!("After rotating 90 deg around Y: {:?}", model_matrix);

    // Scale by a factor
    let scale = Vec3::new(2.0, 2.0, 2.0);
    model_matrix = model_matrix * Mat4::from_scale(scale);
    println!("After scaling by {:?}: {:?}", scale, model_matrix);

    // Transform a point
    let point = Vec3::new(1.0, 1.0, 1.0);
    let transformed_point = model_matrix.transform_point3(point);
    println!("Transformed point {:?} by final matrix: {:?}", point, transformed_point);

    // 3. Quaternion for rotation
    let axis = Vec3::new(0.0, 1.0, 0.0); // Y-axis
    let angle_rad = std::f32::consts::FRAC_PI_4; // 45 degrees
    let rotation_quat = Quat::from_axis_angle(axis, angle_rad);

    println!("\nQuaternion Rotation:");
    println!("Rotation Quat (45 deg around Y): {:?}", rotation_quat);

    let initial_vector = Vec3::new(1.0, 0.0, 0.0); // Vector along X-axis
    let rotated_vector = rotation_quat * initial_vector;
    println!("Initial vector: {:?}", initial_vector);
    println!("Rotated vector: {:?}", rotated_vector);

    // Verify rotation: (1,0,0) rotated around Y by 45 deg becomes (cos(45), 0, sin(45))
    let expected_x = angle_rad.cos();
    let expected_z = angle_rad.sin();
    println!("Expected rotated vector (approx): Vec3({:.3}, 0.0, {:.3})", expected_x, expected_z);
}