A graphics rendering method and system based on GLSL shader

By adopting a GLSL shader-based graphical rendering method in OpenGL, using GPU to process vertex and slice metadata, combined with EBO and cropping technology, the problem of low rendering efficiency of OpenGL three-dimensional model is solved, and more efficient rendering performance is achieved.

CN119313799BActive Publication Date: 2025-04-08SHANDONG ZHIHECHUANG INFORMATION TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411854856.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-17
Publication Date
2025-04-08
Estimated Expiration
2044-12-17

AI Technical Summary

Technical Problem

When using OpenGL for visualization and dynamic rendering of complex three-dimensional models, the rendering efficiency is low, the time is long, and the prior art is not efficient when processing rendering operations in a multi-threaded environment.

Method used

The graphical rendering method based on GLSL shader is adopted. By processing vertex data and computed fragment colors through the vertex shader and fragment shader running in the GPU, EBO efficiently stores and accesses vertex data, crops vertex data that are not within the cone range, reduces computing resource consumption, and optimizes the rendering process through multi-threading.

Benefits of technology

It significantly improves the efficiency of three-dimensional model rendering, shortens the rendering time, reduces memory usage and computing resource consumption, and improves the performance and visual experience of graphics rendering.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119313799B_ABST
    Figure CN119313799B_ABST
Patent Text Reader

Abstract

The present application provides a graphics rendering method and system based on GLSL shader, which relates to the technical field of three-dimensional model rendering. The rendering method includes: loading a 3D model, extracting data of control points of the 3D model in a VBO of a GPU, generating a first geometric primitive, extracting vertex data in the first geometric primitive, generating an index array of the vertex data, creating an EBO, processing the vertex data in sequence using a vertex shader based on the index array stored in the EBO, obtaining the position and interpolation data of each vertex, clipping the vertex data, regenerating a second geometric primitive, converting the second geometric primitive into a fragment, using a fragment shader to calculate the color data of each fragment, and a GPU rendering the color data in parallel to obtain a final output image. While converting a 3D model to a 2D image, the present application reduces resource consumption and memory occupancy, thereby improving rendering efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of three-dimensional model rendering, and in particular to a graphics rendering method and system based on GLSL shader. Background Art

[0002] In the field of 3D model rendering technology, OpenGL (Open Graphics Library), as an open source and cross-platform graphics library, is widely used in the rendering of 2D and 3D graphics. It provides a set of APIs for operating graphics hardware, supporting the creation, rendering, and interactive analysis of complex 3D data sets. OpenGL processes the conversion and rendering of graphics data through the graphics pipeline, including components such as vertex shaders and fragment shaders. OpenGL not only has powerful visualization capabilities, but also supports image processing and finite element analysis, as well as the reconstruction of scalars, vectors, and tensors. However, there are still some technical challenges when using OpenGL for visualization and dynamic rendering of complex 3D models.

[0003] The Chinese invention patent with application publication date of October 25, 2024 and application publication number CN118840462A provides a method and system for dynamic rendering of 3D models based on OpenGL. The patent designs a 3D model visualization algorithm, which includes the creation of VAO, VBO, and EBO, the binding of VAO, the reading of index data and the connection of line segments, and the implementation of polyhedron rendering; designs a 3D model dynamic rendering algorithm, which includes the screening and partial rendering of RGB data, thread synchronization algorithm, timer and callback function algorithm. In the dynamic rendering process, the application stores rendering operations and interactive operations in different threads, but the next thread will be executed only after a thread is released, so the rendering takes a long time and is inefficient. Summary of the invention

[0004] In order to improve rendering efficiency and shorten rendering time, the present application provides a graphics rendering method and system based on GLSL shader.

[0005] In the first aspect, the present application provides a graphics rendering method based on GLSL shader, which adopts the following technical solution:

[0006] A graphics rendering method based on GLSL shader, the rendering method adopts GLSL shader running in GPU, the GLSL shader includes: vertex shader and fragment shader, the rendering method includes the following steps:

[0007] Model loading: load the 3D model to be rendered;

[0008] Primitive assembly: extract the control point data of the 3D model from the VBO of the GPU, generate the first geometric primitive based on the control point data, extract the vertex data in the first geometric primitive, and generate an index array of the vertex data;

[0009] Storing vertex data: creating an EBO and storing the index array in the EBO in sequence;

[0010] Shading: including first processing, clipping, rasterization and second processing;

[0011] First processing: The vertex shader processes the vertex data in sequence according to the index array to obtain the position and interpolation data of each vertex;

[0012] Clipping: Based on the position of each vertex, the vertex data under the current perspective is converted to the clipping plane, the key point data is obtained, and the second geometric primitive is generated based on the key point data;

[0013] Rasterization: converting the second geometric primitive into pixel-level fragments;

[0014] Second processing: the fragment shader calculates the color data of each fragment according to the interpolation data;

[0015] Rendering image: The GPU renders the color data to a frame buffer to generate an output image.

[0016] By adopting the above technical solution, the present application extracts the control point data from the VBO (vertex buffer object) of the 3D model, and generates the first geometric primitives (such as triangles, lines, etc.) based on these data, and then extracts the vertex data in these first geometric primitives to determine the rendered object. The present application also creates an EBO (element buffer object), and stores the index array of vertex data in the EBO in sequence, which helps to efficiently access and process vertex data during the rendering process. Through the index array, vertex data can be reused, reducing memory usage and improving rendering efficiency. The present application converts the vertex data under the current viewing angle to the clipping plane, obtains the key point data under the current viewing angle, and generates the second geometric primitive based on these key point data, so that only the geometric primitives within the cone will be rendered, reducing the consumption of computing resources and further improving rendering efficiency. The GLSL shader uses the parallel computing power of the GPU to efficiently process a large amount of graphics data, further reducing the amount of calculation in the rendering process and improving rendering efficiency.

[0017] Optionally, after executing the step of storing vertex data and before executing the step of shading, the method further includes:

[0018] First calculation: calculate the centroid of each first geometric primitive, record it as the first centroid, and integrate all the first centroids into a first centroid set; calculate the centroid of a triangle formed by any three non-collinear control points, record it as the second centroid, and integrate all the second centroids into a second centroid set;

[0019] Traverse the center of gravity: traverse all the first centers of gravity, obtain the first center of gravity that coincides with the second center of gravity, and record it as the third center of gravity;

[0020] Determine the index array: use the index array of the vertices of the first geometric primitive where the third centroid is located as the first-level index array in the EBO, and use the index array of the vertices of the first geometric primitive where the remaining first centroid is located as the second-level index array in the EBO.

[0021] By adopting the above technical solution, the present application calculates the centroids (first centroids) of all first geometric primitives and the centroids (second centroids) of triangles formed by any three non-collinear control points, and then obtains the overlapping first and second centroids, records the first centroid as the third centroid, and then uses the index array of the vertices of the first geometric primitive where the third centroid is located as the first-level index array, which helps to improve the accuracy and efficiency of rendering, because important visual features are often concentrated in the central area of ​​the first geometric primitive. By using the index arrays of these vertices as the first-level index array, the memory access and computing overhead in the rendering process can be further reduced. The present application also classifies the index arrays of the remaining vertices as a second-level index array. By setting the level of the index array, it can better control the resource allocation and priority management in the rendering process, which helps to achieve higher-quality graphics rendering effects with limited computing resources, thereby meeting the user's dual needs for graphics application performance and visual experience.

[0022] Optionally, after executing the step of traversing the centroid and before executing the step of determining the index array, the method further includes:

[0023] First statistics: count the number of the third centroids, recorded as the first data;

[0024] First judgment: judge whether the first data is less than one, if so, execute the step of primitive assembly; if not, execute the step of determining the index array.

[0025] By adopting the above technical solution, the present application first counts the number of third centroids. If there is no third centroid (the number is zero), the step of primitive assembly is performed to re-divide the first geometric primitive. If there is a third centroid, the step of determining index data is performed to set the level of the index array so that the most important geometric primitives can be given priority in the subsequent shading step.

[0026] Optionally, after executing the coloring step and before executing the image rendering step, the method further includes:

[0027] Initialization: Assign the color data of the i-th fragment to the first rendering thread;

[0028] Second calculation: calculate the average value of the color data of the i-th pixel, recorded as the first average value; calculate the average value of the color data of the i+1-th pixel, recorded as the second average value, and calculate the similarity between the first average value and the second average value, recorded as the second data;

[0029] Second judgment: judging whether the second data is greater than a preset threshold, if so, executing the step of replacing the fragment; if not, executing the step of first allocating;

[0030] Fragment replacement: take the i+1th fragment as the new ith fragment and perform the initialization steps;

[0031] First allocation: sending the color data stored in the first rendering thread to the GPU;

[0032] Thread replacement: store the color data of the i+1th fragment in the second rendering thread, use the second rendering thread as the new first rendering thread, use the i+1th fragment as the new i-th fragment, and perform the second calculation step.

[0033] By adopting the above technical solution, the present application calculates the average values ​​of the color data of the i-th and i+1-th fragments respectively, which helps to understand the color difference or similarity between adjacent fragments. By calculating the similarity of the two average values ​​and comparing the similarity with a preset threshold, if the similarity is greater than the preset threshold, it indicates that the color difference between the two fragments is small, and the similarity between other fragments and these fragments is continued to be calculated, and the fragments with similarity greater than the preset threshold are stored in the same rendering thread (first rendering thread). If the similarity is less than the preset threshold, the first allocation step is executed, and the color data in this rendering thread (first rendering thread) is sent to the GPU for unified rendering. After that, the fragments corresponding to the similarity less than the preset threshold are added to the new rendering thread (second rendering thread), and the similarity between the remaining fragments is recalculated. The present application can add fragments with higher similarity to the same rendering thread for unified rendering, thereby improving rendering efficiency.

[0034] Optionally, after executing the second judgment step and before executing the first allocation step, the method further includes:

[0035] Third calculation: calculate the similarity between the color data of the kth pixel in the i+1th fragment and the color data of the k+1th pixel, recorded as the third data;

[0036] Third judgment: judging whether the third data is greater than a preset threshold, if so, executing the first replacement step; if not, executing the second allocation step;

[0037] First replacement: taking the k+1th pixel point as the new kth pixel point and performing the third calculation step;

[0038] Second allocation: allocating the k+1th pixel point to the third rendering thread, and executing the first replacement step until a preset stop condition is met;

[0039] Third allocation: Send the color data stored in the third rendering thread to the GPU.

[0040] By adopting the above technical solution, when the second data is less than the preset threshold, that is, the color data difference between the i-th fragment and the i+1-th fragment is large, the present application calculates the similarity of the color data of two adjacent pixels (k-th and k+1-th) in the i+1-th fragment, which is recorded as the third data. The third data helps to understand the color difference or similarity between the pixels inside the fragment. The present application also compares the third data with the preset threshold. If the third data is greater than the preset threshold, the similarity between the next pixel and the k+1-th pixel is calculated; otherwise, the k+1-th pixel is assigned to a new rendering thread (the third rendering thread), and the above steps are executed in a loop until all the pixels in the current fragment are traversed, and then the color data in the third rendering thread is sent to the rendering pipeline of the GPU for rendering.

[0041] Optionally, after performing the second allocation step and before performing the third allocation step, the method further includes:

[0042] Second statistics: counting the number of pixels in the third rendering thread, recorded as fourth data;

[0043] Fourth judgment: judge whether the fourth data is one, if so, execute the coloring step; if not, execute the third allocation step.

[0044] By adopting the above technical solution, the present application determines whether there is a calculation error in the color data of the i+1th fragment through the number of pixels (fourth data) in the third rendering thread. Because the color data in the image has connectivity, if the fourth data is equal to one, it indicates that there is a calculation error in the color data of the fragment, and it is necessary to execute the coloring step and recalculate the color data of the current fragment. Otherwise, it indicates that the color data of the pixels in the i+1th fragment are quite different and can be assigned to two rendering threads for rendering, so that the pixels in one rendering thread have a high degree of similarity, thereby improving rendering efficiency (for example, for areas with a large number of similar colors, a regional rendering algorithm can be used to speed up the rendering process; the parallel processing function of the GPU can also be used to render the pixels in the thread).

[0045] Optionally, the step of performing rasterization also includes: adding labels to pixel points in the fragment, wherein the labels include transparent objects and non-transparent objects, discarding pixel points in the fragment with labels of transparent objects, recorded as first pixel points; retaining pixel points in the fragment with labels of non-transparent objects, recorded as second pixel points.

[0046] By adopting the above technical solution, the present application adds a label to each pixel to determine whether the pixel belongs to a transparent object. If so, the pixel is discarded and not processed in the subsequent steps. Otherwise, the pixel is retained. By discarding the pixel of the transparent object, the number of pixels that need to be processed during the rendering process is reduced, thereby improving the rendering efficiency. During the rendering process, the processing of transparent objects is more complicated than that of non-transparent objects. Discarding the pixel of the transparent object can simplify the rendering order and reduce potential rendering errors and image aliasing.

[0047] Optionally, after executing the step of rendering the image, the method further includes:

[0048] Transparent rendering: put the first pixel into the rendering queue, use the depth peeling algorithm to render the first pixel, and obtain n first images;

[0049] Image merging: Merge the first image in the rendering queue with the output image in sequence to obtain the final output image.

[0050] By adopting the above technical solution, the pixels of transparent objects are discarded in the rasterization step, which simplifies the subsequent rendering steps. However, the transparent objects still need to be rendered in the actual rendering process. The present application renders the first pixel by adopting a depth peeling algorithm to obtain n first images after rendering, and then merges the first images with the output image in the order in the rendering queue to obtain the final output image. The present application can accurately show the effect of transparent objects in the scene and reduce image aliasing and errors caused by incorrect rendering order.

[0051] Optionally, after performing the transparent rendering step and before performing the image merging step, the method further includes:

[0052] Creating a buffer: Creating a depth buffer, wherein the depth buffer stores depth information of a first pixel;

[0053] Filter pixels: Based on the depth information, obtain the first pixel between the near plane and the far plane, which is recorded as the third pixel.

[0054] Image sorting: obtaining the first image corresponding to the third pixel, and sorting the first image in descending order of depth information to obtain a first image sequence;

[0055] Regenerate Queue: Put the first image sequence into the rendering queue one by one.

[0056] By adopting the above technical solution, the present application can exclude those pixels outside the effective scene range by screening the first pixel point (recorded as the third pixel point) between the near plane and the far plane, thereby reducing unnecessary rendering calculations and resource waste, and improving rendering efficiency. The present application sorts the first image corresponding to the screened third pixel point so that it is rendered in the order of depth information from large to small (i.e., from back to front), so that farther objects are rendered first and closer objects are rendered later, so that the occlusion relationship of transparent objects can be correctly handled. The sorted first image sequence is put into the rendering queue in sequence, so that the images in the rendering queue are arranged in the correct order, thereby generating a final output image with the correct transparency effect.

[0057] In the second aspect, the present application provides a graphics rendering system based on GLSL shader, which adopts the following technical solution:

[0058] A graphics rendering system based on a GLSL shader, wherein the rendering system uses a GLSL shader running in a GPU, wherein the GLSL shader includes: a vertex shader and a fragment shader, and the rendering system is used to implement the rendering method, and the rendering system also includes:

[0059] Model loading module, used to load the 3D model to be rendered;

[0060] A primitive assembly module is connected to the model loading module for extracting control point data of the 3D model from the VBO of the GPU, generating a first geometric primitive based on the control point data, extracting vertex data from the first geometric primitive, and generating an index array of the vertex data;

[0061] A vertex data storage module is connected to the primitive assembly module for creating an EBO and storing the index array in the EBO in sequence;

[0062] A shading module, which is in communication with the vertex data storage module, comprises a first processing unit, a clipping unit, a rasterization unit, and a second processing unit;

[0063] A first processing unit, used to process vertex data in sequence according to the index array using a vertex shader to obtain the position and interpolation data of each vertex;

[0064] A clipping unit, configured to convert vertex data at a current viewing angle to a clipping plane based on the position of each vertex, obtain key point data, and generate a second geometric primitive based on the key point data;

[0065] A rasterization unit, configured to convert the second geometric primitive into a pixel-level fragment;

[0066] A second processing unit, configured to calculate color data of each fragment according to the interpolation data using a fragment shader;

[0067] The image rendering module is connected to the coloring module for rendering the color data to a frame buffer using a GPU to generate an output image.

[0068] By adopting the above technical solution, the present application extracts control point data from the vertex buffer object (VBO) of the 3D model, generates geometric primitives (such as triangles) based on these data, extracts vertex data of these geometric primitives, and stores the index array of vertex data in EBO, which can optimize vertex access in the rendering process and improve rendering efficiency. The present application also uses a vertex shader to process vertex data, calculates the position of each vertex and the interpolation data passed to the fragment shader. Convert the vertex data to the clipping plane, and generate new geometric primitives based on the key point data. Convert the geometric primitives to pixel-level fragments. Use the fragment shader to calculate the color data of each fragment. Use the GPU to render the color data to the frame buffer to generate an output image. The present application can reduce the repeated storage and access of vertex data through the use of index arrays and EBO, thereby saving memory resources. Through the clipping unit, the vertex data that is not within the range of the cone is eliminated, reducing the data that needs to be processed later and improving the rendering efficiency.

[0069] In summary, the present application includes at least one of the following beneficial technical effects:

[0070] 1. This application creates an EBO (element buffer object) and stores the index array of vertex data in the EBO in sequence, which helps to efficiently access and process vertex data during the rendering process. Through the index array, vertex data can be reused, memory usage can be reduced, and rendering efficiency can be improved.

[0071] 2. This application converts the vertex data under the current viewing angle to the clipping plane, obtains the key point data under the current viewing angle, and generates a second geometric primitive based on these key point data, so that only the geometric primitives within the viewing cone will be rendered, reducing the consumption of computing resources and further improving rendering efficiency.

[0072] 3. GLSL shaders utilize the parallel computing capabilities of the GPU to efficiently process large amounts of graphics data, further reducing the amount of calculations in the rendering process and improving rendering efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0073] Figure 1 is a flow chart of Example 1 of the present application;

[0074] Figure 2 is a flow chart of Example 2 of the present application;

[0075] Figure 3 is a flow chart of Example 3 of the present application;

[0076] Figure 4 This is a flowchart from S71 transparent rendering to S76 image merging in Example 4 of the present application. DETAILED DESCRIPTION

[0077] The following combination Figures 1 to 4 This application is described in further detail.

[0078] GLSL shaders are programs that run on the GPU and are specifically designed to handle computational tasks in graphics rendering. The parallel processing capabilities of the GPU enable GLSL shaders to efficiently process large amounts of vertex data and pixel data, thereby accelerating the graphics rendering process.

[0079] The vertex shader is a part of the GLSL shader, which is responsible for processing the vertex data of each vertex. The vertex shader receives vertex data (such as position, color, texture coordinates, etc.) from the CPU, and performs transformation, lighting, and projection calculations on these data, and finally outputs the position of each vertex and the interpolated data passed to the fragment shader. The fragment shader is also a part of the GLSL shader, which is responsible for receiving interpolated data (such as color, texture coordinates, etc.) from the vertex shader, and further calculates these data, and finally outputs the color data of each pixel.

[0080] In the graphics rendering process, vertex shaders and fragment shaders work together. First, the vertex shader processes the input vertex data to generate transformed vertex information. Then, this vertex information is used to generate geometric primitives (such as triangles). Next, the geometric primitives are subdivided into smaller fragments (called fragments), each of which contains interpolated data from the vertex shader. Finally, the fragment shader processes this interpolated data to generate the final color data.

[0081] VBO (Vertex Buffer Object) is a memory area in the GPU used to store vertex data.

[0082] EBO (Element Buffer Object) is used to store index arrays of vertex data, which define how vertices are composed into geometric primitives.

[0083] Control points are key points that define the shape of a 3D model.

[0084] Embodiment 1: This embodiment discloses a graphics rendering method based on GLSL shader, referring to Figure 1 The rendering method includes: S1 model loading, S2 primitive assembly, S3 vertex data storage, S4 shading and S5 image rendering. First, the 3D model to be rendered is loaded, and then the control point data of the 3D model is extracted from the VBO of the GPU, and the first geometric primitive is generated based on the control point data. At the same time, the vertex data in the first geometric primitive is extracted, and then an EBO storing the vertex data index array is created, and then color data is generated based on the vertex data through the shading step, and then the color data is rendered to the frame buffer by using the GPU to obtain the output image. The specific process of this embodiment is as follows:

[0085] S1 model loading, loading 3D model files to be rendered (such as OBJ, FBX, GLTF and other formats). These 3D model files contain the model's geometry information (vertices, edges, faces), material information (color, texture), and possible animation data.

[0086] When loading the 3D model file to be rendered, the structure of the 3D model file will also be parsed, the geometric information will be converted into a format suitable for GPU processing (such as vertex buffer object VBO), and resources such as textures, vertex shaders, and fragment shaders will be loaded and bound.

[0087] For example, use programming languages ​​such as C++ or Python to write code to read 3D model files in OBJ format, which contain information such as vertices (v), texture coordinates (vt), normals (vn), and faces (f). Traverse each line of the OBJ file, parse the corresponding data according to the identifier at the beginning of the line (such as v, vt, vn, f), and store the data of vertices, texture coordinates, normals, and faces in corresponding data structures.

[0088] Merge the parsed vertex data (position, texture coordinates, normals) and convert them into a format suitable for GPU processing (such as a floating point array). Create a vertex buffer object (VBO) to store these vertex data.

[0089] According to the material information in the OBJ file, load the corresponding texture file (such as PNG, JPG). Write and compile the vertex shader and fragment shader, then create a GLSL shader program and bind it to the rendering thread.

[0090] S2 primitive assembly, extracts the control point data of the 3D model in the GPU's VBO, generates the first geometric primitive (such as point, line, triangle) based on the control point data, then extracts the vertex data in the first geometric primitive (including vertex position, color, texture coordinates, etc.), and then generates an index array of the vertex data.

[0091] S3 stores vertex data. Using graphics APIs such as OpenGL, an EBO is created and the index arrays of vertex data in the OBJ file are stored in the EBO in sequence. These index arrays are used to reference vertex data, and each element in the index array corresponds to a vertex. In this way, vertex data can be reused, memory usage can be reduced, and rendering efficiency can be improved.

[0092] S4 shading, including S41 first processing, S42 clipping, S43 rasterization and S44 second processing.

[0093] S41 is the first processing. Based on the index array stored in EBO, the vertex shader is used to process the vertex data in sequence according to the storage order of the index array in EBO (such as model transformation, view transformation and projection transformation). The vertex shader calculates the position of each vertex and generates interpolation data (such as color, texture coordinates, normals, etc.) passed to the fragment shader. These interpolation data will be interpolated to each fragment in the rasterization stage.

[0094] Model transformation: Convert vertices from model space to world space. This process involves using the model matrix to rotate, scale, and translate the object.

[0095] For example, the rotation matrix A that translates the target by 2 units along the positive x-axis and 3 units along the positive y-axis without any rotation or scaling is as follows:

[0096] ;

[0097] The first three columns of the rotation matrix A represent the rotation and scaling parts (the present embodiment uses the unit matrix, indicating no rotation and scaling), and the first three elements of the last column represent the translation amounts on the x-axis, y-axis, and z-axis, respectively.

[0098] View transformation: Use the view matrix to transform vertices from world space to observation space (also known as camera space).

[0099] For example, assume that the camera is initially located at the origin and faces the negative z-axis. If the camera moves 2 units along the negative x-axis and 3 units along the negative y-axis, the view matrix B1 will be as follows:

[0100] .

[0101] If the camera is rotated 45 degrees around the y-axis but not translated, the view matrix B2 will be as follows:

[0102] .

[0103] Projection transformation: Converts vertices from viewing space to clipping space and prepares them for clipping. Projection transformations include perspective projection or orthographic projection.

[0104] For example, if the camera's field of view is set to 45 degrees, the aspect ratio is 16:9, the near plane depth of the clipping space is 0.1, and the far plane depth of the clipping space is 100, then the projection matrix C is:

[0105] .

[0106] S42 clipping, based on the position of each vertex, converts the vertex data under the current perspective to the clipping plane to remove vertices that are not in the field of view and obtains the key point data under the current perspective, that is: converts the vertex data from the observation space (or camera space) to the clipping space, and checks whether they are located in the clipping volume defined by the six clipping planes (left, right, top, bottom, near, and far). If the vertex is outside the clipping volume, the vertex is discarded; otherwise, the vertex is retained and used as the key point data, and then the second geometric primitive is generated based on the key point data.

[0107] S43 rasterizes and converts the second geometric primitives into pixel-level fragments, which contain interpolation data passed by the vertex shader and serve as inputs of the fragment shader in the second processing S44.

[0108] S44 second processing, the fragment shader calculates the color data of each fragment based on the interpolation data, including:

[0109] S441. The fragment shader receives interpolation data transmitted by the vertex shader, such as lighting data, texture coordinates, etc.

[0110] S442. The fragment shader samples color data from the texture map according to the texture coordinates of the fragment.

[0111] S443. The fragment shader calculates the lighting effects of the fragment, such as diffuse reflection, specular reflection, etc., according to the lighting data.

[0112] S444. The fragment shader calculates the shadow effect of the fragment to simulate the lighting and shadow in the real world.

[0113] The fragment shader combines the results of texture sampling, lighting calculations, and shadow processing to calculate the color data for each fragment.

[0114] S5 renders the image, the GPU renders the color data to a frame buffer, and once all primitives are rendered and merged into the frame buffer, the content of the frame buffer can be output to a display as an output image or saved as an image file.

[0115] The frame buffer is a memory area in the GPU that is used to store the final rendering results.

[0116] This embodiment creates an EBO that stores an index array of vertex data, and then uses a vertex shader to process the vertex data in sequence based on the order of the index array stored in the EBO to obtain the position of each vertex and the interpolation data passed to the fragment shader. The vertices are then clipped to obtain key point data, and the second geometric primitives are regenerated based on the key point data. The number of the first geometric primitives is reduced through the clipping step, and the number of geometric primitives that need to be rendered in subsequent steps is reduced, thereby reducing the consumption of computing resources and improving rendering efficiency. The second geometric primitives are then converted into fragments, and the fragment shader calculates the color data of each fragment. The GPU then renders the color data in parallel to obtain an output image. This application can reduce memory usage and improve rendering efficiency.

[0117] Example 2: Reference Figure 2 The difference between this embodiment and embodiment 1 is that after executing S3 to store vertex data and before executing S4 to color, the following is also included:

[0118] S31 is a first calculation, which is to calculate the centroid of each first geometric primitive and record it as the first centroid. All the calculated first centroids are integrated into a set, and the set is recorded as the first centroid set.

[0119] Then, three control points are randomly selected, and the selected three control points are on different lines. Then, the centroid of the triangle formed by these three control points is calculated, which is recorded as the second centroid. All the calculated second centroids are integrated into another set, which is recorded as the second centroid set.

[0120] S32 traverses the centroids, checks all the first centroids in the first centroid set one by one, and determines whether the currently traversed first centroid coincides with a second centroid in the second centroid set. If so, the first centroid that coincides with the second centroid is recorded as the third centroid.

[0121] S33 is a first statistics step, which is to count the number of the third centers of gravity and record the number of the third centers of gravity as first data.

[0122] S34 is the first judgment, judging whether the first data is less than one. If so, it means that there is no first center of gravity that coincides with the second center of gravity, and it is necessary to execute S2 primitive assembly again to regenerate the first geometric primitive. This loop is not exited until the first data is less than one for several consecutive times, and S4 coloring is executed; if not, S35 is executed to determine the index array.

[0123] S35 determines the index array, uses the index array of the vertices of the first geometric primitive where the third centroid is located as the first-level index array in the EBO, and uses the index array of the vertices of the first geometric primitive where the remaining first centroid is located as the second-level index array in the EBO.

[0124] This embodiment compares the positions of the first centroid and the second centroid, and uses the index array of the vertices of the first geometric primitive corresponding to the two when the two coincide as the first-level index array, and uses the index array of the vertices of the first geometric primitive where the remaining first centroid is located as the second-level index array. This embodiment can improve rendering efficiency by setting the rendering priority of the first geometric primitive.

[0125] Example 3: Reference Figure 3 The difference between this embodiment and the first embodiment is that after executing S4 shading and before executing S5 image rendering, the present embodiment further includes:

[0126] S6 multi-thread processing, including: S601 initialization, S602 second calculation, S603 second judgment, S604 fragment replacement, S605 third calculation, S606 third judgment, S607 first replacement, S608 second allocation, S609 second statistics, S610 fourth judgment, S611 third allocation, S612 first allocation and S613 thread replacement.

[0127] S601 initializes and distributes the color data of the i-th fragment to the first rendering thread.

[0128] S602 is a second calculation, which is to calculate the average value of the color data of the i-th pixel, and record it as a first average value.

[0129] Then, the average value of the color data of the i+1th pixel is calculated and recorded as the second average value.

[0130] Then, the similarity between the first average value and the second average value is calculated and recorded as the second data.

[0131] S603 is the second judgment, judging whether the second data is greater than a preset threshold. If so, it indicates that the i+1th fragment is similar in color to the i-th fragment, and then S604 is executed to replace the fragment and calculate the color similarity between the i+1th fragment and the next fragment; if not, the similarity of the pixels contained in the i+1th fragment is calculated, that is, the third calculation of S605 is executed.

[0132] S604 replaces the fragment, takes the i+1th fragment as the new ith fragment, and executes S601 initialization.

[0133] S605: third calculation, calculating the similarity between the color data of the kth pixel in the (i+1)th fragment and the color data of the (k+1)th pixel, and recording it as the third data.

[0134] S606 is the third judgment, judging whether the third data is greater than a preset threshold. If so, it means that the color data of the two pixels are similar, and then S607 is executed to first replace and calculate the similarity between the color data of the k+1th pixel and the color data of the next pixel. If not, it indicates that the color data of the kth pixel is quite different from the color data of the k+1th pixel, and it is necessary to execute S608 the second allocation to allocate the k+1th pixel to the new rendering thread for rendering.

[0135] S607: first replacement, taking the k+1th pixel point as the new kth pixel point, and performing S605: third calculation.

[0136] S608 second allocation, allocates the k+1th pixel point to the third rendering thread, executes S607 first replacement, until the preset stop condition is met, and executes S609 second statistics, wherein the stop condition refers to: all the pixels in the i+1th fragment are allocated to the corresponding rendering thread.

[0137] S609 second statistics: counting the number of pixels in the third rendering thread and recording it as fourth data.

[0138] S610 is the fourth judgment, judging whether the fourth data is one. If so, it indicates that there is a calculation error in the pixel point, and it is necessary to re-execute S4 coloring to recalculate the color data of the pixel point in the fragment; if not, execute S611 the third allocation.

[0139] S611 third allocation, sending the color data stored in the third rendering thread to the GPU for rendering processing.

[0140] S612 first allocation, sending the color data stored in the first rendering thread to the GPU for rendering processing.

[0141] S613 thread replacement, distribute the color data of the i+1th fragment to the second rendering thread, and use the second rendering thread as the new first rendering thread, use the i+1th fragment as the new i-th fragment, and perform the second calculation in S602.

[0142] The first rendering thread, the second rendering thread and the third rendering thread are used to indicate that different contents are stored in different rendering threads.

[0143] This embodiment first determines whether the fragments are highly similar through the second judgment step. If so, the two are assigned to the same rendering thread. If not, the third judgment step is used to further determine whether the pixels in the i+1th fragment are highly similar. If so, the i+1th fragment is assigned to the second rendering thread, and the rendering thread and the currently processed fragment are updated to prepare for the next round of processing. Otherwise, the pixel points corresponding to the third data being less than the preset threshold are stored in the third rendering thread. This embodiment can make the pixel points in the same rendering thread have a higher similarity and reduce the complexity of rendering.

[0144] Example 4: The difference between this example and Example 1 is that, in executing S43 rasterization, it also includes: adding labels to each pixel in the fragment respectively, wherein the labels include labels of transparent objects and labels of non-transparent objects, discarding the pixels in the fragment with labels of transparent objects, recorded as first pixels; retaining the pixels in the fragment with labels of non-transparent objects, recorded as second pixels.

[0145] Reference Figure 4 , after executing S5 to render the image, it also includes:

[0146] S71 is transparent rendering, putting the first pixel into a rendering queue, and rendering the first pixel by using a depth peeling algorithm to obtain n first images.

[0147] The rendering queue is a list for storing pixels to be rendered.

[0148] S72 creates a buffer, creating a depth buffer, wherein the depth buffer stores depth information of the first pixel point, where the depth information refers to the distance information of the pixel point in the 3D space, and is used to determine which first pixel points are in front and which first pixel points are in the back.

[0149] S73 filters the pixel points, and based on the depth information stored in the depth buffer in the buffer created in S72, obtains the first pixel point between the near plane and the far plane, which is recorded as the third pixel point.

[0150] The near plane and far plane define the visible range in the scene. The near plane is the plane closest to the observer (camera) in the clipping space, and the far plane is the plane farthest from the observer (camera) in the clipping space.

[0151] S74: sorting images, obtaining the first image corresponding to the third pixel point, and sorting the first image in descending order of depth information to obtain a first image sequence.

[0152] S75 regenerates the queue, and sequentially puts the first image sequence into the rendering queue. The rendering queue adopts a first-in-first-out setting, so that the first image that enters first in the image merging in S76 can be combined with the output image generated in the rendering image in S5 first.

[0153] S76 image merging, merging the first image in the rendering queue with the output image generated in S5 rendering image in sequence to obtain a final output image.

[0154] This embodiment first shields the pixels in the transparent object and renders the pixels of the non-transparent object first, which reduces the consumption of computing resources and improves the accuracy of rendering the non-transparent object. Then, the pixel points in the transparent object are rendered. During the rendering process, the depth information of the first image corresponding to the transparent object is taken into account. In the image merging step, the rendering is performed in the order of the depth information from large to small, so that the final output image is more visually realistic and consistent.

[0155] Embodiment 5: This embodiment discloses a graphics rendering system based on GLSL shader, the rendering system comprising:

[0156] The model loading module is responsible for loading the 3D models to be rendered. These 3D models are stored in multiple formats (such as OBJ, FBX, GLTF, etc.), and contain the model's geometric information (such as vertex coordinates, normals, texture coordinates, etc.), material information, and possible animation data. The model loading module needs to parse these files and convert them into data structures that the rendering pipeline can understand, such as vertex buffer objects (VBO) and index buffer objects (if applicable). In addition, this module also needs to handle transformations such as scaling, rotation, and translation of the model so that the model can be correctly presented in the scene.

[0157] The primitive assembly module is connected to the model loading module and is responsible for extracting the control point data of the 3D model from the GPU's VBO. The control point is the key point that constitutes the basic shape of the 3D model, such as the vertices of polygons such as triangles and quadrilaterals. Based on these control point data, this module can generate the first geometric primitives, which are the basic building blocks of the rendering pipeline, such as points, lines, triangles, etc. In this process, this module will also extract the vertex data in the first geometric primitives, and generate the corresponding index array based on the vertex data to prepare for subsequent rendering.

[0158] The vertex data storage module is connected to the primitive assembly module in communication. This module is responsible for creating EBO and storing the index array of the vertex data in the EBO in sequence.

[0159] The shading module is connected to the vertex data storage module in communication, and includes a first processing unit, a clipping unit, a rasterization unit and a second processing unit.

[0160] The first processing unit is responsible for processing the vertex data in sequence using the vertex shader based on the index array stored in the EBO to obtain the position of each vertex and the interpolation data passed to the fragment shader.

[0161] The clipping unit is responsible for converting the vertex data at the current viewing angle to the clipping plane based on the position of each vertex to determine which vertices are visible at the current viewing angle, thereby obtaining the key point data at the current viewing angle, and generating a second geometric primitive based on the key point data.

[0162] The rasterization unit is responsible for converting the second geometric primitive into pixel-level fragments, each of which contains multiple pixels.

[0163] The second processing unit is responsible for calculating the color data of each fragment based on the interpolation data by using a fragment shader.

[0164] The image rendering module is connected to the coloring module for rendering the color data to the frame buffer using the GPU to generate an output image for display on the display.

[0165] The model loading module of this embodiment is used to load the 3D model, the primitive assembly module extracts control point data from VBO to generate geometric primitives and extracts vertex data, the vertex data storage module creates EBO to store vertex indexes, the shading module processes vertex data and generates color data of pixel-level fragments through vertex shaders, clipping, rasterization and fragment shaders, and finally the image rendering module renders the color data to the frame buffer to obtain the output image. These modules jointly complete the conversion from 3D model to 2D image, while reducing resource consumption and memory occupancy, and improving rendering efficiency.

[0166] The above are all preferred embodiments of the present application, and the protection scope of the present application is not limited thereto. Therefore, any equivalent changes made according to the structure, shape, and principle of the present application should be included in the protection scope of the present application.

Claims

1. A graphics rendering method based on a GLSL shader, wherein the rendering method uses a GLSL shader running in a GPU, and the GLSL shader includes: Vertex shader and fragment shader, characterized in that the rendering method comprises: Model loading: load the 3D model to be rendered; Primitive assembly: extract the control point data of the 3D model from the VBO of the GPU, generate the first geometric primitive based on the control point data, extract the vertex data in the first geometric primitive, and generate an index array of the vertex data; Storing vertex data: creating an EBO and storing the index array in the EBO in sequence; Shading: including first processing, clipping, rasterization and second processing; First processing: The vertex shader processes the vertex data in sequence according to the index array to obtain the position and interpolation data of each vertex; Clipping: Based on the position of each vertex, the vertex data under the current perspective is converted to the clipping plane, the key point data is obtained, and the second geometric primitive is generated based on the key point data; Rasterization: converting the second geometric primitive into pixel-level fragments; Second processing: the fragment shader calculates the color data of each fragment according to the interpolation data; Initialization: Assign the color data of the i-th fragment to the first rendering thread; Second calculation: calculate the average value of the color data of the i-th pixel, recorded as the first average value; calculate the average value of the color data of the i+1-th pixel, recorded as the second average value, and calculate the similarity between the first average value and the second average value, recorded as the second data; Second judgment: judging whether the second data is greater than a preset threshold, if so, executing the step of replacing the fragment; if not, executing the step of third calculation; Third calculation: calculate the similarity between the color data of the kth pixel in the i+1th fragment and the color data of the k+1th pixel, recorded as the third data; Third judgment: judging whether the third data is greater than a preset threshold, if so, executing the first replacement step; if not, executing the second allocation step; First replacement: taking the k+1th pixel point as the new kth pixel point and performing the third calculation step; Second allocation: allocating the k+1th pixel point to the third rendering thread, and executing the first replacement step until a preset stop condition is met; Third allocation: sending the color data stored in the third rendering thread to the GPU; Fragment replacement: take the i+1th fragment as the new ith fragment and perform the initialization steps; First allocation: sending the color data stored in the first rendering thread to the GPU; Thread replacement: assign the color data of the i+1th fragment to the second rendering thread, use the second rendering thread as the new first rendering thread, use the i+1th fragment as the new i-th fragment, and perform the second calculation step; Rendering image: The GPU renders the color data to a frame buffer to generate an output image.

2. The graphics rendering method based on GLSL shader according to claim 1, characterized in that: After executing the step of storing vertex data and before executing the step of shading, the following steps are also included: First calculation: calculate the centroid of each first geometric primitive, record it as the first centroid, and integrate all the first centroids into the first centroid set; calculate the centroid of any three non-collinear control points, record it as the second centroid, and integrate all the second centroids into the second centroid set; Traverse the center of gravity: traverse all the first centers of gravity, obtain the first center of gravity that coincides with the second center of gravity, and record it as the third center of gravity; Determine the index array: use the index array of the vertices of the first geometric primitive where the third centroid is located as the first-level index array in the EBO, and use the index array of the vertices of the first geometric primitive where the remaining first centroid is located as the second-level index array in the EBO.

3. The graphics rendering method based on GLSL shader according to claim 2, characterized in that: After executing the step of traversing the centroid and before executing the step of determining the index array, the method further includes: First statistics: count the number of the third centroids, recorded as the first data; First judgment: judge whether the first data is less than one, if so, execute the step of primitive assembly; if not, execute the step of determining the index array.

4. The graphics rendering method based on GLSL shader according to claim 1, characterized in that: After executing the second allocation step and before executing the third allocation step, the method further includes: Second statistics: counting the number of pixels in the third rendering thread, recorded as fourth data; Fourth judgment: judge whether the fourth data is one, if so, execute the coloring step; if not, execute the third allocation step.

5. The graphics rendering method based on GLSL shader according to any one of claims 1 to 3, characterized in that: The step of performing rasterization also includes: adding labels to the pixel points in the fragment, wherein the labels include transparent objects and non-transparent objects, discarding the pixel points in the fragment whose labels are transparent objects and recorded as first pixel points; retaining the pixel points in the fragment whose labels are non-transparent objects and recorded as second pixel points.

6. The graphics rendering method based on GLSL shader according to claim 5, characterized in that: After executing the step of rendering the image, it also includes: Transparent rendering: put the first pixel into the rendering queue, use the depth peeling algorithm to render the first pixel, and obtain n first images; Image merging: Merge the first image in the rendering queue with the output image in sequence to obtain the final output image.

7. The graphics rendering method based on GLSL shader according to claim 6, characterized in that: After the step of performing transparent rendering and before the step of performing image merging, the method further includes: Creating a buffer: Creating a depth buffer, wherein the depth buffer stores depth information of a first pixel; Filter pixels: Based on the depth information, obtain the first pixel between the near plane and the far plane, which is recorded as the third pixel. Image sorting: obtaining the first image corresponding to the third pixel, and sorting the first image in descending order of depth information to obtain a first image sequence; Regenerate Queue: Put the first image sequence into the rendering queue one by one.

8. A graphics rendering system based on a GLSL shader, wherein the rendering system uses a GLSL shader running in a GPU, and the GLSL shader includes: A vertex shader and a fragment shader, characterized in that the rendering system is used to implement the rendering method according to any one of claims 1 to 7, and the rendering system further comprises: Model loading module, used to load the 3D model to be rendered; A primitive assembly module is connected to the model loading module for extracting control point data of the 3D model from the VBO of the GPU, generating a first geometric primitive based on the control point data, extracting vertex data from the first geometric primitive, and generating an index array of the vertex data; A vertex data storage module is connected to the primitive assembly module for creating an EBO and storing the index array in the EBO in sequence; A shading module, which is in communication with the vertex data storage module, comprises a first processing unit, a clipping unit, a rasterization unit, and a second processing unit; A first processing unit, used to process vertex data in sequence according to the index array using a vertex shader to obtain the position and interpolation data of each vertex; A clipping unit, configured to convert vertex data at a current viewing angle to a clipping plane based on the position of each vertex, obtain key point data, and generate a second geometric primitive based on the key point data; A rasterization unit, configured to convert the second geometric primitive into a pixel-level fragment; A second processing unit, configured to calculate color data of each fragment according to the interpolation data using a fragment shader; The image rendering module is connected to the coloring module for rendering the color data to a frame buffer using a GPU to generate an output image.

Citation Information

Patent Citations

  • GPU-based three-dimensional image data rendering method and device

    CN113012269A

  • GPU (Graphics Processing Unit) method for efficiently coloring multi-field dense point clouds

    CN117911597A

  • OpenGL-based three-dimensional model dynamic rendering method and system

    CN118840462A