Method for uniform drawing of OpenGL multi-class objects based on VBO internal encoding

By using VBO in-code and a unified shader, the performance bottleneck and resource redundancy issues caused by state switching in traditional OpenGL rendering methods are resolved, enabling efficient and unified rendering of heterogeneous objects and improving GPU rendering efficiency and system scalability.

CN122066838BActive Publication Date: 2026-07-03NANCHANG HANGKONG UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
NANCHANG HANGKONG UNIVERSITY
Filing Date
2026-04-16
Publication Date
2026-07-03

AI Technical Summary

Technical Problem

Traditional OpenGL rendering methods suffer from performance bottlenecks due to frequent switching of rendering states and resource separation, making it difficult to achieve efficient rendering of large-scale heterogeneous scenes, and resource management is highly complex.

Method used

The method of VBO internal encoding is adopted to store vertex coordinates, color components, texture coordinates and object type tags in a single VBO, and to define a unified vertex and fragment shader. The processing logic is dynamically determined by the object type tag, avoiding state switching and unifying resource management.

Benefits of technology

It improves GPU rendering efficiency and frame rate stability, reduces memory usage and code complexity, and leverages modern GPU features to enhance rendering performance for large-scale scenes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122066838B_ABST
    Figure CN122066838B_ABST
Patent Text Reader

Abstract

The application relates to an OpenGL multi-class object uniform drawing method based on VBO internal coding, which comprises the following steps: constructing a mixed data structure containing vertex coordinates, color components, texture coordinates and object type marks, and storing the mixed data structure in the memory of a VBO in an interlaced arrangement; defining a uniform vertex shader and a fragment shader; judging whether texture mapping is needed according to the object type marks during a rendering process; and deleting the VBO object and the shader program after the rendering is completed, and releasing the display memory and the memory resources. The application links map elements with different rendering logics, realizes the uniform drawing of heterogeneous objects under a single pipeline, fully utilizes the parallel processing capacity of a GPU, avoids frequent switching of the shader, effectively saves the communication bandwidth between a CPU and the GPU, simplifies the development difficulty of multi-map element mixed rendering in a complex scene, and facilitates the horizontal expansion of a system in large-scale geographic information visualization.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer graphics technology, and provides a unified drawing method for multiple OpenGL objects based on VBO internal encoding. Background Technology

[0002] In geographic information systems or large-scale scene rendering, maps are typically composed of various geometric elements such as points, lines, and polygons. To address the fundamental differences in data structure and rendering requirements among these geometric types, traditional OpenGL rendering pipelines usually employ a separate rendering architecture. The core idea of ​​this architecture is to configure independent, highly specialized rendering pipelines for different types of features, such as points, lines, and polygons. Specifically, during rendering, the application needs to prepare buffers containing vertex positions and color attributes for point and line objects, and bind them to a shader program that only processes basic colors. For polygonal features, additional buffers containing attributes such as texture coordinates must be prepared, and a different, more complex shader program supporting texture sampling and blending calculations must be switched to. This technical approach forces developers to maintain multiple shader programs and buffer objects for different features. When rendering a scene containing a massive number of blended features, the engine has to frequently switch between binding different shader programs, vertex array objects, and texture states. Each switch interrupts the GPU's rendering pipeline, resulting in expensive state reset and verification overhead, preventing the GPU from maintaining an efficient continuous computing state, thus causing a significant drop in rendering performance. Furthermore, maintaining multiple sets of resources increases the complexity of memory management and code logic, making it difficult to leverage the efficient rendering features of modern GPUs, such as instantiation rendering. Ultimately, these factors collectively cause traditional methods to struggle to achieve smooth loading and interaction when faced with the real-time rendering requirements of large-scale, heterogeneous geographical features, becoming the main bottleneck for performance improvement. Summary of the Invention

[0003] The purpose of this invention is to provide a unified rendering method for multiple OpenGL objects based on VBO internal encoding. This method can effectively overcome the performance bottlenecks and implementation complexity inherent in traditional separate rendering architectures due to forced state switching and resource separation, the GPU pipeline stagnation and performance loss caused by frequent and forced rendering state switching, the resource redundancy and memory management complexity caused by multiple independent buffers and shader programs, the compatibility issues of being difficult to adapt to modern GPU efficient rendering mechanisms, and the problems of poor scalability and maintenance difficulties of existing technologies in large-scale heterogeneous scenes.

[0004] The technical solution adopted in this invention is: a unified drawing method for multiple OpenGL objects based on VBO internal encoding, comprising the following steps:

[0005] S1: For the data of each vertex, construct a hybrid data structure containing vertex coordinates, color components, texture coordinates and object type markers, and store the hybrid data structure in the memory of the VBO in an interleaved manner;

[0006] S2: Define a unified vertex shader and fragment shader, compile the defined vertex shader and fragment shader and create shader programs, activate the shader programs after the shader programs are compiled, bind VBOs and set vertex attribute pointers, and call the drawing function to start the graphics rendering pipeline;

[0007] S3: In the vertex processing stage of the graphics rendering pipeline, the vertex shader performs coordinate transformation and parses the object type marker in the VBO, passing the object type marker in the VBO as a non-interpolated variable out to the rasterization stage.

[0008] S4: In the fragment processing stage of the graphics rendering pipeline, the fragment shader receives the object type flag and executes the branch logic according to the object type flag value: if the current drawing object is a face object, then texture sampling is performed; if it is a point object or a line object, then the color component is directly output.

[0009] S5: After the graphics rendering pipeline has finished executing and the image has been drawn to the frame buffer, unbind the VBO and shader program, delete the VBO object and shader program, release video memory and memory resources, and complete the rendering.

[0010] Furthermore, the specific method for step S1 is as follows:

[0011] S101: Define an array in memory, and arrange all the data of a single vertex in a continuous manner according to the pattern of vertex coordinates-color component-texture coordinates-object type marker, so that the data of all vertices form a continuous linear block in memory;

[0012] S102: Use OpenGL to generate a buffer object and obtain its ID. Then bind the buffer object to the GL_ARRAY_BUFFER attribute value in OpenGL as a vertex buffer. Then use data transfer instructions to upload the constructed memory data to the GPU memory at once.

[0013] S103: Define the method for the GPU to parse memory data by setting vertex attribute pointers. The specific steps are as follows: specify the step size of the vertex attribute pointer as the total number of bytes of all attributes of a single vertex, and set the offset of the starting byte for vertex coordinates, color components, texture coordinates and object type markers respectively, so that the shader can accurately read different types of data from the same VBO.

[0014] Further, the method for defining the vertex shader in step S2 is as follows: The vertex shader includes three input attributes that strictly correspond to the VBO data layout and two unified variables: The three input attributes include the vertex coordinate variable pos located at position 0, the color component variable acolor located at position 1, and the texture data variable texCoord located at position 2; The two unified variables include the model-view matrix mv_matrix and the projection matrix proj_matrix; Wherein, the vertex coordinate variable pos includes x, y, and z components, the color component variable acolor includes r, g, b, and a components, and the texture data variable texCoord includes u and v coordinates and the object type marker f;

[0015] The vertex shader performs matrix transformation operations on the vertex coordinate variable pos using the projection matrix proj_matrix and the model-view matrix mv_matrix, and assigns the result to the built-in variable gl_Position. At the same time, it directly assigns the color component variable acolor to the output variable varyingColor, assigns the u and v coordinates of the texture data variable texCoord to the output variable tc, and assigns the object type marker to the output variable bUseTexture, thereby passing the data to the rasterization stage.

[0016] The fragment shader is defined as follows: It includes three input variables that strictly correspond to the vertex shader output: the interpolated color variable `varyingColor`, the texture coordinate variable `tc`, and the texture usage flag `bUseTexture`. The fragment shader uses the `texture` function to sample the texture color from the texture sampler based on the texture coordinate variable `tc`. If the object type flag `bUseTexture` is true, it means the current object needs to perform a blending operation between the texture color and the color variable `varyingColor`. Conversely, if the object type flag `bUseTexture` is false, it means the current object does not need to perform a blending operation between the texture color and the color components. The final calculated result is assigned to the built-in output variable as the final pixel color `vec4` value.

[0017] Create and load vertex shader and fragment shader objects, compile them, attach the vertex shaders and fragment shaders to the program object for linking, and activate the rendering pipeline.

[0018] Further, the specific method of step S3 is as follows: the vertex shader reads the vertex coordinates and object type marker of the current vertex from the VBO through the input attributes; the vertex coordinates are transformed to clip space using the model-view matrix mv_matrix and the projection matrix proj_matrix and assigned to the built-in output variable gl_Position to determine the shape of the graphic; when processing the object type marker, the vertex shader assigns the object type marker to the output variable named out and prohibits the GPU from performing linear interpolation on the out variable during the rasterization stage, so that the status code passed to the fragment shader remains the original discrete integer value; for texture coordinates and color components, the GPU is allowed to perform linear interpolation during the rasterization stage to ensure that the fragment shader receives values ​​with a smooth transition.

[0019] Further, the specific method of step S4 is as follows: the fragment shader receives the object type marker, corresponding texture coordinates, and color components passed from the vertex shader; the fragment shader makes a judgment based on the object type marker: when the object type marker indicates that the current primitive is a face object, the fragment shader uses the interpolated texture coordinates to call the texture sampling function, extracts the corresponding texel color, and mixes the texel color and vertex color components to calculate the texture color and vertex color; if the object type marker indicates that the current primitive is a point object or a line object, the fragment shader ignores the texture sampling step and only outputs the passed vertex color components as the final pixel color.

[0020] Furthermore, in step S5, the current VBO and shader program are unloaded from the rendering pipeline context by calling the application programming interface, followed by object destruction, VBO handle recycling, and sequential separation and deletion of the attached shader module and the connected program pair.

[0021] The beneficial effects of this invention are as follows:

[0022] (1) When traditional methods are used to draw points, lines, and surfaces, the shader program, vertex array object and texture binding state must be switched frequently due to the differences in attribute structure. Each switch forces the GPU pipeline to be cleared and reset, resulting in idle hardware resources and huge driving overhead. This invention uses a unified vertex data structure that includes vertex position, color components, texture coordinates and key types, and encodes the vertex data of all heterogeneous elements into a single VBO according to this structure. This allows the GPU to bind the VBO and call a unified shader program only once when drawing the entire scene. The shader program reads the type flag and dynamically determines the processing logic for the current vertex, such as whether to sample the texture, thereby avoiding state switching during the drawing process. The GPU pipeline can work continuously at full capacity, improving the throughput efficiency of geometric data and frame rate stability.

[0023] (2) Traditional architecture requires maintaining an independent VBO and a corresponding custom shader program group for each element type, resulting in multiple similar but independent data buffers and compiled code in video memory or memory. This not only causes fragmentation and redundant occupation of storage resources, but also makes the resource loading, binding and lifecycle management logic of the application extremely complex. This invention integrates the previously scattered multiple sets of resources into one set by unifying storage with a single VBO and unifying adaptation with a single set of shaders, which greatly reduces the overall occupation of memory and video memory and avoids resource redundancy. At the same time, it simplifies the resource management logic to the management of a single buffer and a single shader program, which significantly reduces code complexity and reduces the risk of errors due to inconsistency in the coordination of multiple resources.

[0024] (3) Traditional architectures are difficult to effectively utilize the high-efficiency features of modern GPUs, such as instantiation rendering, due to the separation of data and shaders. Instantiation rendering requires the ability to process similar objects with different attributes in a single drawing call. The continuous, structurally consistent vertex data stream created by this invention, which includes type tags, naturally constitutes an ideal data source. On this basis, the system can easily organize primitives of different types or parameters, such as faces with different colors and textures, and complete the rendering of all heterogeneous elements in a single drawing call by expanding the data format or combining instantiation arrays. This minimizes the number of drawing calls from CPU to GPU and the data transmission overhead, further releasing the parallel computing potential of GPUs and providing a clear performance expansion path for dealing with larger scene data in the future. Attached Figure Description

[0025] To more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0026] Figure 1 This is a flowchart of a method according to an embodiment of the present invention. Detailed Implementation

[0027] To better understand the above-described objects, features, and advantages of the present invention, the invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. Many specific details are set forth in the following description to provide a thorough understanding of the invention; however, the invention may be practiced in other ways different from those described herein, and therefore, the invention is not limited to the specific embodiments disclosed below.

[0028] like Figure 1As shown, this embodiment of the invention provides a unified rendering method for multiple OpenGL objects based on VBO internal encoding. It is mainly applied to real-time visualization systems that require efficient rendering of a large number of heterogeneous graphical objects in the same view, or real-time simulation and training systems that display a large number of different dynamic objects on the same screen, with different objects having different states. In these scenarios, the map contains points, lines, and polygons simultaneously, resulting in a large amount of data, and the viewpoint will frequently zoom, translate, or rotate. After the map data is uploaded to the VBO from the CPU, the shader, during the rendering phase, binds to the corresponding VBO and reads vertex attribute data from it according to the agreed data layout and encoding specifications, thereby executing the corresponding graphics rendering process.

[0029] Specifically, the steps include the following:

[0030] S1: For each vertex's data, construct a hybrid data structure containing vertex coordinates, color components, texture coordinates, and object type markers, and store the hybrid data structure in the VBO's memory in an interleaved manner; the specific method is as follows:

[0031] S101: Define an array in memory, arranging all the data of a single vertex consecutively in the pattern of vertex coordinates-color components-texture coordinates-object type marker, so that the data of all vertices form a contiguous linear block in memory. That is, define a hybrid data structure VertexData, where the data of a single vertex contains 10 floating-point numbers, each occupying 4 bytes: containing 3D vertex coordinates (x, y, z), 4D color components (r, g, b, a), 2D texture coordinates (u, v), and an object type marker. The formula for calculating the memory stride of a single vertex is:

[0032] ;

[0033] in, For the three-dimensional vertex coordinate components, For color components, For texture coordinate components, Mark the object type. This indicates the number of characters occupied by a single-precision floating-point number type in memory. In this embodiment of the invention, The value of is 3. The value of is 4. The value of is 2. The value of is 1. Substituting this into the formula, the memory stride occupied by a single vertex is calculated to be 40 bytes.

[0034] S102: Use OpenGL to generate a buffer object and obtain its ID. Then, bind this buffer object to the OpenGL attribute value GL_ARRAY_BUFFER target as a vertex buffer. Finally, use data transfer instructions to upload the constructed memory data to the GPU memory all at once. If the total number of vertices to be drawn is N, the formula for calculating the total memory size (BufferSize) to be allocated to the VBO is:

[0035] .

[0036] S103: Define the method for the GPU to parse memory data by setting vertex attribute pointers. The attribute channel offset algorithm in the method for parsing memory data is defined as follows:

[0037] Let offset be the starting offset of the i-th attribute, then the offset of the position variable pos is... The offset of the color variable acolor The offset between the texture and the tag variable texCoord .

[0038] In this embodiment of the invention, when creating a vertex attribute pointer, the number of vertices n to be drawn must first be recorded, and the vertex data is stored according to the method described in step S101. During storage, a conditional judgment is made based on the object type marker, i.e., the bUseTexture attribute. If the attribute is true, it indicates that the object type is a face object, and the object type marker value f = 1.0; otherwise, if the attribute is false, it indicates that the object type is a point or line object, and the object type marker value f = 0.0. After the data storage is completed, the application programming interface provided by OpenGL needs to be called sequentially to bind the current VBO object ID, and then the data transfer instructions provided by OpenGL are called to bind the vertex data to the VBO. Subsequently, the glVertexAttribPointer function is called to configure the attribute channel based on the above offset algorithm. Finally, the glEnableVertexAttribArray function provided by OpenGL is called to enable the vertex attribute channel described by the glVertexAttribPointer function, so that OpenGL can read and use the vertex attribute data during drawing. After the data transfer is completed, the vertex data on the CPU side is deleted.

[0039] S2: Define a unified vertex shader and fragment shader, compile the defined vertex shader and fragment shader to create a shader program, activate the shader program after compilation, bind the VBO and set the vertex attribute pointers, and call the drawing function to start the graphics rendering pipeline.

[0040] The method for defining a vertex shader is as follows: The vertex shader includes three input attributes that strictly correspond to the VBO data layout and two unified variables: The three input attributes include the vertex coordinate variable pos at position 0, the color component variable acolor at position 1, and the texture data variable texCoord at position 2; The two unified variables include the model-view matrix mv_matrix and the projection matrix proj_matrix; Among them, the vertex coordinate variable pos includes x, y, and z components, the color component variable acolor includes r, g, b, and a components, and the texture data variable texCoord includes u and v coordinates and the object type marker f.

[0041] As a unified variable, the vertex shader performs an affine transformation algorithm on the vertex coordinate variable pos using the projection matrix proj_matrix and the model-view matrix mv_matrix, and assigns the result to the built-in variable gl_Position. At the same time, the color component variable acolor is directly assigned to the output variable varyingColor, the u and v coordinates of the texture data variable texCoord are assigned to the output variable tc, and the object type marker is assigned to the output variable bUseTexture, thereby passing the data to the rasterization stage.

[0042] The method for defining a fragment shader is as follows: A fragment shader includes three input variables that strictly correspond to the output of the vertex shader: the interpolated color variable varyingColor, the texture coordinate variable tc, and the texture usage flag bUseTexture.

[0043] The fragment shader uses the `texture` function to sample the texture color from the texture coordinate variable `tc` in the texture sampler. If the object type flag `bUseTexture` is true, it means that the current object needs to perform a multiplication and mixing operation between the texture color and the color variable `varyingColor`. Conversely, if the object type flag `bUseTexture` is false, it means that the current object does not need to perform a mixing operation between the texture color and the color components. The final calculated result is assigned to the built-in output variable as the final pixel color `vec4` value.

[0044] Vertex and fragment shader objects are created and loaded, compiled, and then linked to the program object to activate the rendering pipeline. In this embodiment of the invention, the graphics rendering pipeline is started by calling the glDrawArrays drawing function provided in OpenGL.

[0045] S3: In the vertex processing stage of the graphics rendering pipeline, the vertex shader performs coordinate transformations and simultaneously parses the object type markers in the VBO, passing these markers as non-interpolated variables (out) to the rasterization stage. Specifically, the vertex shader reads the vertex coordinates and object type markers of the current vertex from the VBO through input attributes. For frequent scaling, translation, and rotation operations in the scene, the model-view matrix (mv_matrix) is composed of the view matrix... It is composed of the affine transformation matrix of the model, and the specific algorithm is as follows:

[0046] Let the translation vector be... Scaling a 3D vector Taking a rotation angle θ around the Z-axis as an example, we construct a 4x4 dimensional translation matrix T, scaling matrix S, and rendering matrix. and the model-view matrix mv_matrix:

[0047] ;

[0048] ;

[0049] ;

[0050] ;

[0051] in, This represents the translation distance of the model along the x-axis in three-dimensional space. This represents the translation distance of the model along the y-axis in three-dimensional space. This represents the translation distance of the model along the z-axis in three-dimensional space. This represents the scaling factor of the model along the x-axis. This represents the scaling factor of the model along the y-axis. This represents the scaling factor of the model along the z-axis.

[0052] Furthermore, utilizing the constructed model-view matrix The projection matrix `proj_matrix` transforms the vertex coordinates to clip space, and the final output is defined by the built-in variable `gl_Position`, with the vertex coordinates represented as column vectors. T ,in, This represents the coordinates of the vertex on the x-axis. This represents the y-coordinate of the vertex. This represents the coordinates of the vertex on the z-axis. The complete mathematical formula for matrix-vector multiplication of this coordinate transformation is:

[0053] ;

[0054] in, This indicates a built-in output variable.

[0055] When processing object type tags, the vertex shader assigns the object type tag to an output variable named `out`. To prevent the GPU from corrupting the discrete classification tags during rasterization, this embodiment of the invention modifies the output variable `out` with the `flat` keyword in the shader algorithm and employs a non-interpolation propagation algorithm, ensuring that the tag value received by any pixel `j` generated by rasterization is consistent with the target object type tag. Strictly equal to the original label value of the vertex generated by the corresponding primitive. This means that the rasterization linear interpolation for this variable is forcibly canceled, so that the status code passed to the fragment shader remains the original discrete integer value, i.e., 1.0 or 0.0; for texture coordinates and color components, the regular smooth linear interpolation algorithm is used to ensure that the fragment shader receives values ​​with a smooth transition.

[0056] S4: In the fragment processing stage of the graphics rendering pipeline, the fragment shader receives the object type flag and executes branch logic based on the object type flag value: if the current drawing object is a face object, texture sampling is performed; if it is a point object or a line object, the color components are directly output. Specifically, the fragment shader constructs a rendering algorithm based on the object type flag condition, receiving the object type flag bUseTexture passed from the vertex shader, along with the corresponding texture coordinates and color components, denoted as... ,in, Represents the vertex color vector. This represents the color component value of the vertex color vector in the red channel. This represents the color component value of the vertex color vector in the green channel. This represents the color component value of the vertex color vector in the blue channel. This represents the color component value of the vertex color vector in the alpha channel; the fragment shader determines this based on the object type flag:

[0057] When the object type flag bUseTexture > 0.5 is detected, it indicates that the current primitive is a face object, and the texture sampling and blending algorithm is executed: the corresponding texel color is extracted. ,in, Represents the texture sampled color vector. This represents the color component value of the texture sampled color vector in the red channel. This represents the color component value of the texture sampled color vector in the green channel. This represents the color component value of the texture sampled color vector in the blue channel. This represents the color component value of the texture sampled color vector in the alpha channel, and the final fragment color is obtained by performing a Hadamard product between the texel color and the vertex color. The specific formula is as follows:

[0058] .

[0059] When the object type flag bUseTexture ≤ 0.5 is detected, it indicates that the current primitive is a point or line object. The fragment shader then ignores the texture sampling step and directly applies the following formula, outputting only the vertex color passed down as the final pixel color:

[0060] .

[0061] S5: After the graphics rendering pipeline finishes execution and the image is drawn to the frame buffer, the VBO and shader program are unbound, and the VBO object and shader program are deleted to release video memory and memory resources, thus completing the rendering. This embodiment of the invention executes the cleanup algorithm according to the OpenGL specification: by calling the glBindBuffer(GL_ARRAY_BUFFER, 0) function and the glUseProgram(0) function, the current VBO and shader program are unloaded from the rendering pipeline context. Then, the objects are destroyed, the glDeleteBuffers function is called to reclaim the VBO handle, and the glDetachShader function and the glDeleteProgram function are called sequentially to separate and delete the attached shader modules and the linked program pairs.

[0062] In the map drawing process, this invention uses texture coordinates and encodes information of multiple object types within VBOs to achieve unified shader drawing of point, line, and surface objects, presenting richer texture details and visual layers, and better meeting the requirements of efficient and unified rendering in complex scenes.

[0063] This invention connects map elements with different rendering logics, enabling unified rendering of heterogeneous objects within a single pipeline. The embodiments of this invention fully utilize the parallel processing capabilities of the GPU, eliminating the state overhead caused by frequent shader switching, significantly saving communication bandwidth between the CPU and GPU. Furthermore, it leverages the storage regularity of VBOs, simplifying the development of multi-primitive hybrid rendering in complex scenes and facilitating the horizontal scaling of the system in large-scale geographic information visualization.

[0064] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A unified rendering method for multiple OpenGL objects based on VBO internal encoding, characterized in that, Includes the following steps: S1: For the data of each vertex, construct a hybrid data structure containing vertex coordinates, color components, texture coordinates and object type markers, and store the hybrid data structure in the memory of the VBO in an interleaved manner; S2: Define a unified vertex shader and fragment shader, compile the defined vertex shader and fragment shader and create shader programs, activate the shader programs after the shader programs are compiled, bind VBOs and set vertex attribute pointers, and call the drawing function to start the graphics rendering pipeline; The method for defining a vertex shader is as follows: A vertex shader includes three input attributes that strictly correspond to the VBO data layout and two unified variables: The three input attributes include the vertex coordinate variable pos at position 0, the color component variable acolor at position 1, and the texture data variable texCoord at position 2; The two unified variables include the model-view matrix mv_matrix and the projection matrix proj_matrix; Wherein, the vertex coordinate variable pos includes x, y, and z components, the color component variable acolor includes r, g, b, and a components, and the texture data variable texCoord includes u and v coordinates and the object type marker f; The vertex shader performs matrix transformation operations on the vertex coordinate variable pos using the projection matrix proj_matrix and the model-view matrix mv_matrix, and assigns the result to the built-in variable gl_Position. At the same time, it directly assigns the color component variable acolor to the output variable varyingColor, assigns the u and v coordinates of the texture data variable texCoord to the output variable tc, and assigns the object type marker to the output variable bUseTexture, thereby passing the data to the rasterization stage. The fragment shader is defined as follows: It includes three input variables that strictly correspond to the vertex shader output: the interpolated color variable `varyingColor`, the texture coordinate variable `tc`, and the texture usage flag `bUseTexture`. The fragment shader uses the `texture` function to sample the texture color from the texture sampler based on the texture coordinate variable `tc`. If the object type flag `bUseTexture` is true, it means the current object needs to perform a blending operation between the texture color and the color variable `varyingColor`. Conversely, if the object type flag `bUseTexture` is false, it means the current object does not need to perform a blending operation between the texture color and the color components. The final calculated result is assigned to the built-in output variable as the final pixel color `vec4` value. Create and load vertex shader and fragment shader objects, compile them, attach the vertex shader and fragment shader to the program object for linking, and activate the rendering pipeline; S3: In the vertex processing stage of the graphics rendering pipeline, the vertex shader performs coordinate transformation and parses the object type marker in the VBO, passing the object type marker in the VBO as a non-interpolated variable out to the rasterization stage. S4: In the fragment processing stage of the graphics rendering pipeline, the fragment shader receives the object type flag and executes the branch logic according to the object type flag value: if the current drawing object is a face object, then texture sampling is performed; if it is a point object or a line object, then the color component is directly output. S5: After the graphics rendering pipeline has finished executing and the image has been drawn to the frame buffer, unbind the VBO and shader program, delete the VBO object and shader program, release video memory and memory resources, and complete the rendering.

2. The OpenGL multi-object unified rendering method based on VBO internal encoding according to claim 1, characterized in that, The specific method for step S1 is as follows: S101: Define an array in memory, and arrange all the data of a single vertex in a continuous manner according to the pattern of vertex coordinates-color component-texture coordinates-object type marker, so that the data of all vertices form a continuous linear block in memory; S102: Use OpenGL to generate a buffer object and obtain its ID. Then bind the buffer object to the GL_ARRAY_BUFFER attribute value in OpenGL as a vertex buffer. Then use data transfer instructions to upload the constructed memory data to the GPU memory at once. S103: Define the method for the GPU to parse memory data by setting vertex attribute pointers. The specific steps are as follows: specify the step size of the vertex attribute pointer as the total number of bytes of all attributes of a single vertex, and set the offset of the starting byte for vertex coordinates, color components, texture coordinates and object type markers respectively, so that the shader can accurately read different types of data from the same VBO.

3. The OpenGL multi-object unified rendering method based on VBO internal encoding according to claim 2, characterized in that, The specific method of step S3 is as follows: The vertex shader reads the vertex coordinates and object type marker of the current vertex from the VBO through the input attributes; it transforms the vertex coordinates to clip space using the model-view matrix mv_matrix and the projection matrix proj_matrix and assigns them to the built-in output variable gl_Position to determine the shape of the graphic; when processing the object type marker, the vertex shader assigns the object type marker to the output variable named out and prohibits the GPU from performing linear interpolation on the out variable during the rasterization stage, so that the status code passed to the fragment shader remains the original discrete integer value; for texture coordinates and color components, the GPU is allowed to perform linear interpolation during the rasterization stage to ensure that the fragment shader receives values ​​with a smooth transition.

4. The OpenGL multi-object unified rendering method based on VBO internal encoding according to claim 3, characterized in that, The specific method of step S4 is as follows: The fragment shader receives the object type marker, corresponding texture coordinates, and color components passed from the vertex shader; the fragment shader makes a judgment based on the object type marker: when the object type marker indicates that the current primitive is a face object, the fragment shader uses the interpolated texture coordinates to call the texture sampling function, extracts the corresponding texel color, and mixes the texel color and vertex color components to calculate the texture color and vertex color; if the object type marker indicates that the current primitive is a point object or a line object, the fragment shader ignores the texture sampling step and only outputs the passed vertex color components as the final pixel color.

5. The OpenGL multi-object unified rendering method based on VBO internal encoding according to claim 4, characterized in that, In step S5, the current VBO and shader program are unloaded from the rendering pipeline context by calling the application programming interface. Then, the objects are destroyed, the VBO handles are recycled, and the attached shader modules and the connected program pairs are separated and deleted in sequence.

Citation Information

Patent Citations

  • Map linear symbol drawing method and system based on graphics processing unit (GPU)

    CN103268626A

  • Method for automatically drawing OpenGL program by using Vulkan

    CN121169663A