Ray tracing algorithm implementation

We've covered everything there is to say! We are now ready to write our first ray tracer. You should now be able to guess how the ray tracing algorithm works.

First, take a moment to notice that the propagation of light in nature is simply countless rays of light emitted from a light source that bounce around until they reach the surface of our eyes. Therefore, ray tracing is very elegant because it is directly based on what is happening around us. Apart from the fact that it follows the light path in reverse order, it is nothing less than a perfect simulator of nature.

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

Ray tracing algorithms obtain images composed of pixels. For each pixel in the image, it emits a main ray into the scene. The direction of this chief ray is obtained by tracing a line from the eye to the center of that pixel. Once we set the direction of the main ray, we check every object in the scene to see if it intersects any of them. In some cases, the main ray will intersect multiple objects. When this happens, we select the object whose intersection point is closest to the eye. We then fire a shadow ray from the intersection point towards the light source (Figure 1):

Figure 1: We emit a chief ray through the pixel center to check for possible object intersections. When we find a point we cast a shadow ray to determine if the point is illuminated or in shadow

If the ray does not intersect an object on its way to the light source, the hit point will be illuminated. If it intersects another object, that object casts a shadow on it (Figure 2):

Figure 2: Small sphere casting shadow on larger sphere. The shadow ray intersects the small sphere before reaching the light source

If we repeat this for each pixel, we obtain a two-dimensional representation of the three-dimensional scene (Figure 3):

Figure 3: To render a frame, we emit a main ray for each pixel of the frame buffer. Here is a pseudocode implementation of the algorithm:

Here is a pseudocode implementation of the algorithm:

for (int j = 0; j < imageHeight; ++j) { 
    for (int i = 0; i < imageWidth; ++i) { 
        // compute primary ray direction
        Ray primRay; 
        computePrimRay(i, j, &primRay); 
        // shoot prim ray in the scene and search for the intersection
        Point pHit; 
        Normal nHit; 
        float minDist = INFINITY; 
        Object object = NULL; 
        for (int k = 0; k < objects.size(); ++k) { 
            if (Intersect(objects[k], primRay, &pHit, &nHit)) { 
                float distance = Distance(eyePosition, pHit); 
                if (distance < minDistance) { 
                    object = objects[k]; 
                    minDistance = distance;  //update min distance 
                } 
            } 
        } 
        if (object != NULL) { 
            // compute illumination
            Ray shadowRay; 
            shadowRay.direction = lightPosition - pHit; 
            bool isShadow = false; 
            for (int k = 0; k < objects.size(); ++k) { 
                if (Intersect(objects[k], shadowRay)) { 
                    isInShadow = true; 
                    break; 
                } 
            } 
        } 
        if (!isInShadow) 
            pixels[i][j] = object->color * light.brightness; 
        else 
            pixels[i][j] = 0; 
    } 
} 

As we've seen, the beauty of ray tracing is that it only requires a few lines of code; one can write a basic ray tracer in 200 lines. Unlike other algorithms (such as scanline renderers), ray tracing requires very little effort to implement.

This technique was first described by Arthur Appel in 1969 in a paper titled "Some Techniques for Machine Rendering of Solid Shading". So, if this algorithm is so wonderful, why hasn't it replaced all other rendering algorithms? The main reason then (and even to some extent today) was speed. As Appel mentioned in his paper:

This approach is very time-consuming, often requiring thousands of times more computing time than wireframing to achieve beneficial results. About half the time is spent determining the point-to-point correspondence between the projection and the scene.

In other words, it's slow (as Kajiya - one of the most influential researchers in all of computer graphics history - once said: "Ray tracing isn't slow - the computer is"). Finding the intersection between rays and geometry is very time consuming. For decades, the speed of the algorithm has been a major drawback of ray tracing. However, as computers become faster and faster, this is no longer an issue. One thing must be said though: ray tracing is still much slower compared to other techniques such as the z-buffer algorithm. However, today with fast computers we can calculate frames in minutes or less that used to take an hour. Real-time and interactive ray tracers are a hot topic.

In summary, it is important to remember that the rendering routine can be considered two independent processes. The first step determines whether a point on the object's surface is visible from a specific pixel (the visibility part), and the second step shades that point (the shading part). Unfortunately, both steps require expensive and time-consuming ray geometry intersection testing. The algorithm is elegant and powerful, but forces us to trade rendering time for accuracy and vice versa. Since Appel published his paper, much research has been done to speed up ray-object intersection routines. As computers became more powerful and combined with these acceleration technologies, ray tracing became available for use in everyday production environments, and by today's standards it is the de facto used by most, if not all, rendering offline software Methods. Video game engines still use rasterization algorithms. However, with the recent advent of GPU-accelerated ray tracing (2017-2018) and RTX technology, real-time ray tracing has also become possible. While some video games already offer modes that turn on ray tracing, it's limited to simple effects like crisp reflections and shadows.


Original link:Ray tracing algorithm implementation - BimAnt

Guess you like

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