This project implements a Marching Cubes algorithm using CUDA and OpenGL for visualization.
- CUDA Toolkit (11.0 or newer recommended, requires
cubwhich is included in modern Toolkits) - CMake (3.18+)
- C++ Compiler (MSVC, GCC, Clang)
- OpenGL libraries
- GLFW3 library
- GLAD (Used for OpenGL function loading)
-
Create a build directory:
mkdir build cd build -
Configure with CMake:
cmake ..
Note: If CMake cannot find GLAD, you may need to download
glad.candglad.hfrom https://glad.dav1d.de/ and place them in thesrcdirectory or adjustCMakeLists.txt. -
Build:
cmake --build . --config Release -
Run:
./Release/CudaSDF.exe
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.
The implementation follows a standard GPU Marching Cubes pipeline:
- Generate SDF: Evaluates a Signed Distance Function (sphere) on a grid.
- Count Vertices: Checks each cell to see if it intersects the surface and counts required vertices.
- Scan Vertices: Uses
cub::DeviceScanto compute prefix sums of vertex counts, determining the memory offset for each cell's vertices. - Create Vertices: Interpolates vertex positions along edges and stores them in a Vertex Buffer Object (VBO) mapped from OpenGL.
- Count Indices: Determines the number of triangle indices required for each cell.
- Scan Indices: Computes prefix sums for index offsets.
- Create Indices: Generates triangle topology indices into an Element Array Buffer (EBO) mapped from OpenGL.
- Render: Draws the mesh using OpenGL.
- 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