3D cartoon-like explosion in 180 lines of bare C++

3D cartoon-like explosion in 180 lines of bare C++

The idea is simple: the sphere has an equation of the form x^2 + y^2 + z^2 – r^2 = 0 ; but the function f(x,y,z) = x^2 + y^2 + y^2 + z^2 – r^2 is defined anywhere in the 3D space. I compute this normal vector by simple final differences on our signed distance function:

Of course, since we draw a simple sphere, the normal can be obtained much easier, but this approach will serve me later. When my ray crosses the sphere at some point (x,y,z), the value of the function g(x,y,z) sets the pixel color of the sphere.

Source: github.com