raytracingDevTeam / raytracing

Raytracing source code from Shirley's Raytracing in One Weekend
MIT License
0 stars 0 forks source link

Add depth (bounces of the ray) as parameter #59

Open Ale32 opened 6 years ago

Ale32 commented 6 years ago

It's definitely useful to have the depth variable as a parameter as the other render options like width, height and samples.

andrearastelli commented 6 years ago

To use this parameter, after some thinking, we may go in two different directions:

1 - Recursive approach with fixed value.

This approach is the more weird one, because in our current implementation we may go and define another parameter in the color function, and this parameter is always used as a constant.

Like this:

Color ray_color(const Ray &r, Hitable *world, int depth, const int max_depth)
{
    // ... some code that do bouncing stuff
    if (depth < max_depth)
    {
        return value1 + value2 * ray_color(r, world, depth + 1, max_depth);
    }
}

Something like this. Is not really pretty, though, because when we eventually add multithreading we may want to convert this function from recursive to iterative to gain full control over the process.

2 - Change the function from recursive to iterative

Basically, the function will be updated into an iterative one, this way the process will be a little less weird, and will be easier to convert the application into a multithreaded one.

Also the depth parameter, that is basically only an index (used to count from 0 up to max_depth bounces, this way can be omitted.

Color ray_color(const Ray &r, Hitable *hit, const int max_depth)
{
    for (int i=0; i < max_depth; ++i)
    {
        // Ray bouncing and color attenuation code
    }
}