2022-10-18 09:35:04 +00:00
|
|
|
#include "monochrome_opengl_program.h"
|
2022-09-23 15:54:49 +00:00
|
|
|
#include <QFile>
|
2022-10-18 09:35:04 +00:00
|
|
|
#include <QOpenGLFunctions>
|
2022-09-23 15:54:49 +00:00
|
|
|
#include <dust3d/base/debug.h>
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
static const QString& loadShaderSource(const QString& name)
|
2022-09-23 15:54:49 +00:00
|
|
|
{
|
|
|
|
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);
|
2022-10-18 09:35:04 +00:00
|
|
|
auto insertResult = s_shaderSources.insert({ name, stream.readAll() });
|
2022-09-23 15:54:49 +00:00
|
|
|
return insertResult.first->second;
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
void MonochromeOpenGLProgram::addShaderFromResource(QOpenGLShader::ShaderType type, const char* resourceName)
|
2022-09-23 15:54:49 +00:00
|
|
|
{
|
|
|
|
if (!addShaderFromSourceCode(type, loadShaderSource(resourceName)))
|
|
|
|
dust3dDebug << "Failed to addShaderFromResource, resource:" << resourceName << ", " << log().toStdString();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MonochromeOpenGLProgram::isCoreProfile() const
|
|
|
|
{
|
|
|
|
return m_isCoreProfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MonochromeOpenGLProgram::load(bool isCoreProfile)
|
|
|
|
{
|
|
|
|
if (m_isLoaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_isCoreProfile = isCoreProfile;
|
|
|
|
if (m_isCoreProfile) {
|
|
|
|
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/monochrome_core.vert");
|
|
|
|
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/monochrome_core.frag");
|
|
|
|
} else {
|
|
|
|
addShaderFromResource(QOpenGLShader::Vertex, ":/shaders/monochrome.vert");
|
|
|
|
addShaderFromResource(QOpenGLShader::Fragment, ":/shaders/monochrome.frag");
|
|
|
|
}
|
|
|
|
bindAttributeLocation("vertex", 0);
|
|
|
|
bindAttributeLocation("color", 1);
|
|
|
|
bindAttributeLocation("alpha", 2);
|
|
|
|
link();
|
|
|
|
bind();
|
|
|
|
m_isLoaded = true;
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
int MonochromeOpenGLProgram::getUniformLocationByName(const std::string& name)
|
2022-09-23 15:54:49 +00:00
|
|
|
{
|
|
|
|
auto findLocation = m_uniformLocationMap.find(name);
|
|
|
|
if (findLocation != m_uniformLocationMap.end())
|
|
|
|
return findLocation->second;
|
|
|
|
int location = uniformLocation(name.c_str());
|
2022-10-18 09:35:04 +00:00
|
|
|
m_uniformLocationMap.insert({ name, location });
|
2022-09-23 15:54:49 +00:00
|
|
|
return location;
|
|
|
|
}
|