v1.3.0Spatial LODBSD-2-Clause

Generic multi-scale spatial hierarchy, 6-plane frustum culling, and quadtree LOD engine in pure C. Canonical headers are <ttypt/areole.h> and <ttypt/areole_glsl.h>.

GitHub
git clone https://github.com/tty-pt/libareole

libareole logo

libareole

npm C99 BSD-2-Clause Spatial LOD Frustum Culling

Generic multi-scale spatial hierarchy, 6-plane frustum culling, and quadtree LOD engine in pure C. Canonical headers are <ttypt/areole.h> and <ttypt/areole_glsl.h>.

Pure C (C99/C11), zero-dependency, standalone spatial hierarchy engine. Designed as the generic multi-scale spatial index and frustum culling engine for real-time 3D, 2.5D, and 2D procedural terrain, planetary rendering, and continuous simulation.


1. Quickstart

Initialize a single-arena spatial hierarchy and query visible LOD chunks against a $4 \times 4$ camera ModelView-Projection (MVP) matrix:

#include <ttypt/areole.h>

/* 1. Create system at startup */
ar_config_t cfg = {
    .base_w = 160,
    .base_h = 128,
    .grid_dim = 32,
    .pool_capacity = 128,
    .max_active = 64,
    .max_levels = 16,
    .sampler = my_sampler,
    .user_ctx = NULL
};
ar_system_t *sys = ar_system_create_ex(&cfg);

/* 2. Query using exact camera MVP matrix each frame */
float modelview[16], proj[16], mvp[16];
ar_mat4_lookat(modelview, eye, target, up);
ar_mat4_perspective(proj, fov_rad, aspect, near_z, far_z);
ar_mat4_multiply(mvp, proj, modelview);

ar_cull_opts_t opts = {
    .z_min = -2000.0f,
    .z_max = 2000.0f,
    .sag_radius = 24000.0f,
    .focal_x = cam_x,
    .focal_y = cam_y,
    .margin = 150.0f,
    .target_quad_px = 2.0f,
    .viewport_w = 1920.0f,
    .viewport_h = 1080.0f
};

const ar_node_t *visible[64];
size_t count = ar_system_query_matrix(sys, world_id, mvp, &opts, visible, 64);

/* 3. Clean up on shutdown */
ar_system_destroy(sys);

Build & Link

cc app.c $(pkg-config --cflags --libs areole) -lm -o app

2. Architectural Highlights

  1. Domain-Agnostic Spatial Primitives (ar_ API):
  • Zero game-specific concepts (elevation, biomes, water, structures) in the core engine.
  • Generic sample points provide a 32-bit scalar displacement (int32_t val, Q28.4 fixed-point by default), packed 32-bit color/attributes (rgba), 32-bit bitmask (flags), and an 8-byte application payload (attr[8]).
  1. Runtime-Configurable Single-Arena Allocation & Bounded ar_sample_t Sub-Pool (ar_config_t):
  • Configurable grid resolution (grid_dim), pool capacity (pool_capacity), bounded full-sample sub-pool (samples_pool_capacity, samples_max_level), active query budget (max_active), hierarchy depth (max_levels), value scale (value_scale), and toroidal periods (period_x, period_y).
  • Entire ar_system_t instance, O(1) LRU node pool, contiguous vertex buffers, and spatial hash table are allocated in a single contiguous memory arena at initialization (ar_stats_t.arena_bytes) with zero runtime heap allocations during camera motion.
  1. Exact SAT 6-Plane Frustum Culling, Cached Z Tightening & 2D Software Hi-Z Occluder:
  • Extracts exact normalized Gribb-Hartmann frustum planes directly from any column-major $4 \times 4$ ModelView-Projection (MVP) matrix ($row_3 \pm row_0, row_3 \pm row_1, row_3 \pm row_2$) with exact SAT box testing and optional cached vertical bounds tightening (use_cached_bounds).
  • Optional 96ร—54 (8ร—8 tiled) conservative 2D software Hi-Z linear-W occluder (ar_occluder_t, ar_point_3d_fn, precomputed 4ร—4 occ_cell_min) culls terrain chunks hidden behind foreground ridges front-to-back.
  1. Pluggable Vertical, 3D Manifold Envelope & Horizon Culling Callbacks (ar_bounds_fn, ar_bounds_3d_fn, ar_cull_3d_fn):
  • Supports arbitrary 2.5D vertical envelopes (bounds_fn), full 3D non-Euclidean manifold bounding boxes (bounds_3d_fn), and manifold normal-cone/horizon visibility culling (cull_3d_fn) for curved or planetary surfaces.
  1. Coarse-First Binary Max-Heap Refinement, Continuous LOD Morphing, 4-Wide Row Batching & Async Population ($O(1)$ Active Chunks):
  • Decoupled into 6 focused compilation units (src/ar_math.c, ar_occ.c, ar_pool.c, ar_populate.c, ar_probe.c, ar_solver.c) with binary max-heap priority splitting, breadth-first budget coarsening, continuous per-item LOD morph factors (ar_draw_item_t.morph), vectorized scanline (ar_row_sampler_fn) and 4-wide row-coherent (ar_eval_row4_fn) sampling, per-frame populate caps (max_populates_per_frame), and non-blocking background population via native <ttypt/wq.h> (cfg.wq, AR_NODE_FLAG_DEFERRED) or a custom external async job-pool hook (ar_async_ops_t via ar_system_set_async_ops() and ar_system_poll_async() for WASM Web Worker pools).
  1. Configurable Point, Attribute & Height-Only Probes (ar_probe, ar_probe_val):
  • Interpolates scalar val, blends rgba color channels, and interpolates application attr[0..7] using configurable curves (AR_INTERP_SMOOTHSTEP, AR_INTERP_LINEAR, AR_INTERP_QUINTIC, AR_INTERP_NEAREST, or custom ar_interp_fn), plus zero-attribute height-only fast paths (ar_probe_val(), ar_probe_view_val()) and per-frame telemetry (ar_metrics_t, ar_system_get_metrics()).
  1. GLSL ES 3.0 Uniform & Shader Parity (<ttypt/areole_glsl.h>):
  • Standardized uniform layouts and GLSL functions (ar_apply_spherical_sag_ex, ar_glsl_ref_eval_vertex) with bit/float parity tested C reference implementations.

3. Public C API (<ttypt/areole.h>)

Core Types

#include <ttypt/areole.h>

/* Generic sample evaluated at each grid vertex */
typedef struct {
    int32_t  val;        /* Generic scalar displacement / field value (Q28.4) */
    uint32_t rgba;       /* Packed 32-bit color or RGBA attributes */
    uint32_t flags;      /* Generic bitmask flags (bits 0..29 user-defined; bit 30 AR_NODE_FLAG_DEFERRED; bit 31 AR_NODE_FLAG_SPARSE) */
    uint8_t  attr[8];    /* Generic 8-byte application payload (materials, blend) */
} ar_sample_t;

/* Runtime configuration for ar_system_create_ex(&cfg) */
typedef struct {
    int32_t           base_w, base_h;   /* Base level-0 spatial extent in world units */
    uint16_t          grid_dim;         /* Quads per chunk edge (required > 0) */
    uint16_t          pool_capacity;    /* Total cached chunk slots (required > 0) */
    uint16_t          max_active;       /* Max active chunks per query (required > 0) */
    uint8_t           max_levels;       /* Max hierarchy levels (required > 0) */
    uint32_t          default_rgba;     /* Fallback RGBA when sample->rgba == 0 */
    float             z_min, z_max;     /* Default vertical bounds envelope */
    float             target_quad_px;   /* Default screen-space quad subdivision threshold */
    float             margin;           /* Default world-space culling margin */
    float             value_scale;      /* Fixed-point scalar divisor (0 = 16.0f) */
    int32_t           period_x, period_y; /* Optional toroidal world periods */
    ar_sampler_fn     sampler;          /* Required vertex sampler callback */
    ar_row_sampler_fn row_sampler;      /* Optional vectorized scanline/row sampler */
    ar_eval_row4_fn   eval_row4;        /* Optional 4-wide row-coherent batch sampler for mixed reuse rows */
    ar_sparsity_fn    sparsity;         /* Optional discrete sparsity callback */
    void             *user_ctx;         /* Opaque context pointer passed to callbacks */
    uint32_t          max_populates_per_frame; /* Optional per-frame new-chunk populate budget */
    ar_bounds_fn      bounds_fn;        /* Optional vertical envelope callback */
    ar_bounds_3d_fn   bounds_3d_fn;     /* Optional 3D manifold AABB callback */
    ar_cull_3d_fn     cull_3d_fn;       /* Optional 3D manifold normal-cone/horizon culling callback */
    void             *bounds_ctx;       /* Opaque context passed to bounds/cull callbacks */
    struct stl_wq    *wq;               /* Optional stele workqueue for async background chunk population */
    uint16_t          samples_pool_capacity; /* Optional cap on nodes retaining full ar_sample_t[] */
    uint8_t           samples_max_level;     /* Max LOD level eligible for full ar_sample_t[] */
} ar_config_t;

/* Optional external async job-pool hook (e.g. WASM Web Worker stl_job pool) */
typedef struct {
    int32_t (*submit)(void *ctx, uint16_t world, uint8_t level,
                      int32_t ox, int32_t oy, int32_t w, int32_t h,
                      uint32_t grid_dim, uint32_t default_rgba);
    int (*poll_copy)(void *ctx, int32_t job_id,
                     int32_t *out_values, uint32_t *out_rgba, ar_sample_t *out_samples,
                     uint32_t vert_count,
                     int32_t *out_min_val, int32_t *out_max_val, uint32_t *out_flags);
    void (*discard)(void *ctx, int32_t job_id);
    void *ctx;
} ar_async_ops_t;

void ar_system_set_async_ops(ar_system_t *sys, const ar_async_ops_t *ops);
bool ar_system_poll_async(ar_system_t *sys);

4. Installation & Build

Prebuilt Packages

Prebuilt packages for Linux (APT / Alpine / Arch / Fedora-RHEL), macOS (Homebrew), Windows (winget / MSYS2), OpenBSD, and mobile static sysroots for Android (https://tty.pt/android/ โ€” arm64-v8a, armeabi-v7a, x86_64) and iOS (https://tty.pt/ios/ โ€” libareole.xcframework) are distributed via tty.pt packages. Use libareole as the package name.

Build from Source

Builds use the shared libstele makefile rules (include ttypt/include.mk, resolved via MAKEFLAGS += -I... / TTYPT_MK / pkg-config --variable=include_mk stele):

make              # builds lib/libareole.so and lib/libchunk.so
make wasm         # builds WebAssembly reactor (npm/dist/areole.wasm)
make test         # runs standalone C unit and GLSL parity test suite
make npm-test     # runs Node.js WebAssembly integration test suite
sudo make install # install headers and libraries to system

5. JavaScript & WebAssembly Bindings (@tty-pt/areole)

For browser, Node.js, and WebGL2 engines, libareole ships an isomorphic WebAssembly spatial hierarchy and culling package on npm:

npm config set @tty-pt:registry https://tty.pt/npm/
npm install @tty-pt/areole

See npm/README.md for the full TypeScript and WebAssembly API reference.


License

BSD 2-Clause License โ€” Copyright (c) 2026, tty-pt. See LICENSE.

Maintained by:tty-pt