ray tracing pseudocode

I don't understand the if (depth > MAX) return Black part. Does it have something to do with shadows, because in other algorithms they shoot a shadow ray towards the light source to check for shadows but they don't have it here?

  • 1
    $\begingroup$ Hello and welcome. Are you trying to circumvent the future MIT license by posting a image or what up with that? $\endgroup$
    – joojaa
    Dec 28, 2015 at 17:10

1 Answer 1


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.