2018-04-02 06:05:06 +00:00
|
|
|
#include <QSurfaceFormat>
|
2018-10-03 05:52:35 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <map>
|
2018-04-02 06:05:06 +00:00
|
|
|
#include "modelshaderprogram.h"
|
|
|
|
|
2018-10-03 05:52:35 +00:00
|
|
|
const QString &ModelShaderProgram::loadShaderSource(const QString &name)
|
|
|
|
{
|
|
|
|
static std::map<QString, QString> 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;
|
|
|
|
}
|
2018-04-02 06:05:06 +00:00
|
|
|
|
2018-10-04 12:51:01 +00:00
|
|
|
ModelShaderProgram::ModelShaderProgram(bool usePBR)
|
2018-04-02 06:05:06 +00:00
|
|
|
{
|
2018-10-03 05:52:35 +00:00
|
|
|
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 {
|
2018-10-04 12:51:01 +00:00
|
|
|
if (usePBR) {
|
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShaderSource(":/shaders/pbr.vert"));
|
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShaderSource(":/shaders/pbr.frag"));
|
|
|
|
} else {
|
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShaderSource(":/shaders/default.vert"));
|
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShaderSource(":/shaders/default.frag"));
|
|
|
|
}
|
2018-10-03 05:52:35 +00:00
|
|
|
}
|
2018-04-02 06:05:06 +00:00
|
|
|
this->bindAttributeLocation("vertex", 0);
|
|
|
|
this->bindAttributeLocation("normal", 1);
|
|
|
|
this->bindAttributeLocation("color", 2);
|
2018-05-10 09:16:22 +00:00
|
|
|
this->bindAttributeLocation("texCoord", 3);
|
2018-10-04 12:51:01 +00:00
|
|
|
this->bindAttributeLocation("metalness", 4);
|
|
|
|
this->bindAttributeLocation("roughness", 5);
|
2018-04-02 06:05:06 +00:00
|
|
|
this->link();
|
|
|
|
|
|
|
|
this->bind();
|
|
|
|
m_projMatrixLoc = this->uniformLocation("projMatrix");
|
|
|
|
m_mvMatrixLoc = this->uniformLocation("mvMatrix");
|
|
|
|
m_normalMatrixLoc = this->uniformLocation("normalMatrix");
|
|
|
|
m_lightPosLoc = this->uniformLocation("lightPos");
|
2018-05-10 09:16:22 +00:00
|
|
|
m_textureIdLoc = this->uniformLocation("textureId");
|
|
|
|
m_textureEnabledLoc = this->uniformLocation("textureEnabled");
|
2018-04-02 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::projMatrixLoc()
|
|
|
|
{
|
|
|
|
return m_projMatrixLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::mvMatrixLoc()
|
|
|
|
{
|
|
|
|
return m_mvMatrixLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::normalMatrixLoc()
|
|
|
|
{
|
|
|
|
return m_normalMatrixLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::lightPosLoc()
|
|
|
|
{
|
|
|
|
return m_lightPosLoc;
|
|
|
|
}
|
2018-05-10 09:16:22 +00:00
|
|
|
|
|
|
|
int ModelShaderProgram::textureEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_textureEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::textureIdLoc()
|
|
|
|
{
|
|
|
|
return m_textureIdLoc;
|
|
|
|
}
|