Ray Tracing: Reflections and Refractions

Another advantage of ray tracing is that by extending the idea of ​​light propagation we can simulate effects such as reflection and refraction very easily, which is very convenient when simulating glass materials or mirrors. In a 1979 paper titled "An Improved Illumination Model for Shadow Display," Turner Whitted first described how to extend Appel's ray tracing algorithm to achieve more advanced rendering. Whitted's idea extended Appel's ray model to include calculations of reflection and refraction.

In optics, reflection and refraction are well-known phenomena. Although later lessons are devoted to reflection and refraction, we will look at what is needed to simulate them. We will use the example of a glass sphere with refractive and reflective properties. As long as we know the direction in which the light intersects the ball, it's easy to calculate what will happen to it.

NSDT tool recommendationThree.js AI texture development kit - YOLO synthetic data generator - GLTF/GLB online editing - 3D model format online conversion - Programmable 3D scene editor -  REVIT export 3D model plug-in - 3D model semantic search engine

Both reflection and refraction directions are based on the normal at the intersection point and the direction of the incident ray (chief ray). In order to calculate the direction of refraction, we also need to specify the refractive index of the material. Although we said before that light travels in straight lines, we can think of refraction as the bending of light. When a photon hits an object with a different medium (and therefore a different refractive index), its direction changes. The science of this will be discussed in more depth later. As long as we remember that both effects depend on the normal vector and the direction of the incident ray, and that refraction depends on the refractive index of the material, we can move on.

Likewise, we must also realize that an object like a glass ball is both reflective and refractive. We need to calculate both for a given point on the surface, but how do we mix them? Do we mix the 50% reflection result with the 50% refraction result? Unfortunately, it's more complicated than that. The blend of values ​​depends on the angle between the chief ray (or viewing direction) and the object normal and refractive index. Fortunately for us, however, there is an equation that calculates exactly how each substance should be mixed. This equation is called the Fresnel equation. To keep things simple, all we need to know now is that it exists and will help determine the blend value.

Figure 1: Calculating reflected and refracted light rays using the laws of optics

Let's review. How does Whitted's algorithm work? We emit the primary ray from the eye and the closest intersection point (if any) with an object in the scene. If the ray hits a non-diffuse or opaque object, we have to do extra computational work. To calculate the final color at that point on the glass sphere, you need to calculate the reflected and refracted colors and mix them. Remember, we do this in three steps. Calculate the reflected color, calculate the refracted color, and then apply the Fresnel equation.

First, we calculate the reflection direction. To do this we need two items: the normal at the intersection point and the direction of the chief ray. Once we get the reflection direction, we fire a new ray. Going back to our previous example, let's say the reflected ray hits the red sphere. Using Appel's algorithm, we determine how much light reaches that point on the red sphere by firing a shadow ray towards the light. This gets the color (black if there is a shadow) times the light intensity and returns it to the surface of the glass sphere.

Now, we do the same thing with refraction. Since the ray passes through the glass sphere, it is called a transmitted ray (light travels from one side of the sphere to the other; it is transmitted). To calculate the direction of transmission, we need the normal to the hit point, the chief ray direction, and the refractive index of the material (in this case, the refractive index of the glass material is probably about 1.5). After the new direction is calculated, the refracted ray continues to the other side of the glass sphere. Likewise, the light is refracted again due to the change in medium. As you can see in the adjacent image, the direction of light changes as it enters and leaves the glass object. Refraction occurs every time you change media, and the one medium the light comes out of and the two media it enters have different refractive indexes. The refractive index of air is very close to 1, and the refractive index of glass is around 1.5. To achieve the effect, refraction bends the light rays slightly. This process makes the object appear to move when looking through or looking at it through an object with a different index of refraction. Let's imagine that as the refracted ray leaves the glass sphere, it hits a green sphere. We calculated the local illumination of the intersection between the green sphere and the refracted ray (by shooting the shadow ray). The color (black if there is a shadow) is then multiplied by the light intensity and returned to the surface of the glass sphere

Finally, we calculate the Fresnel equation. We need the refractive index of the glass sphere, the angle between the chief ray and the normal to the hit point. Using the dot product (which we will explain later), the Fresnel equation returns two mixed values.

Here's some pseudocode to reinforce how it works:

// compute reflection color
color reflectionCol = computeReflectionColor(); 

// compute refraction color
color refractionCol = computeRefractionColor(); 

float Kr; // reflection mix value
float Kt; // refraction mix value

fresnel(refractiveIndex, normalHit, primaryRayDirection, &Kr, &Kt);

// mix the two colors. Note that Kt = 1 - Kr
glassBallColorAtHit = Kr * reflectionColor + Kt * refractionColor;

In the above code, we stated Kt = 1 - Kr in the comments. In other words, Kr + Kt = 1. This is because, in nature, light cannot be created or destroyed. Therefore, if some of the incident light is reflected, the remainder of the incident light (the portion that is not reflected) must be refracted. If you calculate the sum of reflected and refracted light, it equals the amount of incident light. Normally, the Fresnel equation gives us the values ​​of Kr and Kt (if it's done correctly, their sum should equal 1), so you can directly use the values ​​returned by the function. However, it would be enough if we only have one of them. If you have Kr, you can get Kt= (1 - Kr). If you have Kt, you can get Kr =(1 - Kt).

The final advantage of this algorithm is that it is recursive (which is also somewhat of a curse!). In the case of our current study, reflected rays hit a red, opaque sphere, and refracted rays hit a green, opaque, diffuse sphere. However, we would imagine that the red and green spheres are also glass spheres. In order to find the colors that reflected and refracted rays return, we have to perform the same process with the red and green spheres used for the original glass spheres: that is, throw more reflected and refracted rays into the scene. This is a drawback of the ray tracing algorithm and can sometimes cause headaches. Imagine that our camera is inside a box with only a reflective surface. In theory, the light is caught and will continue to bounce off the walls of the box endlessly (or until you stop the simulation). Therefore, we must set an arbitrary limit that prevents rays from interacting and thus recursing infinitely. Every time light is reflected or refracted, its depth increases. When the ray depth is greater than the maximum recursion depth, we stop the recursive process. Your image doesn't have to look completely accurate, but an approximation is better than no result at all.


Original link:Ray Tracing: Reflection and Refraction - BimAnt

Guess you like

Origin blog.csdn.net/shebao3333/article/details/134992547