DirectXMath API Reference - Windows Graphics

Introduction to DirectXMath

The DirectXMath library is a set of common math types and functions used in DirectX development. It is designed to be fast, flexible, and easy to use, leveraging SIMD instructions for performance critical operations.

This API provides essential mathematical types like vectors (XMVECTOR), matrices (XMMATRIX), and quaternions (XMVECTOR with specific packing), along with functions for common operations such as:

Key Concepts

Core Data Types

XMVECTOR

A 4-component vector. Can represent points, directions, colors, or packed matrix rows.

Typically uses 128-bit SIMD registers.

XMMATRIX

A 4x4 matrix, typically represented as an array of four XMVECTOR objects, each representing a row.

Used for transformations.

XMFLOAT2, XMFLOAT3, XMFLOAT4

Structs for 2D, 3D, and 4D floating-point vectors with standard C++ alignment.

Useful for data structures not requiring SIMD optimization.

XMFLOAT4X4

A 4x4 matrix struct using standard C++ alignment.

Common Functions

The library is organized into functional categories. Here are a few examples:

Vector Operations

Matrix Transformations

Quaternion Operations

Example Usage (C++)

Here's a simple example demonstrating how to create a translation matrix and apply it to a vector:

#include <DirectXMath.h>
#include <iostream>

using namespace DirectX;

int main() {
    // Define a translation vector
    XMVECTOR translation = XMVectorSet(10.0f, 5.0f, -2.0f, 0.0f);

    // Create a translation matrix
    XMMATRIX translationMatrix = XMMatrixTranslationFromVector(translation);

    // Define a point (as a vector)
    XMVECTOR point = XMVectorSet(1.0f, 2.0f, 3.0f, 1.0f); // W=1 for a point

    // Apply the translation matrix to the point
    XMVECTOR transformedPoint = XMVector4Transform(point, translationMatrix);

    // Output the results (requires helper functions to print XMVECTOR)
    std::cout << "Original Point: (" << XMVectorGetX(point) << ", " << XMVectorGetY(point) << ", " << XMVectorGetZ(point) << ")" << std::endl;
    std::cout << "Translated Point: (" << XMVectorGetX(transformedPoint) << ", " << XMVectorGetY(transformedPoint) << ", " << XMVectorGetZ(transformedPoint) << ")" << std::endl;

    return 0;
}