RepositoryC++ITItaly
andrebuilds

andrebuilds/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.

3stars
0forks
0issues
1views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: accretion-disk

#1 Repository of the week

1
TINIX TRENDING·Topic: black-hole

#1 Repository of the week

1
TINIX TRENDING·Topic: compute-shader

#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.

OpenGL C++17 License

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

FileDescription
black_hole.cppMain 3D GPU simulation — compute shader dispatching, camera controls, N-body physics, accretion disk, UBO management
geodesic.compOpenGL 4.3 compute shader — GPU null geodesic integration (RK4), adaptive stepping, Doppler/redshift coloring, starfield
grid.vert / grid.fragVertex and fragment shaders for the spacetime curvature grid
2D_lensing.cpp2D gravitational lensing visualization with modern OpenGL (VAO/VBO), interactive ray spawning
CPU-geodesic.cppCPU-based 3D geodesic ray tracer with OpenMP parallelization
ray_tracing.cppCPU Whitted-style ray tracer (spheres, reflections, shadows)
CMakeLists.txtBuild configuration with CMake and vcpkg integration
vcpkg.jsonDependency 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:

  • GLFW — Windowing and input
  • GLEW — OpenGL extension loading
  • GLM — Mathematics library

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)

KeyAction
W / A / S / DMove camera forward / left / backward / right
MouseLook around (free camera)
GToggle N-body gravity on/off
ESCClose the window

BlackHole2D (2D Lensing)

InputAction
Left ClickSpawn 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.

RepositoryPython
Python-simulation/Black-hole-simulation-using-python

Non-spinning black hole simulation based on geodesics equation

146
Dec 5, 2023
RepositoryC++
jrouwe/JoltPhysics

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.

12.7k
Dec 3, 2026
RepositoryC++
projectchrono/chrono

High-performance C++ library for multiphysics and multibody dynamics simulations

3.2k
Nov 25, 2026
RepositoryC++
bulletphysics/bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

14.9k
Oct 9, 2026
RepositoryC++
ange-yaghi/engine-sim

Combustion engine simulator that generates realistic audio.

9.7k
Jun 17, 2024
RepositoryC++
OpenTTD/OpenTTD

OpenTTD is an open source simulation game based upon Transport Tycoon Deluxe

8.5k
Nov 30, 2026
RepositoryC++
PetoiCamp/OpenCat-Quadruped-Robot

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.

5.6k
Nov 18, 2026
RepositoryC++
The-Powder-Toy/The-Powder-Toy

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.

5.4k
Nov 21, 2026
RepositoryC++
ProjectPhysX/FluidX3D

The fastest and most memory efficient lattice Boltzmann CFD software, running on all GPUs and CPUs via OpenCL. Free for non-commercial use.

5.4k
Nov 26, 2026