From 5c25d7d4f861f6e54f8d490e293b84e4e131c4e6 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Wed, 3 Oct 2018 13:52:35 +0800 Subject: [PATCH] Move shader from source code to separated folder --- resources.qrc | 4 ++ shaders/default.core.frag | 20 +++++++ shaders/default.core.vert | 20 +++++++ shaders/default.frag | 18 +++++++ shaders/default.vert | 19 +++++++ src/modelshaderprogram.cpp | 104 ++++++++----------------------------- src/modelshaderprogram.h | 2 + 7 files changed, 105 insertions(+), 82 deletions(-) create mode 100644 shaders/default.core.frag create mode 100644 shaders/default.core.vert create mode 100644 shaders/default.frag create mode 100644 shaders/default.vert diff --git a/resources.qrc b/resources.qrc index 248b9fa0..3392db02 100644 --- a/resources.qrc +++ b/resources.qrc @@ -6,6 +6,10 @@ resources/tree-branch-more.png resources/tree-branch-open.png resources/tree-vline.png + shaders/default.vert + shaders/default.frag + shaders/default.core.vert + shaders/default.core.frag ACKNOWLEDGEMENTS.html AUTHORS CONTRIBUTORS diff --git a/shaders/default.core.frag b/shaders/default.core.frag new file mode 100644 index 00000000..0e835ce0 --- /dev/null +++ b/shaders/default.core.frag @@ -0,0 +1,20 @@ +#version 150 +in highp vec3 vert; +in highp vec3 vertNormal; +in highp vec3 vertColor; +in highp vec2 vertTexCoord; +out highp vec4 fragColor; +uniform highp vec3 lightPos; +uniform highp sampler2D textureId; +uniform highp int textureEnabled; +void main() +{ + highp vec3 L = normalize(lightPos - vert); + highp float NL = max(dot(normalize(vertNormal), L), 0.0); + highp vec3 color = vertColor; + if (textureEnabled == 1) { + color = texture(textureId, vertTexCoord).rgb; + } + highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0); + fragColor = vec4(col, 1.0); +} \ No newline at end of file diff --git a/shaders/default.core.vert b/shaders/default.core.vert new file mode 100644 index 00000000..4aafcdb5 --- /dev/null +++ b/shaders/default.core.vert @@ -0,0 +1,20 @@ +#version 150 +in vec4 vertex; +in vec3 normal; +in vec3 color; +in vec2 texCoord; +out vec3 vert; +out vec3 vertNormal; +out vec3 vertColor; +out vec2 vertTexCoord; +uniform mat4 projMatrix; +uniform mat4 mvMatrix; +uniform mat3 normalMatrix; +void main() +{ + vert = vertex.xyz; + vertNormal = normalMatrix * normal; + vertColor = color; + vertTexCoord = texCoord; + gl_Position = projMatrix * mvMatrix * vertex; +} \ No newline at end of file diff --git a/shaders/default.frag b/shaders/default.frag new file mode 100644 index 00000000..4f460ab1 --- /dev/null +++ b/shaders/default.frag @@ -0,0 +1,18 @@ +varying highp vec3 vert; +varying highp vec3 vertNormal; +varying highp vec3 vertColor; +varying highp vec2 vertTexCoord; +uniform highp vec3 lightPos; +uniform highp sampler2D textureId; +uniform highp int textureEnabled; +void main() +{ + highp vec3 L = normalize(lightPos - vert); + highp float NL = max(dot(normalize(vertNormal), L), 0.0); + highp vec3 color = vertColor; + if (textureEnabled == 1) { + color = texture2D(textureId, vertTexCoord).rgb; + } + highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0); + gl_FragColor = vec4(col, 1.0); +} \ No newline at end of file diff --git a/shaders/default.vert b/shaders/default.vert new file mode 100644 index 00000000..63a207d4 --- /dev/null +++ b/shaders/default.vert @@ -0,0 +1,19 @@ +attribute vec4 vertex; +attribute vec3 normal; +attribute vec3 color; +attribute vec2 texCoord; +varying vec3 vert; +varying vec3 vertNormal; +varying vec3 vertColor; +varying vec2 vertTexCoord; +uniform mat4 projMatrix; +uniform mat4 mvMatrix; +uniform mat3 normalMatrix; +void main() +{ + vert = vertex.xyz; + vertNormal = normalMatrix * normal; + vertColor = color; + vertTexCoord = texCoord; + gl_Position = projMatrix * mvMatrix * vertex; +} \ No newline at end of file diff --git a/src/modelshaderprogram.cpp b/src/modelshaderprogram.cpp index 90175bdc..cd4a161b 100644 --- a/src/modelshaderprogram.cpp +++ b/src/modelshaderprogram.cpp @@ -1,91 +1,31 @@ #include +#include +#include #include "modelshaderprogram.h" -static const char *vertexShaderSourceCore = - "#version 150\n" - "in vec4 vertex;\n" - "in vec3 normal;\n" - "in vec3 color;\n" - "in vec2 texCoord;\n" - "out vec3 vert;\n" - "out vec3 vertNormal;\n" - "out vec3 vertColor;\n" - "out vec2 vertTexCoord;\n" - "uniform mat4 projMatrix;\n" - "uniform mat4 mvMatrix;\n" - "uniform mat3 normalMatrix;\n" - "void main() {\n" - " vert = vertex.xyz;\n" - " vertNormal = normalMatrix * normal;\n" - " vertColor = color;\n" - " vertTexCoord = texCoord;\n" - " gl_Position = projMatrix * mvMatrix * vertex;\n" - "}\n"; - -static const char *vertexShaderSource = - "attribute vec4 vertex;\n" - "attribute vec3 normal;\n" - "attribute vec3 color;\n" - "attribute vec2 texCoord;\n" - "varying vec3 vert;\n" - "varying vec3 vertNormal;\n" - "varying vec3 vertColor;\n" - "varying vec2 vertTexCoord;\n" - "uniform mat4 projMatrix;\n" - "uniform mat4 mvMatrix;\n" - "uniform mat3 normalMatrix;\n" - "void main() {\n" - " vert = vertex.xyz;\n" - " vertNormal = normalMatrix * normal;\n" - " vertColor = color;\n" - " vertTexCoord = texCoord;\n" - " gl_Position = projMatrix * mvMatrix * vertex;\n" - "}\n"; - -static const char *fragmentShaderSourceCore = - "#version 150\n" - "in highp vec3 vert;\n" - "in highp vec3 vertNormal;\n" - "in highp vec3 vertColor;\n" - "in highp vec2 vertTexCoord;\n" - "out highp vec4 fragColor;\n" - "uniform highp vec3 lightPos;\n" - "uniform highp sampler2D textureId;\n" - "uniform highp int textureEnabled;\n" - "void main() {\n" - " highp vec3 L = normalize(lightPos - vert);\n" - " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n" - " highp vec3 color = vertColor;\n" - " if (textureEnabled == 1) {\n" - " color = texture(textureId, vertTexCoord).rgb;\n" - " }\n" - " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n" - " fragColor = vec4(col, 1.0);\n" - "}\n"; - -static const char *fragmentShaderSource = - "varying highp vec3 vert;\n" - "varying highp vec3 vertNormal;\n" - "varying highp vec3 vertColor;\n" - "varying highp vec2 vertTexCoord;\n" - "uniform highp vec3 lightPos;\n" - "uniform highp sampler2D textureId;\n" - "uniform highp int textureEnabled;\n" - "void main() {\n" - " highp vec3 L = normalize(lightPos - vert);\n" - " highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n" - " highp vec3 color = vertColor;\n" - " if (textureEnabled == 1) {\n" - " color = texture2D(textureId, vertTexCoord).rgb;\n" - " }\n" - " highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n" - " gl_FragColor = vec4(col, 1.0);\n" - "}\n"; +const QString &ModelShaderProgram::loadShaderSource(const QString &name) +{ + static std::map s_shaderSources; + auto findShader = s_shaderSources.find(name); + if (findShader != s_shaderSources.end()) { + return findShader->second; + } + QFile file(name); + file.open(QFile::ReadOnly | QFile::Text); + QTextStream stream(&file); + auto insertResult = s_shaderSources.insert({name, stream.readAll()}); + return insertResult.first->second; +} ModelShaderProgram::ModelShaderProgram() { - this->addShaderFromSourceCode(QOpenGLShader::Vertex, QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile ? vertexShaderSourceCore : vertexShaderSource); - this->addShaderFromSourceCode(QOpenGLShader::Fragment, QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile ? fragmentShaderSourceCore : fragmentShaderSource); + if (QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile) { + this->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShaderSource(":/shaders/default.core.vert")); + this->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShaderSource(":/shaders/default.core.frag")); + } else { + this->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShaderSource(":/shaders/default.vert")); + this->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShaderSource(":/shaders/default.frag")); + } this->bindAttributeLocation("vertex", 0); this->bindAttributeLocation("normal", 1); this->bindAttributeLocation("color", 2); diff --git a/src/modelshaderprogram.h b/src/modelshaderprogram.h index 6d9a562f..11c3a6d2 100644 --- a/src/modelshaderprogram.h +++ b/src/modelshaderprogram.h @@ -1,6 +1,7 @@ #ifndef MODEL_SHADER_PROGRAM_H #define MODEL_SHADER_PROGRAM_H #include +#include class ModelShaderProgram : public QOpenGLShaderProgram { @@ -12,6 +13,7 @@ public: int lightPosLoc(); int textureIdLoc(); int textureEnabledLoc(); + static const QString &loadShaderSource(const QString &name); private: int m_projMatrixLoc; int m_mvMatrixLoc;