Pages

Wednesday 16 December 2009

Flash Ray Tracer Reflections

Finally managed to get reflections properly working in my actionscript raytracer. The mathematics behind reflections is very simple. Given an incoming light ray V, and a surface normal N, we can calculate the reflected light ray R using:

a = V.N
R = V + 2*a*N

I applied this theory to the ray tracer and the results are beautiful. Its amazing how much realism a few shadows and reflections can create! The number of reflections obviously changes the image realism. The images below show 1, 2 and 3 reflections per ray. Surprisingly there really isn't much of a reduction in performance - and for the purposes of a flash raytracer 3 reflections should be plenty!




Just need to add some functionality for box reflection and then I'll set the engine rendering some nice scenes!

One further extension I might set myself is diffuse reflections. Instead of light being reflected directly along the normal, there could be random fluctuations in the reflection vector. I think this could produce quite a nice wave effect. I'll keep you posted!

On a performance note the image with 3 reflections took just over 2 minutes to render in 1600x1600 with no real optimisation. For a resolution of 400x400 pixels it takes about 2.5 seconds. Not too bad at all in my opinion!

Update: Here are a few fuzzy shots. The first one is less fuzzy and the second one is more fuzzy. Quite an interesting effect I think:



Decided to go a bit mad and rendered a rather large image with a load of boxes and reflective spheres. Inspired by SuperJers PixelMachine, but rendered completely using flash and AS3, not OpenGL and C++. Here it is (Something has gone wrong with the reflections of the boxes - there is no shadowing - but I didn't have the heart to stop it once it was half way through!):

Here is another nice one, 2880 by 2880 pixels, back to the original room but with the reflection stuff added. Looks quite nice in the super high resolution!

Update: I've rendered few more large reflective images - there is still something slightly wrong with the reflections I think, check them out:




2 comments:

  1. How could no one be commenting on this blog? All of your posts are amazing! I really want to add as3 raytracing to my as3 hypothetical-molecule generator... I just discovered your blog today but I will be back!

    ReplyDelete
  2. Hi Chris, great to have you on the blog! Sorry for not getting back to you sooner but I'm currently sitting third year physics exams which is rather time consuming.

    The hypothetical-molecule generator sounds very interesting, and I think the raytracer would certainly be capable of creating renders like this. You'd just need a 3d map of the atom locations in the molecule, and bonds, and colours depending on the elements.

    When I'm finished with exams I am going to work on getting everything working very well in the raytracer, including diffraction, and diffuse lighting. Once I've done this I'd be happy to share the source code!

    Thanks for the positive feedback,

    Sam

    ReplyDelete