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.
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.
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
Project Steps
OpenGL Setup - Part 1: CMake Configuration
Configure CMake to find and link OpenGL, GLFW, GLEW, and GLM libraries.
OpenGL Setup - Part 2: Window Creation
Create a GLFW window and initialize OpenGL context for 2D rendering.
OpenGL Setup - Part 3: Render Loop
Create the main render loop to keep the window responsive and ready for drawing.
2D Rendering - Part 1: Coordinate Systems
Set up a 2D coordinate system using orthographic projection to map world coordinates to screen pixels.
2D Rendering - Part 2: Filled Shapes
Learn to draw filled circles using trigonometry and GL_TRIANGLE_FAN for efficient rendering.
2D Rendering - Part 3: Outlines and Styling
Draw circle outlines with GL_LINE_LOOP and style shapes using colors and line widths.
2D Rendering - Part 4: Aspect Ratio Correction
Fix circle distortion by adjusting orthographic projection to match the window's aspect ratio.
Schwarzschild Physics - Part 1: Metric Basics
Implement the Schwarzschild metric and understand conserved quantities for light rays in curved spacetime.
Schwarzschild Physics - Part 2: RK4 Integration
Implement Runge-Kutta 4th order integration to trace light ray paths through curved spacetime.
Visualization - Part 1: Ray Paths
Draw light ray paths showing gravitational lensing using OpenGL lines.
Visualization - Part 2: Visual Enhancements
Add professional visual touches: background stars, photon sphere visualization, event horizon circle, and color-coded light rays.
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.
Project Organization
Refactor the single-file simulator into a well-organized multi-file C++ project with proper separation of concerns
Local Setup Required
This advanced project requires external libraries and will compile code that you download and run locally.
Want to Know More?
Read the full write-up about this project and support our work on Patreon.
View on PatreonProject Repository
See the final version of this project on GitHub. Please report any issues there.
View on GitHub