Italyandrebuilds/simulating-black-hole
Real-time black hole simulation in C++ and OpenGL, GPU ray marching with Schwarzschild geodesics, gravitational lensing, accretion disk with Doppler beaming, and interactive 2D/3D visualization.
Featured achievements
The project's latest awards
#1 Repository of the week
#1 Repository of the week
#1 Repository of the week
Simulating a Black Hole
A real-time black hole simulation built from scratch in C++ and OpenGL, featuring gravitational lensing, relativistic ray tracing, and geodesic integration based on the Schwarzschild metric.
The simulation models Sagittarius A*, the supermassive black hole at the center of the Milky Way (mass ≈ 8.54 × 10³⁶ kg), and renders the visual effects of extreme spacetime curvature.
Features
- 3D GPU Black Hole — Real-time compute shader ray marching with RK4 geodesic integration, adaptive step size, accretion disk with Doppler beaming and gravitational redshift, procedural starfield background, N-body gravity, and dynamic resolution scaling
- 2D Gravitational Lensing — Interactive 2D visualization of light ray deflection around a Schwarzschild black hole using RK4 integration in polar coordinates, with click-to-spawn rays and fading trails
- CPU Ray Tracers — Two CPU-based reference implementations: a Whitted-style ray tracer and an OpenMP-parallelized geodesic ray tracer
- Spacetime Grid — Visual representation of spacetime curvature around massive objects
Project Structure
| File | Description |
|---|---|
black_hole.cpp | Main 3D GPU simulation — compute shader dispatching, camera controls, N-body physics, accretion disk, UBO management |
geodesic.comp | OpenGL 4.3 compute shader — GPU null geodesic integration (RK4), adaptive stepping, Doppler/redshift coloring, starfield |
grid.vert / grid.frag | Vertex and fragment shaders for the spacetime curvature grid |
2D_lensing.cpp | 2D gravitational lensing visualization with modern OpenGL (VAO/VBO), interactive ray spawning |
CPU-geodesic.cpp | CPU-based 3D geodesic ray tracer with OpenMP parallelization |
ray_tracing.cpp | CPU Whitted-style ray tracer (spheres, reflections, shadows) |
CMakeLists.txt | Build configuration with CMake and vcpkg integration |
vcpkg.json | Dependency manifest (GLFW, GLM, GLEW) |
Prerequisites
- C++17 compiler (MSVC, GCC, or Clang)
- CMake 3.21 or higher
- vcpkg package manager
- OpenGL 4.3 compatible GPU (for the 3D compute shader simulation)
- Visual Studio (recommended on Windows) or any compatible build system
Dependencies
Managed automatically via vcpkg:
Building
1. Clone the repository
git clone https://github.com/andrebuilds/simulating-black-hole.git
cd simulating-black-hole
2. Configure with CMake
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake
Replace <path-to-vcpkg> with your vcpkg installation path (e.g., C:/vcpkg).
vcpkg will automatically download and install all dependencies on the first run.
3. Build
Build a specific target:
# 3D GPU Black Hole (recommended)
cmake --build build --target BlackHole3D --config Release
# 2D Gravitational Lensing
cmake --build build --target BlackHole2D --config Release
Or build everything:
cmake --build build --config Release
4. Run
# 3D Simulation
./build/Release/BlackHole3D.exe
# 2D Lensing
./build/Release/BlackHole2D.exe
Note: Shader files (
.vert,.frag,.comp) are automatically copied to the output directory during the build.
Controls
BlackHole3D (3D GPU Simulation)
| Key | Action |
|---|---|
W / A / S / D | Move camera forward / left / backward / right |
| Mouse | Look around (free camera) |
G | Toggle N-body gravity on/off |
ESC | Close the window |
BlackHole2D (2D Lensing)
| Input | Action |
|---|---|
| Left Click | Spawn a new light ray at the cursor position |
Physics
The simulation solves null geodesics (light paths) in Schwarzschild spacetime. The Schwarzschild metric describes the geometry of spacetime around a non-rotating, uncharged black hole:
$$ds^2 = -\left(1 - \frac{r_s}{r}\right)c^2 dt^2 + \left(1 - \frac{r_s}{r}\right)^{-1} dr^2 + r^2 d\Omega^2$$
where $r_s = \frac{2GM}{c^2}$ is the Schwarzschild radius.
Light rays are traced by integrating the geodesic equations using the 4th-order Runge-Kutta (RK4) method. The GPU compute shader uses adaptive step sizes — smaller steps near the event horizon for accuracy, larger steps far away for performance.
Visual Effects
- Gravitational Lensing — Light bends around the black hole, distorting the background starfield
- Accretion Disk — Hot inner regions appear blue-white, cooler outer regions appear red-orange
- Doppler Beaming — The approaching side of the disk appears brighter (relativistic D³ boosting)
- Gravitational Redshift — Light escaping the gravitational well is redshifted
- Einstein Ring — Light from directly behind the black hole forms a ring around it
Technical Details
- Geodesic integration uses RK4 on both CPU and GPU
- The compute shader processes each pixel independently, making it massively parallel
- Dynamic resolution: lower resolution while the camera is moving, full resolution when stationary
- N-body gravity uses Newton's third law optimization (compute each pair once)
- Double precision (
double) is used for physics calculations to avoid floating-point artifacts near the event horizon
Developed by andrebuilds
Similar Projects
Discover other similar projects and AI models.
Non-spinning black hole simulation based on geodesics equation
A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West and Death Stranding 2.
High-performance C++ library for multiphysics and multibody dynamics simulations
Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
OpenTTD is an open source simulation game based upon Transport Tycoon Deluxe
An open source quadruped robot pet framework for developing Boston Dynamics-style four-legged robots that are perfect for STEM, coding & robotics education, IoT robotics applications, AI-enhanced robotics application services, research, and DIY robotics kit development.
Written in C++ and using SDL, The Powder Toy is a desktop version of the classic 'falling sand' physics sandbox, it simulates air pressure and velocity as well as heat.
The fastest and most memory efficient lattice Boltzmann CFD software, running on all GPUs and CPUs via OpenCL. Free for non-commercial use.