I've sucessfully implemented a ray tracer from scratch in C and it's marvelous:
The surface normal on this sphere is taken by computing the intersection point, A, and treating this as a euclidean vector with origin, B, the center of the sphere. To normalize it, I first subtracted A with B, yielding C:
C = (Ax-Bx,Ay-By)
From this, it's divided by the magnitude:
N = C/|C|
The illumination is calculated by taking the dot product of N and LIGHT_SOURCE and multiplying it with 255:
if (dot_product<0.0) dot_product = 0.0;
illumination = (unsigned char)(dot_product*255.0);
When I tried setting the light source to (0.0, 0.0, -1.0), it produce something like this:
I expected the shade to be something like it's brighter on the middle and becoming more dimmer on the side.
Anyone know what's causing the problem?
Source code: https://gist.github.com/harieamjari/6297a35400460edbabbd8e39720ed0f5
This can be compiled if you already have libpng16.