Opencv learning notes () -- Build opencv project "Master opencv" chp.3 that supports OpenGL

Source: Internet
Author: User

From opencv2.4beta, OpenGL can have interfaces to the highgui module. Based on the reading in chapter 3 of Master opencv, this article describes how to embed OpenGL virtual objects in opencv display.

Note: To enable opencv to support OpenGL and not use pre-compiled libraries, use the cmake rebuild project. Note that enable_opengl = Yes (in version 2.4.2, by default, enable_opengl = No). Label changes in the advanced version of cmake are graphic interfaces. You only need to check the check box of with_opengl.

In practice, I encountered a problem. The vs Project obtained by using cmake (with_opengl added) cannot be compiled and passed. It has been depressing for a long time. Because OpenGL is supported in Vs, you do not need to install it. After finding this bug, you need to use <GL \ GL in the \ modules \ core \ SRC \ opengl_interop.cpp file. h> Add # include <windows. h>, (actually # include
<Gl. h> # include <windows. h> must be added before compilation. Here, we only re-compile opencv_core243d.lib and opencv_highgui243d.lib.

We have obtained the opencv lib that supports OpenGL. The next step is how to use opencv to create an OpenGL window. The basic calling method is similar to the mouse usage in opencv, which is implemented through the callback function. The core code is as follows:

// callback functionvoid onDraw(void* param){// Draw something using OpenGL here}int main(void){string openGLWindowName = "OpenGL Test";namedWindow(openGLWindowName, WINDOW_OPENGL);resizeWindow(openGLWindowName, 640, 480);setOpenGlContext(openGLWindowName);setOpenGlDrawCallback(openGLWindowName, onDraw, NULL);updateWindow(openGLWindowName); // when neededreturn 0;}

Previously, the last parameter called namedwindow usually uses the default value or window_autosize. This time, window_opengl is used, and setopenglcontext is called to establish a window Association. To draw a virtual object in this window, a callback function is required, setopengldrawcallback is used. Note that the first parameter of this function is the window name, the second parameter is the callback function name, and the third parameter is the callback function parameter, the ondraw callback function is a nonargument function, so it is null. Similar to MFC re-painting, you need to call invalidate or uadate. You need to call updatewindow when re-painting is required.

Upload one of my most basic OpenGL demos: http://download.csdn.net/detail/yang_xian521/5023063 (attached my rebulid's support for OpenGL lib), as shown below:

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.