Image Generation (Raytrace 1)
Image Generation (Raytrace 1)
With the Covid fall out, work, Beyond the Border and other things I haven’t really had the chance or motivation to do much on the image generation for the last month.
To get something moving I decided to try and implement a simple raytracer. It is very limited at the moment but at least managed to produce an image and I can work on improving it in the future.
In A-level days I did some raytracing with PovRAY where you would write a text file in a scene description language get the ray tracer to run it and then watch as the computer whined, spun its fans and very slowly made an image. It was part magic and part an exercise in waiting, reworking a file and frustration.
The raytracer at its heart fires a ray out of the camera until it hits a surface (hence tracing rays) it then feeds that surface information back to render a pixel. Reflections and lighting can be accomplished via more ray firing.
First Image
In this limited version I just support coloured sphere and planes with a background colour the very first (correct) image I got was the uninspiring:
Reflections
This isn’t the most impressive thing aside from the few hours of coding it took me to get there. We dont have any lighting or reflections so everything looks flat and 2D. To make things look a little more interesting and to convince myself I was doing something useful I added reflections. This is done by tracing another ray when we contact the surface incident to the original and merging the result of the secnd ray trace with the surface colour to feed back. The results look nice I added some more spheres and moved the camera a bit for a nicer render)
Next Steps
The code for the very naive version is available on Github it isnt too long. While being naive it ran in 0.25s to produce a 1024 by 1024 image. Which is more a testament to modern hardware than anything.
Things I might work on for upcoming posts:
- Optimisation – Some form of oct-tree to speed up rendering – particularly when I have a lot of primitives and rays.
- Lighting. Currently we just have reflections and the ambient colour of the surface which is all assumed to have constant lighting. I would like to add some lighting via ambient occollusion (firing more rays) and using that to determine how well lit things are. I probably want to look up surface lighting.
- More primitives – at least tori and boxes for testing
- Material textures for example normal maps. Probably based on math matical functions initially.
- Ray tracing of mathematically interesting shapes fractals etc.
- Optics – tracing rays through solids.