diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 586b5197..6ef88601 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) openGLWidget.show(); - QGLShaderProgram* shader = new QGLShaderProgram; + QSharedPointer shader(new QGLShaderProgram); if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( #version 140 diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.cpp index 380c6fe0..15f16852 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.cpp @@ -21,6 +21,11 @@ void OpenGLWidget::setViewableRegion(Region viewableRegion) setupWorldToCameraMatrix(); } +void OpenGLWidget::setShader(QSharedPointer shader) +{ + mShader = shader; +} + void OpenGLWidget::initializeGL() { GLenum err = glewInit(); @@ -50,9 +55,9 @@ void OpenGLWidget::initializeGL() glDepthFunc(GL_LEQUAL); glDepthRange(0.0, 1.0); - shader = new QGLShaderProgram; + mShader = QSharedPointer(new QGLShaderProgram); - if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( + if (!mShader->addShaderFromSourceCode(QGLShader::Vertex, R"( #version 140 in vec4 position; // This will be the position of the vertex in model-space @@ -71,11 +76,11 @@ void OpenGLWidget::initializeGL() } )")) { - std::cerr << shader->log().toStdString() << std::endl; + std::cerr << mShader->log().toStdString() << std::endl; exit(EXIT_FAILURE); } - if (!shader->addShaderFromSourceCode(QGLShader::Fragment, R"( + if (!mShader->addShaderFromSourceCode(QGLShader::Fragment, R"( #version 130 in vec4 worldPosition; //Passed in from the vertex shader @@ -92,17 +97,17 @@ void OpenGLWidget::initializeGL() } )")) { - std::cerr << shader->log().toStdString() << std::endl; + std::cerr << mShader->log().toStdString() << std::endl; exit(EXIT_FAILURE); } - shader->bindAttributeLocation("position", 0); - shader->bindAttributeLocation("normal", 1); - shader->bindAttributeLocation("material", 2); + mShader->bindAttributeLocation("position", 0); + mShader->bindAttributeLocation("normal", 1); + mShader->bindAttributeLocation("material", 2); - if (!shader->link()) + if (!mShader->link()) { - std::cerr << shader->log().toStdString() << std::endl; + std::cerr << mShader->log().toStdString() << std::endl; exit(EXIT_FAILURE); } @@ -127,17 +132,17 @@ void OpenGLWidget::paintGL() //Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader->bind(); + mShader->bind(); - shader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix); - shader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix); + mShader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix); + mShader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix); for (OpenGLMeshData meshData : mMeshData) { QMatrix4x4 modelToWorldMatrix{}; modelToWorldMatrix.translate(meshData.translation); modelToWorldMatrix.scale(meshData.scale); - shader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix); + mShader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix); glBindVertexArray(meshData.vertexArrayObject); @@ -146,7 +151,7 @@ void OpenGLWidget::paintGL() glBindVertexArray(0); } - shader->release(); + mShader->release(); GLenum errCode = glGetError(); if(errCode != GL_NO_ERROR) diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index 755563fa..a56ed7f1 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -61,10 +61,7 @@ public: // For our purposes we use a single shader for the whole volume, and // this example framework is only meant to show a single volume at a time - void setShader(QGLShaderProgram* shader) - { - this->shader = shader; - } + void setShader(QSharedPointer shader); // Convert a SurfaceMesh to OpenGL index/vertex buffers. Inlined because it's templatised. template @@ -127,7 +124,7 @@ private: // Index/vertex buffer data std::vector mMeshData; - QGLShaderProgram* shader; + QSharedPointer mShader; // Matrices QMatrix4x4 worldToCameraMatrix;