QT under OpenGL (knowledge)

Source: Internet
Author: User

Although QT is not known for efficiency, but in fact there is enough optimization, the most typical is the default graphics double buffering, according to the programming conventions under windows need to be manually opened, and through the additional interface calls to use, which in the previous simple graphics programming learning compared to QT,WIN32 GDI feels particularly deep when there is no extra processing, the Win32 animation program that Flash, and the QT program is very stable. In fact, I think that Qt's other problems are much more effective than the efficiency, such as the sense that the QT program Refresh under Windows obviously no Windows native program Fast , this is worth improving.
OpenGL only handles drawing with 3D graphics and basically does not provide the ability to create a user interface, so creating a user interface for OpenGL applications must use other graphics packages (such as Motif, MFC, and so on). QT's OpenGL module is a good solution to this problem, and it provides an OpenGL component class Qglwidget that inherits from Qwidget, making the part class as useful as other parts of Qt, as well as using OpenGL's API interface directly when drawing widgets. The classes that support OpenGL in Qt include the following:

    • Qglwidget: Easy-to-use QT widget for rendering OpenGL scenes.

    • Qglcolormap: Used to install user-defined color maps in Qglwidget.

    • Qglcontext: Encapsulates a scenario for OpenGL rendering.

    • Qglformat: Specifies the display mode for OpenGL-dyed scenes.

    • Qglframebufferobject and Qglpixelbuffer provide support for GL Framebuffer objects and GL pixel buffering, respectively.

    • A derived class of qglpaintengine:qpaintengine that provides the OpenGL drawing engine for Qpainter.

    • The INITIALIZEGL () registration function, where you set the render drawing properties of GL, define the display list, load the fixed textures, and so on initialization work. Initializegl () is called only once before calling PAINTGL (), and is no longer called.

    • PAINTGL () Draws a function that uses an interface in OpenGL to plot the scene, and Qglwidget's PaintEvent (qpaintevent*) automatically calls PAINTGL () for the part's display to be drawn. You can also call PAINTGL () when a redraw is required through UPDATEGL ().

    • RESIZEGL () This function is used to handle the actions required for each matrix of the OpenGL drawing pipeline when the size of the part changes. The function PAINTGL () before the first call, Initializegl () is called after the first call, and then each time the Qglwidget's missing size is changed, the function is called to set the view, projection matrix, and so on.


Learning OpenGL for a long time, but also in the framework of QT experience OpenGL, which is also the benefits of learning OpenGL, learning D3D words are not so happy, in fact, it also led me to learn Western learning-_-! really do not know is good or bad ... Just as a programmer's feeling, if the world only left windows, then really lost too much color.

 

This completes the task of drawing a rectangle using OpenGL, which is called a completely normal OpenGL function in PAINTGL. Just as we've learned about ordinary OpenGL functions, there's no difference. One of the main codes is in OpenGL::p AINTGL (), which requires extra attention, which is that this is different from the normal QT program, where the normal QT program places the work of redrawing in PaintEvent, but, as you can imagine, actually PAINTGL is just a virtual interface called in Qglwidget paintevent , and QT can do enough OpenGL prep work outside. The INITIALIZEGL,RESIZEGL,PAINTGL 3 additional virtual interfaces make up a simple but powerful OpenGL framework , like the glut abstraction framework and the framework I built when I was learning Win32 OpenGL. Knowing this, you can divide the programming of OpenGL in QT into two parts, and one part is the field of OpenGL that consists of the INITIALIZEGL,RESIZEGL,PAINTGL three virtual interfaces, in which we can do our customary OpenGL operations, Other GUI-related processing, such as the input of the program, is left to the original framework of QT.

OpenGL from Win32 to QT

In fact, in the drawing of two-dimensional graphics, it is entirely possible to use the more convenient qpainter without using OpenGL's native interface. Qpainter is just an API, and the actual rendering is performed by Qpaintengine. Qt comes with Qglpaintengine, providing OpenGL backstage for Qpainter. The Qpainter, created on qglwidget basis, automatically takes qglpaintengine as the drawing engine. In this way, any 2D graph drawn using this Qpainter type will automatically resolve to OpenGL, indirectly with the native interface of GL for two-dimensional drawing. The advantage of using Qpainter instead of OpenGL commands is that rendering can be easily toggled between the platform default engine and OpenGL. This will not only take advantage of both advantages, but also directly use the many advanced features provided by the QT Two-dimensional mapping system.

QT provides a QTOPENGL module to support OpenGL modules with three-dimensional graphics capabilities. When using the Qtopengl module, the corresponding project file (*.pro) must be added:

QT + = OpenGL

To declare the use of the Qtopengl module so that Qmake adds the required OpenGL libraries when using *.pro to generate the makefile required by the compiler.

Using the Create OpenGL drawing component under Qt is implemented mainly by inheriting qglwidget and reproducing related drawing functions, qglwidget derives from Qwidget, so qglwidget can be used like qwidget in most cases, Just use OpenGL's function instead of qpainter to implement the drawing. The use of OpenGL is usually achieved through the subclass Qglwidget, and Qglwidget provides three virtual functions to be overloaded to achieve OpenGL rendering:

OpenGL is notified by calling Updategl () in any place where it needs to refresh the scene, but Updategl () is not itself implemented. As with Qwidget, mouse and keyboard events are handled by event handler functions such as Mousepressevent () and Keypressevent (). To create an animation, simply start Qtimer, and then call Updategl (). It should be noted, however, that if you need to draw PAINTGL () the same functionality in a function other than the Qglwidget derived class, you need to first use the makecurrent () interface to mark the context object you are drawing later as the current context object Context).

If local OpenGL supports overlay drawing (Overlay paint) (both Windows WGL and X11 GLX support) Qglwidget can also be passed with INITIALIZEOVERLAYGL (), similar to the aforementioned RESIZEOVERLAYGL (), PAINTOVERLAYGL (), and UPDATEOVERLAYGL () are plotted on overlapping layers. The Makeoverlaycurrent () function can be used to mark overlaps as the current context object.

In addition to support for OpenGL native interfaces, Qglwidget also provides a shallow encapsulation of OpenGL applications on multiple operating system platforms. QT provides the ability to load images and bind them to textures. Interfaces such as Gluint bindtexture (qimage&, glenum target, glint format, qglcontext::bindoptions) can be used to bind the image to the requested OpenGL texture index. and use Deletetexture (gluint ID) to delete the requested texture index. QT tracks the pixel images/images that are bound to the texture internally, so that you can reuse the textures when using the same image file or pixel graph. Because Qimage is hardware-independent, it is possible to draw a texture picture in another thread, load OpenGL as a texture after drawing is complete, and do not need to process in OpenGL render threads, thus greatly improving the response speed of OpenGL drawing parts.

QT also provides the use of arbitrary characters (including non-ASCII characters such as Chinese characters) in a given Qfont font format at any three-dimensional location in the OpenGL three-dimensional plotting scene, greatly simplifying the operation of drawing characters in three-dimensional scenes.


QT under OpenGL (knowledge)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.