Retrieve OpenGL Context from Qt 5.5 on OSX, retrieveosx

Source: Internet
Author: User

Retrieve OpenGL Context from Qt 5.5 on OSX, retrieveosx

In the latest Qt 5.5, the QOpenGLWidget is much better and has less bugs than the QGLWidget, but it doesn't supply the good API to retrieve the native OpenGL context. on Mac OSX the situation wocould be a bit more difficult, since the platform is in Obj-C not C \ C ++ as Windows and Linux.

So what you have to do to get the native Cocoa OpenGL context is to follow these steps.

Write a header fileCCocoaGLContext. hTo declare the function interface.

#pragma oncevoid * GetCocoaGLContext(void * x);

Write a Obj-CCCocoaGLContext. mmFile, super easy

#import <QCocoaNativeContext>#import "CCocoaGLContext.h"void * GetCocoaGLContext(void * x){    QCocoaNativeContext * p = (QCocoaNativeContext *)(x);    if (p)    {        NSOpenGLContext * x = p->context();        return [x CGLContextObj];    }    return 0;}

In the application, that wocould be easy to get the native OpenGL handle.

QVariant NativeHandle(GLContext->nativeHandle());#if defined(__APPLE__)CGLContextObj GLContext = reinterpret_cast<CGLContextObj>(GetCocoaGLContext(NativeHandle.data()));CGLShareGroupObj  GLShareGroup  = CGLGetShareGroup(GLContext);#elif defined(_MSC_VER)QWGLNativeContext WGLContext = NativeHandle.value<QWGLNativeContext>();#elseQGLXNativeContext GLXContext = NativeHandle.value<QGLXNativeContext>();#endif

It's super useful to initialize the OpenCL with GL Interop on 3 platforms.

Related Article

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.