This project demonstrates parallel implementations of 2D canvas drawing and 3D raytracing using both Win32 API (Windows-only) and SDL2 (cross-platform).
- canvas-winapi.lua - Win32 GDI canvas with red rectangle
- canvas-sdl2.lua - SDL2 canvas with red rectangle (cross-platform)
- ray3d-winapi.lua - Win32 raytracer with reflections, shadows, animated spheres
- ray3d-sdl2.lua - SDL2 raytracer with same features (cross-platform)
| Feature | Win32 | SDL2 |
|---|---|---|
| 2D rectangle drawing | ✓ | ✓ |
| Mouse cursor visible | ✓ | ✓ |
| Escape key exits | ✓ | ✓ |
| Window resize | ✓ | ✓ |
| Platform | Windows only | Cross-platform |
| Feature | Win32 | SDL2 |
|---|---|---|
| 3 animated spheres (RGB) | ✓ | ✓ |
| Reflective surfaces | ✓ | ✓ |
| Checkerboard floor | ✓ | ✓ |
| Dynamic shadows | ✓ | ✓ |
| Multi-bounce reflections | ✓ | ✓ |
| Dynamic resolution | ✓ | ✓ |
| Smooth time-based animation | ✓ | ✓ |
| Escape key exits | ✓ | ✓ |
| Platform | Windows only | Cross-platform |
cd /home/arkenidar/luajit/win_script
wine luajit.exe canvas-winapi.lua
wine luajit.exe ray3d-winapi.luacd /home/arkenidar/luajit/win_script
luajit canvas-sdl2.lua
luajit ray3d-sdl2.lua- Wine (Linux) or Windows
- LuaJIT (Windows build:
luajit.exe)
- LuaJIT (native build)
- SDL2 library (
libSDL2-2.0.so.0on Linux,SDL2.dllon Windows)
Install SDL2 on Linux:
sudo apt-get install libsdl2-2.0-0- Uses FFI to call Win32 APIs:
user32.dll,kernel32.dll,gdi32.dll - GDI for 2D drawing (
Rectangle,CreateSolidBrush) StretchDIBitsfor pixel buffer rendering (raytracer)- Win32 message loop (
GetMessageW,DispatchMessageW) - Timer-based animation (
SetTimer,WM_TIMER)
- Uses FFI to call SDL2 APIs
- SDL2 renderer for 2D drawing
SDL_Texturewith streaming for pixel buffer rendering (raytracer)- SDL2 event loop (
SDL_PollEvent) - Time-based animation (
SDL_GetTicks) - Automatic library detection (Linux
.so/ Windows.dll)
- Camera model: Perspective projection with aspect ratio correction
- Ray-sphere intersection: Analytic quadratic solution
- Ray-plane intersection: Floor at y = -2
- Lighting: Single directional light with diffuse shading
- Shadows: Ray tracing from hit point to light source
- Reflections: Recursive ray tracing (max depth 2)
- Materials: Configurable reflectivity per sphere (0.6-0.7)
- Animation: Spheres orbit around center using time-based rotation
- Dynamic resolution: Renders at exact window size (no oversampling)
Both Win32 and SDL2 versions follow the same logical structure:
- FFI Declarations: Platform-specific API types and functions
- Utility Functions:
wstr()for Win32, helper functions - Raytracer Logic:
raytrace(w, h, angle)- Main rendering looptrace_ray()- Recursive ray traceris_in_shadow()- Shadow testing
- Window/Event Handling: Platform-specific message/event loop
- Main Function: Window creation and initialization
- Win32: ~30-60 FPS at 640x480 (depends on CPU)
- SDL2: Similar performance to Win32
- Rendering: Real-time ray tracing with reflections and shadows
- Resolution: Dynamically adjusts to window size
Possible improvements:
- Anti-aliasing (supersampling)
- More complex geometry (triangles, meshes)
- Multiple light sources
- Texture mapping
- Refractions (transparent materials)
- Depth of field
- Ambient occlusion
- Path tracing
Educational/demonstration code. Use freely.