在OS X的Qt 4.8.6中更改QGLWidgets的OpenGL上下文版本 [英] Changing the OpenGL Context Version for QGLWidgets in Qt 4.8.6 on OS X

查看:111
本文介绍了在OS X的Qt 4.8.6中更改QGLWidgets的OpenGL上下文版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Qt 4.8.6通过QGLWidget渲染OpenGL内容.我正在使用的计算机是OS X 10.9.4的Macbook Pro.

I want to use Qt 4.8.6 to render OpenGL content with a QGLWidget. The machine i'm working on is a macbook pro with OS X 10.9.4.

通过将QGLFormat对象与请求的3.2核心概要文件的格式版本一起传递来创建QGLWidget.我遇到的问题是,无论我指定哪种GLFormat,QGLContext报告的OpenGL版本始终为1.0.

The QGLWidget is created by passing a QGLFormat object with a requested format version of the 3.2 core profile. The problem i am encountering is that the OpenGL version reported by the QGLContext remains 1.0, no matter what GLFormat I specify.

研究了主题之后,我发现了 Qt OpenGL核心配置文件指南.但是,示例源代码报告了与以前相同的OpenGL版本1.0.奇怪的是电话

After researching the topic i found the Qt OpenGL Core Profile Tutorial. However the example source code reports the same OpenGL version 1.0 from before. Curiously the call

qDebug() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion();
qDebug() << "Context valid: " << context()->isValid();
qDebug() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion();
qDebug() << "OpenGl information: VENDOR:       " << (const char*)glGetString(GL_VENDOR);
qDebug() << "                    RENDERDER:    " << (const char*)glGetString(GL_RENDERER);
qDebug() << "                    VERSION:      " << (const char*)glGetString(GL_VERSION);
qDebug() << "                    GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);

报告的版本字符串为2.1

reported a version string of 2.1

Widget OpenGl:  1 . 0 
Context valid:  true 
Really used OpenGl:  1 . 0 
OpenGl information: VENDOR:        NVIDIA Corporation 
                    RENDERDER:     NVIDIA GeForce GT 750M OpenGL Engine 
                    VERSION:       2.1 NVIDIA-8.26.26 310.40.45f01 
                    GLSL VERSION:  1.20 

使用从2011年开始在此OS X opengl上下文讨论中建议的可可代码版本号已更改为

Using the Cocoa code suggested in this OS X opengl context discussion from 2011 the output of the version numbers changed to

Widget OpenGl:  1 . 0 
Context valid:  true 
Really used OpenGl:  1 . 0
OpenGl information: VENDOR:        NVIDIA Corporation 
                    RENDERDER:     NVIDIA GeForce GT 750M OpenGL Engine 
                    VERSION:       4.1 NVIDIA-8.26.26 310.40.45f01 
                    GLSL VERSION:  4.10 

虽然驱动程序现在报告了预期的OpenGL版本号,但我仍然只能获得1.0 QGLWidget上下文.传递给QGLWidget构造函数的QGLFormat对象是使用

While the driver is now reporting expected OpenGL version number, i am still only able to get a 1.0 QGLWidget context. The QGLFormat object that is passed to the QGLWidget constructor is set up using

QGLFormat fmt;
fmt.setProfile(QGLFormat::CoreProfile);
fmt.setVersion(3, 2);
fmt.setSampleBuffers(true);

对于为什么我仍然只获得1.0版本的上下文,我有些茫然.即使没有Cocoa框架生成的OpenGL Context,也应该可以将上下文版本提高到2.1,但是无论将QGLFormat传递给构造函数,它的版本都固定为1.0.

I am somewhat at a loss as to why i am still only getting a version 1.0 context. Even without the Cocoa framework generated OpenGL Context it should be possible to increase the context version to 2.1, but it remains fixed at 1.0 regardless of the QGLFormat passed to the constructor.

非常感谢有关QGLWidget Context为何保持在1.0版的任何指示.

Any pointers as to why the QGLWidget Context remains at version 1.0 are very much appreciated.

进一步的实验表明,该代码在Ubuntu 13.04 Linux上返回了所请求的OpenGL版本.该问题似乎特定于OSX.

Further experimentation showed that the code returns the requested OpenGL version on a Ubuntu 13.04 Linux. The issue seems to be specific to OS X.

我构建了一个最小的非工作示例

I build a minimal non-/working example

#include <QtOpenGL/QGLFormat>
#include <QtOpenGL/QGLWidget>
#include <QtGui/QApplication>
#include <QtCore/QDebug>

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    QGLFormat fmt = QGLFormat::defaultFormat();
    fmt.setVersion(3,2);
    fmt.setProfile(QGLFormat::CoreProfile);
    fmt.setSampleBuffers(true);

    QGLWidget c(fmt);
    c.show();
    qDebug() << c.context()->requestedFormat();
    qDebug() << c.context()->format();

    return app.exec();
}

可以在Ubuntu中使用

which can be build in Ubuntu using

g++ main.cpp -I/usr/include/qt4 -lQtGui -lQtCore -lQtOpenGL -lGL -o test

或在OS X下

g++ main.cpp -framework OpenGL -framework QtGui -framework QtCore -framework QtOpenGL -o test

它输出两行QGLFormat调试输出.第一行是请求的格式,第二行是实际的上下文格式.两者都应显示major.minor版本号3.2.似乎可以在Ubuntu Linux上运行,但是在使用OS X时失败.

It prints two lines of QGLFormat debug output. The first is the requested format and the second line is the actual context format. Both are supposed to show a major.minor version number of 3.2. It seems to be working under Ubuntu Linux, but fails when using OS X.

好玩的时光.这可能是Qt4.8.6中的错误,因为在再次编译示例Qt5.3.1时不会发生此问题. 错误报告已提交.

Fun times. It might be a bug in Qt4.8.6, since the issue does not occur when compiling the example agains Qt5.3.1. A bug report has been filed.

其他人可以验证此行为吗?

Can someone else verify this behaviour?

推荐答案

是.那是特定于平台的.请在此处找到解决方法.

Yes. That's platform specific. Please find solution here.

重写QGLContex :: chooseMacVisual以指定特定于平台的初始化.

Override QGLContex::chooseMacVisual to specify platform specific initialization.

CustomGLContext.hpp:

CustomGLContext.hpp:

#ifdef Q_WS_MAC
void* select_3_2_mac_visual(GDHandle handle);
#endif // Q_WS_MAC

class CustomGLContext : public QGlContext {
  ...
#ifdef Q_WS_MAC
  void* chooseMacVisual(GDHandle handle) override {
    return select_3_2_mac_visual(handle); // call cocoa code
  }
#endif // Q_WS_MAC
};

gl_mac_specific.mm:

gl_mac_specific.mm:

void* select_3_2_mac_visual(GDHandle handle)
{
static const int Max = 40;
NSOpenGLPixelFormatAttribute attribs[Max];
int cnt = 0;

attribs[cnt++] = NSOpenGLPFAOpenGLProfile;
attribs[cnt++] = NSOpenGLProfileVersion3_2Core;

attribs[cnt++] = NSOpenGLPFADoubleBuffer;

attribs[cnt++] = NSOpenGLPFADepthSize;
attribs[cnt++] = (NSOpenGLPixelFormatAttribute)16;

attribs[cnt] = 0;
Q_ASSERT(cnt < Max);


return [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
}

这篇关于在OS X的Qt 4.8.6中更改QGLWidgets的OpenGL上下文版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆