c++ - 无法在 QGLWidget 中设置所需的 OpenGL 版本

标签 c++ qt opengl opengl-3

我正在尝试在 Qt 4.8.2 中使用 QGLWidget。我注意到 QGLWidget 创建的默认上下文不显示 3.1 以上的 OpenGL 的任何输出。 The Qt wiki has a tutorial演示了使用 OpenGL 3.3 绘制一个简单的三角形。当我尝试运行本教程时,出现空白屏幕。如果我将 OpenGL 版本更改为 3.1,我会得到预期的输出(红色三角形)。

我的视频卡支持 OpenGL 4.2,在创建 QGLWidget 之前调用 QGLFormat::openGLVersionFlags() 显示 Qt 检测到 OpenGL 4.2 和所有以前的桌面版本。

这是另一个最小的例子:

#include <QApplication>
#include <QGLWidget>
#include <QDebug>
#include <QtDeclarative/qdeclarativeview.h>

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

    qDebug() << "OpenGL Versions Supported: " << QGLFormat::openGLVersionFlags();

    QGLFormat qglFormat;
    qglFormat.setVersion(4,2);  // get expected output with (3,1) and below, else blank window
    qglFormat.setProfile(QGLFormat::CoreProfile);
    qglFormat.setSampleBuffers(true);

    QGLWidget* qglWidget = new QGLWidget(qglFormat);

    QString versionString(QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
    qDebug() << "Driver Version String:" << versionString;
    qDebug() << "Current Context:" << qglWidget->format();

    QDeclarativeView mainView;
    mainView.setViewport(qglWidget);
    mainView.setSource(QString("helloworld.qml"));
    mainView.show();

    return app.exec();
}

这是输出:

OpenGL Versions Supported:  QFlags(0x1|0x2|0x4|0x8|0x10|0x20|0x40|0x1000|0x2000|0x4000|0x8000|0x10000) 
Driver Version String: "4.2.0 NVIDIA 295.53" 
Current Context: QGLFormat(options QFlags(0x1|0x2|0x4|0x10|0x20|0x80|0x200|0x400) , plane  0 , depthBufferSize  24 , accumBufferSize  16 , stencilBufferSize  8 , redBufferSize  8 , greenBufferSize  8 , blueBufferSize  8 , alphaBufferSize  -1 , samples  4 , swapInterval  0 , majorVersion  4 , minorVersion  2 , profile  1 )  

第一行的 QFlags() 枚举列表描述了支持的 OpenGL 版本。该列表显示我支持除 OpenGL/ES 版本之外的所有变体。第三行的 QFlags() 描述了格式选项(alpha channel 、模板缓冲区等)。

有谁知道为什么 QGLWidget 不能与 >= 3.1 一起使用?我在 Linux 上,有 Nvidia GT440,glxinfo 显示它支持 OpenGL 4.2.0。驱动程序版本打印在上面的示例输出中。我不太确定还能尝试什么。

编辑:在这次编辑之前,我对问题的解释犯了一些非常严重的错误/假设。这个问题仍然很相似,但希望现在更有意义。抱歉造成任何混淆。

最佳答案

您应该将 OpenGL 查询移到 mainView.show(); 之后。在 show() 之前,OpenGL 上下文尚未初始化。

关于c++ - 无法在 QGLWidget 中设置所需的 OpenGL 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11000014/

相关文章:

c++ - 如何解析这些未知大小的数据

c++ - 是否存在使用 "__attribute__((warn_unused_result))"不起作用的条件?

c++ - 结构破坏中的奇怪行为

qt - qmake 如何确定在 Makefile 中使用的编译器?

c++ - QT UDP 套接字返回空数据报

c++ - 设置 OpenGL - 它不会工作

c++ - 使用闭源库 boost 分布式

c++ - 来自字符串的 Qt QString - 奇怪的字母

c++ - 英特尔高清显卡-帧缓冲区不完整?

c++ - 上下文创建失败