Physics simulations in C++ and OpenGL
Physics With OpenGL is a small engine written in C++ on top of raw OpenGL, built to simulate real physical phenomena one scene at a time. The first two simulations are black holes: a 2D scene where you fire beams of light and watch them bend or get swallowed, and a 3D scene that ray-marches every pixel to render gravitational lensing around a black hole with an accretion disk. While this project has not been touched in a long time, if I do ever find the time I do plan on targeting other projects like water physics or solar system physics.
Table of Contents
The Simulations
2D black holes
- Fire light in three ways: a sheet of parallel rays from any edge, a circular burst from a point, or a single ray you aim by dragging
- Pick real black holes from a preset list (Messier 87, Sagittarius A*, TON 618, Cygnus X-1, Gaia BH1). Each one’s Schwarzschild radius is computed from its actual mass with rs = 2GM/c²
- Place several black holes at once and watch rays weave between them
- Tune the speed of light and gravitational strength live from an ImGui panel
- Every ray leaves a glowing trail so you can see its full path
3D black hole
- A full-screen ray-marching shader traces a ray for every pixel, bending each one toward the black hole before sampling a starfield, which produces real-time gravitational lensing
- An accretion disk rendered around the event horizon
- A background of 2,000 stars distributed evenly over a sphere
- A warped spacetime grid that visualizes the gravity well
- A free-flying 3D camera, switchable with a static view
How the Physics Works
Polar mode (2D, one black hole). Each ray carries its position in polar coordinates around the black hole, (r, φ), plus how fast each is changing. Every frame, the engine evaluates a simplified form of the equations for how light moves in Schwarzschild spacetime, and advances the ray with a 4th-order Runge–Kutta (RK4) integrator. RK4 samples the motion at four points across each step and takes a weighted average, so rays stay accurate on tight orbits near the photon sphere, where a simple Euler step would drift badly.
Cartesian mode (2D, any number of black holes). Polar coordinates only work around a single center, so for multiple black holes each ray instead gets an acceleration summed from every black hole, also integrated with RK4. After each step the velocity is renormalized, because light always moves at c; only its direction changes. This mode is a visual approximation rather than exact general relativity.
3D lensing. The fragment shader starts a ray at the camera for each pixel and marches it forward up to 128 steps. Each step nudges the ray’s direction toward the black hole, more strongly the closer it gets, using adaptive step sizes: large steps in empty space, small steps near the event horizon where accuracy matters. Rays that fall inside the horizon return black; everything else samples the starfield cubemap in its final, bent direction. This is also an approximation tuned to look right, not a full geodesic solver.
Engine Architecture
Engine. Owns the window, the main loop, the renderer and the asset manager, plus a map of named scenes it can create, switch between and delete.
Scenes. Each simulation is a scene deriving from an abstract Scene class that receives the engine in its constructor. A scene runs its own physics, decides what to draw, owns its camera, and handles its own input.
Renderer. Created once by the engine, because it holds all the GPU-side data: shader programs, plus vertex and index buffers for every shape (rects, cubes, spheres, triangles). Scenes call it with a position, color, rotation and texture, and it sets the uniforms, picks the shader and issues the draw.
Camera. An abstract base with two implementations: a free-flying 3D camera and a fixed 2D orthographic one. Each updates the renderer’s view and projection matrices, and a scene can switch between them.
Assets. A map from names to textures. Textures load per scene, but once loaded they’re kept, so other scenes reuse them without reloading.
Actions. GLFW reports keys as integers. Each scene keeps its own map from key codes to action names, so the same key can mean different things in different scenes. The engine catches every key event, asks the current scene whether it has an action registered for that key, and if so sends it the action name along with whether the key was pressed or released.
Controls
| Key | Action |
|---|---|
← / → |
Rotate the menu carousel |
Enter |
Open the selected simulation / return to the menu |
C |
Switch camera |
P |
Pause (3D) |
| Mouse | Fire rays, using the mode picked in the panel (2D) |
Building From Source
Windows only for now:
- Clone the repository
- Open
Physics_With_OpenGL.slnin Visual Studio - Build and run
Dependencies (included in the repo): OpenGL, GLFW, GLAD, GLM, stb_image, FreeType, Dear ImGui 1.92.6
Roadmap
- 2D black holes
- 3D black hole
- 2D and 3D gravity and solar systems
- Cloth and rope physics
- Sand and granular materials
- Balls in a shakeable box
- Pendulums and springs
- Waves
- Fluid simulation
Be safe friend, don’t you dare go hollow.