2013-12-21 22:48:54 +00:00
|
|
|
#include "OpenGLShaderProgram.h"
|
|
|
|
|
|
|
|
#include <QOpenGLShaderProgram>
|
|
|
|
#include <QDir>
|
2015-12-03 22:04:50 +00:00
|
|
|
#include "OpenGLFunctions.h"
|
2013-12-22 14:04:33 +00:00
|
|
|
#include "OpenGLRenderer.h"
|
|
|
|
#include "OpenGLSharedState.h"
|
2015-12-07 22:32:55 +00:00
|
|
|
#include "OpenGLVertexArray.h"
|
2013-12-21 22:48:54 +00:00
|
|
|
#include "Texture2D.h"
|
|
|
|
#include "Texture3D.h"
|
|
|
|
#include "Texture4D.h"
|
|
|
|
#include "Color.h"
|
2013-12-22 17:47:24 +00:00
|
|
|
#include "Logs.h"
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
OpenGLShaderProgram::OpenGLShaderProgram(const string &name, OpenGLRenderer *renderer)
|
2015-11-09 21:30:46 +00:00
|
|
|
: renderer(renderer), name(name) {
|
2013-12-21 22:48:54 +00:00
|
|
|
program = new QOpenGLShaderProgram();
|
2013-12-22 14:04:33 +00:00
|
|
|
functions = renderer->getOpenGlFunctions();
|
2013-12-22 17:47:24 +00:00
|
|
|
compiled = false;
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
OpenGLShaderProgram::~OpenGLShaderProgram() {
|
2013-12-21 22:48:54 +00:00
|
|
|
delete program;
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
void OpenGLShaderProgram::addVertexSource(const string &path) {
|
2015-12-07 22:32:55 +00:00
|
|
|
QFile file(QString(":/shaders/%1.vert").arg(QString::fromStdString(path)));
|
2015-11-09 21:30:46 +00:00
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2013-12-22 17:47:24 +00:00
|
|
|
source_vertex += QString(file.readAll()).toStdString();
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-10 23:36:50 +00:00
|
|
|
Logs::error() << "[OpenGL] Can't open vertex file " << file.fileName().toStdString() << endl;
|
2013-12-22 17:47:24 +00:00
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
void OpenGLShaderProgram::addFragmentSource(const string &path) {
|
2015-12-07 22:32:55 +00:00
|
|
|
QFile file(QString(":/shaders/%1.frag").arg(QString::fromStdString(path)));
|
2015-11-09 21:30:46 +00:00
|
|
|
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2013-12-22 17:47:24 +00:00
|
|
|
source_fragment += QString(file.readAll()).toStdString();
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-12-10 23:36:50 +00:00
|
|
|
Logs::error() << "[OpenGL] Can't open fragment file " << file.fileName().toStdString() << endl;
|
2013-12-22 17:47:24 +00:00
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
void OpenGLShaderProgram::destroy(OpenGLFunctions *) {
|
2015-12-10 22:41:42 +00:00
|
|
|
program->removeAllShaders();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::compile() {
|
2015-12-10 23:36:50 +00:00
|
|
|
string prefix = string("#version ") + OPENGL_GLSL_VERSION + "\n\n";
|
2015-12-07 22:32:55 +00:00
|
|
|
|
|
|
|
program->addShaderFromSourceCode(QOpenGLShader::Vertex, QString::fromStdString(prefix + source_vertex));
|
|
|
|
program->addShaderFromSourceCode(QOpenGLShader::Fragment, QString::fromStdString(prefix + source_fragment));
|
|
|
|
|
|
|
|
program->bindAttributeLocation("vertex", 0);
|
|
|
|
program->bindAttributeLocation("uv", 1);
|
2013-12-22 17:47:24 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
if (not program->link()) {
|
2015-12-10 23:36:50 +00:00
|
|
|
Logs::warning() << "[OpenGL] Error while compiling shader " << name << endl
|
|
|
|
<< program->log().toStdString() << endl;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else if (program->log().length() > 0) {
|
2015-12-10 23:36:50 +00:00
|
|
|
Logs::debug() << "[OpenGL] Shader " << name << " compilation output:" << endl
|
|
|
|
<< program->log().toStdString() << endl;
|
2015-12-03 22:04:50 +00:00
|
|
|
} else {
|
2015-12-10 23:36:50 +00:00
|
|
|
Logs::debug() << "[OpenGL] Shader " << name << " compiled" << endl;
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 22:32:55 +00:00
|
|
|
bool OpenGLShaderProgram::bind(OpenGLSharedState *state) {
|
2015-11-09 21:30:46 +00:00
|
|
|
if (not compiled) {
|
2013-12-22 17:47:24 +00:00
|
|
|
compile();
|
|
|
|
compiled = true;
|
|
|
|
}
|
|
|
|
|
2015-12-03 22:04:50 +00:00
|
|
|
if (program->bind()) {
|
|
|
|
int texture_unit = 0;
|
|
|
|
renderer->getSharedState()->apply(this, texture_unit);
|
2015-12-07 22:32:55 +00:00
|
|
|
if (state) {
|
|
|
|
state->apply(this, texture_unit);
|
|
|
|
}
|
2015-12-03 22:04:50 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::release() {
|
2013-12-21 22:48:54 +00:00
|
|
|
program->release();
|
|
|
|
}
|
|
|
|
|
2015-12-07 22:32:55 +00:00
|
|
|
void OpenGLShaderProgram::draw(OpenGLVertexArray *vertices, OpenGLSharedState *state) {
|
|
|
|
if (bind(state)) {
|
|
|
|
vertices->render(functions);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2015-12-03 22:04:50 +00:00
|
|
|
release();
|
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|