C++ 在OS X上更改Qt4.8.6中QGLWidgets的OpenGL上下文版本
我想使用Qt4.8.6来使用QGLWidget呈现OpenGL内容。我正在使用的机器是一台带有OS X 10.9.4的macbook pro QGLWidget是通过将QGLFormat对象与3.2核心概要文件的请求格式版本一起传递而创建的。我遇到的问题是,QGLContext报告的OpenGL版本仍然是1.0,不管我指定了什么样的GLContext格式 在研究了这个话题之后,我找到了答案。但是,示例源代码报告了与以前相同的OpenGL版本1.0。奇怪的是,电话响了C++ 在OS X上更改Qt4.8.6中QGLWidgets的OpenGL上下文版本,c++,macos,qt,opengl,C++,Macos,Qt,Opengl,我想使用Qt4.8.6来使用QGLWidget呈现OpenGL内容。我正在使用的机器是一台带有OS X 10.9.4的macbook pro QGLWidget是通过将QGLFormat对象与3.2核心概要文件的请求格式版本一起传递而创建的。我遇到的问题是,QGLContext报告的OpenGL版本仍然是1.0,不管我指定了什么样的GLContext格式 在研究了这个话题之后,我找到了答案。但是,示例源代码报告了与以前相同的OpenGL版本1.0。奇怪的是,电话响了 qDebug() <&
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);
使用2011年建议的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
虽然驱动程序现在报告了预期的OpenGL版本号,但我仍然只能获得1.0 QGLWidget上下文。传递给QGLWidget构造函数的QGLFormat对象是使用
QGLFormat fmt;
fmt.setProfile(QGLFormat::CoreProfile);
fmt.setVersion(3, 2);
fmt.setSampleBuffers(true);
我有点不知所措,为什么我仍然只得到1.0版的上下文。即使没有Cocoa框架生成的OpenGL上下文,也可以将上下文版本增加到2.1,但无论传递给构造函数的QGLFormat是什么,它都保持在1.0
任何关于QGLWidget上下文为何保持在1.0版的提示都非常感谢
更新1
进一步的实验表明,该代码在Ubuntu13.04Linux上返回请求的OpenGL版本。这个问题似乎是针对OSX的
更新2
我构建了一个最小的非/工作示例
#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();
}
或者在OSX下
g++ main.cpp -framework OpenGL -framework QtGui -framework QtCore -framework QtOpenGL -o test
它打印两行QGLFormat调试输出。第一行是请求的格式,第二行是实际的上下文格式。两者都应该显示3.2的主版本号和次版本号。它似乎在Ubuntu Linux下工作,但在使用OS X时失败
更新3
快乐时光。这可能是Qt4.8.6中的一个bug,因为在重新编译Qt5.3.1的示例时不会出现此问题。A
其他人可以验证此行为吗?可以。这是特定于平台的。请找到解决办法 Override QGLContex::chooseMacVisual以指定特定于平台的初始化 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:
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];
}
void*选择\u 3\u 2\u mac\u视觉(GDHandle)
{
静态常数int Max=40;
NSOpenGLPixelFormatAttribute属性属性[Max];
int-cnt=0;
attribs[cnt++]=NSOpenGLPFAOpenGLProfile;
属性[cnt++]=NSOpenGLProfileVersion3_2核心;
attribs[cnt++]=NSOpenGLPFADoubleBuffer;
attribs[cnt++]=NSOpenGLPFADepthSize;
属性[cnt++]=(NSOpenGLPixelFormatAttribute)16;
attribs[cnt]=0;
Q_断言(cnt
欢迎链接到某个解决方案,但请确保您的答案在没有它的情况下是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面最相关的部分,以防目标页面不可用。
#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
};
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];
}