dust3d/application/sources/model_opengl_program.cc

66 lines
2.2 KiB
C++
Raw Normal View History

2022-09-19 13:30:03 +00:00
#include <QOpenGLFunctions>
#include <QFile>
2022-09-20 12:48:22 +00:00
#include <dust3d/base/debug.h>
2022-09-19 13:30:03 +00:00
#include "model_opengl_program.h"
static const QString &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;
}
2022-09-20 12:48:22 +00:00
void ModelOpenGLProgram::addShaderFromResource(QOpenGLShader::ShaderType type, const char *resourceName)
{
if (!addShaderFromSourceCode(type, loadShaderSource(resourceName)))
dust3dDebug << "Failed to addShaderFromResource, resource:" << resourceName << ", " << log().toStdString();
}
bool ModelOpenGLProgram::isCoreProfile() const
{
return m_isCoreProfile;
}
2022-09-19 13:30:03 +00:00
void ModelOpenGLProgram::load(bool isCoreProfile)
{
if (m_isLoaded)
return;
m_isCoreProfile = isCoreProfile;
if (m_isCoreProfile) {
2022-09-20 12:48:22 +00:00
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/model_core.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/model_core.frag");
2022-09-19 13:30:03 +00:00
} else {
2022-09-20 12:48:22 +00:00
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/model.vert");
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/model.frag");
2022-09-19 13:30:03 +00:00
}
bindAttributeLocation("vertex", 0);
bindAttributeLocation("normal", 1);
bindAttributeLocation("color", 2);
bindAttributeLocation("texCoord", 3);
bindAttributeLocation("metalness", 4);
bindAttributeLocation("roughness", 5);
bindAttributeLocation("tangent", 6);
bindAttributeLocation("alpha", 7);
link();
bind();
m_isLoaded = true;
}
2022-09-20 12:48:22 +00:00
int ModelOpenGLProgram::getUniformLocationByName(const std::string &name)
2022-09-20 12:48:22 +00:00
{
auto findLocation = m_uniformLocationMap.find(name);
if (findLocation != m_uniformLocationMap.end())
return findLocation->second;
int location = uniformLocation(name.c_str());
m_uniformLocationMap.insert({name, location});
2022-09-20 12:48:22 +00:00
return location;
}