Shader now passed by shared pointer.

This commit is contained in:
David Williams 2014-05-25 23:40:55 +02:00
parent b97bf52214
commit 9e835a1110
3 changed files with 23 additions and 21 deletions

View File

@ -88,7 +88,7 @@ int main(int argc, char *argv[])
openGLWidget.show(); openGLWidget.show();
QGLShaderProgram* shader = new QGLShaderProgram; QSharedPointer<QGLShaderProgram> shader(new QGLShaderProgram);
if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"(
#version 140 #version 140

View File

@ -21,6 +21,11 @@ void OpenGLWidget::setViewableRegion(Region viewableRegion)
setupWorldToCameraMatrix(); setupWorldToCameraMatrix();
} }
void OpenGLWidget::setShader(QSharedPointer<QGLShaderProgram> shader)
{
mShader = shader;
}
void OpenGLWidget::initializeGL() void OpenGLWidget::initializeGL()
{ {
GLenum err = glewInit(); GLenum err = glewInit();
@ -50,9 +55,9 @@ void OpenGLWidget::initializeGL()
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
glDepthRange(0.0, 1.0); glDepthRange(0.0, 1.0);
shader = new QGLShaderProgram; mShader = QSharedPointer<QGLShaderProgram>(new QGLShaderProgram);
if (!shader->addShaderFromSourceCode(QGLShader::Vertex, R"( if (!mShader->addShaderFromSourceCode(QGLShader::Vertex, R"(
#version 140 #version 140
in vec4 position; // This will be the position of the vertex in model-space 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); exit(EXIT_FAILURE);
} }
if (!shader->addShaderFromSourceCode(QGLShader::Fragment, R"( if (!mShader->addShaderFromSourceCode(QGLShader::Fragment, R"(
#version 130 #version 130
in vec4 worldPosition; //Passed in from the vertex shader 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); exit(EXIT_FAILURE);
} }
shader->bindAttributeLocation("position", 0); mShader->bindAttributeLocation("position", 0);
shader->bindAttributeLocation("normal", 1); mShader->bindAttributeLocation("normal", 1);
shader->bindAttributeLocation("material", 2); 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); exit(EXIT_FAILURE);
} }
@ -127,17 +132,17 @@ void OpenGLWidget::paintGL()
//Clear the screen //Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->bind(); mShader->bind();
shader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix); mShader->setUniformValue("worldToCameraMatrix", worldToCameraMatrix);
shader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix); mShader->setUniformValue("cameraToClipMatrix", cameraToClipMatrix);
for (OpenGLMeshData meshData : mMeshData) for (OpenGLMeshData meshData : mMeshData)
{ {
QMatrix4x4 modelToWorldMatrix{}; QMatrix4x4 modelToWorldMatrix{};
modelToWorldMatrix.translate(meshData.translation); modelToWorldMatrix.translate(meshData.translation);
modelToWorldMatrix.scale(meshData.scale); modelToWorldMatrix.scale(meshData.scale);
shader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix); mShader->setUniformValue("modelToWorldMatrix", modelToWorldMatrix);
glBindVertexArray(meshData.vertexArrayObject); glBindVertexArray(meshData.vertexArrayObject);
@ -146,7 +151,7 @@ void OpenGLWidget::paintGL()
glBindVertexArray(0); glBindVertexArray(0);
} }
shader->release(); mShader->release();
GLenum errCode = glGetError(); GLenum errCode = glGetError();
if(errCode != GL_NO_ERROR) if(errCode != GL_NO_ERROR)

View File

@ -61,10 +61,7 @@ public:
// For our purposes we use a single shader for the whole volume, and // 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 // this example framework is only meant to show a single volume at a time
void setShader(QGLShaderProgram* shader) void setShader(QSharedPointer<QGLShaderProgram> shader);
{
this->shader = shader;
}
// Convert a SurfaceMesh to OpenGL index/vertex buffers. Inlined because it's templatised. // Convert a SurfaceMesh to OpenGL index/vertex buffers. Inlined because it's templatised.
template <typename MeshType> template <typename MeshType>
@ -127,7 +124,7 @@ private:
// Index/vertex buffer data // Index/vertex buffer data
std::vector<OpenGLMeshData> mMeshData; std::vector<OpenGLMeshData> mMeshData;
QGLShaderProgram* shader; QSharedPointer<QGLShaderProgram> mShader;
// Matrices // Matrices
QMatrix4x4 worldToCameraMatrix; QMatrix4x4 worldToCameraMatrix;