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-05 07:37:01 +00:00
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Vertex, loadShaderSource(":/shaders/default.vert"));
|
2018-10-04 12:51:01 +00:00
|
|
|
if (usePBR) {
|
2018-10-05 07:37:01 +00:00
|
|
|
this->addShaderFromSourceCode(QOpenGLShader::Fragment, loadShaderSource(":/shaders/pbr-qt.frag"));
|
2018-10-04 12:51:01 +00:00
|
|
|
} else {
|
|
|
|
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-10-09 02:19:12 +00:00
|
|
|
this->bindAttributeLocation("tangent", 6);
|
2019-11-02 02:43:23 +00:00
|
|
|
this->bindAttributeLocation("alpha", 7);
|
2018-04-02 06:05:06 +00:00
|
|
|
this->link();
|
|
|
|
|
|
|
|
this->bind();
|
2018-10-05 07:37:01 +00:00
|
|
|
m_projectionMatrixLoc = this->uniformLocation("projectionMatrix");
|
|
|
|
m_modelMatrixLoc = this->uniformLocation("modelMatrix");
|
2018-10-09 02:19:12 +00:00
|
|
|
m_normalMatrixLoc = this->uniformLocation("normalMatrix");
|
2018-10-05 07:37:01 +00:00
|
|
|
m_viewMatrixLoc = this->uniformLocation("viewMatrix");
|
2018-04-02 06:05:06 +00:00
|
|
|
m_lightPosLoc = this->uniformLocation("lightPos");
|
2018-05-10 09:16:22 +00:00
|
|
|
m_textureIdLoc = this->uniformLocation("textureId");
|
|
|
|
m_textureEnabledLoc = this->uniformLocation("textureEnabled");
|
2018-10-09 02:19:12 +00:00
|
|
|
m_normalMapIdLoc = this->uniformLocation("normalMapId");
|
|
|
|
m_normalMapEnabledLoc = this->uniformLocation("normalMapEnabled");
|
|
|
|
m_metalnessMapEnabledLoc = this->uniformLocation("metalnessMapEnabled");
|
|
|
|
m_roughnessMapEnabledLoc = this->uniformLocation("roughnessMapEnabled");
|
|
|
|
m_ambientOcclusionMapEnabledLoc = this->uniformLocation("ambientOcclusionMapEnabled");
|
|
|
|
m_metalnessRoughnessAmbientOcclusionMapIdLoc = this->uniformLocation("metalnessRoughnessAmbientOcclusionMapId");
|
2019-08-08 11:24:33 +00:00
|
|
|
m_mousePickEnabledLoc = this->uniformLocation("mousePickEnabled");
|
|
|
|
m_mousePickTargetPositionLoc = this->uniformLocation("mousePickTargetPosition");
|
2019-08-17 10:13:11 +00:00
|
|
|
m_mousePickRadiusLoc = this->uniformLocation("mousePickRadius");
|
2018-04-02 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 07:37:01 +00:00
|
|
|
int ModelShaderProgram::projectionMatrixLoc()
|
2018-04-02 06:05:06 +00:00
|
|
|
{
|
2018-10-05 07:37:01 +00:00
|
|
|
return m_projectionMatrixLoc;
|
2018-04-02 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 07:37:01 +00:00
|
|
|
int ModelShaderProgram::modelMatrixLoc()
|
2018-04-02 06:05:06 +00:00
|
|
|
{
|
2018-10-05 07:37:01 +00:00
|
|
|
return m_modelMatrixLoc;
|
2018-04-02 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 02:19:12 +00:00
|
|
|
int ModelShaderProgram::normalMatrixLoc()
|
|
|
|
{
|
|
|
|
return m_normalMatrixLoc;
|
|
|
|
}
|
|
|
|
|
2018-10-05 07:37:01 +00:00
|
|
|
int ModelShaderProgram::viewMatrixLoc()
|
2018-04-02 06:05:06 +00:00
|
|
|
{
|
2018-10-05 07:37:01 +00:00
|
|
|
return m_viewMatrixLoc;
|
2018-04-02 06:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-10-09 02:19:12 +00:00
|
|
|
|
|
|
|
int ModelShaderProgram::normalMapEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_normalMapEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::normalMapIdLoc()
|
|
|
|
{
|
|
|
|
return m_normalMapIdLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::metalnessMapEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_metalnessMapEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::roughnessMapEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_roughnessMapEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::ambientOcclusionMapEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_ambientOcclusionMapEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::metalnessRoughnessAmbientOcclusionMapIdLoc()
|
|
|
|
{
|
|
|
|
return m_metalnessRoughnessAmbientOcclusionMapIdLoc;
|
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:33 +00:00
|
|
|
int ModelShaderProgram::mousePickEnabledLoc()
|
|
|
|
{
|
|
|
|
return m_mousePickEnabledLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelShaderProgram::mousePickTargetPositionLoc()
|
|
|
|
{
|
|
|
|
return m_mousePickTargetPositionLoc;
|
|
|
|
}
|
2019-08-17 10:13:11 +00:00
|
|
|
|
|
|
|
int ModelShaderProgram::mousePickRadiusLoc()
|
|
|
|
{
|
|
|
|
return m_mousePickRadiusLoc;
|
|
|
|
}
|