Reflections

As a final bonus feature, I implemented reflective surfaces. I do this by casting a ray in the direction of the reflection of the incident view ray. If this reflective ray hits an object, I continue recursively until some halting criteria.





Previous scene rendered with reflections and soft shadows
xml file used to create images

For each Intersection with a geometry I calculate the diffuse and specular lighting at that position. I then test whether the reflective index,c1, is above a certain threshold. If it is, I cast a reflected ray from the point of intersection. If I encounter another geometry, I then calculate the lighting at this point again, and I check whether its reflective index, c2, multiplied by c1 is larger than a certain threshold.
I.e. c1*c2 > thres.
If it is, I continue, calculating the colour of the pixels at each point of intersection until finally c1*c2*…*cn < thres.

I then calculate the colour of my original point of intersection as the sum of the colours at each reflected point of intersection, weighted by the product of the reflective coefficients of the surfaces before.
Alternatively I stop if a reflected ray does not intersect anything. (An option here would be to have the reflected ray that does not intersect anything take on the colour of the background).
The reflected light calculation is done independently of the type of light being considered. We calculate the diffuse and specular contribution, and then we compute the colour due to reflection after that. The total colour then becomes:

C =r*rc + d*(1-rc)

Where rc is the reflective index, d is the diffuse/specular lighting contribution, and we then weight the lighting contribution by the inverse of how reflective it is.

Home
Link to zip file of java project