0

I am currently taking a computer graphics course in C++ and having to do exams for it. The current task says:

Make sure, the recursive function 'trace_recursive' gets stopped at the recursion depth 'max-depth' without changing the code of 'trace-recursive'.

I have worked with recursion before, but always with my own recursive functions, where I could set my own end to the function. Is there any way to kill a recursive function from 'outside' the function in C++ without killing the whole program?

I always thought you need to stop recursion from inside the function and I did not find anything online that helps me with my problem.

Edit:

This is the method, that calls 'trace_recursive':

glm::vec3 evaluate_reflection(
RenderData &data,           // class containing raytracing information
int depth,                  // the current recursion depth
glm::vec3 const& P,         // world space position
glm::vec3 const& N,         // normal at the position (already normalized)
glm::vec3 const& V)         // view vector (already normalized){

glm::vec3 Refl = reflect(V, N);
float epsilon = data.context.params.ray_epsilon;
Ray ray(P + epsilon*Refl, Refl);
glm::vec3 Mirror = trace_recursive(data, ray, depth);

return Mirror;}

I am not allowed to post the code of the actual function 'trace_recursive'

What my method is asked to do is:

Create the vector Refl; create the ray; create the vector mirror by using trace_recursive. If trace recursive reaches a recursive depth of for example 5, it should return the current value and stop, even if it would not stop by itself.

malissa3
  • 1
  • 2
  • For an example? Add some code... – Nur Nov 17 '21 at 11:52
  • Related questions (but not what you're looking for): [Does c++ limit recursion depth?](https://stackoverflow.com/questions/2630054/does-c-limit-recursion-depth) and [Limit recursive calls in C++ (about 5000)?](https://stackoverflow.com/questions/16126241/limit-recursive-calls-in-c-about-5000) – Stef Nov 17 '21 at 14:29
  • Since your function `evaluate_reflection` has a parameter `int depth,`, modifying its code to stop the recursion when it reaches some maximum depth seems straightforward. But I'm intrigued by the *"without changing the code of 'trace-recursive'."* mention in your task. – Stef Nov 17 '21 at 14:31
  • 1
    @Stef yes, my first idea was to check and change the code of trace_recursive, too, but then I saw the "without changing the code", what totally confuses me. Well, I have done the rest of the task, it seems to work most of the times, so I probably just leave it be for now. Thank you :) – malissa3 Nov 17 '21 at 15:50

0 Answers0