Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

raytracing_cpp

A real-time 2D light and shadow simulation in C++ with SDL3. A light source casts 1000 rays outward; each ray marches pixel by pixel until it leaves the window or strikes an occluder, carving a shadow out of the light field behind it.

Drag with the left mouse button to move the light. A second circle drifts vertically and bounces off the top and bottom edges, so the shadow sweeps continuously without input.

Despite the name, this is ray casting in 2D — screen-space rays against circle geometry — not a 3D raytracer. It was written to get comfortable with SDL3's surface API and with thinking in rays before attempting the 3D version.

How it works

Everything renders into a single SDL_Surface obtained from the window; there's no renderer or texture pipeline. Each frame clears the surface, draws rays, then draws the two circles over them.

  • generateRays — distributes RAYS_N rays evenly over 2π from the light's centre. Regenerated whenever the light moves.
  • fillRays — steps each ray one pixel at a time along (cos θ, sin θ), filling a RAY_THICKNESS-sized rect at each step, and stops on either a window-bounds check or a squared-distance test against the occluder. Comparing squared distances avoids a sqrt per step, which matters at 1000 rays × hundreds of steps × 50 fps.
  • fillCircle — scanline fill over the circle's bounding box, same squared-distance test.

Tunable via the #defines at the top of src/raytracing.cpp: window size, ray count, ray thickness, palette.

Build

Requires SDL3 discoverable through pkg-config.

make
./raytracing.exe

The SDL3 headers under src/include, the import libraries under src/lib, and SDL3.dll are intentionally gitignored — supply your own SDL3 install.

About

Raytracing simulation in cpp

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages