OpenGL 2D Blackhole Simulator v2

Build a 2D gravitational lensing simulator with OpenGL. Visualize light ray bending around a black hole using the Schwarzschild metric and numerical integration. (Updated version with geometric units and improved code organization)

~4 hours
16 steps
Download & Run Locally

Support Free C++ Education

Help us keep this platform free for everyone! Your support enables us to create more high-quality lessons, exercises, and interactive content.

Become a Patron

What You'll Build

An interactive 2D visualization showing light ray trajectories bending around a black hole. Watch as rays with different impact parameters either escape to infinity or spiral into the event horizon, demonstrating gravitational lensing in real-time.

Learning Objectives

  • Set up OpenGL development environment
  • Implement 2D rendering with OpenGL
  • Understand the Schwarzschild metric
  • Implement Runge-Kutta 4th order numerical integration
  • Visualize gravitational lensing in 2D

Ready to Start?

Start Project

Sign in to retain your progress

What's New in This Version

  • Geometric units (c=G=1) for cleaner equations, better numerical stability, and lower memory usage
  • Symplectic integration (Velocity Verlet) for improved energy conservation
  • Adaptive timestep for better performance far from the black hole
  • Improved Windows support, validation checks, better code consistency between steps

Project Steps

Step 1

OpenGL Setup - Part 1: CMake Configuration

Configure CMake to find and link OpenGL, GLFW, GLEW, and GLM libraries.

~20 min
Step 2

OpenGL Setup - Part 2: Window Creation

Create a GLFW window and initialize OpenGL context for 2D rendering.

~15 min
Step 3

OpenGL Setup - Part 3: Render Loop

Create the main render loop to keep the window responsive and ready for drawing.

~20 min
Step 4

2D Rendering - Part 1: Coordinate Systems

Set up a 2D coordinate system using orthographic projection to map world coordinates to screen pixels.

~20 min
Step 5

2D Rendering - Part 2: Filled Shapes

Learn to draw filled circles using trigonometry and GL_TRIANGLE_FAN for efficient rendering.

~20 min
Step 6

2D Rendering - Part 3: Outlines and Styling

Draw circle outlines with GL_LINE_LOOP and style shapes using colors and line widths.

~15 min
Step 7

2D Rendering - Part 4: Aspect Ratio Correction

Fix circle distortion by adjusting orthographic projection to match the window's aspect ratio.

~15 min
Step 8

Schwarzschild Physics - Part 1: Metric Basics

Implement the Schwarzschild metric and understand conserved quantities for light rays in curved spacetime.

~30 min
Step 9

Schwarzschild Physics - Part 2: RK4 Integration

Implement Runge-Kutta 4th order integration to trace light ray paths through curved spacetime.

~40 min
Step 10

Visualization - Part 1: Ray Paths

Draw light ray paths showing gravitational lensing using OpenGL lines.

~40 min
Step 11

Visualization - Part 2: Visual Enhancements

Add professional visual touches: background stars, photon sphere visualization, event horizon circle, and color-coded light rays.

~40 min
Step 12

Optimization - Part 1: Geometric Units

Convert from SI units to geometric units (c=G=1) for cleaner equations and better numerical stability.

~30 min
Step 13

Optimization - Part 2: Symplectic Integration

Replace RK4 with Velocity Verlet for better energy conservation and simpler code.

~25 min
Step 14

Optimization - Part 3: Adaptive Timestep

Implement adaptive timestep to improve performance far from the black hole while maintaining accuracy nearby.

~20 min
Step 15

Visualization - Part 3: Multiple Ray Scenarios

Implement three distinct ray scenarios (parallel, point source, and orbiting) with ~100 total rays to create a comprehensive, visually stunning gravitational lensing demonstration.

~22 min
Step 16

Project Organization

Refactor the single-file simulator into a well-organized multi-file C++ project with proper separation of concerns

~120 min

Local Setup Required

This advanced project requires external libraries and will compile code that you download and run locally.

Required Dependencies:
opengl glfw glew glm

Want to Know More?

Read the full write-up about this project and support our work on Patreon.

View on Patreon

Project Repository

See the final version of this project on GitHub. Please report any issues there.

View on GitHub

Ready to Start?

Start Project

Sign in to retain your progress