Skip to content

nickscha/cdlod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cdlod

A C89 standard compliant, single header, nostdlib (no C Standard Library) Continuous Distance-Dependent Level of Detail (CDLOD).

For more information please look at the "cdlod.h" file or take a look at the "examples" or "tests" folder.

Warning

THIS PROJECT IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! USE THIS PROJECT AT YOUR OWN RISK!

Latest Release Downloads License C Standard nostdlib

Features

  • C89 compliant — portable and legacy-friendly
  • Single-header API — just include cdlod.h
  • nostdlib — no dependency on the C Standard Library
  • Minimal binary size — optimized for small executables
  • Cross-platform — Windows, Linux, MacOs
  • Strict compilation — built with aggressive warnings & safety checks

Quick Start

Download or clone cdlod.h and include it in your project.

#include "cdlod.h" /* Continuous Distance-Dependent Level of Detail */

/* (1) Define a height function for a given x and z coordinate */
float custom_height_function(float x, float z)
{
  (void) x;
  (void) y;

  /* Flat Plane (replace with perlin/simplex noise or other height algorithms) */
  return 0.0f;
}

int main() {
    /* (2) Define a memory buffer for the gneerated cdlod vertice and indices */
    #define VERTICES_CAPACITY 200000
    #define INDICES_CAPACITY 200000
    float vertices[VERTICES_CAPACITY];
    int indices[INDICES_CAPACITY];
    int vertices_count = 0;
    int indices_count  = 0;

    /* (3) Define when different lod ranges should be applied (distance to camera) */
    /*     This has to be sorted in an ascending order                             */
    /*     First = Highest Level of Detail                                         */
    float lod_ranges[] = {0.0f, 50.0f, 100.0f, 200.0f, 400.0f};
    float patch_size   = 64.0f;
    int grid_radius    = 1;
    float skirt_depth  = 5.0f;

    /* (4) Define an eye/camera position from which the CDLOD grid should be generated */
    float camera_position_x =  0.0f;
    float camera_position_y = 10.0f;
    float camera_position_z =  0.0f;

    float camera_front_x    =  0.0f;
    float camera_front_z    = -1.0f; /* For example -1.0f in right-hand-layout (e.g. OpenGL) is front facing */

    /* (5) Generate the CDLOD vertices and indices */
    cdlod(
        vertices, VERTICES_CAPACITY, &vertices_count,            /* Vertices data                                   */
        indices, INDICES_CAPACITY, &indices_count,               /* Indices data                                    */
        camera_position_x, camera_position_y, camera_position_z, /* Camera position                                 */
        camera_front_x, camera_front_z,                          /* Camera front facing vector                      */
        custom_height_function,                                  /* Y-Heightmap function                            */
        patch_size,                                              /* How large is each patch                         */
        5, lod_ranges,                                           /* Number of lod levels and the ranges             */
        grid_radius,                                             /* How big is the grid (1=3x3, 3=5x5 patches, ...) */
        skirt_depth
    );
 
    return 0;
}

Define max. lod levels

By default maximum 8 lod levels are supported. If you want to increase/decrease that amount you can either use the compile flag -DCDLOD_MAX_LODS=16 or define it before including the header.

#define CDLOD_MAX_LODS 16
#include "cdlod.h"

Benchmark Results

The cdlod_test.c measures cpu cycle counts and time in milliseconds for the cdlod function.

The following settings were used:

  • 5 LOD ranges
  • 64.0f per patch size
  • 9 grid radius (equals 19x19 patches)

This is captured on a Intel Core i7-7700HQ CPU @ 2.14 GHz with a single core.

It averages around 0.03 milliseconds which makes this algorithm suitable and render buget friendly for software rasterizers.

cdlod_test.c:119 [perf]
cdlod_test.c:119 [perf] +-------------------------------------------------------+-------------------------------------------------------+
cdlod_test.c:119 [perf] | cylces                                                | time_ms                                               |
cdlod_test.c:119 [perf] +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
cdlod_test.c:119 [perf] |         min |         max |         avg |         sum |         min |         max |         avg |         sum |
cdlod_test.c:119 [perf] +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
cdlod_test.c:119 [perf] |       91712 |      508728 |      106040 |  1060407695 |      0.0326 |      0.1812 |      0.0378 |    378.2178 |  10000 x cdlod
cdlod_test.c:119 [perf] +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+

The build script used for this can be found in tests/build.bat

cc -s -O2 -std=c89 -pedantic -Wall -Wextra -Werror -Wvla -Wconversion -Wdouble-promotion -Wsign-conversion -Wmissing-field-initializers -Wuninitialized -Winit-self -Wunused -Wunused-macros -Wunused-local-typedefs -o cdlod_test.exe cdlod_test.c

Run Example: nostdlib, freestsanding

In this repo you will find the "examples/cdlod_win32_nostdlib.c" with the corresponding "build.bat" file which creates an executable only linked to "kernel32" and is not using the C standard library and executes the program afterwards.

"nostdlib" Motivation & Purpose

nostdlib is a lightweight, minimalistic approach to C development that removes dependencies on the standard library. The motivation behind this project is to provide developers with greater control over their code by eliminating unnecessary overhead, reducing binary size, and enabling deployment in resource-constrained environments.

Many modern development environments rely heavily on the standard library, which, while convenient, introduces unnecessary bloat, security risks, and unpredictable dependencies. nostdlib aims to give developers fine-grained control over memory management, execution flow, and system calls by working directly with the underlying platform.

Benefits

Minimal overhead

By removing the standard library, nostdlib significantly reduces runtime overhead, allowing for faster execution and smaller binary sizes.

Increased security

Standard libraries often include unnecessary functions that increase the attack surface of an application. nostdlib mitigates security risks by removing unused and potentially vulnerable components.

Reduced binary size

Without linking to the standard library, binaries are smaller, making them ideal for embedded systems, bootloaders, and operating systems where storage is limited.

Enhanced performance

Direct control over system calls and memory management leads to performance gains by eliminating abstraction layers imposed by standard libraries.

Better portability

By relying only on fundamental system interfaces, nostdlib allows for easier porting across different platforms without worrying about standard library availability.

About

C89, single header, nostdlib continuous distance-dependent level of detail

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published