我最近做了我的第一个OpenGL程序渲染一个立方体,我扩展到包括其他一些基本功能(旋转,缩放,潘,鼠标选择的顶点)。但是,我认为该程序使用的是某些版本的OpenGL esx.x,因为我不能使用glReadPixels和gl_depth_component一起启用鼠标选择仅可见的顶点。
为了解决这个问题,我修改了我的旧程序,在我的主函数中手动将默认的表面格式指定为OpenGL Version3.0,但是现在该程序在尝试创建GLWidget实例时抛出以下错误。
ASSERT:"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);
}一旦我继续通过这个断点,断言错误就会被抛出。调试它把我带到了这个地方。
上面的代码..。
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版本执行一些OpenGL ES版本不需要的初始化?
我还是OpenGL的新手,所以任何帮助都会很感激!
更新我不知道发生了什么,但是在将更新的代码与旧的工作代码合并之后,一切都很好。希望这不会发生在其他人身上!
发布于 2016-05-28 23:39:21
您需要将initializeOpenGLFunctions();添加到GLWidget::initGL()函数中。
https://stackoverflow.com/questions/36234962
复制相似问题