所以我创建了测试应用程序来开发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{
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();
}我的控制
#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();
};我的control.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(){
}但是,当到达intializeOpenGLFunctions时,程序会崩溃。我做错了什么?
发布于 2019-12-15 10:38:09
在QOpenGLFunctions::initializeOpenGLFunctions()被调用的时候,一个有效的OpenGL上下文必须到位/当前,但是所显示的代码只创建上下文--它不调用QOpenGLContext::makeCurrent。
尝试在调用initializeOpenGLFunctions之前添加以下内容.
auto *surface = new QOffscreenSurface;
surface->setFormat(format);
surface->create();
openGLContext->makeCurrent(surface);(如果它有效,那么很明显,您可能希望使surface成为Control的私有成员。)
https://stackoverflow.com/questions/59342850
复制相似问题