Qt InitializePenglFunctions使应用程序崩溃
所以我创建了测试应用程序来开发一些OpenGL Qt知识 这是我到目前为止的完整代码 Mi main.qmlQt InitializePenglFunctions使应用程序崩溃,qt,opengl,Qt,Opengl,所以我创建了测试应用程序来开发一些OpenGL Qt知识 这是我到目前为止的完整代码 Mi main.qml import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.0 Window { visible: true width: 1000 height: 600 title: qsTr("Qt-QML-VR Tester") Column{ wi
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
Window {
visible: true
width: 1000
height: 600
title: qsTr("Qt-QML-VR Tester")
Column{
width: 100
spacing: 10
anchors.centerIn: parent
Button {
id: startTest
width: parent.width
height: 50
text: "Start Test"
onClicked: {
control.startTest();
}
}
Button {
id: stopTest
width: parent.width
height: 50
text: "Stop Test"
onClicked: {
control.stopTest();
}
}
}
}
Mi main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "control.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
Control control(nullptr);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.rootContext()->setContextProperty("control",&control);
engine.load(url);
return app.exec();
}
#包括
#包括
#包括
#包括“control.h”
int main(int argc,char*argv[])
{
QCoreApplication::setAttribute(Qt::AA_enableHighdDiscaling);
QApplication应用程序(argc、argv);
控制(nullptr);
qqmlaplicationengine;
const-QUrl-url(QStringLiteral(“qrc:/main.qml”);
QObject::connect(&engine),&QQmlApplicationEngine::objectCreated,
&应用程序,[url](QObject*obj,const-quorl和objUrl){
如果(!obj&&url==objUrl)
QCoreApplication::退出(-1);
},Qt::QueuedConnection);
engine.rootContext()->setContextProperty(“控件”、&control);
引擎加载(url);
返回app.exec();
}
我的控制
#ifndef CONTROL_H
#define CONTROL_H
#include <QObject>
#include <QDebug>
#include <QGuiApplication>
#include <QScreen>
#include <QTimer>
#include <QOpenGLFunctions>
#include <QMatrix4x4>
#include <QOpenGLShaderProgram>
#include "targettest.h"
class Control : public QObject, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit Control(QObject *parent = nullptr);
~Control();
Q_INVOKABLE void startTest();
Q_INVOKABLE void stopTest();
private:
// Open GL Related variables.
QOpenGLContext *openGLContext;
//QOffscreenSurface *offscreenSurface;
QOpenGLShaderProgram *shaderProgram;
GLuint glid_PosAttr;
GLuint glid_PerpectiveMatrix;
bool initializeOpenGL();
// Other variables and functions.
bool systemInitialized;
bool doIntialization();
};
\ifndef控制
#定义控制
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“targettest.h”
类控制:公共QObject、受保护QopenglFunction
{
Q_对象
公众:
显式控制(QObject*parent=nullptr);
~Control();
Q_可调用的void startTest();
Q_可调用的void stopTest();
私人:
//打开总账相关变量。
QOpenGLContext*openGLContext;
//QoffScreensSurface*屏幕外表面;
QOpenGLShaderProgram*着色器程序;
胶合胶;
GLuint glid_透视矩阵;
bool初始化为engl();
//其他变量和函数。
bool系统初始化;
bool-doitialization();
};
我的控件.cpp
#include "control.h"
const char * Control::vertexShaderSource =
"attribute highp vec4 posAttr;\n"
"attribute highp vec4 colAttr;\n"
"varying highp vec2 texCoord;\n"
"uniform highp mat4 matrix;\n"
"void main() {\n"
" texCoord = posAttr.xy;\n"
" gl_Position = matrix * posAttr;\n"
"}\n";
const char * Control::fragmentShaderSource =
"varying highp vec2 texCoord;\n"
"uniform sampler2D ourTexture;\n"
"void main() {\n"
// " gl_FragColor = texture2D(ourTexture, texCoord);\n"
// " gl_FragColor = vec4(1.0,1.0,1.0,1.0);\n"
" gl_FragColor = vec4(texCoord.x,texCoord.y,0.0,1.0);\n"
"}\n";
Control::Control(QObject *parent) : QObject(parent)
{
systemInitialized = false;
}
/////////////////////////////// INTIALIZATION FUNCTIONS
bool Control::initializeOpenGL(){
QSurfaceFormat format;
format.setMajorVersion( 4 );
format.setMinorVersion( 1 );
format.setProfile( QSurfaceFormat::CompatibilityProfile );
openGLContext = new QOpenGLContext();
openGLContext->setFormat( format );
if( !openGLContext->create() ){
qDebug() << "Open GL Context initialization failed";
return false;
}
qDebug() << "Before";
initializeOpenGLFunctions();
qDebug() << "After";
shaderProgram = new QOpenGLShaderProgram(this);
if (!shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource)){
qDebug() << "ERROR compiling vertex shader";
qDebug() << shaderProgram->log();
return false;
}
if (!shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource)){
qDebug() << "ERROR compiling fragment shader";
qDebug() << shaderProgram->log();
return false;
}
shaderProgram->link();
glid_PosAttr = static_cast<GLuint>(shaderProgram->attributeLocation("posAttr"));
glid_PerpectiveMatrix = static_cast<GLuint>(shaderProgram->uniformLocation("matrix"));
return true;
}
bool Control::doIntialization(){
if (!initializeOpenGL()) return false;
qDebug() << "Open GL Initialized";
return true;
}
////////////////////////////////////////// CONTROL FUNCTIONS
void Control::startTest(){
if (!systemInitialized){
if (!doIntialization()){
qDebug() << "INITIALIZATION FAILED";
return;
}
systemInitialized = true;
}
timer.start(10);
qDebug() << "Started rendering";
}
void Control::stopTest(){
timer.stop();
qDebug() << "Stopped rendering";
}
Control::~Control(){
}
#包括“control.h”
常量字符*控件::vertexShaderSource=
“属性highp vec4 posAttr;\n”
“属性highp vec4 colAttr;\n”
“可变高值vec2 texCoord;\n”
“一致高p mat4矩阵;\n”
“void main(){\n”
“texCoord=posAttr.xy;\n”
“gl_位置=矩阵*posAttr;\n”
“}\n”;
常量字符*控件::fragmentShaderSource=
“可变高值vec2 texCoord;\n”
“均匀纹理;\n”
“void main(){\n”
//gl_FragColor=texture2D(我们的纹理,texCoord);\n
//“gl_FragColor=vec4(1.0,1.0,1.0,1.0);\n”
“gl_FragColor=vec4(texCoord.x,texCoord.y,0.0,1.0);\n”
“}\n”;
控件::控件(QObject*父对象):QObject(父对象)
{
systemInitialized=false;
}
///////////////////////////////初始化函数
bool控件::初始化engl(){
QSurfaceFormat格式;
格式。setMajorVersion(4);
格式.setMinorVersion(1);
setProfile(QSurfaceFormat::CompatibilityProfile);
openGLContext=new QOpenGLContext();
openGLContext->setFormat(格式);
如果(!openGLContext->create()){
qDebug()统一位置(“矩阵”);
返回true;
}
bool控件::doIntialization(){
如果(!initializePengl())返回false;
qDebug()在调用QOpenGLFunctions::InitializePenglFunctions()
的点上,有效的OpenGL上下文必须处于适当位置/当前位置,但显示的代码仅创建上下文--它不会调用
尝试在调用InitializePenglFunctions
之前添加以下内容
auto *surface = new QOffscreenSurface;
surface->setFormat(format);
surface->create();
openGLContext->makeCurrent(surface);
(如果它能工作,那么很明显,您可能希望使surface
成为Control
的私有成员)您得到了什么错误?我没有得到任何错误。应用程序崩溃