OpenGL 2D Blackhole Simulator

Build a 2D gravitational lensing simulator with OpenGL. Visualize light ray bending around a black hole using the Schwarzschild metric and numerical integration.

~4 hours
13 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?

Sign in to retain your progress

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

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 13

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?

Sign in to retain your progress