我最近制作了我的第一个 OpenGL 程序来渲染一个立方体,我扩展了它以包括一些其他基本功能(旋转、缩放、平移、鼠标选择顶点)。但是,我认为该程序正在使用某些版本的 OpenGL ES x.x,因为我无法将 glReadPixels 与 gl_depth_component 结合使用来启用仅可见顶点的鼠标选择。
为了解决这个问题,我修改了我的旧程序,在我的主函数中手动将默认表面格式指定为 OpenGL 3.0 版,但现在该程序在尝试创建 GLWidget 实例时抛出以下错误。
断言:“QOpenGLFunctions::isInitialized(d_ptr)”
我的主要功能(main.cpp):
#include "mainwindow.h"
#include <QApplication>
#include <QOpenGLFunctions>
int main(int argc, char *argv[])
{
QSurfaceFormat format;
format.setSamples(16);
format.setDepthBufferSize(24);
format.setVersion(3,0);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat( format);
QApplication a(argc, argv);
MainWindow w; //breakpoint here - step into
w.show();
return a.exec();
}
进入这个断点会引导我进入我的 MainWindow 构造函数 (mainwindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); //breakpoint here - step into
ui->GLwidget2->getmainwindowpointer(this);
connect(ui->actionTop,SIGNAL(triggered()),ui->GLwidget2,SLOT(settopview()));
connect(ui->actionRight, SIGNAL(triggered()), ui->GLwidget2, SLOT(setrightview()));
connect(ui->actionIso,SIGNAL(triggered()),ui->GLwidget2,SLOT(setisoview()));
//connect(ui->checkBox_perspective,SIGNAL(released()),ui->GLwidget2,SLOT(setperspective()));
connect(ui->checkBox_legend, SIGNAL(released()), ui->GLwidget2, SLOT(setlegend()));
connect(ui->checkBox_edges, SIGNAL(released()), ui->GLwidget2, SLOT(setedges()));
connect(ui->checkBox_faces, SIGNAL(released()), ui->GLwidget2, SLOT(setfaces()));
connect(ui->actionFind_vertex,SIGNAL(triggered(bool)),ui->GLwidget2,SLOT(startvertexsearch()));
connect(ui->actionFit_to_screen,SIGNAL(triggered()),ui->GLwidget2,SLOT(fittoscreen()));
connect(ui->pushButton_selectMode,SIGNAL(released()),ui->GLwidget2,SLOT(setSelectMode()));
connect(ui->pushButton_cancelSelect,SIGNAL(released()),ui->GLwidget2,SLOT(setCancelSelectMode()));
}
步入此断点会引导我进入 ui_mainwindow.h,它会在我的主窗口中创建对象并到达此行:
[上面的代码...]
pushButton = new QPushButton(centralWidget);
pushButton->setObjectName(QStringLiteral("pushButton"));
verticalLayout_2->addWidget(pushButton);
horizontalLayout->addLayout(verticalLayout_2);
GLwidget2 = new GLWidget(centralWidget); /break point here - step into
[下面的代码...]
这一切都运行良好,直到我的 GLWidget 的构造函数被调用... (glwidget.cpp)
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent = 0) :
QOpenGLWidget(parent)
{ //breakpoint here
alpha = 0;
beta = 0;
distance = defaultdistance;
QSurfaceFormat format;
format.setSamples(16);
format.setDepthBufferSize(24);
format.setVersion(3,0);
format.setProfile(QSurfaceFormat::CoreProfile);
this->setFormat(format);
}
只要我继续越过这个断点,就会抛出 ASSERT 错误。调试它会把我带到 qopenglfunctions.h 中的这个地方
[上面的代码...]
inline GLuint QOpenGLFunctions::glCreateProgram()
{
#ifdef QT_OPENGL_ES_2
GLuint result = ::glCreateProgram();
#else
Q_ASSERT(QOpenGLFunctions::isInitialized(d_ptr)); //this line
GLuint result = d_ptr->CreateProgram();
#endif
Q_OPENGL_FUNCTIONS_DEBUG
return result;
}
[下面的代码...]
此时d_ptr的值为NULL,我猜测是错误原因。我是否需要对 OpenGL ES 版本不需要的桌面 OpenGL 版本执行一些初始化?
我对 OpenGL 还是个新手,所以任何帮助都将不胜感激!
更新 我不确定发生了什么,但是在将更新的代码与旧的工作代码合并后一切正常。希望这不会发生在其他人身上!
最佳答案
您需要添加 initializeOpenGLFunctions();
到您的 GLWidget::initGL()
函数中。
关于c++ - 带有 QT 错误 : ASSERT :"QOpenGLFunctions::isInitialized(d_ptr)". 的 OpenGL 无法创建 OpenGL 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234962/