程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Retrieve OpenGL Context from Qt 5.5 on OSX,retrieveosx

Retrieve OpenGL Context from Qt 5.5 on OSX,retrieveosx

編輯:C++入門知識

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 would 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 file CCocoaGLContext.h to declare the function interface.

#pragma once

void * GetCocoaGLContext(void * x);

Write a Obj-C CCocoaGLContext.mm file, 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 would 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>();
#else
QGLXNativeContext GLXContext = NativeHandle.value<QGLXNativeContext>();
#endif

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved