Friday, October 21, 2011

Assignment #6 - Refraction

This week we're working on refraction.  After we derived the refraction angle from Snell's Law, I started a new spike in which to start working with refraction in my raytracer.  My very first attempt is a black hole.


The first step I took toward getting to the bottom of this was a bunch of print statements.  The very second thing I did was visualize the refraction directions, depicted in the image below.


That dimple is weird, it reminds me of a singular point.  It's right where the incoming view direction is the same as the surface normal.

The next thing I did was double check my intersection code, where I decide whether a ray is inside or outside an object when it hits the surface.  I found a copy and paste error (when will I stop doing that), and when I fixed it, I discovered that I could at least bend some light through my transparent sphere.


That's an unsettling black mass in my transparent orb.  I wonder if rays are not hitting the plane near the very bottom of the sphere due to my small epsilon guard against allowing a ray to hit an object before it has left the object's surface on it's way to some other destination.  That might explain the black circle at the bottom of the sphere, which might help account for the large black area in the middle of the sphere, if the refraction angles there approach pointing down into it.

I tried some smaller refractive indices for the solid sphere, and though it created much less black space in the center, there's obviously still a problem with the refraction angles.  Here's a screenshot from when I set the refractive index of the sphere equal to that in the air, i.e. both equal to 1.


It seems like when the refractive indices of the air and the sphere are equal to each other, the sphere should become invisible.

I think the dimple in my refraction angles is what's wrong here, considering the image above, where there appears to be a singular point in the same relative position as there does in the refraction angles visualization.

Ok, so I just found my bug, and now I'm getting something that looks like refraction.  I put up another checkered plane behind my refractive sphere, and the resulting image appears similar to other refractive spheres I've seen placed in front of other checkered planes around the internet.


Some unsightly speckles have been added, but the distortion of the checkered plane through the sphere seems to look right to me.  In order to clear up the speckles, I went from one ray per pixel to 25, and that seemed to make a difference, as you can see below.


So now one of the problems with this image is that I've avoided allowing reflections in the case of transparent objects, just to see if I could quickly get the light rays bent in the right directions.  So I think the next step is to go back and split rays that hit transparent objects, one ray should trace the reflection, and the other should trace the refraction.

I also still need to allow for different colors of glass, as right now the only color for transparent objects is perfectly clear.

By the way, here is what the visualized refraction vectors look like, now that they're correct.  The eye is sitting on the negative x-axis looking up the positive x-axis, and the transparent (here, salmon-colored) sphere is right on top of the origin.  I think the main point is that you see nearly a solid color, which reflects the fact that the refracted rays leave the inside of the sphere surface closest to the eye parallel to each other as they travel through the sphere to the opposite side.


And just as a sanity test, I rendered the scene with the transparent sphere having an index of refraction equal to that of the air.  It's nice to see no distortion of the plane in back of the sphere.


Now to handle reflections and refractions together.  And maybe figure out the source of those speckles.

... in progress ...

No comments:

Post a Comment