gpt4 book ai didi

linux - 无法使用 QGLWidget 获取 OpenGL 3.3 函数 (Linux)

转载 作者:太空宇宙 更新时间:2023-11-04 03:42:32 24 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题,因此我编写了一个最小的示例来重现它。

首先,我从 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();

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

最佳答案

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

format.setProfile( QGLFormat::Compatibility );

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

关于linux - 无法使用 QGLWidget 获取 OpenGL 3.3 函数 (Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29169625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com