【问题标题】:Cannot get OpenGL 3.3 functions with QGLWidget (Linux)无法使用 QGLWidget (Linux) 获取 OpenGL 3.3 功能
【发布时间】:2015-03-20 15:02:01
【问题描述】:

我遇到了一个非常奇怪的问题,所以我写了一个最小的例子来重现它。

首先我从QGLWidget派生一个类:

class Demo : public QGLWidget
{
public:
    struct error {};

    explicit Demo( const QGLFormat& format ) : QGLWidget( format )
    {
    }

protected:
    virtual void initializeGL() override
    {
        const GLenum glewState = glewInit();
        if( glewState != GLEW_OK ) throw error();
        unsigned int id;
        glGenSamplers( 1, &id ); //<-- Note this here!
    }
};

这里是main 函数:

int main( int argc, char** argv )
{
    QApplication app( argc, argv );
    QGLFormat format = QGLFormat::defaultFormat();
    format.setVersion( 3, 3 );
    Demo demo( format );
    demo.show();
    return QApplication::exec();
}

我使用 G++ 4.9 构建此代码。当我使用 GDB 运行它时,我得到了以下结果:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000000402add in Demo::initializeGL() () at test.cpp:16
...

显然函数glGenSamplers 指向0。我还添加了另一行

if( glGenSamplers == nullptr ) throw error();

验证这一点,是的,这里出现了错误。但为什么?我错过了什么?

【问题讨论】:

    标签: linux qt opengl glew


    【解决方案1】:

    解决方案是强制QGLWidget 兼容配置文件:

    format.setProfile( QGLFormat::Compatibility );
    

    虽然我不明白为什么glGenSamplers 需要这个。据我了解,这个函数实际上应该是自 3.3 以来核心配置文件的一部分。

    【讨论】:

    • 这不是真正的问题。您遇到了一个众所周知的 GLEW 错误(维护人员多年来一直拒绝修复该错误)。 GLEW 在核心配置文件中无法正常工作。您可以在glewInit() 之前设置glewExperimental=GL_TRUE; 作为解决方法。但是,这还有一些其他问题。最后,最好放弃 GLEW 并使用其他扩展加载器,例如 glLoadGen 或glad。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多