Black Hole Simulation
Introduction
This is a real-time black hole renderer I built from scratch with C++, SFML, and GLSL.
How it works on a high level: every pixel is treated like a ray of light, which is stepped through curve spacetime by numerically integrating the appropriate equations. Depending on where the ray ends up (background stars or the accretion disk), that pixel gets its final color. Rays are processed in parallel.
What I built:
- A custom GLSL ray-marching setup
- RK4 integration with adaptive step sizing
- C++ with SFML rendering pipeline
- A lot of performance tuning and so so much shader debugging
Main goal was learning low-level graphics and numerical simulation in a hands-on way, and this is by on means a very scientifically accurate model. The black hole is not to scale, as the equation I used was dimensionless and the accretion ring coloring was decided by a random inner temperature that made it look cool.
Screenshots
The resolution is low, but these were run on a mid-tier laptop from 2017 without a dedicated GPU.

- Added background stars
- Made disk color more realistic: calculate temperature -> approximate black body radiation -> color
- More realistic disk sizing, note that the photon ring is now visible! I’m not sure what that very thin ring inside the photon ring is, and it might be a bug, but that’s for future me to figure out.
- Added adaptive step sizing and other optimizations - Fixed object edge rendering for arbitrary step size - Added prettier colors and a grid to see how spacetime curves, and it’s very clearly showing gravitational lensing

- Implementing RK4 for the null geodesic equations in 3D.
- Parallelized everything via fragment shader, which was quite difficult, as debugging information could only be encoded through pixel color. I’ve since learned that I should have used a compute shader and passed to a simple fragment shader, which would have been easier to debug and manage.

- Derived the null geodesic equations in 2D via Lagrangian, here I learned the basics of general relativity.
- Implemented RK4 for numerically solving said equations.

- Built basic euclidean raymarching engine.
- More of learning how to program in C++.

- Drew a circle!
- Learned the very basics of C++, SFML, and GLSL.
Future work:
- A more detailed writeup of the equation derivations and algorithm details is in process.
- The center of the bh has trouble rendering bc parameterizing with phi, I’ll try to minimize this by adjusting the dynamic step size to account for starting angle
- If you move, the camera turns slightly on its own, because I’ve yet to implement parallel transport of the camera directions, but this is in progress.
- Accretion disk adjustments:
- Could use better boundary detection.
- Its shape should be flared out a little for more realism.
- Could add doppler effect coloring.