Ray Tracing: Using the Fragment Shader (+ Rendering Triangles, Ambient Lighting, & Soft Shadows) @s0lly
Ray Tracing: Using the Fragment Shader (+ Rendering Triangles, Ambient Lighting, & Soft Shadows)  @s0lly
Uploaded June 2020 | Updated September 2026, 2 hours ago
The entire set of ray tracing calculations occur in the Fragment shader (OpenGL GLSL code) - therefore using the GPU.

I've designed the code to, per fragment / pixel:
- determine the correct ray for each pixel
- accept triangles (by model) to test against by using ray-triangle intersection testing
- determine whether rays hit objects, and if so, what colour should be displayed due to lighting
- but only add said colour based on the lack of any shadows that may be generated by objects obstructing those lights
- those shadows are generated taking into account the radius of the light source - producing nice looking soft shadows where applicable
- and casting ambient rays or reflection rays, to determine any 2nd / 3rd / 4th etc order shading effects for each pixel



Find the shader code (GLSL) here: github.com/s0lly/RayTracingInTheFragmentShaderGLSL - feel free to use to your heart's content.


Happy to answer any questions in the comments below!




Other Social Media stuffs:

twitch.tv/s0llygaming
twitter.com/s0lly
instagram.com/s0lly.gaming
github.com/s0lly
patreon.com/s0lly
Ray Tracing: Using the Fragment Shader (+ Rendering Triangles, Ambient Lighting, & Soft Shadows)CRACKTORIO: A Cryptocurrency Game in Excel - Trailer 1Gardeners of the Galaxy: Trailer (Gamejam in C++)First Games in C++: Dungeons and Flagons (v0.84) - Skeletons Added, Performance Improved!Create Simple Game Tilemaps in ExcelGame Engine 25: Next StepsFirst Games in C++: Krazy Karts (v0.72) - AI Finishes Complex Lap (Just!!)Ray Tracing in C++: Setting the SceneKrazy Karts Intro (COMING SOON)Channel SoundtrackGame Engine 22: Simple DronesOpenGL Engine - Showcase 5: Particle Effects
s0lly |

Ray Tracing: Using the Fragment Shader (+ Rendering Triangles, Ambient Lighting, & Soft Shadows)

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER