A random talk on Shadow technology in games (Shadow technology comparison) __gpu/opengl/shader

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_55288aa20100e2fk.html

With the more and more high-end hardware, a variety of previously expected and not the effect of more and more applications to online games. This article is about the current implementation of the shadow in the game, their advantages and disadvantages as well as the application of the occasion. Generally speaking, there are three kinds of shadow techniques applied in the game, projective shadow, shadow Map and Shadow map. We'll introduce them in turn.
projective Shadow
A random talk on Shadow technology in game
The principle of projection can be applied to any map.

Is what we usually call projection shadow, its implementation is relatively simple, there are two specific steps:
1. Take the light source as the point of view, render the object with shadow to a ready shadow texture. Note that the rendering here, when calculating the projection, needs to be converted from the device space to the texture space to facilitate the subsequent textures.
2. Render the object covered by the shadow and then overlay the shadow onto the object. This is the most critical step for the entire projective shadow. Through the shadow texture provided above, a pass can be used to render the object affected by the shadow. It can also be rendered using multiple passes, depending on the actual application through blend. When rendering shadow texture, the API is required to generate the final mapping coordinates for it. In DX, you need to set the D3dtss_texcoordindex to D3dtss_tci_cameraspaceposition and set the D3dtss_texturetransformflags to D3dttff_. Projected. If you use OGL, you can set the Gl_texture_gen_mode to gl_eye_linear and set the texture matrix to be a projection texture matrix.

The projective shadow principle is simple, realizes conveniently, the request to the hardware disposition is very low, the effect is also good. For a game that doesn't require a lot of shadow effects, you can add only a few code. However, since projective shadow is needed to compute the object that can be projected by the shadow, it is necessary to compute the corresponding receiver according to the Shadow Caster, which consumes a lot of CPU time when the scene object is too large. At the same time, the management of object rendering in scene is also a challenge to real-time application, we must carefully manage the hierarchical relationship of object rendering.

Shadow MapShadow map is a mature technology, and for its principles, many of the first-time enthusiasts of graphics can clearly speak out his implementation steps. In short, it is a technology based on image space. First, it needs to render the entire scene as a point of view based on the location of the light source to get their depth information relative to the light source, and then use the depth information to determine which part of the scene is shaded.
A random talk on Shadow technology in game
Judge whether the point is in the shadow

Standard Shadow Map, you can find an example of it in the DX SDK, and it doesn't seem to be a complex implementation. But unfortunately its role is only to stay in the ability to teach. In other words, it is not practical, even in the example, you can easily find its shortcomings.
First of all, its precision is not enough, although the DX example can be seen in the past, but because the shadow map is based on the entire scene to render, that is, it will use a map to render the entire scene shadow. In this way, the texture size used to store information directly affects the quality of the shadow. Small scenes can also barely cope with, but only need to encounter a slightly larger scene, the shadow is likely to appear very serious sawtooth, affecting the quality of the shadow. In addition, the accuracy of depth buffering is difficult to solve, especially when the shadow of the caster and receiver is very close to the situation, z-fighting happened very clearly.
Of course Shadow map also has its own advantages, easy to implement, high efficiency, easy to understand, and so on, especially because it is based on the image space of a technology, you can use a small price to achieve the effect of soft shadow. So in recent years, many graphic scholars have also put forward many improved algorithms based on the principle of Shadow map, and Shadow map is more and more used in large games. What we are going to introduce is also the idea of an improved algorithm for Shadow map ――perspective Shadow map (PSM), which needs to be noted that the idea itself is imperfect. But with it as the leading thought, many new algorithms have been put forward in recent years.
PSM is in the current camera perspective of the space in the calculation, with the traditional graphics theory, is normalized in the space of the device in the calculation. Its basic idea is to map the scene to the Post-perspective space first, and then in this space to generate a standard shadow map by converting the transformed light into a normalized space.
In the following figure, S is the visual cone produced by the scene bounding,l Light, V is the cone of sight, and M contains all the suspected from V to L. The key to this algorithm is to finally be able to generate the yellow area in the image below H,h contains all the space the shadow map needs to contain. This is also the smallest bounding to generate post-perspective space.
A random talk on Shadow technology in game

With the bounding, the post-perspective space is easy to get.
Based on the above calculation, the matrix of post-perspective space is obtained. The other steps are consistent with the standard shadow map.
A random talk on Shadow technology in game
The standard shadow Map
A random talk on Shadow technology in game
Perspective Shadow Map

The same point of view, the same texture size, the use of PSM we can get more detailed results.
But PSM also has some flaws, such as the shadow of the quality of more dependent on the point of view, shadow near and far from the z distribution is too big difference. TSM,LISPSM in recent years have been based on the principle of PSM to make some improvements in these problems, but also make shadow map more use in the game.
Shadow VolumeShadow volumes technology is a geometry based technique that requires the contours of the geometry in a certain direction to produce a closed volume, and then the shadow of the scene can be determined by the projection of the light (usually using a template buffer to simulate the light projection).
The greatest advantage of this technique is that the shadow can be accurate to the pixel level. The shadow's effect will be very meticulous. But it also has some drawbacks, first of all the geometry has a certain requirements (must be closed), but also for the efficiency of the loss is very large (high filling rate).
In order to construct the shadow body, we need to draw from the position of the light by projecting each vertex of the shadow object, these projected rays form a closed volume, any point in this volume that we can think of as being covered by shadows. Conversely, the point outside the volume is in the light.

A random talk on Shadow technology in game
Objects in the shadow will receive the shadow.

In general, there are two basic algorithms for judging whether an object is in shadow: Z-pass and Z-fail. Simply put, the z-pass algorithm is to draw a ray from the point of view, when the rays into the shadow body, stencil plus 1, when the rays out of the shadow body, stencil minus 1. In this case, if the value of the stencil is 0 after the calculation is completed, it means that the object is not in the shadow. For the z-fail algorithm, the difference is that when rendering the shadow, there are some special judgments about the situation on the back face and the front face, and when the z test for the back face fails, the stencil value is added to 1, and the z of the face of front When test fails, the stencil value is reduced by 1.
For building shadow volume, it is important to find the contour of an object. The basic idea is to find all the shared edges that are facing the opposite two triangles, eventually forming the contours of the entire object.

SummaryThis paper explains several ways to generate shadows in the current mainstream. On the whole, each has its own application situation. Projective shadow is more suitable for use in scenarios where the requirements for efficiency are higher and the shadow requirements are not completely true. Shadow map is more suitable for shadow rendering of static objects in scenes, and it can also achieve softer soft shadows. And shadow volume is more suitable for the dynamic objects in the scene, such as characters, monsters, windmills and so on. You can choose the right technology according to the characteristics and requirements of your specific project.

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.