When executing the glDrawArrays () function in Rubeck.cpp, an exception occurs in 9 out of 10 casesException thrown at 0x00000000535D7C20 (nvoglv64.dll) in Rubeck.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
In 1 out of 10 cases a beautiful triangle is drawn.
Rubeck.cpp
#include "Rubeck.h" #include <QDebug> #include <QOpenGLContext> Rubeck::Rubeck(QWidget *parent) : QGLWidget(parent) { ui.setupUi(this); width = 800; height = 640; depth = 1; this->setFixedSize(QSize(width, height)); this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); this->move(100, 50); timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateGL())); } void Rubeck::initializeGL() { qglClearColor(Qt::white); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_VERTEX_ARRAY); VAO = new QOpenGLVertexArrayObject(this); f = new QOpenGLFunctions; f->initializeOpenGLFunctions(); VBO = new QOpenGLBuffer(QOpenGLBuffer::Type::VertexBuffer); qDebug() << VBO->create() << " create VBO"; VBO->setUsagePattern(QOpenGLBuffer::UsagePattern::StreamDraw); vShad = new QOpenGLShader(QOpenGLShader::Vertex); qDebug() << vShad->compileSourceFile(":/Rubeck/Resources/vp1.vert") << " vShad"; fShad = new QOpenGLShader(QOpenGLShader::Fragment); qDebug() << fShad->compileSourceFile(":/Rubeck/Resources/fp0.frag") << " fShad"; shaderProgram = new QOpenGLShaderProgram(this); shaderProgram->create(); qDebug() << shaderProgram->addShader(vShad) << " add vShad"; qDebug() << shaderProgram->addShader(fShad) << " add fShad"; qDebug() << shaderProgram->link() << " link"; GLfloat vertices[] = { -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f }; GLfloat vertices1[9]; qDebug() << shaderProgram->bind() << " shPr bind"; VAO->bind(); qDebug() << VBO->bind() << " VBO bind"; VBO->allocate(vertices, sizeof(GLfloat) * 9); qDebug() << VBO->read(0, vertices1, sizeof(GLfloat) * 9) << " VBO read"; shaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(GLfloat) * 3); shaderProgram->enableAttributeArray(0); VAO->release(); } void Rubeck::resizeGL(int aW, int aH) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width, 0, height, depth, 0); glViewport(0, 0, (GLint)aW, (GLint)aH); timer->start(); } void Rubeck::paintGL() { timer->stop(); qglClearColor(Qt::white); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw(); timer->start(); } void Rubeck::draw() { shaderProgram->bind(); VAO->bind(); f->glDrawArrays(GL_TRIANGLES, 0, 3); VAO->release(); } Rubeck.h
#pragma once #include <QtWidgets/QWidget> #include "qgl.h" #include "ui_Rubeck.h" #include <QTimer> #include "QOpenGLBuffer" #include <QOpenGLShader> #include <QOpenGLVertexArrayObject> #include <QOpenGLFunctions> class Rubeck : public QGLWidget { Q_OBJECT public: Rubeck(QWidget *parent = Q_NULLPTR); unsigned width; unsigned height; unsigned depth; private: Ui::RubeckClass ui; QTimer * timer; QOpenGLBuffer *VBO; QOpenGLShader * vShad; QOpenGLShader * fShad; QOpenGLShaderProgram * shaderProgram; QOpenGLVertexArrayObject * VAO; QOpenGLFunctions * f; void initializeGL() override; void resizeGL(int aW, int aH) override; void paintGL() override; void draw(); }; main.cpp
#include "Rubeck.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Q_INIT_RESOURCE(Rubeck); Rubeck w; w.show(); return a.exec(); } vp1.vert
#version 330 core layout (location = 0) in vec3 position; void main() { gl_Position = vec4(position.x, position.y, position.z, 1.0); } fp0.frag
#version 330 core out vec4 color; void main() { color = vec4(1.0f, 0.5f, 0.2f, 1.0f); } Debug in all cases gives "true"
Drivers updated.
What can be caused and why does the program sometimes work?