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.
- 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
| 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) |
- 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
Managed automatically via vcpkg:
git clone https://github.com/andrebuilds/simulating-black-hole.git
cd simulating-black-holecmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmakeReplace <path-to-vcpkg> with your vcpkg installation path (e.g., C:/vcpkg).
vcpkg will automatically download and install all dependencies on the first run.
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 ReleaseOr build everything:
cmake --build build --config Release# 3D Simulation
./build/Release/BlackHole3D.exe
# 2D Lensing
./build/Release/BlackHole2D.exeNote: Shader files (
.vert,.frag,.comp) are automatically copied to the output directory during the build.
| 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 |
| Input | Action |
|---|---|
| Left Click | Spawn a new light ray at the cursor position |
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:
where
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.
- 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
- 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