1

Apparently i cant use recursion in opengl and i have to calculate refraction and reflection (mixed using fresnel) for N bounces of a light ray so my recursive function calls twice to itself mixing the two values and returning. How can i do this iteratively? can i do this in a nice and clean way?

Currently i just sample randomly using fresnel as probability between reflection and refraction. The result is nice but it needs a lot of samples to reduce noise and its relatively slow.

The code is something like this (pseudocode):

MAX_BOUNCES=4

color render(ray,bounce){
    if(bounce>MAX_BOUNCES)return black;
    p=intersectscene(ray);
    if(p.nointersection)return backgroundcolor(ray);
    n=calcnormal(p);
    f=fresnel(ref_index1,ref_index2,angle(n,ray));
    return render(reflect(ray,n),bounce+1)*f+(1-f)*render(refract(ray,n),bounce+1);
}
Txoka
  • 13
  • 6
  • *"Apparently i cant use recursion in opengl"* -- Do you mean you can't use recursion in a GLSL shader? If not, what is stopping you? – Romen Oct 24 '19 at 14:18
  • You can add rays and their fresnel multiplier to a local array (of fixed size) and then walk through that array to accumulate its contribution. – Nico Schertler Oct 24 '19 at 16:00
  • You can convert such ray-tracing to iterative by using stack/heap buffer or tree structure (of fixed size) ... see the linked duplicate for working example in GLSL... – Spektre Oct 25 '19 at 15:00

0 Answers0