I am writing a raytracer in F# using montecarlo sampling I would like to make my recursive calls tail recursive but I am not sure of this is possible with MC raytracing as one has to evaluate sampled hemisphere before returning.
The accululator variables are there to facilitate the tail recursion and works for naiive raytracers which just trace along a single ray until it terminates. At the moment the problem is the for loop.
Any help / pointers / references I can read would be appreciated
Currently my code looks as follows:
if surfaceGeometry.IntersectionAcceptable realSolution t 1.0f (PointForRay ray t) then let emittedShading = surface.Emitted let e = accEmitted + accScatter*emittedShading let mcSamples = surface.SampleCount //TODO rewrite this to make it tail recursive let mutable totalShading = e/surface.MCNormalization for _ in 0..mcSamples-1 do let (doesRayContribute,outRay,cosOfIncidence) = surface.Scatter ray t ((int)newTraceDepth) let shading = surface.BRDF*cosOfIncidence / (surface.PDF*surface.MCNormalization) let s = accScatter*shading totalShading <- totalShading + (rayTrace newTraceDepth (outRay , e , s)) totalShading else accEmitted + backgroundColor*accScatter