Introduction to the Doom 3 shadow Algorithm

Source: Internet
Author: User
[Hotball technical expert shop] Introduction to Doom 3 light-playing System
 

(2004-10-21)

In this article, we will introduce the two most important parts of the Doom 3 lighting system: Shadow and concave-convex regression.
 

[Author/Yan Bingzhe]

Original article link http://www.poweruser.com.tw/

Shadow generation

After a long wait, id software officially launched Doom 3 in August this year. Although Doom 3 is not quite different in gameplay from the previous doom series games, the Doom 3 engine is a completely different and brand-new design. The biggest feature of the Doom 3 engine lies in its light logging system. It combines the complete shadow and concave and convex textures, and claims to use a consistent Lighting Model to treat all objects in the entire scenario equally. It has a very good effect and has set a new milestone for the 3D engine.

As we all know, shadows are the places where the light is not shining. In other words, if there is something between a place and the light source, it will be in the shadow. From the physical point of view, it is actually the opposite: where light is shining, it will reflect the light. Therefore, it is brighter than a place without any light. From any angle, the shadow is not only related to an object, and the entire scene will have an impact on the shadow.

Therefore, to generate a correct shadow in 3D drawing, you must consider the entire scenario. This is very different from the general lighting and coloring of objects. In a 3D drawing, changes in the color and brightness of an object are related only to the object itself and the position of the light source (sometimes with the position of the observer ). It does not involve other objects. Therefore, the effect is regional. However, the shadow effect is holistic, because any object may overwrite another object and cause a shadow as the position of the light source changes. Therefore, it is quite difficult to draw a shadow correctly.
 


Obstacles have shadows on the sphere.

Shadow with 3D graphics chip

Generally, the 3D graphics chip is designed for the regional lighting effect. The 3D chip does not retain the triangle surface data of the entire scenario. It only performs the coloring action on the Triangle Surface currently sent in. Therefore, under normal circumstances, a 3D chip cannot determine whether a triangular surface is covered by another triangular surface, so that it is in the shadow.

However, the 3D chip still has to deal with some "integrity" work. The simplest example is to deal with hidden surfaces. Objects that are closer to the observer during 3D plotting will cover objects that are far away. Therefore, the 3D chip needs to be able to determine which triangle faces will be covered. Because the 3D chip does not retain any triangular surface information, it cannot check whether the previously drawn triangular surface will cover the currently drawn triangular surface. To solve this problem, the current 3D chip usually uses Z buffer. In this way, the 3D chip can avoid hidden surface judgment in the unit of triangle surface, but in pixel.

It can be seen that the problem of shadow is very similar to the problem of eliminating hidden surfaces. If the observer is located at the light source and the entire scene is painted once, you can know which objects are directly illuminated by the light source, that is, the light will be reflected, bright objects. Relatively, no objects are directly exposed to the shadow. This method is actually the basic principle of shadow map. However, there are still many troublesome problems to overcome in shadow map, and Doom 3 does not use this method, so we will not discuss it more here. But the prospect of shadow map is promising. The latest 3DMark05 is mainly to use shadow map to generate shadows. Shadow Map has a relatively simple version, but it cannot be used in the entire scenario.

Doom 3 uses the so-called volumetric shadow, or stencel shadow (because volumetric shadow usually needs to be used to stencel buffer ). The basic principle of volumetric shadow is to "cast" the shadow of all objects. The object to be projected is in the shadow. On the contrary, an object that is not "projected" is a part that is directly illuminated by a light source and will be reflected. The biggest difference between volumetric shadow and shadow map is that shadow map is basically composed of pixel, but volumetric shadow is processed directly on the triangle rather than the object as a pixel.

Volumetric shadow

The basic method of volumetric shadow is to first find the outer frame of an object. Then, a "volume" is extended from the external frame ". This volume is the shadow range, also known as shadow volume. Any pixel in this volume is in the shadow. Therefore, the problem is how to determine whether a pixel is in volume. This is where the stencel buffer plays a role.

The stencel buffer is a bit similar to the Z buffer, which records a number for each pixel. When you draw a triangle, you can specify an operation, such as adding a number to one or subtracting one. In addition, you can specify that only the pixel of the stencel number in a specific value is processed when you draw a triangle. For example, you can only draw pixel with a value of 0 on the stencel, but not on the other pixel.


Volumetric shadow.

To determine which pixels are in shadow volume using stencel buffer, first clear the stencel value of each pixel to 0. Next, draw all the shadow volume on the triangle, which is directed at the white part of the observer, and set stencer to "add one ". In this way, all pixel after shadow volume (including Shadow volume) will have a positive value for stencer. Next, draw the part of shadow volume that is back to the observer, but this time, set stencel to "minus one ". In this way, the value of the stencel is returned to 0 after shadow volume but not in shadow volume, but the value of the stencer of pixel in shadow volume is still positive. In this way, you can use the stencel value of each pixel to determine whether it is in the shadow.

Although the basic principle of volumetric shadow is as follows, there are still many problems. The most obvious problem is that when the observer runs into shadow volume, volumetric shadow will fail. In addition, in 3D plotting, a Z clip plane is usually set to limit the range of scene plotting. However, if it is switched to shadow volume, it will generate holes and cause problems. As a result, many people have developed methods to solve these problems. Among them, the most effective method is Carmack's reverse, which was proposed by John Carmack of id software (many others contributed ). This method is used to reverse the Z buffer test, so that the observer can never appear in shadow volume. This solves the first problem. The solution to the second problem can be solved by canceling the remote Z clip plane, but this may be less efficient. Another method is to enable the display chip to process the Z clip plane on the hardware, so that the shadow volume can automatically block the shadow volume on the Z clip plane so that it will not generate holes.

After these improvements, volumetric Shadow becomes a fairly reliable method. Because it is based on a triangular surface, it is very accurate, unlike shadow map and other methods, there will be insufficient resolution issues. In addition, it does not require a lot of special hardware support, but only needs stencel buffer. Stencel buffer is a standard function of OpenGL, so many 3D chips are supported. This certainly has some impact on the decision of id software to use volumetric shadow in Doom 3.

However, volumetric shadow does not have all disadvantages. The biggest problem is that a large amount of fillrate is consumed when shadow volume is drawn. Moreover, because shadow volume is generated by the external frame of the 3D model of the object, the processor must process the calculation of the External Frame (this is not suitable for the display chip ). This increases the computing workload of the processor. It cannot be used on objects that use pasters with transparent areas. Some games use such pasters to draw complex objects, such as trees. Finally, the Shadows produced by voluemtric Shadow are sharp, but shadows in the real world are usually blurred. This is because the light source in the real world is not a small point, but has a certain size. To use volumetric shadow to generate a blurred shadow, you can use multiple neighboring point light sources to simulate a light source with a large size, but of course it will be slower. These are the main problems of shadow volume.

Concave and convex textures
In addition to the complete shadow, the extensive use of bump mapping is another feature of the Doom 3 engine. In the Doom 3 engine, most objects have normal vector textures and are used for computing the light. Therefore, almost all playing light is measured in pixel, unlike previous games that used vertex to play light. This can achieve better results.

In addition to general concave and convex textures, Doom 3 uses another method to generate concave and convex textures on objects such as characters and monsters. Due to the limited speed of display chips, 3D models of common characters or monsters do not have many triangular surfaces. In particular, Doom 3 uses volumetric shadow in large quantities and requires the processor to process these triangles to calculate the outer frames of objects. Therefore, the number of triangles cannot be too large. Therefore, id software uses a special method in Doom 3: first, design 3D models of characters and monsters with a large number of triangles. Then, simplify these 3D models to generate a 3D model with a small number of triangles. This 3D model is the 3D model used by the game community. Then, compare the original complex 3D model with the simplified 3D model to generate appropriate concave and convex textures. In this way, a simple 3D model can be used to produce effects that look close to the original complex 3D model when the concave and convex textures are used for lighting.

Conclusion
Doom 3 is a rather bold attempt by id software. Id software tries to achieve the ultimate goal of "all things have shadow" in Doom 3, so it uses volumetric shadow in large quantities. In addition, almost all objects use concave and convex textures, and the effect of hitting the light is quite good. However, this also creates a great burden on the display card and processor. Since Doom 3 is not very good in gameplay, it is important for other gaming companies to be willing to use the Doom 3 engine to develop games.
 

Alternative opinion: What is shadow map?
Because hotball did not discuss shadow map in detail, I did not use this technology in the case of different doom3 at the time, but used shadow volume as a means to generate complex shadows, so I took the opportunity to analyze it on behalf of others.

When Lance Williams published the concept of shadow ing in 1978, its starting point was to effectively use the Z-BufferAlgorithmAnd quickly generate shadows for each arbitrary object. The basic principle is as follows: if I draw the entire scenario from the perspective of the light source today, the natural Z buffer will contain the "pixel closest to the light source" distance information. The information in Z buffer is called shadow map, or depth map. It is used as a "texture", so texture is often added after the term.

The problem is that we now have this information, but how can we determine which pixel is in the shadow and which are not? Therefore, in order to parse the visible range of the entire scenario, we must use ambient light to draw a new scenario from the user's perspective. Because the light source and the user's perspective are different, the coordinate system is naturally different, therefore, we must first convert the value of Z in the shadow map to the user's coordinate system (this conversion may be different in implementation, for example, NVIDIA described in the file), and then compares the Z value in the user's perspective Z buffer. If the two are the same, the pixel is not in the shadow, and vice versa. Finally, the entire scenario is drawn by the entire light equation. pixel color, which is not in the shadow, is the color of the last normal Painting Process. pixel color in the shadow is the result of the second ambient light painting.

This is why Shadow ing can be used to process triangular surfaces with holes, because it is used to obtain shadow data directly from the light source perspective, instead of using shadow volume as the shadow range. In addition, as long as the position of the light source and the object remains unchanged, the shadow map can be used by multiple frames, but the user's viewpoint can be changed because these shadows are independent views (View-independent ). In addition, shadow ing has the advantages of the existing texture mechanism, and the linear distribution of complexity of this algorithm is also a benefit, for example, the number of objects drawn in the scenario is doubled, and the energy and time required for the entire operation are doubled, which is helpful for evaluating the game performance.

Shadow ing does not have any disadvantages. The first is the resolution and Z buffer accuracy. If the former is too low in resolution, there will be obvious blockings on the edge of the shadow, that is, the Sawtooth; if the latter is the color depth of the light source (depth) the value should be the same as the user's perspective (not in the shadow), but it is slightly different (in the shadow) due to precision issues. This is a very serious computing error. In addition, because shadow map samples during the comparison process (do not forget that it is a texture), it is easy to produce the alias problem, especially in the area near the edge. For example, the self-shadow aliasing that often appears in Shadow Mapping is an example. Because of the precision of sampling, the shadow itself is caused, and moire pattern is the best symbol of this problem. You can use bilinear filter to overcome the Sawtooth feeling of the edge, but this increases the computing burden. The latter's razor's edge problem caused by limited resolution and precision. In addition to trying to improve resolution and accuracy, we can add bias factor, which is also known as polygon offset by chip manufacturers.

However, one thing is easy to ignore. No matter what shadow ing method has been improved, they all have the same inherent limitations: if the light source is located somewhere outside the scene and viewed from a distant perspective, for example, spot light, it will work very well, which is also the "innate hypothesis" of Shadow Mapping ". However, what if the position of the light source is within the scenario? What should I do at this time? Of course, we can use a cube with six faces to represent this positional light source, and then draw the entire scenario from each of the six sides, however, in addition to the "explosive growth" Operation energy and memory space, cube cannot completely represent a scattering light source, so its seams will certainly cause some problems, in particular, the bias factor is absolutely different in the joint edge and the plane of the cube, which will cause a considerable degree of difficulty.

Finally, in the field of real-time graphic computing such as 3D games, in order to maintain a smooth FPS, we often do not use the cool and cool technology, and the lessons of history prove that, as long as you care about it, simple technologies can also achieve great results. From how John Carmack effectively uses lightmap in quake to make spot light effects, you can see a few points. In the "entrapment" movieFamous saying"It's impossible. But do able." (impossible, but possible) may be the best bet. For the real-time rendering field, finding an alternative to making complex results is the biggest challenge, but this is also a required capability for game designers.


Wen ⊙ renhao)

Original article link http://www.poweruser.com.tw/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.