Move shader from source code to separated folder

master
Jeremy Hu 2018-10-03 13:52:35 +08:00
parent 997d9efd75
commit 5c25d7d4f8
7 changed files with 105 additions and 82 deletions

View File

@ -6,6 +6,10 @@
<file>resources/tree-branch-more.png</file> <file>resources/tree-branch-more.png</file>
<file>resources/tree-branch-open.png</file> <file>resources/tree-branch-open.png</file>
<file>resources/tree-vline.png</file> <file>resources/tree-vline.png</file>
<file>shaders/default.vert</file>
<file>shaders/default.frag</file>
<file>shaders/default.core.vert</file>
<file>shaders/default.core.frag</file>
<file>ACKNOWLEDGEMENTS.html</file> <file>ACKNOWLEDGEMENTS.html</file>
<file>AUTHORS</file> <file>AUTHORS</file>
<file>CONTRIBUTORS</file> <file>CONTRIBUTORS</file>

20
shaders/default.core.frag Normal file
View File

@ -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);
}

20
shaders/default.core.vert Normal file
View File

@ -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;
}

18
shaders/default.frag Normal file
View File

@ -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);
}

19
shaders/default.vert Normal file
View File

@ -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;
}

View File

@ -1,91 +1,31 @@
#include <QSurfaceFormat> #include <QSurfaceFormat>
#include <QFile>
#include <map>
#include "modelshaderprogram.h" #include "modelshaderprogram.h"
static const char *vertexShaderSourceCore = const QString &ModelShaderProgram::loadShaderSource(const QString &name)
"#version 150\n" {
"in vec4 vertex;\n" static std::map<QString, QString> s_shaderSources;
"in vec3 normal;\n" auto findShader = s_shaderSources.find(name);
"in vec3 color;\n" if (findShader != s_shaderSources.end()) {
"in vec2 texCoord;\n" return findShader->second;
"out vec3 vert;\n" }
"out vec3 vertNormal;\n" QFile file(name);
"out vec3 vertColor;\n" file.open(QFile::ReadOnly | QFile::Text);
"out vec2 vertTexCoord;\n" QTextStream stream(&file);
"uniform mat4 projMatrix;\n" auto insertResult = s_shaderSources.insert({name, stream.readAll()});
"uniform mat4 mvMatrix;\n" return insertResult.first->second;
"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";
ModelShaderProgram::ModelShaderProgram() ModelShaderProgram::ModelShaderProgram()
{ {
this->addShaderFromSourceCode(QOpenGLShader::Vertex, QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile ? vertexShaderSourceCore : vertexShaderSource); if (QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile) {
this->addShaderFromSourceCode(QOpenGLShader::Fragment, QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile ? fragmentShaderSourceCore : fragmentShaderSource); 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("vertex", 0);
this->bindAttributeLocation("normal", 1); this->bindAttributeLocation("normal", 1);
this->bindAttributeLocation("color", 2); this->bindAttributeLocation("color", 2);

View File

@ -1,6 +1,7 @@
#ifndef MODEL_SHADER_PROGRAM_H #ifndef MODEL_SHADER_PROGRAM_H
#define MODEL_SHADER_PROGRAM_H #define MODEL_SHADER_PROGRAM_H
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QString>
class ModelShaderProgram : public QOpenGLShaderProgram class ModelShaderProgram : public QOpenGLShaderProgram
{ {
@ -12,6 +13,7 @@ public:
int lightPosLoc(); int lightPosLoc();
int textureIdLoc(); int textureIdLoc();
int textureEnabledLoc(); int textureEnabledLoc();
static const QString &loadShaderSource(const QString &name);
private: private:
int m_projMatrixLoc; int m_projMatrixLoc;
int m_mvMatrixLoc; int m_mvMatrixLoc;