1
NateTheGrappler/ PhysicsWithOpenGL
A C++ OpenGL engine for simulating real-world physics
  • NateTheGrappler
NateTheGrappler
Physics With OpenGL
Feburary 20th, 2026|Github|Content
  • C++
  • OpenGL
  • Graphics
  • Physics Simulation
  • GLFW
  • ImGui

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

3D black hole

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:

  1. Clone the repository
  2. Open Physics_With_OpenGL.sln in Visual Studio
  3. Build and run

Dependencies (included in the repo): OpenGL, GLFW, GLAD, GLM, stb_image, FreeType, Dear ImGui 1.92.6

Roadmap


Be safe friend, don’t you dare go hollow.