reflection & refraction

November 6th, 2005 by Bramz

I’ve added reflection and refraction to the Simple shader. It was pretty easy, just implementing what i’ve written down in my article Reflections & Refractions in Raytracing. In this implementation, the refraction index is a property of the shader though and it is given as n2 / n1.

To implement reflections and refraction, there’s also the problem of maximum ray depth to be solved. Because you use a recursive algorithm, you may get infinite loops if you don’t take appropriate measures. One way to do that is to add age (recursion depth) to the view ray, but this must be incremented on each castRay call (easy to forget). But since the ray tracer isn’t thread safe anymore anyway, it’s easier and safe to keep state of this in the ray tracer itself: increment age when entering the function, decrement on exit.

Now the Simple shader can do most traditional ray tracer “tricks”, as is shown. The image is rendered using the script you can find in the folder examples/shaders in the CVS.

rendered by examples/shaders/

11 Responses to “reflection & refraction”

  1. Mercury Says:

    It’s looking great so far, Bramz!

    You need to do some nicer filtering, you can see some ugliness the further the tiles are from the viewer. Trilinear and Anisotropic Filtering are nice and pretty, though. :]

    Also, you could make it look way cooler if you did some interesting stuff with the shadows.

    Long live the Symphony in the Memory of a Great Man!

  2. bramz Says:

    thanks =)

    there’s no filtering on the CheckerBoard texture yet (only on the Image texture which isn’t used in this script). So currently, i rely on raw super sampling per pixel …

    About the shadows, there’s not much done about lighting yet … I still only have point lights. Other lights are still to be implemented, and also a photon mapper. That should improve the shadows, shouldn’t it?

  3. bramz Says:

    hi thomas,

    I believe the same is mentioned in the pbrt book? Though that method has a pathological case where it completely fails. IIRC it has something to do with a checkerboard of two black/white checkerboards (the one is the negative of the other) so that the result should be uniformely black or white again. If you use the method with the area integral, some of the other color will leak through. I’m not sure if that’s so bad though ;)

    Another method (also mentioned in the pbrt) is to simply take multiple samples form the checkerboard.

    Hmm, well, whatever :)

  4. thomas Says:

    heya mercury :)

    re antialiasing the checkerboard: given pixelspace differentials of the intersection point you can do an analytic estimate of the area integral, which in can be formed from the integral of the 1d piecewise checker function… i read the details in some renderman book by larry gritz, don’t have a proper reference right now (at 5:10am) though.

    however all this is really special cased, proper texture filtering will be much more worthwhile!

  5. bramz Says:

    my thoughts exactly …

    I agree on the EWA filter. on the priority list definitely are good shading, indirect lighting (photon mapping) and meshes (so we can finally import that blender scene of yours ;) )

    BTW, what about reviewing the reflections & refractions article? ;) Now I must go to bed …

  6. thomas Says:

    given that we both have pbrt, we should look to implement the ewa filter (with fixes mentioned in the forum- not sure if the source reflects that).

    it would be very nice to get that near-analytic, to the point where liar can use only 16-25 samples per pixel in a final render and not worry about ever getting texture aliasing.

    good direct and indirect lighting are, of course, the next big priorities after we’ve got some good shading and sampling. then comes the spacial subdiv…

  7. bramz Says:

    That’s part of the GNU Free Documentation License. You’re not allowed to change anything in it (see copyright notice in front of appendix A). The addendum is about how to apply this license to your documentation, and that’s by adding a notice _like_ that in front (which I did on page 2 ;) )

  8. bramz Says:

    OK, Blender support is on the todo list ;) Probably going to write it in python … Has anyone specs of the file format?

  9. thomas Says:

    now that all my exams are done and failed i finally have the time to finish reviewing the article. was just reading over the manual thingy included with the cvs (hoping to get a texnixcentre version of your refl/refr doc with it) btw, and noticed that you should remove the addendum from the GPL stuff, and change stuff like (c) YEAR YOUR NAME etc ;)

    about photon mapping: i personally dislike the method, however i’m interested in using it to accelerate monte carlo algorithms via shadow photons and importance sampling. for caustics i still dream of implementing metropolis light transport one day :)

  10. tomz Says:

    Looking good so far :) But definitely need something to import blender scenes: I have some nice car models laying around which want to be rendered ;-)

  11. tomz Says:

    Well as Blender can export to .obj maybe that is something to consider. Just use the wavefront format although I’m not so sure wether it is possible to export _scenes_, objects/meshes is not a problem, that I’m sure of.