dust3d/application/sources/monochrome_opengl_program.cc

61 lines
2.0 KiB
C++
Raw Normal View History

#include "monochrome_opengl_program.h"
2022-09-23 15:54:49 +00:00
#include <QFile>
#include <QOpenGLFunctions>
2022-09-23 15:54:49 +00:00
#include <dust3d/base/debug.h>
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);
auto insertResult = s_shaderSources.insert({ name, stream.readAll() });
2022-09-23 15:54:49 +00:00
return insertResult.first->second;
}
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;
}
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());
m_uniformLocationMap.insert({ name, location });
2022-09-23 15:54:49 +00:00
return location;
}