简体   繁体   English

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

[英]Can't set desired OpenGL version in QGLWidget

I'm trying to use QGLWidget in Qt 4.8.2. 我正在尝试在Qt 4.8.2中使用QGLWidget。 I noticed that the default context QGLWidget creates doesn't show any output for OpenGL above 3.1. 我注意到QGLWidget创建的默认上下文没有显示3.1以上的OpenGL的任何输出。 The Qt wiki has a tutorial that demonstrates using OpenGL 3.3 to draw a simple triangle. Qt wiki有一个教程 ,演示如何使用OpenGL 3.3绘制一个简单的三角形。 When I try to run the tutorial, I get a blank screen. 当我尝试运行教程时,我得到一个空白屏幕。 If I change the OpenGL version to 3.1, I get the expected output (a red triangle). 如果我将OpenGL版本更改为3.1,我会得到预期的输出(红色三角形)。

My video card supports OpenGL 4.2, and calling QGLFormat::openGLVersionFlags() before creating the QGLWidget shows that Qt detects OpenGL 4.2 and all previous desktop versions. 我的视频卡支持OpenGL 4.2,在创建QGLWidget之前调用QGLFormat::openGLVersionFlags()表明Qt检测到OpenGL 4.2和所有以前的桌面版本。

Here's another minimal example: 这是另一个最小的例子:

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

Here's the output: 这是输出:

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 )  

The QFlags() enum list on the first line describes the OpenGL versions supported. 第一行的QFlags()枚举列表描述了支持的OpenGL版本。 The list shows I support all variants except for OpenGL/ES versions. 该列表显示我支持除OpenGL / ES版本之外的所有变体。 QFlags() on the third line describes format options (alpha channel, stencil buffer, etc). 第三行的QFlags()描述了格式选项(alpha通道,模板缓冲区等)。

Anyone know why QGLWidget won't work with anything >= 3.1? 任何人都知道为什么QGLWidget不适用于任何> = 3.1的东西? I'm on Linux, have an Nvidia GT440, and glxinfo shows it supports OpenGL 4.2.0. 我在Linux上,有一个Nvidia GT440,glxinfo显示它支持OpenGL 4.2.0。 The driver version is printed in the sample output above. 驱动程序版本打印在上面的示例输出中。 I'm not too sure what else to try. 我不太确定还有什么可以尝试的。

Edit: I made some pretty bad mistakes/assumptions with my explanation of the problem before this edit. 编辑:在编辑之前,我对问题的解释做了一些非常糟糕的错误/假设。 The issue is still similar, but hopefully makes a bit more sense now. 问题仍然类似,但希望现在更有意义。 Sorry for any confusion. 对不起任何困惑。

You should move the OpenGL queries down after mainView.show(); 您应该在mainView.show();之后将OpenGL查询向下移动mainView.show(); . Before show() the OpenGL context has not been initialized. show()之前,OpenGL上下文尚未初始化。

Short version: update to Qt5, it's fixed there. 简短版本:更新到Qt5,它已在那里修复。

PS And if you can use 5.4 you should probably use QtOpenGL... classes instead of QGL... ones. PS如果你可以使用5.4你应该使用QtOpenGL ...类而不是QGL ...的。

Long version: So, in case anyone ever come across a problem like this one. 长版:所以,如果有人遇到像这样的问题。

I tried to create NOT OpenGL 3.0 context with my Intel HD3000 on ubuntu 14.04 and Qt 4.8.6. 我尝试在ubuntu 14.04和Qt 4.8.6上使用我的Intel HD3000创建NOT OpenGL 3.0上下文。 I came up with this test code provided at the end of the answer. 我想出了答案末尾提供的测试代码。 Tried creating 3.1, 1.2, 2.1.. etc. contexts Core Compatible.. but always end up with context->format().majorVersion() showing the requested version, and glGetString(GL_VERSION) showing 3.0. 尝试创建3.1,1.2,2.1 ..等上下文Core Compatible ..但总是以context->format().majorVersion()显示请求的版本, glGetString(GL_VERSION)显示3.0。

After about 3 hours I noticed that by default Qt Creator uses Qt4 instead of recent Qt 5, installed on my system. 大约3个小时后,我注意到默认情况下Qt Creator使用的是Qt4而不是最近的Qt 5,安装在我的系统上。 And after I recompiled the project with Qt 5.2.1 exactly the same code started working as expected. 在用Qt 5.2.1重新编译项目之后,完全相同的代码开始按预期工作。

Hope this may help someone. 希望这可以帮助某人。

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

    MainWindow w;
    w.show();

    QGLFormat glFormat;
    glFormat.setVersion(3, 1);
    glFormat.setProfile(QGLFormat::NoProfile);
    glFormat.setSampleBuffers(true);
    glFormat.setDefaultFormat(glFormat);
    glFormat.setSwapInterval(1);
    QGLWidget widget(glFormat);
    widget.makeCurrent();

    const QGLContext *context = widget.context();

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        qWarning("Failed to initialize GLEW\n");
    }
    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);
    qDebug() << "endstuff\n";

    return a.exec();
}

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

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