Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Opengl under QT (recognition)

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Although Qt is not famous for its efficiency, there is in fact enough optimization, the most typical of which is the default graphics double buffering. According to the programming convention under Windows, it needs to be turned on manually and can only be used through additional interface calls. This is especially profound when comparing Qt,Win32 GDI when learning simple graphics programming in the past. When there is no extra processing, the Win32 animation program flashes, and the Qt program is very stable. In fact, I think other problems with Qt are much more serious than efficiency, such as the personal perception that Qt programs refresh under Windows is obviously not as fast as Windows native programs, which is worth improving.

OpenGL only deals with 3D graphics and basically does not provide the function to create a user interface, so creating a user interface for OpenGL applications must use other graphical toolkits (such as Motif, MFC, etc.). The OpenGL module of Qt solves this problem very well, it provides an OpenGL component class QGLWidget that inherits from Qwidget, so that it can be used like other parts of Qt, and the API interface of OpenGL can be used directly when drawing widgets. The main classes that support OpenGL in Qt are as follows:

QGLWidget: an easy-to-use Qt widget for rendering OpenGL scenes.

QGLColormap: used to install user-defined face in QGLWidget.

QGLContext: encapsulates the scene for OpenGL rendering.

QGLFormat: specifies the display mode of the OpenGL scene.

QGLFrameBufferObject and QGLPixelBuffer provide support for GL frame buffer objects and GL pixel buffering, respectively.

A derived class of QGLPaintEngine:QPaintEngine that provides an OpenGL drawing engine for QPainter.

InitializeGL () registers the function, where you set the rendering properties of GL, define the display list, load fixed textures, and so on. Before initializeGL () is called only once before calling paintGL (), it is not called again.

PaintGL () drawing function, where the interface in OpenGL is used to draw the scene, and the paintEvent (QPaintEvent*) of QGLWidget will automatically call paintGL () to display and draw the widget. You can also call paintGL () when you need to redraw through updateGL ().

ResizeGL () this function is used to deal with the operations that need to be done on each matrix of the OpenGL drawing pipeline when the size of the part changes. Before the function paintGL () is called for the first time, initializeGL () is called for the first time, and then whenever the missing size of the QGLWidget changes, the function is called to set the view, projection matrix, and so on.

I have been learning OpenGL for a long time, and it is time to feel OpenGL under the framework of Qt. This is also the advantage of learning OpenGL. If you learn D3D, you will not be so Happy. In fact, it also leads to my honest learning from east and west. I don't know if it's good or bad. Just as a programmer's feeling, if only Windows is left in the world, then it really loses too much color.

This completes a task of drawing a rectangle using OpenGL. The calls in paintGL are completely ordinary OpenGL functions, just like the ordinary OpenGL functions we have learned, and there is no difference. The most important code is in OpenGL::paintGL (), which requires extra attention, that is, unlike ordinary Qt programs, ordinary Qt programs put the redrawing work in paintEvent, but it is conceivable that paintGL is just a virtual interface called in paintEvent in QGLWidget, and Qt can do enough OpenGL preparation outside. The three additional virtual interfaces of initializeGL,resizeGL,paintGL constitute a simple but powerful OpenGL framework, just like the framework abstracted by GLUT and the framework I built when I was learning Win32 OpenGL. After knowing this, we can divide the programming of OpenGL in Qt into two parts. One part is the field of OpenGL, which is composed of three virtual interfaces of initializeGL,resizeGL,paintGL, in which we can perform the OpenGL operations that we are used to. However, the input of the program and other GUI-related processing are still left to the original framework of Qt to complete.

OpenGL from Win32 to Qt

In fact, in drawing two-dimensional graphics, it is entirely possible to use the more convenient QPainter instead of using the native interface of OpenGL. QPainter is just an API, and the actual rendering is performed by QPaintEngine. Qt comes with QGLPaintEngine, which provides OpenGL background for QPainter. QPainter, created on the basis of QGLWidget, automatically uses QGLPaintEngine as a painting engine. In this way, any 2D graphics drawn using this QPainter type will be automatically parsed to OpenGL, indirectly using the native interface of GL to achieve two-dimensional graphics drawing. The advantage of using QPainter instead of the OpenGL command is that you can easily switch rendering between the platform default engine and OpenGL. In this way, we can not only make full use of their respective advantages, but also directly use many advanced functions provided by the Qt two-dimensional drawing system.

Qt provides QtOpenGL module to support the OpenGL module of 3D graphics function. When using the QtOpengl module, you must add it to the corresponding project file (* .pro):

QT + = opengl

To declare the use of the QtOpengl module so that qmake can add the required opengl libraries when using * .pro to generate the makefile required by the compiler.

The use of creating Opengl drawing components under Qt is mainly achieved by inheriting QGLWidget and reproducing related drawing functions. QGLWidget is derived from QWidget, so in most cases QGLWidget can be used like QWidget, only using the function of OpenGL instead of QPainter to achieve drawing. The use of OpenGL is usually realized by subclassing QGLWidget. QGLWidget provides three virtual functions for overloading to realize the drawing of OpenGL:

You can tell OpenGL to redraw by calling updateGL () wherever you need to refresh the scene painting, but updateGL () itself does not need to be implemented. Like QWidget, mouse and keyboard events are handled through event handlers such as mousePressEvent () and keyPressEvent (). To create an animation, simply start QTimer and call updateGL (). It should be noted, however, that if you need to draw with the same function as paintGL () in a function other than the QGLWidget derived class, you need to use the makeCurrent () interface to mark the context object (Context) drawn later as the current context object (Current Context).

If the local OpenGL supports overlay painting (Overlay Paint) (both WGL of windows and GLX of X11) QGLWidget can also draw overlay layers through similar initializeOverlayGL (), resizeOverlayGL (), paintOverlayGL (), and updateOverlayGL (). The makeoverlayCurrent () function can be used to mark an overlay as the current context object.

In addition to the support for OpenGL native interfaces, QGLWidget also provides shallow encapsulation for OpenGL applications on multiple operating system platforms. Qt provides the ability to load an image and bind it to a texture. You can use GLuint bindTexture (Qp_w_picpath&, GLenum target, GLint format, QGLContext::BindOptions) and other interfaces to bind the image to the applied OpenGL texture index, and use deleteTexture (GLuint id) to delete the applied texture index. Qt internally tracks pixel maps / images that have been bound to textures, so that textures can be reused when using the same image file or pixel map. Because QImage is hardware independent, the texture image can be drawn in another thread and loaded into OpenGL as a texture after painting, without the need for processing in the OpenGL rendering thread, so the response speed of OpenGL drawing parts can be greatly improved.

Qt also provides the ability to draw arbitrary characters (including non-ASCII characters such as Chinese characters) according to the given QFont font format at any 3D location in the OpenGL 3D drawing scene, which greatly simplifies the operation of drawing characters in the 3D scene.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report