Imitating the qtopengl example vowelcube, the problem of the qpainter hybrid problem of the 3D and Qt graphics engine

Source: Internet
Author: User

Imitating the qtopengl example vowelcube, the problem of the qpainter hybrid problem of the 3D and Qt graphics engine

C ++ Gui
Programming with QT 4, secondedition
This book introduces the mixed example of OpenGL 3D drawing engine and qpainter: vowelcube. I am secretly happy. Can I implement a similar function? After testing the results, I learned how the example works in the evening and the morning.

The book introduces that QT's drawing engine can be mixed with OpenGL Renderer in a drawing context (rendercontext), but note that if such a mix is used, therefore, you must switch the rendering context correctly. Although there are related introductions in the book, I think it is still a bit negligent-a little introduction is complete, we do not know that if we do not do this, it will not show graphics.

First, we need to clarify a problem. Whether to create such a hybrid program is convenient initializegl (), resizegl (), paintgl (), constructor using the qwidget base class, paintevent (), and resizeevent () function? After my experiment, I think mixed use is the best. If you have learned how to use glut to build OpenGL, I think you will agree with me.

When constructing the qglwidget subclass, I can write the initialization code to both the constructor and initializegl () function. I think that since it is a mixed OpenGL and qpainter application, we must use the paintevent () function. Because if you use the paintgl () function, you can only draw OpenGL images, right. You can either use resizegl () or override resizeevent () when re-painting is required. Depending on your needs, it is better to rewrite resizegl () according to the glut thinking. However, I believe that you can write a resizegl () function by yourself.

In order for OpenGL and qpainter to draw the image in a rendering context, you need to manually turn off some switches. The following code disables automatic filling of background images and automatic switching of cache (buffer zone ).

setAutoBufferSwap( false );setAutoFillBackground( false );

Note that when using qpainter to draw a two-dimensional image, you must use the begin () and end () member functions of qpainter to wrap (qpainter implicitly calls begin () during construction () function) to ensure the integrity of the two-dimensional Drawing (in fact, Windows programming's GDI and DirectX and OpenGL are doing this ). When using OpenGL to draw 3D images and set OpenGL, if it is not in the initializegl (), resizegl (), paintgl () functions, you need to use makecurrent () convert rendering context to OpenGL rendering. In addition, when initializing OpenGL or using OpenGL functions for rendering, remember to set clockwise to the front. That is

glFrontFace( GL_CW );

Alternatively, when OpenGL rendering is performed, all the spaces of OpenGL are pushed into the stack. After the painting is completed, the stack pops up (the vowelcube program is used in this example) and is wrapped using the following two statements:

glPushAttrib( GL_ALL_ATTRIB_BITS );glPopAttrib( );

If this is not done, the two-dimensional drawing drawn using qpainter cannot be displayed. I haven't encountered this problem in the previous glut programming. I 've been here for a long time. Therefore, we hope that this error will not occur again in the future.

With these rules, I began to study how to create a wide variety of qtopengl programs.


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.