This Raytracer project shows 3 spheres. The green sphere is partly inside the blue sphere! ** Use Turbo Mode (<Shift> key + Green Flag) Press <1> to see the picture in finer resolution Press <5> to see the picture again in lower resolution A "Ray Tracer" traces rays from the viewer through each pixel on the screen and calculates which object it hits first in 3D space (ie. which object is closest). The pixel is coloured with the colour of that object. More advanced Ray Tracers add different types of lighting, shadows and reflections of other objects. They also include not just spheres (which have easier math) but other 3D objects. The lighting is made up of 3 components: - Ambient Light - general background reflected light that even gets into dark places such as under a bed - Diffuse Light - sphere lighting that is stronger if the light shines more "straight on" towards the sphere. (Diffuse Light Intensity does not depend where the viewer is) - Specular Reflected Light - the highlight produced when light is reflected off a sphere almost directly towards the viewer. I worked out the following math with pen and paper: - where a ray from the viewer intersected each sphere (I used a parallel projection to keep the math simpler) - the angle between any 2 normalized rays/vectors (I drew a picture and used trigonometry rather than just using the dot product) - the angle of a light ray reflected off the sphere (I drew a picture and used trigonometry) Writing the project would have been quicker if I had used existing formulae rather than "doing the math".
This project has a companion project in RGB cf hsb colours: https://scratch.mit.edu/projects/381878751/ * Huge thanks to @Geotale (14 years old). After always thinking that raytracing projects would be too hard, I was inspired by their project: "How to Make Your Own Raytracer" https://scratch.mit.edu/projects/377006401 also made part 2 including lighting and shadows: I have yet to try shadows. * Also inspired by Sphere Raytracer (With Reflections) by * Amazed by Ray Marching by Some web references for lighting - ambient, diffuse, specular, shadows and recursive reflection https://en.wikipedia.org/wiki/Phong_reflection_model https://cal.cs.umbc.edu/Courses/CMSC435-F15/Slides/raytrace.pdf https://www.rose-hulman.edu/class/cs/csse451/examples/notes/present8.pdf