To increase the expressiveness of scenes, adding illumination effects to rendering is essential. First, a certain algorithm is used to generate the illumination rendering effect for the entire scene, and then the illumination rendering effect is stored as a texture. Then, the illumination effects corresponding to each polygon in the scene are stored in the illumination map, in this way, in real-time rendering, you only need to add multi-texture fusion to the scene based on the original texture rendering to render the light shining effect. However, the obtained illumination map corresponds to each single polygon in the scenario. Therefore, if n polygon exist in the scenario, N illumination maps are required, in this way, texture binding is required for each polygon during rendering. Due to the nature of hardware, this operation is time-consuming and affects the overall rendering efficiency, therefore, we need to combine the illumination textures corresponding to each polygon to obtain the illumination textures of the entire scene to avoid texture switching during rendering. As a result, the entire lighting map generation process includes:

**1.**

Scene Rendering with illumination

**2.**

Generation of a single polygon illumination texture map

**3.**

Combine polygon illumination texture maps in the entire scenario

Scene Rendering with illumination

This operation is the key to generating the illumination texture map for each polygon in the next step. In general, the pre-processing lighting effects used in the game are global lighting, including shadows, diffuse, and other effects that cannot be achieved by conventional rendering pipelines. Because preprocessing is used to generate the illumination, the illumination algorithm has nothing to do with the viewpoint, so that the illumination effect can be generated for the entire scenario. The radiation level algorithm is consistent with the above features, so it is often used to generate the illumination effect of the scene, and the illumination effect is easily mapped with each polygon to obtain the illumination texture map.

Generate a polygon illumination Map

The generation of a single polygon illumination map is closely related to the implementation of the radiation degree algorithm. The radiation algorithm segments the scene in a grid, and then colors each small patch. Therefore, for an input original scenario, after being rendered by the radiation degree algorithm, it becomes another scenario after the mesh is refined, but the two scenarios are easily matched. In implementation, the preprocessing operation of the radiation degree algorithm is to use the octotree to divide the scenario, based on this division principle, it is easy to map an original polygon to multiple sub-polygon after Mesh Subdivision.

As shown in the following figure, multiple sub-polygon are obtained after space division of an original polygon. The operating unit of the radiation degree algorithm is these sub-polygon. After obtaining the coloring effect of each sub-polygon, combine multiple sub-polygon to obtain the illumination coloring texture of the original polygon.

Because an October tree is used for scene segmentation, and its split plane has the axial alignment property, it is easy to match the child polygon with the original polygon by using this property. After the child polygon is matched with the original polygon, the corresponding relationship is required to generate the illumination map of the original polygon. When the radiation level algorithm generates global illumination, due to the high cost of the algorithm, the Space Division will not be too detailed. Otherwise, too many sub-polygon will cause the Global Illumination generation time to be too large. In this case, when the texture of the original polygon is generated by the combination of sub-polygon, the texture granularity needs to be determined separately. Generally, the brightness of the illumination texture is greater than that of the scene, in this way, you only need additional texture color interpolation to generate a smooth illumination map.

As shown in, the blue mesh is a grid with a polygon texture, Which is finer than the scene division in the radiation degree algorithm.

The specific operations for generating illumination textures are as follows:

**1.**

Rotate the original polygon and all its child polygon to an axis alignment plane (xoy, XOZ, yoz) based on its normal vector.

When determining the color value at the texture mesh, it becomes a degradation to a plane. AABB is more efficient.

**2.**

Find the original polygon after rotation and the AABB box (surrounding plane) of the sub polygon ).

**3.**

The size of the texture mesh is determined based on the size of the rectangle surrounded by the original polygon and the texture precision, and the rectangle surrounded is subdivided.

**4.**

For each small grid block in the texture mesh, traverse the entire sub-polygon list and use this small block to enclose the rectangle with the sub-polygon.

The final color value of this small block is determined based on the result of the intersection test. In this example, you can determine the value based on the interpolation range.

Returns the number of child polygon to be sent.

**5.**

Use the texture mesh and the color of each mesh block to generate the final illumination map of the original polygon.

**6.**

Determine the texture coordinates of each vertex based on the projection relationship.

Combine polygon illumination texture maps

After completing the preceding operations, you can obtain the texture coordinates of each polygon and the texture coordinates of the polygon. However, because the size of the original polygon is not fixed in the scenario, the size of the illumination map of the original polygon is not fixed, and the variation range may be large, if multiple textures are used directly for scene rendering, the rendering efficiency is low, and hardware resources cannot be used well, therefore, we need to combine the textures of each polygon to obtain the illumination texture of the entire scene.

The problem of texture merging can be described as an algorithm problem in which N rectangles of varying sizes cannot overlap with each other, minimize the total area after you arrange the data in this way. This is actually an NP problem. It is difficult to find an optimal solution. Therefore, we can only use a method similar to the optimal solution to replace the optimal solution. Greedy algorithms can better solve such NP problems. The greedy algorithm is used to solve the problem in the optimal direction for each operation. In this way, the optimal solution may not be obtained, but it is often very similar to the optimal solution.

Apply the greedy algorithm to the texture merge operation to obtain the following Operation Principle: For each texture, select a location that can generate at least blank areas for placement. For the data structure used during the operation, combined with the spatial segmentation operation in three-dimensional scenarios, it can also be organized using a tree structure. For each texture to be placed, use the greedy principle to traverse the entire tree to find an optimal position and then place and update the entire tree.

The procedure is as follows:

**1.**

Calculate all texture areas and obtain the width (or height) of the final texture maxwidth

The determination principle is as follows: maxwidth = max (maximum width of all textures, SQRT (allarea ))

**2.**

Initialize a blank texture: Height = 0, width = maxwidth and generate a binary tree structure to maintain it.

**3.**

All textures are sorted based on their height. The height is large and the height is small.

**4.**

For each texture, find its optimal placement location and update the information of the relevant nodes in the tree and the structure of the entire tree.

**Texture Filling**

** ****Tree node Structure**

Note: The greedy algorithm is used to determine the maximum width of the final texture (otherwise, the algorithm cannot control the increase of the two free variables ), therefore, the greedy principle is changed to minimizing the height of the total texture. Therefore, it is necessary to sort all textures by height. Otherwise, the effect of the layout will be poor.

**Last sorted by height**

Sort by height

After completing the preceding operations, you can obtain the combined illumination map for the entire scenario: