I dont understand the if(depth>MAX)return Black part. Is it something to do with shadows because in other algorithms they shoot a shadow ray towards the light source to check for shadows but they dont have it here
This is a recursive function (it calls itself). Each time it calls itself it does so with
depth + 1 instead of
depth. When it is called with
depth > MAX, it simply returns
Black rather than call itself again. This ensures that the function will eventually stop recursing and return a value. It is the colour equivalent of returning zero.
You can visualise this as a light ray reflecting from multiple surfaces successively. Each time the ray reflects from a surface the same function is used to calculate the colour of the light in that direction. If you set
MAX to 0, there will be no reflections in your resulting image. If you set
MAX to 2, there will be reflections of reflections which will look more realistic, but take longer to calculate.
Intuitively, MAX is the number of surfaces the ray will bounce off before it is decided what colour should be displayed.