/**************************************************************************** ** ** Copyright (C) 2014 Digia Plc ** All rights reserved. ** For any questions to Digia, please use contact form at http://qt.digia.com ** ** This file is part of the QtDataVisualization module. ** ** Licensees holding valid Qt Enterprise licenses may use this file in ** accordance with the Qt Enterprise License Agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. ** ** If you have questions regarding the use of this file, please use ** contact form at http://qt.digia.com ** ****************************************************************************/ #include "shaderhelper_p.h" #include QT_BEGIN_NAMESPACE_DATAVISUALIZATION void discardDebugMsgs(QtMsgType type, const QMessageLogContext &context, const QString &msg) { Q_UNUSED(type) Q_UNUSED(context) Q_UNUSED(msg) // Used to discard warnings generated during shader test compilation } ShaderHelper::ShaderHelper(QObject *parent, const QString &vertexShader, const QString &fragmentShader, const QString &texture, const QString &depthTexture) : m_caller(parent), m_program(0), m_vertexShaderFile(vertexShader), m_fragmentShaderFile(fragmentShader), m_textureFile(texture), m_depthTextureFile(depthTexture) { } ShaderHelper::~ShaderHelper() { delete m_program; } void ShaderHelper::setShaders(const QString &vertexShader, const QString &fragmentShader) { m_vertexShaderFile = vertexShader; m_fragmentShaderFile = fragmentShader; } void ShaderHelper::setTextures(const QString &texture, const QString &depthTexture) { m_textureFile = texture; m_depthTextureFile = depthTexture; } void ShaderHelper::initialize() { if (m_program) delete m_program; m_program = new QOpenGLShaderProgram(m_caller); if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, m_vertexShaderFile)) qFatal("Compiling Vertex shader failed"); if (!m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, m_fragmentShaderFile)) qFatal("Compiling Fragment shader failed"); m_program->link(); m_positionAttr = m_program->attributeLocation("vertexPosition_mdl"); m_normalAttr = m_program->attributeLocation("vertexNormal_mdl"); m_uvAttr = m_program->attributeLocation("vertexUV"); m_mvpMatrixUniform = m_program->uniformLocation("MVP"); m_viewMatrixUniform = m_program->uniformLocation("V"); m_modelMatrixUniform = m_program->uniformLocation("M"); m_invTransModelMatrixUniform = m_program->uniformLocation("itM"); m_depthMatrixUniform = m_program->uniformLocation("depthMVP"); m_lightPositionUniform = m_program->uniformLocation("lightPosition_wrld"); m_lightStrengthUniform = m_program->uniformLocation("lightStrength"); m_ambientStrengthUniform = m_program->uniformLocation("ambientStrength"); m_shadowQualityUniform = m_program->uniformLocation("shadowQuality"); m_colorUniform = m_program->uniformLocation("color_mdl"); m_textureUniform = m_program->uniformLocation("textureSampler"); m_shadowUniform = m_program->uniformLocation("shadowMap"); m_gradientMinUniform = m_program->uniformLocation("gradMin"); m_gradientHeightUniform = m_program->uniformLocation("gradHeight"); m_lightColorUniform = m_program->uniformLocation("lightColor"); m_volumeSliceIndicesUniform = m_program->uniformLocation("volumeSliceIndices"); m_colorIndexUniform = m_program->uniformLocation("colorIndex"); m_cameraPositionRelativeToModelUniform = m_program->uniformLocation("cameraPositionRelativeToModel"); m_color8BitUniform = m_program->uniformLocation("color8Bit"); m_textureDimensionsUniform = m_program->uniformLocation("textureDimensions"); m_sampleCountUniform = m_program->uniformLocation("sampleCount"); m_alphaMultiplierUniform = m_program->uniformLocation("alphaMultiplier"); m_preserveOpacityUniform = m_program->uniformLocation("preserveOpacity"); m_minBoundsUniform = m_program->uniformLocation("minBounds"); m_maxBoundsUniform = m_program->uniformLocation("maxBounds"); m_sliceFrameWidthUniform = m_program->uniformLocation("sliceFrameWidth"); m_initialized = true; } bool ShaderHelper::testCompile() { bool result = true; // Discard warnings, we only need the result QtMessageHandler handler = qInstallMessageHandler(discardDebugMsgs); if (m_program) delete m_program; m_program = new QOpenGLShaderProgram(); if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, m_vertexShaderFile)) result = false; if (!m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, m_fragmentShaderFile)) result = false; // Restore actual message handler qInstallMessageHandler(handler); return result; } void ShaderHelper::bind() { m_program->bind(); } void ShaderHelper::release() { m_program->release(); } void ShaderHelper::setUniformValue(GLuint uniform, const QVector2D &value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValue(GLuint uniform, const QVector3D &value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValue(GLuint uniform, const QVector4D &value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValue(GLuint uniform, const QMatrix4x4 &value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValue(GLuint uniform, GLfloat value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValue(GLuint uniform, GLint value) { m_program->setUniformValue(uniform, value); } void ShaderHelper::setUniformValueArray(GLuint uniform, const QVector4D *values, int count) { m_program->setUniformValueArray(uniform, values, count); } GLuint ShaderHelper::MVP() { if (!m_initialized) qFatal("Shader not initialized"); return m_mvpMatrixUniform; } GLuint ShaderHelper::view() { if (!m_initialized) qFatal("Shader not initialized"); return m_viewMatrixUniform; } GLuint ShaderHelper::model() { if (!m_initialized) qFatal("Shader not initialized"); return m_modelMatrixUniform; } GLuint ShaderHelper::nModel() { if (!m_initialized) qFatal("Shader not initialized"); return m_invTransModelMatrixUniform; } GLuint ShaderHelper::depth() { if (!m_initialized) qFatal("Shader not initialized"); return m_depthMatrixUniform; } GLuint ShaderHelper::lightP() { if (!m_initialized) qFatal("Shader not initialized"); return m_lightPositionUniform; } GLuint ShaderHelper::lightS() { if (!m_initialized) qFatal("Shader not initialized"); return m_lightStrengthUniform; } GLuint ShaderHelper::ambientS() { if (!m_initialized) qFatal("Shader not initialized"); return m_ambientStrengthUniform; } GLuint ShaderHelper::shadowQ() { if (!m_initialized) qFatal("Shader not initialized"); return m_shadowQualityUniform; } GLuint ShaderHelper::color() { if (!m_initialized) qFatal("Shader not initialized"); return m_colorUniform; } GLuint ShaderHelper::texture() { if (!m_initialized) qFatal("Shader not initialized"); return m_textureUniform; } GLuint ShaderHelper::shadow() { if (!m_initialized) qFatal("Shader not initialized"); return m_shadowUniform; } GLuint ShaderHelper::gradientMin() { if (!m_initialized) qFatal("Shader not initialized"); return m_gradientMinUniform; } GLuint ShaderHelper::gradientHeight() { if (!m_initialized) qFatal("Shader not initialized"); return m_gradientHeightUniform; } GLuint ShaderHelper::lightColor() { if (!m_initialized) qFatal("Shader not initialized"); return m_lightColorUniform; } GLuint ShaderHelper::volumeSliceIndices() { if (!m_initialized) qFatal("Shader not initialized"); return m_volumeSliceIndicesUniform; } GLuint ShaderHelper::colorIndex() { if (!m_initialized) qFatal("Shader not initialized"); return m_colorIndexUniform; } GLuint ShaderHelper::cameraPositionRelativeToModel() { if (!m_initialized) qFatal("Shader not initialized"); return m_cameraPositionRelativeToModelUniform; } GLuint ShaderHelper::color8Bit() { if (!m_initialized) qFatal("Shader not initialized"); return m_color8BitUniform; } GLuint ShaderHelper::textureDimensions() { if (!m_initialized) qFatal("Shader not initialized"); return m_textureDimensionsUniform; } GLuint ShaderHelper::sampleCount() { if (!m_initialized) qFatal("Shader not initialized"); return m_sampleCountUniform; } GLuint ShaderHelper::alphaMultiplier() { if (!m_initialized) qFatal("Shader not initialized"); return m_alphaMultiplierUniform; } GLuint ShaderHelper::preserveOpacity() { if (!m_initialized) qFatal("Shader not initialized"); return m_preserveOpacityUniform; } GLuint ShaderHelper::maxBounds() { if (!m_initialized) qFatal("Shader not initialized"); return m_maxBoundsUniform; } GLuint ShaderHelper::minBounds() { if (!m_initialized) qFatal("Shader not initialized"); return m_minBoundsUniform; } GLuint ShaderHelper::sliceFrameWidth() { if (!m_initialized) qFatal("Shader not initialized"); return m_sliceFrameWidthUniform; } GLuint ShaderHelper::posAtt() { if (!m_initialized) qFatal("Shader not initialized"); return m_positionAttr; } GLuint ShaderHelper::uvAtt() { if (!m_initialized) qFatal("Shader not initialized"); return m_uvAttr; } GLuint ShaderHelper::normalAtt() { if (!m_initialized) qFatal("Shader not initialized"); return m_normalAttr; } QT_END_NAMESPACE_DATAVISUALIZATION