A terrain digging method and electronic device based on GPU calculation
By processing DEM data and image data on the GPU, generating vertex and mesh models, and removing masked textures in the fragment shader, the problem of low CPU pit digging efficiency is solved, and efficient and real-time terrain pit digging effect is achieved. Especially when digging holes multiple times, performance is significantly improved.
Patent Information
- Application Number
- CN202411330109.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-24
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2044-09-24
AI Technical Summary
In the prior art, CPUs are inefficient for terrain digging, which cannot meet the needs of large-scale terrain real-time rendering, especially when multiple terrain digging is significantly insufficient.
Using GPU-based computing method, by uploading DEM data and image data to the GPU, generating vertex and mesh models of the area to be dug, creating masked textures, and removing some fragments from the fragment shader to achieve efficient rendering of the terrain and Digging effect.
It significantly improves the efficiency and real-time performance of terrain pit digging, reduces CPU burden, realizes pixel-level terrain pit digging effect, and shows more obvious performance advantages when digging multiple pits.
Smart Images

Figure CN118840449B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image processing, and in particular to a terrain digging method and electronic equipment based on GPU calculation. Background Art
[0002] With the rapid development of computer graphics and game development, the requirements for 3D terrain rendering and real-time rendering are getting higher and higher. Especially in large-scale terrain scenes, how to achieve efficient and real-time terrain modification has become an urgent problem to be solved. Before the emergence of GPU, the research on large-scale terrain rendering mainly focused on the reduction and simplification of terrain, mainly using CPU for processing. However, as the scale of terrain data continues to increase, the processing power of CPU has gradually become a bottleneck and cannot meet the needs of real-time rendering.
[0003] In recent years, GPU technology has developed rapidly, and its powerful parallel computing capabilities have been widely used in graphics rendering, scientific computing, machine learning and other fields. The emergence of GPU provides a new solution for large-scale real-time terrain rendering.
[0004] Modern GPUs have hundreds of processors and a large number of arithmetic logic units, which can perform a large number of computing tasks in parallel. This gives GPUs a significant advantage in processing large-scale terrain data. GPU-based terrain rendering technology has become a hot topic in current research.
[0005] The existing technologies involved in this solution include:
[0006] GPU, the full name of which is Graphics Processing Unit, is a microprocessor specially designed for handling graphics and visual computing tasks. Compared with the traditional central processing unit (CPU), GPU has more cores and can process thousands of threads at the same time, which makes it more efficient when performing parallel computing tasks. GPU reduces the graphics card's dependence on the CPU and performs some of the original CPU's work, especially in 3D graphics processing. The core technologies used by GPU include hardware T&L (geometry transformation and lighting processing), cubic environment material mapping and vertex blending, texture compression and bump mapping, dual texture four-pixel 256-bit rendering engine, etc., and hardware T&L technology can be said to be the symbol of GPU.
[0007] In this solution, texture is one of the commonly used technologies in graphics rendering, which can be used to simulate the material and color of the surface. In this solution, by creating a pit texture and passing it to the GPU, the pit effect can be realized in the fragment shader.
[0008] Digital Elevation Model (DEM) is a digital simulation of the ground terrain (i.e., a digital expression of the terrain surface morphology) achieved through limited terrain elevation data. It is a physical ground model that represents the ground elevation in the form of a set of ordered numerical arrays. It is a branch of the Digital Terrain Model (DTM), and various other terrain characteristic values can be derived from it.
[0009] Shader is a program used in GPU to process graphics rendering. Fragment Shader and Vertex Shader are two key components in the graphics rendering process, which work together to convert the input graphics data into the image that is finally rendered on the screen. By modifying the code in the fragment shader, specific fragments can be eliminated or modified to achieve the pitting effect.
[0010] Vertex shader: A vertex shader is a set of instruction codes that are executed when a vertex is rendered. It is mainly responsible for processing the input vertex data (such as position, color, texture coordinates, etc.), performing a series of transformation operations (such as translation, rotation, scaling, etc.), and outputting the transformed vertex position and attributes. The vertex shader converts vertices from object space (or model space) to clip space (or standardized device coordinate space) in preparation for the subsequent rendering process. When rendering a vertex, the API executes the instructions in the vertex shader. The vertex shader allows the user to control the rendering process of each vertex, including determining its position, whether it is displayed on the screen, etc.
[0011] Fragment shader: The fragment shader is responsible for processing the fragments (pixels) of each primitive (such as a triangle) and calculating the final color and depth values of each fragment. It can perform various operations such as texture mapping, lighting calculations, shadow processing, etc., and finally output the color and depth information of each fragment. The fragment shader is a very core part of the graphics rendering pipeline and is essential for generating high-quality image effects. It allows users to programmatically control the color and depth of each pixel to achieve complex visual effects.
[0012] GDAL (Geospatial Data Abstraction Library) is a powerful open source geospatial data processing library that provides a wide range of tools for reading, writing and processing multiple geospatial data formats. GDAL was originally designed for processing raster data, but with the release of version 2.0, it has integrated the OGR library to support the processing of vector data.
[0013] OpenGL (Open Graphics Library) is a cross-programming language and cross-platform programming graphics programming interface. It abstracts computer resources into OpenGL objects and abstracts operations on these resources into OpenGL instructions.
[0014] API, short for Application Programming Interface, is a set of predefined functions, protocols, and tools for building software applications. API allows different software systems to interact with each other and is a key technology for implementing modular programming. Through API, developers can use a function or access a service without having to understand the internal workings.
[0015] OpenGLES (OpenGL for Embedded Systems) is a subset of the OpenGL 3D graphics API, designed for embedded devices such as mobile phones, PDAs, and game consoles, and removes many unnecessary and low-performance API interfaces. This solution can be widely used in computer games, virtual reality, digital earth, and terrain research. In these scenarios, terrain modification is a common requirement, such as terrain destruction effects in games and terrain interactions in virtual reality. Traditional terrain modification methods usually require complex data processing and calculations on the CPU side, which not only increases the burden on the CPU, but also limits the real-time and efficiency of terrain modification. This method uses GPU computing to dig pits in the terrain. Compared with the existing method of using the CPU to calculate the terrain mesh and re-interpolate, it can greatly improve the efficiency of terrain digging, especially when multiple digging is required on the terrain, the advantage is more obvious. Summary of the invention
[0016] The purpose of the present invention is to overcome the problem of low efficiency of digging pits using a CPU in the prior art, and to provide a terrain digging method and electronic equipment based on GPU calculation.
[0017] In a first aspect, the present invention provides a terrain digging method based on GPU computing.
[0018] S1: Upload DEM data and image data to the GPU, and the GPU generates vertices of the area to be dug according to the DEM data and constructs a grid model of the area to be dug;
[0019] S2: Upload the pit digging data, and create a mask layer according to the image data and the pit digging data; S3: Transfer the mask layer to the GPU as a texture;
[0020] S4: constructing a vertex shader to receive the vertex data; constructing a fragment shader code to receive the image data and the mask; modifying the fragment shader of the layer, receiving the texture, removing some fragments of the texture, and obtaining a new texture;
[0021] S5: adding a layer of the pit to be dug according to the grid model of the area to be dug, and rendering it in the GPU; S6: applying the new texture to the grid model of the area to be dug, and the GPU outputs the digging terrain.
[0022] In this solution, by uploading DEM (digital elevation model) data and image data directly to the GPU, the high-speed memory bandwidth and parallel processing capabilities of the GPU are utilized to achieve fast data loading and preparation. The GPU automatically generates vertices of the area to be dug based on the DEM data, and then builds an accurate mesh model. This process is completed on the GPU, which greatly reduces the burden on the CPU and improves the real-time performance of terrain generation. At the same time, by writing vertex shader and fragment shader code, full control of the terrain rendering process is achieved. The vertex shader processes vertex data, and the fragment shader processes pixel data. The two work together to achieve accurate rendering of the digging effect. In the fragment shader, the dynamic generation of the digging effect is achieved by receiving the mask texture and culling some of its fragments. This process is completely executed in parallel on the GPU, ensuring the efficiency and real-time performance of rendering.
[0023] Preferably, the image size is obtained according to the image data, a blank image is created in the area to be dug according to the image size, the size of the blank image is consistent with the image data, and the transparency of all pixels of the blank image is set to 0; the digging data includes one or more polygons, and the transparency of all pixels of the digging part of the area to be dug on the blank image is set to 1 according to the digging data to obtain a mask.
[0024] By uploading pit data, such as the location, shape, and size of the pit, combined with image data, you can flexibly create a pit mask in the specified area. This provides users with a high degree of customization. The creation of the mask is based on accurate image data and pit data, ensuring that the pit effect is highly consistent with the real terrain. By passing the mask as a texture to the GPU, the pit effect can be achieved through texture mapping, further improving the flexibility and efficiency of rendering. By using the GPU's efficient texture processing capabilities, the creation and application of the pit mask is accelerated.
[0025] Preferably, the image data and DEM data are read by GDAL, including vertex coordinates, indices and texture coordinates.
[0026] Preferably, the fragment shader for modifying the mask layer needs to define a uniform variable to receive the texture, and the partial fragments are fragments with a transparency greater than 0.5.
[0027] An electronic device comprises at least one processor and a memory in communication with the at least one processor; the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute any one of the terrain digging methods based on GPU computing described above.
[0028] Compared with the prior art, the present invention has the following beneficial effects:
[0029] The present invention provides a terrain digging method and electronic device based on GPU calculation. By traversing each pixel of the terrain image to be dug through GPU calculation, pixels within the polygon of the digging layer are removed, so that the terrain digging effect at the pixel level can be achieved. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 is a flow chart of the present invention;
[0031] Figure 2 This is a flow chart of Embodiment 1 of the present invention;
[0032] Figure 3 The vertices and grid diagram of the area to be dug in Example 1 of the present invention;
[0033] Figure 4 This is a pit digging range diagram of Example 1 of the present invention;
[0034] Figure 5 This is a schematic diagram of points that need to be removed in Example 1 of the present invention;
[0035] Figure 6 The edge point map of embodiment 1 of the present invention;
[0036] Figure 7 The reconstructed terrain grid model overlay image map of embodiment 1 of the present invention;
[0037] Figure 8 is a schematic diagram of superimposed images according to Embodiment 1 of the present invention;
[0038] Fig. 9 This is a schematic diagram of the overall pit digging picture of Example 1 of the present invention;
[0039] Fig.10 A grid map is superimposed on the picture processed by the GPU of Example 1 of the present invention;
[0040] Fig.11 This is a time comparison chart of the pit digging method of Example 1 (unit: ms);
[0041] Fig.12 The structure of an electronic device according to Embodiment 2 of the present invention using a method according to Embodiment 1
[0042] Schematic diagram. DETAILED DESCRIPTION
[0043] The present invention is further described in detail below in conjunction with test examples and specific implementation methods. However, this should not be understood as the scope of the above subject matter of the present invention being limited to the following embodiments, and all technologies realized based on the content of the present invention belong to the scope of the present invention.
[0044] In addition, in the description of the technical solution of the present invention, unless otherwise clearly specified / defined / restricted, the terms "set", "install", "connect", "connected", "provided with", "laid", and "arranged" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection, and can be welding, riveting, bolting, threading, and other commonly used connection means in the field. This connection can be a mechanical connection, an electrical connection, or a communication connection; it can be a direct connection, or an indirect connection through an intermediate medium, and it can be the internal connection of two elements.
[0045] Example 1
[0046] A terrain digging method and electronic equipment based on GPU calculation, the flow chart is as follows Figure 1 As shown, the following steps are included:
[0047] S1: Upload DEM data and image data, generate vertices of the area to be dug, and build a grid model of the area to be dug;
[0048] S2: upload the pit data and create a mask layer;
[0049] S3: Upload the mask layer as a texture to the GPU;
[0050] S4: constructing a vertex shader to receive the vertex data; constructing a fragment shader code to receive the image data and the mask; modifying the fragment shader of the mask, receiving the texture, removing some fragments of the texture, and obtaining a new texture;
[0051] S5: adding a layer of the pit to be dug according to the grid model of the area to be dug, and rendering it in the GPU; S6: applying the new texture to the grid model of the area to be dug, and the GPU outputs the digging terrain.
[0052] As a specific embodiment, the following steps are included:
[0053] User uploads data: upload DEM data and image data to GPU, use GDAL library to read DEM data and construct vertex data. Vertex data includes vertex coordinates, index, texture coordinates, etc. Figure 2 As shown;
[0054] Further, the GPU generates vertices of the area to be dug according to the DEM data, constructs a mesh model of the area to be dug, and OpenGL constructs a vertex shader code to receive the vertex data; the image size is passed to the GPU according to the image data, and the GDAL creates a blank image according to the image size, the size of the blank image is consistent with the image data, and the transparency of all pixels of the blank image is set to 0;
[0055] Upload the pit digging data, which is one or more polygons. GDAL reads the pit digging data and sets the transparency of all pixels in the pit digging area to 1 on the blank image. The resulting image is the mask layer, such as Fig. 9 As shown;
[0056] The OpenGL constructs the fragment shader code, the GPU receives the image data, and the mask is input into the GPU as a texture; specifically, a layer to be dug is added according to the mesh model of the area to be dug, and rendered in the GPU, and the fragment shader of the layer is modified at the same time, a uniform variable is defined to receive the texture passed in by S5, and fragments with a transparency greater than 0.5 are removed in the fragment shader. Part of the shader code is as follows:
[0057] Uniform sample 2D mask_tex; / / define a uniform variable mask_tex
[0058] In vec4 layer_tilec; / / Define an input variable to store texture coordinates
[0059] void mask_me(inout vec4 color) / / defines a function mask_me, which accepts an inout type vec4 parameter color
[0060] {
[0061] vec4 mask=texture(mask_tex,layer_tilec.st); if(mask.a>0.5)
[0062] discard;
[0063] }
[0064] This code is written in GLSL (OpenGL Shading Language), a high-level language for writing OpenGL shaders (such as vertex shaders and fragment shaders). GLSL allows developers to write and execute programs directly on the GPU to process vertex and pixel data in the graphics rendering process.
[0065] Code description: "uniform sampler2D mask_tex;": defines a uniform variable mask_tex, which is the texture passed in by S5. Uniform variables are the same for all fragments that call it during shader execution. They are usually used to transfer data from the CPU to the GPU.
[0066] First, sample the pixel value corresponding to the current fragment from the mask_tex texture through texture(mask_tex,layer_tilec.st), and store this value in the vec4 type variable mask. Here, layer_tilec is mainly used to save the texture coordinates and other information of the current layer to be dug. The pixel value of the overlapping area between the current area to be dug and the digging range image can be extracted through the texture method, that is, the mask variable; modify the fragment shader of the layer, receive the texture, remove some fragments of the texture, and obtain a new texture.
[0067] Check if the alpha component (transparency) of the mask is greater than 0.5. If so, the discard statement is executed. Discard is a special statement in GLSL that tells the GPU to discard the fragment currently being processed, which will not have any effect on the final rendered image.
[0068] S7: Apply the new texture to the grid model of the area to be dug, and the GPU outputs the digging terrain, such as Fig.10 shown.
[0069] Among them, the grid model of the area to be dug is as follows: Figure 3 As shown, the grid model of the area to be dug is constructed by modifying the value of the specified pixel of the image through python and opencv.
[0070] The process of transferring texture to GPU in S5 uses OpenGL to communicate with GPU. CPU and GPU are in a CS mode, i.e. client-server mode. The client cannot directly access server resources.
[0071] Operations can only be performed through network protocol interaction and are performed by the server. The CPU is the client and the GPU is the server. The specific steps of the blocking OpenGL API include:
[0072] First, glGenTextures: notifies the graphics card to create a texture object and returns a handle; second, glBindTexture: binds the texture to a specific texture target;
[0073] Finally, glTexImage2D: uploads the image RGB data to the GPU.
[0074] The existing terrain digging method based on CPU calculation usually includes the following steps:
[0075] 1. Generate vertices of the area to be dug based on terrain data and build a grid model of the area to be dug, such as Figure 3 As shown;
[0076] 2. Through user input, one or more polygonal areas to be dug are formed on the terrain grid, such as Figure 4 As shown;
[0077] 3. Traverse each vertex in the area to be dug. If it is in the digging area, it will be removed. If it is not in the digging range, it will be retained. Figure 5 As shown;
[0078] 4. Calculate the intersection of the grid of the pit area to be dug and the edge of the pit range, and add it to the vertex of the pit area to be dug, such as Figure 6 As shown;
[0079] 5. Reconstruct the terrain mesh, such as Figure 7 As shown;
[0080] 6. Overlay one or more image layers on the terrain grid, such as Figure 8 shown.
[0081] This type of algorithm usually requires the CPU to recalculate vertex positions and build a new terrain mesh, which is inefficient. Especially when multiple areas of the terrain need to be dug, it usually cannot respond in time, affecting the user experience.
[0082] By using the image capture tool, we can get the time difference between the frame time when the rendering is completed and the time difference between the start time of the experiment, and get the time comparison between the CPU digging method and the GPU digging method. Fig.11 As shown in Figure 2, based on the experimental data of ten sets of experiments, it can be concluded that the efficiency of GPU shader mining has been improved by nearly 80%.
[0083] Example 2
[0084] like Fig.12As shown, an electronic device includes at least one processor, a memory connected to the at least one processor, and at least one input / output interface connected to the at least one processor; the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the terrain digging method based on GPU calculation described in the above embodiment. The input / output interface may include a display, a keyboard, a mouse, and a USB interface for inputting and outputting data.
[0085] Those skilled in the art can understand that: all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiments; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM), disks or optical disks, etc. Various media that can store program codes.
[0086] When the above-mentioned integrated unit of the present invention is implemented in the form of a software functional unit and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present invention can be essentially or partly reflected in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks or optical disks.
[0087] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A terrain digging method based on GPU computing, characterized in that: The following steps are involved: S1: Upload DEM data and image data to GPU, and the GPU generates vertices of the area to be dug according to the DEM data, and constructs a grid model of the area to be dug; S2: Upload the pit digging data, and create a mask layer according to the image data and the pit digging data; S3: passing the mask layer as a texture to the GPU; S4: Construct a vertex shader to receive the vertex data; construct a fragment shader code to receive the image data and the mask layer, first sample the pixel value corresponding to the current fragment from the mask_tex texture, and store the pixel value in the variable mask, extract the pixel value of the overlapping area of the current pit-digging area and the pit-digging range image through the texture method, save the layer information of the current pit-digging through layer_tilec, modify the fragment shader of the mask layer, receive the texture, remove some fragments of the texture, and obtain a new texture; S5: adding a layer of the pit to be dug according to the grid model of the area to be dug, and rendering it in the GPU; S6: applying the new texture to the grid model of the area to be dug, the GPU outputs the digging terrain, and after rendering, a fully transparent digging terrain is obtained.
2. The terrain digging method based on GPU calculation according to claim 1, characterized in that: Step S3 specifically includes the following steps: S22: obtaining an image size according to the image data, creating a blank image in the area to be dug according to the image size, wherein the size of the blank image is consistent with the image data, and setting the transparency of all pixels of the blank image to 0; S23: The pit digging data includes one or more polygons. According to the pit digging data, the transparency of all pixels in the pit digging part of the pit digging area to be dug on the blank image is set to 1 to obtain a mask layer.
3. The terrain digging method based on GPU calculation according to claim 1, characterized in that: The image data and DEM data are read by GDAL, including vertex coordinates, indices and texture coordinates.
4. The terrain digging method based on GPU calculation according to claim 1, characterized in that: The fragment shader that modifies the mask needs to define a uniform variable to receive the texture. If the alpha component of the mask is a fragment with a transparency greater than 0.5, a discard statement is executed; the discard statement is a special statement in the GLSL language, which is used to tell the GPU to discard fragments with a transparency greater than 0.
5.
5. An electronic device, characterized in that: It includes at least one processor and a memory communicatively connected to the at least one processor; the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute a terrain digging method based on GPU computing as described in any one of claims 1 to 4.
