简体   繁体   English

在OS X的Qt 4.8.6中更改QGLWidgets的OpenGL上下文版本

[英]Changing the OpenGL Context Version for QGLWidgets in Qt 4.8.6 on OS X

I want to use Qt 4.8.6 to render OpenGL content with a QGLWidget. 我想使用Qt 4.8.6通过QGLWidget渲染OpenGL内容。 The machine i'm working on is a macbook pro with OS X 10.9.4. 我正在使用的计算机是OS X 10.9.4的Macbook Pro。

The QGLWidget is created by passing a QGLFormat object with a requested format version of the 3.2 core profile. 通过将QGLFormat对象与请求的3.2核心概要文件的格式版本一起传递来创建QGLWidget。 The problem i am encountering is that the OpenGL version reported by the QGLContext remains 1.0, no matter what GLFormat I specify. 我遇到的问题是,无论我指定哪种GLFormat,QGLContext报告的OpenGL版本始终为1.0。

After researching the topic i found the Qt OpenGL Core Profile Tutorial . 在研究了主题之后,我找到了Qt OpenGL Core Profile Tutorial However the example source code reports the same OpenGL version 1.0 from before. 但是,示例源代码报告了与以前相同的OpenGL版本1.0。 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);

reported a version string of 2.1 报告版本字符串为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 

Using the Cocoa code suggested in this OS X opengl context discussion from 2011 the output of the version numbers changed to 使用2011年此OS X opengl上下文讨论中建议的Cocoa代码,版本号的输出更改为

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 

While the driver is now reporting expected OpenGL version number, i am still only able to get a 1.0 QGLWidget context. 虽然驱动程序现在报告预期的OpenGL版本号,但我仍然只能获得1.0 QGLWidget上下文。 The QGLFormat object that is passed to the QGLWidget constructor is set up using 使用以下命令设置传递给QGLWidget构造函数的QGLFormat对象

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

I am somewhat at a loss as to why i am still only getting a version 1.0 context. 关于为什么我仍然只得到1.0版本的上下文,我有些茫然。 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. 即使没有Cocoa框架生成的OpenGL上下文,也应该可以将上下文版本提高到2.1,但是无论传递给构造函数的QGLFormat如何,它都保持固定为1.0。

Any pointers as to why the QGLWidget Context remains at version 1.0 are very much appreciated. 非常感谢有关为什么QGLWidget Context保持版本1.0的任何指针。

Update 1 更新1

Further experimentation showed that the code returns the requested OpenGL version on a Ubuntu 13.04 Linux. 进一步的实验表明,该代码在Ubuntu 13.04 Linux上返回了所请求的OpenGL版本。 The issue seems to be specific to OS X. 该问题似乎特定于OSX。

Update 2 更新2

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();
}

which can be build in Ubuntu using 可以使用以下方法在Ubuntu中构建

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

or under OS X 或在OS X下

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

It prints two lines of QGLFormat debug output. 它输出两行QGLFormat调试输出。 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. 两者都应显示major.minor版本号3.2。 It seems to be working under Ubuntu Linux, but fails when using OS X. 它似乎可以在Ubuntu Linux下运行,但是在使用OS X时失败。

Update 3 更新3

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. 这可能是Qt4.8.6中的错误,因为在再次编译示例Qt5.3.1时不会发生此问题。 A bug report has been filed. 已提交错误报告。

Can someone else verify this behaviour? 其他人可以验证此行为吗?

Yes. 是。 That's platform specific. 那是特定于平台的。 Please find solution here . 请在这里找到解决方案。

Override QGLContex::chooseMacVisual to specify platform specific initialization. 重写QGLContex :: chooseMacVisual以指定平台特定的初始化。

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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM