Skip to content
/ CudaSDF Public

SDF to mesh using marching cubes, cuda, BVH and space skipping

Notifications You must be signed in to change notification settings

mite51/CudaSDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CudaSDF

This project implements a Marching Cubes algorithm using CUDA and OpenGL for visualization.

Prerequisites

  • CUDA Toolkit (11.0 or newer recommended, requires cub which is included in modern Toolkits)
  • CMake (3.18+)
  • C++ Compiler (MSVC, GCC, Clang)
  • OpenGL libraries
  • GLFW3 library
  • GLAD (Used for OpenGL function loading)

Build Instructions

  1. Create a build directory:

    mkdir build
    cd build
  2. Configure with CMake:

    cmake ..

    Note: If CMake cannot find GLAD, you may need to download glad.c and glad.h from https://glad.dav1d.de/ and place them in the src directory or adjust CMakeLists.txt.

  3. Build:

    cmake --build . --config Release
  4. Run:

    ./Release/CudaSDF.exe

Project Structure

  • src/main.cpp: Main entry point, OpenGL setup, and rendering loop.
  • src/MarchingCubesKernels.cu: CUDA kernels for SDF generation, vertex counting/creation, and index generation.
  • src/Wrappers.cu: Wrapper functions to call CUDA kernels from C++.
  • src/Commons.cuh: Common data structures (SDFGrid) and helper functions.
  • src/MarchingCubesTables.cuh: Lookup tables for the Marching Cubes algorithm.

Implementation Details

The implementation follows a standard GPU Marching Cubes pipeline:

  1. Generate SDF: Evaluates a Signed Distance Function (sphere) on a grid.
  2. Count Vertices: Checks each cell to see if it intersects the surface and counts required vertices.
  3. Scan Vertices: Uses cub::DeviceScan to compute prefix sums of vertex counts, determining the memory offset for each cell's vertices.
  4. Create Vertices: Interpolates vertex positions along edges and stores them in a Vertex Buffer Object (VBO) mapped from OpenGL.
  5. Count Indices: Determines the number of triangle indices required for each cell.
  6. Scan Indices: Computes prefix sums for index offsets.
  7. Create Indices: Generates triangle topology indices into an Element Array Buffer (EBO) mapped from OpenGL.
  8. Render: Draws the mesh using OpenGL.

Controls

  • W: Toggle wireframe rendering
  • Space: Toggle mesh animation
  • C: Toggle view rotation
  • M: Switch mesh extraction to Marching Cubes
  • D: Switch mesh extraction to Dual Contouring
  • [ / ]: Decrease / increase Dual Contouring normal smoothing angle
  • B: Toggle Dual Contouring normal mode (combined vs dominant primitive gradient)
  • J: Cycle Dual Contouring vertex projection iterations (0–9)
  • K: Toggle Dual Contouring refinement normal mode
  • L: Toggle Dual Contouring adaptive quad diagonal selection
  • V: Toggle UV generation
  • U: Trigger UV unwrap
  • A: Trigger UV atlas packing
  • P: Trigger texture projection
  • T: Toggle texture array mode
  • N: Toggle vertex normals mode (accurate vs SDF gradient)
  • O: Export mesh to OBJ
  • Esc: Exit

demo.mp4

About

SDF to mesh using marching cubes, cuda, BVH and space skipping

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published