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);
}