A method for implementing an adaptation layer based on OpenGL
Through the OpenGL-based adaptation layer implementation method, the difficulties of MBSE tools in cross-platform graphics code generation are solved, the code generation efficiency and readability are improved, unified management of resources and errors is achieved, mathematical operations and curve drawing are optimized, and the human-computer interface code generation process is simplified.
Patent Information
- Application Number
- CN202510188318.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-20
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2045-02-20
AI Technical Summary
Existing MBSE tools such as SCADE and MagicDraw are monopolized by foreign countries, which makes it difficult for my country to use graphics generation codes across platforms in model-driven system engineering, and the generated codes are less readable and efficient.
An OpenGL-based adaptation layer implementation method is proposed, which includes unified management, calculation optimization and external interface. It provides an interface matching the onModel tool, uniformly manages resources, status and errors, optimizes mathematical operations and curve drawing, reduces coupling and achieves seamless integration.
It improves the efficiency and readability of graphics generation code, reduces resource leakage and performance loss, provides a complete error handling mechanism, and simplifies the human-computer interface code generation process.
Smart Images

Figure CN120029621B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of model-driven system engineering, and in particular relates to an OpenGL-based adaptation layer implementation method. Background Art
[0002] Model-Based Systems Engineering (MBSE), a systems engineering approach, utilizes models, rather than documents, as core artifacts throughout the entire system development lifecycle. Compared to traditional software development methods, it effectively addresses issues such as information silos, consistency issues, poor traceability, and low development efficiency. Furthermore, with increasing system complexity, MBSE's advantages are becoming increasingly apparent, particularly in sectors such as aerospace, automotive, defense, and healthcare.
[0003] Currently, mature MBSE tools such as SCADE and MagicDraw are all monopolized by foreign countries. With the improvement of my country's independent research and development capabilities of industrial software, benchmarking tools in the MBSE field are also developing rapidly. Among them, OnModel, as a model-based detailed design tool independently developed by our unit, can provide a graphic design and development environment for embedded human-machine interfaces, as well as code generation functions. In order to ensure that the code can be used across platforms, the underlying layer implements graphics rendering based on OpenGL. In order to make the graphics code generation process smoother, the generated code more readable and more efficient, the present invention proposes an adaptation layer implementation method based on OpenGL. Based on the graphics provided by the onModel tool, an adaptation layer was established from the aspects of rendering efficiency and ease of use. It mainly implements a set of interfaces that match the onModel tool graphics, making the code generation process of the human-machine interface simpler and the generated code graphics rendering more efficient. Summary of the Invention
[0004] (1) Technical issues to be solved
[0005] The technical problem to be solved by the present invention is how to provide an OpenGL-based adaptation layer implementation method to solve the image adaptation problem of model-based design tools.
[0006] (2) Technical solution
[0007] In order to solve the above technical problems, the present invention proposes an OpenGL-based adaptation layer implementation method, which includes: unified management, calculation optimization and external interface. The unified management includes resource, status and error management; the calculation optimization includes mathematical operation optimization and curve and polygon drawing optimization; the external interface includes an interface matching the onModel tool;
[0008] In the unified management part, the adaptation layer defines a structure that stores all status information, resource information, and error information. The onModel tool provides six resource tables: line type, line width, texture, font, gradient, and color. The adaptation layer loads all these resource tables into the structure during initialization and obtains the corresponding resources through indexes when used to implement resource management. At the same time, the structure contains all the states required for drawing. Error management defines an error flag and error parameter flag for each external interface. The input or status is checked during function execution. When an error is detected, the error setting function is used to push the error to the error storage variable of the structure. An error acquisition function is provided to obtain all error information.
[0009] In terms of computational optimization, the adaptation layer first optimizes commonly used mathematical operations in graphics drawing, including trigonometric functions, inverse trigonometric functions, exponential operations, square root operations, and division. The adaptation layer also provides methods for drawing Bezier curves and circular arcs. By calculating the coordinates of each point on the curve, the curve is approximated by a polygon. When approximating an arc, the point spacing is associated with the radius and scaling ratio. When approximating a Bezier curve, the point spacing is proportional to the distance between the starting point, control point, and end point. Complex polygons are drawn using the ear cutting method.
[0010] In terms of external interfaces, the adaptation layer provides interfaces that match the onModel tool. The interfaces are divided into five categories according to their functions: initialization, basic primitive drawing, clipping, interaction, and appearance. When the onModel tool generates code for the human-computer interface, these matching interfaces are called to implement OpenGL calls and complete graphics rendering.
[0011] (3) Beneficial effects
[0012] The present invention proposes a method for implementing an adaptation layer based on OpenGL. The present invention proposes a method for implementing an adaptation layer based on OpenGL, which has the following main features:
[0013] (1) Based on the human-computer interface design elements provided by the onModel tool, a set of matching interfaces is provided to reduce the coupling between the application logic and OpenGL, achieve seamless integration with the onModel tool, and enable the human-computer interface drawn in the onModel tool to generate embedded code more conveniently. The generated code is easier to understand than OpenGL.
[0014] (2) The adaptation layer manages all used states, resources, and error information in a unified manner, which can reduce resource leakage and performance loss, provide a more complete error handling mechanism, and make errors easier to track and repair.
[0015] (3) The adaptation layer implements common mathematical operations, Bezier curve drawing, arc drawing and other functions, which can improve rendering efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 This is a composition diagram of the adaptation layer of the present invention. DETAILED DESCRIPTION
[0017] In order to make the purpose, content and advantages of the present invention more clear, the specific implementation methods of the present invention are further described in detail below with reference to the accompanying drawings and examples.
[0018] To meet the practical functional and performance requirements of OnModel, a model-based software analysis, design, and verification tool, users need to generate corresponding C code based on the designed human-machine interface during the modeling process to implement the graphical design of the embedded human-machine interface. This invention provides an OpenGL-based adaptation layer implementation method. This method establishes an adaptation layer based on the graphics primitives provided by the OnModel tool, encapsulating OpenGL and forming an adaptation layer that matches the OnModel graphics primitives. This method simplifies the process of generating C code from the graphical interface and improves code readability. This method also provides support for the implementation of subsequent OnModel tool functions.
[0019] This paper aims to address the critical issue of generating C code for the human-machine interface (HMI) in OnModel, a proprietary tool developed in the MBSE field. The paper proposes an OpenGL-based adaptation layer implementation. The adaptation layer comprises three functionalities: unified management, computational optimization, and external interfaces. Unified management includes resource, status, and error management; computational optimization includes mathematical operations optimization, as well as curve and polygon drawing optimization; and the external interface includes an interface compatible with the OnModel tool.
[0020] In the unified management part, the adaptation layer defines a structure that stores all status information, resource information, and error information. The onModel tool provides six resource tables: line type, line width, texture, font, gradient, and color. The adaptation layer loads all these resource tables into the structure during initialization, and obtains the corresponding resources through indexes when used to implement resource management. At the same time, the structure contains all the states required for drawing, such as the current drawing mode, whether blending is enabled, the current view depth, the texture target, whether gradient is enabled, whether polygon smoothing is enabled, etc. Error management defines an error identifier and an error parameter identifier for each external interface. The input or status is checked during the execution of the function. When an error is detected, the error setting function is used to push the error to the error storage variable of the structure. An error acquisition function is provided to obtain all error information.
[0021] In the calculation optimization part, the adaptation layer first optimizes the mathematical operations commonly used in graphics drawing, including trigonometric functions, inverse trigonometric functions, exponential operations, square root, division, etc. Among them, the inverse trigonometric function and square root operation are implemented using the table lookup method to reduce computing consumption and improve rendering efficiency. The division operation is optimized for different data types, and error handling is performed to improve program security and robustness. The adaptation layer provides a method for drawing Bezier curves and arcs. By calculating the coordinates of each point on the curve, the curve is approximated by a polygon. In order to improve accuracy while reducing calculation loss, the point interval is associated with the radius and scaling ratio when the arc is approximated. When the Bezier curve is approximated, the point interval is proportional to the distance between the starting point, control point and end point. Complex polygons are drawn using the ear cutting method.
[0022] For external interfaces, the adaptation layer provides interfaces that match the onModel tool. These interfaces are divided into five functional categories: initialization, basic primitive drawing, clipping, interaction, and appearance. When the onModel tool generates code for the human-machine interface, it calls these matching interfaces to implement OpenGL calls and complete graphics rendering.
[0023] Example 1:
[0024] The composition of the present invention is as follows Figure 1 As shown, the code is divided into two parts: the external interface and the internal implementation. Unified management and computational optimization are both part of the internal implementation, which calls the OpenGL interface to achieve final graphics rendering. All OpenGL call functions are declared in mgl_private.h. The external interface provides an interface that matches the primitives in the onModel tool and is called by the application layer.
[0025] The structure used in unified management is defined in mgl_context_type.h. The variables involved in resource management are shown below. All color, line type, line width, font, texture, and gradient data are pointed to the corresponding data tables in the form of pointers. When a resource needs to be used, it is indexed by the current resource serial number to reduce the overhead of repeated resource creation and destruction.
[0026]
[0027] The following table lists some of the relevant variables for state management, including the drawing mode, information about the currently drawn primitives, coordinate transformation information, scaling information, clipping planes, text information, and path information. External API calls essentially modify these state variables and then call OpenGL to render the graphics.
[0028]
[0029]
[0030] The following table lists some of the error flags and related error functions defined by error management. Error flags are defined in mgl_constant.h. Each external interface defines an error flag, some of which are listed here. After each interface call, the parameters are checked. If an error is found, the error information is stored in the assigned error output variable using the error function oglxSetError. Users can retrieve all error information and the error storage status using the mglGetErrors function.
[0031]
[0032] In the calculation optimization part, common mathematical operations are declared in mth.h. They include trigonometric functions, inverse trigonometric functions, square root, exponentiation, modulus, and drawing.
[0033] Since the arccosine function in the inverse trigonometric function changes very slowly at both ends, the formula is used to implement it in the middle part, and the parts with values greater than 0.98 and less than -0.98 at both ends are implemented using a table lookup method, which can balance memory usage and calculation accuracy.
[0034] In the square root operation, the square root of a single-precision floating-point number is calculated by dividing the floating-point number into the exponent and mantissa, and calculating them separately. The exponent part is obtained by shifting, and the mantissa is obtained by looking up a table. Then, the sign bit, exponent, and mantissa are combined to obtain the square root of the single-precision floating-point number. This method can speed up calculation efficiency.
[0035] In the drawing part, the method of drawing the Bezier curve is as follows: first calculate the distance between the starting point, control point and end point, divide the sum of the distances by 10 to get the number of points on the curve that need to be obtained, and then use the Bezier curve formula to obtain the coordinates of each point, put all these points into the vertex buffer, and draw them uniformly.
[0036] The method for drawing an arc is: according to the radius size, set different point intervals. Specifically, when the radius is less than 10, the point interval is 28°; when the radius is less than 20, the point interval is 17°; when the radius is less than 60, the point interval is 9°; when the radius is less than 260, the point interval is 5°; in other cases, the point interval is 3.4°. Then calculate the points on the arcs of these angles, put the vertices into the vertex cache, and draw them uniformly.
[0037] This method of decreasing the interval between points as the radius increases can achieve better visual effects with less computation. When drawing polygons, the ear cutting method is used to draw the polygons.
[0038] Regarding external interfaces, the onModel tool provides the basic elements of human-machine interface design, categorized by function as: primitives, clipping, interaction, and containers. To align with these basic elements, the adaptation layer's interfaces are divided into initialization, primitives, clipping, interaction, and coordinate transformation. All external interface declarations are in mgl.h.
[0039] The interface in initialization is used to initialize the defined global structure and load resource data. Some of the interfaces in initialization are as follows:
[0040]
[0041]
[0042] The interfaces in the basic primitives provide basic primitive drawing functions, including lines, paths, arcs, elliptical arcs, circles, polygons, rings, and text. Paths can be further divided into straight lines, Bezier curves, arcs, horizontal lines, and vertical lines. Some of the interfaces are shown in the following table:
[0043]
[0044] The clipping interface provides functions such as clipping plane, clipping inside / outside of rectangle, clipping custom closed graphics, and judging whether a point is within the clipping range. Some of the interfaces are shown in the following table:
[0045]
[0046]
[0047] The interface of the coordinate transformation part provides functions such as coordinate translation, coordinate rotation, scaling, setting the current viewpoint, and transformation between the user coordinate system and the screen coordinate system. Some of the interfaces are shown in the following table:
[0048]
[0049] The interactive interface provides functions such as determining whether the cursor is within a given circular / rectangular / polygonal area, and the cursor's position within the rectangular / circular area. Some of the interfaces are shown in the following table:
[0050]
[0051]
[0052] Technical effects:
[0053] The present invention proposes an OpenGL-based adaptation layer implementation method, which has the following main features:
[0054] (1) Based on the human-computer interface design elements provided by the onModel tool, a set of matching interfaces is provided to reduce the coupling between the application logic and OpenGL, achieve seamless integration with the onModel tool, and enable the human-computer interface drawn in the onModel tool to generate embedded code more conveniently. The generated code is easier to understand than OpenGL.
[0055] (2) The adaptation layer manages all used states, resources, and error information in a unified manner, which can reduce resource leakage and performance loss, provide a more complete error handling mechanism, and make errors easier to track and repair.
[0056] (3) The adaptation layer implements common mathematical operations, Bezier curve drawing, arc drawing and other functions, which can improve rendering efficiency.
[0057] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A method for implementing an adaptation layer based on OpenGL, characterized in that: The adaptation layer includes: unified management, calculation optimization and external interface. Unified management includes resource, status and error management; calculation optimization includes mathematical operation optimization, curve and polygon drawing optimization; external interface includes interface matching onModel tool; In the unified management part, the adaptation layer defines a structure that stores all status information, resource information, and error information. The onModel tool provides six resource tables: line type, line width, texture, font, gradient, and color. The adaptation layer loads all these resource tables into the structure during initialization and obtains the corresponding resources through indexes when used to implement resource management. At the same time, the structure contains all the states required for drawing. Error management defines an error flag and error parameter flag for each external interface. The input or status is checked during function execution. When an error is detected, the error setting function is used to push the error to the error storage variable of the structure. An error acquisition function is provided to obtain all error information. In terms of computational optimization, the adaptation layer first optimizes commonly used mathematical operations in graphics drawing, including trigonometric functions, inverse trigonometric functions, exponential operations, square root operations, and division. The adaptation layer also provides methods for drawing Bezier curves and circular arcs. By calculating the coordinates of each point on the curve, the curve is approximated by a polygon. When approximating an arc, the point spacing is associated with the radius and scaling ratio. When approximating a Bezier curve, the point spacing is proportional to the distance between the starting point, control point, and end point. Complex polygons are drawn using the ear cutting method. In terms of external interfaces, the adaptation layer provides interfaces that match the onModel tool. The interfaces are divided into five categories according to their functions: initialization, basic primitive drawing, clipping, interaction, and appearance. When the onModel tool generates code for the human-computer interface, these matching interfaces are called to implement OpenGL calls and complete graphics rendering.
2. The OpenGL-based adaptation layer implementation method according to claim 1, wherein: The adaptation layer is divided into two parts: external interface and internal implementation. Unified management and calculation optimization belong to the internal implementation. The internal implementation calls the OpenGL interface to achieve the final graphics rendering. All functions calling OpenGL are declared in mgl_private.h. The external interface provides an interface that matches the primitives in the onModel tool for the application layer to call.
3. The OpenGL-based adaptation layer implementation method according to claim 2, wherein: The structure in unified management is defined in mgl_context_type.h. All color, line type, line width, font, texture and gradient data point to the corresponding data table in the form of pointers. When a resource needs to be used, it is indexed by the current resource serial number.
4. The OpenGL-based adaptation layer implementation method according to claim 2, wherein: Some relevant variables of state management include drawing mode, basic primitive information of the current drawing, coordinate transformation information, scaling information, clipping plane, text information and path information; after the external interface is called, the essence is to change these state quantities, and then call OpenGL to realize graphics rendering.
5. The OpenGL-based adaptation layer implementation method according to claim 2, wherein: Error management defines some error identifiers and related error functions. The error identifiers are defined in mgl_constant.h. First, an error identifier is defined for each external interface. After each interface is called, the parameters will be checked. If an error is found, the error information will be stored in the allocated error output variable through the error function oglxSetError. The user can obtain all error information and the status of the error storage through the mglGetErrors function.
6. The method for implementing an adaptation layer based on OpenGL according to any one of claims 3 to 5, wherein: In the calculation optimization part, mathematical operations are declared in mth.h, including trigonometric functions, inverse trigonometric functions, square root, exponentiation, modulus, and drawing.
7. The method for implementing an adaptation layer based on OpenGL according to claim 6, wherein: In inverse trigonometric functions, the arccosine function changes very slowly at both ends, so the middle part is implemented using a formula, and the parts with values greater than 0.98 and less than -0.98 at both ends are implemented using a lookup table. In the square root operation, the square root of a single-precision floating-point number is calculated by dividing the floating-point number into the exponent and mantissa, and calculating them separately. The exponent is obtained by shifting, and the mantissa is obtained by looking up a table. Then, the sign bit, exponent, and mantissa are combined to obtain the square root of the single-precision floating-point number.
8. The OpenGL-based adaptation layer implementation method according to claim 6, wherein: In the drawing part, the method of drawing the Bezier curve is as follows: first calculate the distance between the starting point, control point and end point, divide the sum of the distances by 10 to get the number of points on the curve to be obtained, then use the Bezier curve formula to obtain the coordinates of each point, put all these points into the vertex buffer, and draw them uniformly; The method for drawing an arc is: according to the radius size, set different point intervals. Specifically, when the radius is less than 10, the point interval is 28°; when the radius is less than 20, the point interval is 17°; when the radius is less than 60, the point interval is 9°; when the radius is less than 260, the point interval is 5°; in other cases, the point interval is 3.4°. Then calculate the points on the arcs of these angles, put the vertices into the vertex cache, and draw them uniformly.
9. The method for implementing an adaptation layer based on OpenGL according to claim 6, wherein: In terms of external interfaces, the onModel tool provides the basic elements of human-computer interface design, which are divided into: basic primitives, clipping, interaction and containers by function; the adaptation layer interface includes: initialization, basic primitives, clipping, interaction and coordinate transformation; all external interface declarations are in mgl.h.
10. The method for implementing an adaptation layer based on OpenGL according to claim 9, wherein: The function of the interface in initialization is to initialize the defined global structure and load resource data; The interface in the basic primitives provides basic primitive drawing functions, including lines, paths, arcs, elliptical arcs, circles, polygons, rings, and text. Among them, paths are further divided into straight lines, Bezier curves, arcs, horizontal lines, and vertical lines. The interface of the clipping part provides the functions of clipping plane, clipping inside / outside of rectangle, clipping custom closed graphics, and judging whether the point is within the clipping range; The interface of the coordinate transformation part provides the functions of coordinate translation, coordinate rotation, scaling, setting the current viewpoint, and transformation between the user coordinate system and the screen coordinate system; The interface of the interactive part provides the function of determining whether the cursor is within a given circular / rectangular / polygonal area and the position of the cursor within the rectangular / circular area.