GAMES101 Lec 13~14 (1): Ray Tracing (1) Whitted style ray tracing

1 Introduction——Shadow Mapping Shadow map
Rasterized shading mentioned before, we know that this is a local phenomenon.
During the shading process, we only consider the shading point itself, the light source, and the camera.
We do not consider other objects, or even Consider the influence of other parts of the material itself on the shading point
. In fact, there will be an occlusion relationship, and there will be shadows.
We couldn’t solve the shadow problem before. Now let’s try to solve it in the range of rasterization

One solution is Shadow Mapping

insert image description here

In essence, the Shadow Mapping method is an image space approach, that is, in the step of generating shadows, we do not need to know the geometric information of the scene (will be described in the implementation steps later) and this method will also produce aliasing problems,
and The classic Shadow Mapping can only deal with point light sources (the point light source will be explained later as an example). The
key idea of ​​this method is that if a point is not in the shadow, then this point can be seen by both the camera and the light source.
From this idea We can see that the shadow should have a clear border, that is, a hard shadow

insert image description here 

1.1 Shadow map principle

  • First of all, we look from the light source to the scene, do rasterization once, we will get what the light source can see, and get a picture insert image description here

    We don't color it, and write down the depth of this picture
    insert image description here
  • We start from the camera and look at the scene againinsert image description here
  • We project the point we see now back to the projection plane just seen by the light source,
    and then compare the depth of the point we see now with the position obtained by projecting back to the light source.
    If the depth is the same, it can be seen.
    insert image description here

    If the depth Inconsistent, then can not see
    insert image description here


    The following is an example
    of a result of our Shadow Mapping

    insert image description here

     The map seen from the perspective of the light source, and the depth
    insert image description here

insert image description here 

Looking at the past from the camera, the test result
looks dirty, why? This is also one of the problems with this method.
There is a precision problem in the equality comparison of floating-point numbers
. People have many ways to deal with precision, but none of them can solve the problem essentially.
Into a picture,
the picture itself has a resolution, if it does not match well with the rendering resolution, there will be aliasing. The
larger the resolution of the depth map, the more expensive it will be
insert image description here

Even if there are problems, it is still the current mainstream methodinsert image description here 

1.2 Existing problems

  • Summarize the problems mentioned. The resolution of
    the hard shadow (point light source)
    shadow map
    and the comparison of floating point numbers
    . Therefore, it is difficult to do global phenomena from the perspective of rasterization.insert image description here

The question about soft and hard shadows
is actually the relationship between the umbra and the penumbra
. If there are soft shadows, it must be because the light source has a certain size.insert image description here 

2 Why Ray Tracing?

  • Rasterization is not good for global effects
    such as soft shadows, glossy reflections, and indirect lighting
    . There are some clever ways to deal with it, but the correctness cannot be guaranteedinsert image description here

Rasterization is fast, but not high qualityinsert image description here 

 Ray tracing is very accurate, but slower
Rasterization is easy to do in real time, and ray tracing is often used offline
insert image description here

 

3 Basic ray tracing algorithm

3.1 Ray definition

  • We first need to define the light.
    Light travels in a straight line without collision, and it is from the light source to the human eye.
    For the third property, we will use the method from the human eye to the light source according to the reversibility of the light path.insert image description here

3.2 Ray Casting
Since ray tracing is tracing, we will start from the end point, that is, from the eye/camera.
The first thing we need to do is ray casting

We assume that we look into the virtual world, and there is an imaging plane in front of us, and we draw the imaging plane into different pixel grids.
For each pixel, we can connect a line from the camera and pass through this pixel, so that we can print a Light can hit the scene.
If the light intersects with an object in the scene, connect the intersection point and the light source to see if the light source can see this point (this point is not in the shadow). If it is visible, then an effective light path will be formed.
Then The energy on this light path can be calculated and coloredinsert image description here

In the following example, we always consider the eye as a pinhole camera, that is, the eye is a point, a position, regardless of the actual camera processing, and the lens or something (this part will be said in path tracing) for objects in the
scene , we assume that perfect refraction and reflection will occur after the light hits it. The
following figure starts from the eye, passes through a pixel of the imaging plane, and casts a ray (eye ray).
This ray will hit a certain position in the scene, we take The nearest intersection
(this step actually solves the problem of depth testing)

insert image description here

When we find a point, we have to consider whether this point will be illuminated. If
we connect a line (shadow ray) from this point to the light source,
it means that it can be illuminated (the black line arrow in the figure below is the law) Line)
With the normal, incident direction, and outgoing direction, we can do shading and write the value of the pixel. At this time, we can use various shading models such as the previous Blinn Phonginsert image description here 

Raycasting does such a thing. Each pixel casts a ray. If it intersects with the scene, find the nearest intersection point, the nearest intersection point and the light source connection, determine whether it is visible, and then calculate the coloring, and write back the value of the pixel. 3.3 Recursion (
Whitted style) ray tracing 
used the method of ray casting before, we still only consider the ray ejection once, but in fact the ray can be ejected many times, which is what the method to be introduced next can do
insert image description here

Let's start with raycasting 
insert image description here

At this point, let’s first consider that the ball is a glass ball.
When the light hits the ball, two things must happen, one is to be reflected, and the other is to be refracted.
insert image description here 

insert image description here 

There is also a little change in the process of calculating the coloring.
Before the light is projected to this point, we can see if the point can be illuminated, and then calculate its coloring
. To calculate the value of the shader (energy loss is also counted), and then add them all back to the value of this pixel insert image description here

4 Intersecting rays with objects

4.1 Rays intersecting implicit surfaces

We need to judge what the light will hit after it is projected, that is, the intersection point is required.
Before finding the intersection point, we first define the light rays mathematically.

  • The definition of a ray is a ray, which has a starting point and a direction. With these two quantities, a ray can be defined. Any
    point on the ray can be expressed by a function with t as an independent variable.
    insert image description here

In order to illustrate the intersection point of the ray and the surface
, we start from the intersection of the ray and the sphere. The intersection point is the point on the sphere and on the ray.
Then the equation can be established. 
insert image description here

There are many ways to solve this equation. We can get the positional relationship according to the solution.
insert image description here We extend it to the general implicit surface. The method is the same. 
insert image description here

 4.2 Intersecting rays with explicit surfaces
For the rendering of explicit surfaces, how rays intersect triangles is a very important topic
in geometry. This method can also be used to determine whether a point is within an object (if the point is within a closed shape , shoot a ray outward, and the number of intersections obtained must be an odd number)
The topic returns to intersecting a ray and a triangle
In the little cow in the figure below,
the easiest way to judge whether a ray intersects it is to judge its triangle faces one by one (each triangular face will have 0 or 1 intersection)
very intuitive but slow (acceleration methods will be introduced later)

insert image description here

How to do the intersection of the triangle and the ray?
The triangle must be in a plane, so the problem can be divided into two parts:
Does the ray intersect the plane?
Is the intersection point inside the triangle?
insert image description here 

The definition of the plane adopts the definition method of the point method
- that is, use a point on the plane and the normal on the plane, and use the method of multiplying the point to be 0 to establish the plane equation 
insert image description here

 We bring the ray equation into the plane equation, solve the intersection point of the ray and the plane,
and then we can judge whether it is inside the triangle
insert image description here

 But people want to combine two steps into one step, which is the following method.
The left side is the point on the ray, and the right side is the point in the triangle represented by the coordinates of the center of gravity. After
the solution is shown in
the figure it is necessary to judge whether it is reasonable. First, t must be positive, and b1 b2 b3 are both non-negative
insert image description here

 

5-Axis Aligned Bounding Box (AABB) Intersection

  • We intersect with each triangle to find the nearest intersection point,
    but the number of calculations = number of pixels × number of triangles × number of ejections is too slow,
    so we need to improve and speed up this process. One of the methods is the bounding box
    insert image description here

 The idea of ​​the bounding box is to surround a complex object with a simple shape to ensure that the object must be within this simple shape. If the light cannot even touch the
bounding box, it must not touch the object in the bounding box.insert image description here

For three-dimensional shapes, the most commonly used is cuboid - the shape formed by three different pairs of opposite sides.
This is also AABB - axis-aligned bounding box
. The so-called axis alignment means that any axis of the cuboid is along the coordinate axis.insert image description here 

Next, we consider the intersection of the ray and the bounding box.
We first consider it from a two-dimensional perspective (formed by two different pairs of opposite faces). The three-dimensional can be obtained in the same way.
For a given ray,
we can separately calculate its vertical and At the intersection of the horizontal plane (t may be positive or negative at this time),
we take the max in min and the min in max,
so we get the value of t that enters and exits the bounding boxinsert image description here 

From the three-dimensional situation,
the light can only be said to have entered the box only
when the light has entered the three groups of opposite sides. As long as the light leaves
any pair of opposite sides , it has left
the box. It is the last entry time, and the min in max is the earliest exit time
. When the entry is less than the exit time, it means that the light is in the box between theminsert image description here 

The ray is not a straight line, but a ray,
so when there is an intersection, we need to check t.
The box is behind the ray—the exit time is negative.
The starting point of the ray is inside the box—the exit time is negative and the entry is positive
. Summarize in AABB In the case of
iff (if and only if), the entry and exit t is less than the exit t and the entry t is a positive number.
At this time, there is an intersectioninsert image description here 

Why do we use axis-aligned bounding boxes?
Because in the case of axis alignment, we can only use the information of a certain axis when calculating t, without using the entire coordinates, which is easier than dot multiplication calculationsinsert image description here 

6 Speeding up ray tracing with axis-aligned bounding boxes

If the scene is very complicated, we will first find the bounding box, and then intersect the objects according to the bounding box.
The process of finding the bounding box is the process of space division

6.1 Uniform Space Division

  • First we find the bounding box of a sceneinsert image description here

Then divide the bounding box into a bunch of grids and insert image description here then determine the grid that intersects with the object 
insert image description here

After finishing the above preprocessing, we get the grids that may contain objects.
In the next process of ray tracing, we only need to judge whether the ray hits the grid with objects. If not, skip it directly. The object in the box intersects
(here it is assumed that the ray and the object intersect very slowly, and the intersect with the box is very fast).
In the figure below, a ray hits along the upper right. How do we know which box it will touch?
The simplest idea is , if it is the upper right, then the next box must be on the right or above (this is also a topic that was not mentioned before rasterization, how to rasterize a line)
insert image description here

 

The so-called acceleration means doing more intersecting between light rays and boxes, and less intersecting light rays and objects. How about the acceleration effect?
​​If it is divided into 1×1 grids, then there is no acceleration effect insert image description here

If the division is too dense, the efficiency is not high 
insert image description here

 According to experience, people probably conclude that it is better to divide the number of grids into 27 times the number of objects in the scene. The
insert image description here
grid division method is more effective on a large number of uniformly distributed objects. 
insert image description here

However, it does not perform well in complex and empty scenes, and there will be a phenomenon similar to looking for a teapot in a stadium.insert image description here 

6.2 Space division

In the grid method, the space is divided into grids of the same size. However, some open places do not need to do this, which is too wasteful. We want to use large boxes where there are no objects, and use dense boxes where there are objects
. Spatial Partitioning Methodinsert image description here

The first on the left is the octree division. First cut the three-dimensional space into eight parts (for two-dimensional, it is four parts in the picture), and then cut it again for each child node. How to stop by analogy, we can set a
similar For example, in the two-dimensional case, when you cut into four pieces, there is no object in the three pieces, so you don’t need to cut down. The idea is to divide to a certain extent and stop cutting if there are no objects. But people don’t like
octrees , because for example, the division method in two dimensions is a quadtree, and the three-dimensional is an octree, so the dimension is higher, that is, the nth power of 2 fork tree, which is not good, and the higher the dimension, the more complicated it is to
solve This problem allows the space to be divided and has nothing to do with the dimension. People invented the KD tree.
The division method of the KD tree and the octree is almost the same, except that it looks along a certain axis every time, and only cuts once, forever In this way,
the space is divided into a result similar to a binary tree. Each time a node is divided, there are only two sub-nodes.
For the sake of uniformity,
there are many ways to cut xyz in sequence, such as BSP tree, which is a method of dividing space into two. It chooses a direction to cut each time. The difference between it and the KD tree is that it is not cut horizontally and vertically, and it will have the problem that the higher the dimension, the harder it is to calculate (cut the two-dimensional line, cut the three-dimensional surface , the higher the dimension, the more complex)

6.2.1 KD tree

The following is an example of KD tree division.
Let’s make it clear that these space division methods are to divide the space acceleration structure before ray tracing, and then perform ray tracing
. right doing

  • At the beginning, it is divided vertically into 1 B
    , then B is divided into 2 C,
    and so oninsert image description here

Which axis is the current node divided along?
Where is
the division? The intermediate node must have child nodes.
The final object is stored on the leaf node.insert image description here 

Let's see how this structure will speed up ray tracing

  • Consider we divide into this case
    insert image description here

First consider the largest bounding box A, and find that there is an intersection, then there may be intersections for the left and right child nodesinsert image description here 

We found that there is an intersection with the blue one on the left.
We assume that it is a leaf node, so let’s do this first, continue to look at the right, and find that there is also an intersectioninsert image description here 

insert image description here 

By analogy, judge 2, then judge C (figure omitted),
and then judge 3, and find that 3 is a leaf node, intersect with the object in 3, and find the intersection pointinsert image description here 

KD tree can create some problems

  • Given the bounding box of a node, we need to know which triangles it intersects with, which is difficult and not easy to write
  • If an object has intersections with many bounding boxes, it may exist in many leaf nodes

    6.3 Object division & BVH

    There are some problems in space division, so people found another method, we do not divide from space, we start from objects, and the acceleration structure formed by this division is the so-called BVH

  • How does BVH work? At the beginning, there is also a box surrounding the scene as the root nodeinsert image description here

 Then we divide the object into two parts (how to find it later), and then re-find the bounding boxes of these two partsinsert image description here

Taking the blue node as an example, we continue to divide and then recalculate the bounding box, and divide it into relatively few triangles, such as about 5 triangles to stopinsert image description here 

 The characteristic of BVH is that an object can refer to exist in a node, and there is no need to calculate the intersection of triangles and bounding boxes, but the
division of BVH is not divided, the bounding boxes may intersect, but there is no problem, we do overlap as much as possible Less is better, this is also the direction of current research.
The figure below summarizes
insert image description here

There are many methods on how to divide a node,
such as choosing the longest axis to divide or taking the middle object (n/2th triangle) (you can use the fast division algorithm)
mainly to ensure the balance of the treeinsert image description here 

Also make a summary about the storage structure of BVH, only child nodes store objects 

 insert image description here

 How to speed up ray tracing is actually similar to KD tree, measuring objects at child nodesinsert image description here

 One is the division of space, and the other is the division of objects. The following is a summary
insert image description here

So far we have finished talking about whitted-style ray tracing. 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/jiuzhouhi/article/details/123189708