A real-time 2D ray casting visualization built with SDL2 that demonstrates light ray propagation and shadow casting.
This application creates an interactive visualization where rays are cast from a light source (controllable circle) and interact with obstacles to create realistic shadow effects. The program uses mathematical ray-circle intersection algorithms to compute accurate light behavior.
- Real-time Raytracing: 100 rays cast simultaneously from a light source
- Interactive Light Source: Mouse-controlled light positioning
- Shadow Casting: Realistic shadows created by circular obstacles
- Smooth Rendering: Uses Bresenham's line algorithm for efficient ray drawing
- Mathematical Precision: Accurate ray-geometry intersection calculations
- SDL2: Graphics rendering and event handling
- C: Core implementation language
- Mathematical Libraries:
math.hfor trigonometric calculations
The project is structured as a modular C application:
- Ray Casting Engine:
limits_to_ray.c/h- Handles ray-geometry intersections - Line Drawing:
line_drawing_alg.c/h- Implements Bresenham's algorithm - Data Structures:
structs.h- Defines Ray and Circle structures - Main Loop:
main.c- SDL2 event loop and rendering
- Ray-Circle Intersection: Quadratic equation solving for precise collision detection
- Ray-Screen Boundary: Parametric line intersection with screen edges
- Bresenham's Line Algorithm: Efficient pixel-perfect line rasterization
- SDL2: Graphics library
- GCC: C compiler with math library support
- Make: Build system (optional)
gcc -o raytracing main.c limits_to_ray.c line_drawing_alg.c -lSDL2 -lm-
Run the application:
./raytracing
-
Controls:
- Mouse Movement: Move the light source (white circle)
- Mouse Click + Drag: Continuous light source positioning
- Close Window: Exit application
-
Visualization:
- White circle: Light source (follows mouse)
- White rays: Light beams cast from source
- Large white circle: Obstacle casting shadows
- Black areas: Shadow regions
Key parameters in main.c:
#define RAYS_NUMBER 100 // Number of rays cast
#define WIDTH 1500 // Screen width
#define HEIGHT 600 // Screen heightThe raytracing uses parametric ray equations:
- Ray:
P(t) = Origin + t * Direction - Circle intersection via discriminant:
B² - 4AC - Screen boundary intersection with parameter bounds checking