paysages3d/src/render/opengl/OpenGLShaderProgram.cpp

93 lines
2.9 KiB
C++
Raw Normal View History

2013-12-21 22:48:54 +00:00
#include "OpenGLShaderProgram.h"
#include <QOpenGLShaderProgram>
#include <QDir>
#include "OpenGLFunctions.h"
#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"
#include "Logs.h"
2013-12-21 22:48:54 +00:00
OpenGLShaderProgram::OpenGLShaderProgram(const std::string &name, OpenGLRenderer *renderer)
: renderer(renderer), name(name) {
2013-12-21 22:48:54 +00:00
program = new QOpenGLShaderProgram();
functions = renderer->getOpenGlFunctions();
compiled = false;
2013-12-21 22:48:54 +00:00
}
OpenGLShaderProgram::~OpenGLShaderProgram() {
2013-12-21 22:48:54 +00:00
delete program;
}
2015-12-07 22:32:55 +00:00
void OpenGLShaderProgram::addVertexSource(const std::string &path) {
QFile file(QString(":/shaders/%1.vert").arg(QString::fromStdString(path)));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
source_vertex += QString(file.readAll()).toStdString();
} else {
Logs::error() << "[OpenGL] Can't open vertex file " << file.fileName().toStdString() << std::endl;
}
2013-12-21 22:48:54 +00:00
}
2015-12-07 22:32:55 +00:00
void OpenGLShaderProgram::addFragmentSource(const std::string &path) {
QFile file(QString(":/shaders/%1.frag").arg(QString::fromStdString(path)));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
source_fragment += QString(file.readAll()).toStdString();
} else {
Logs::error() << "[OpenGL] Can't open fragment file " << file.fileName().toStdString() << std::endl;
}
2013-12-21 22:48:54 +00:00
}
void OpenGLShaderProgram::compile() {
2015-12-07 22:32:55 +00:00
std::string prefix = std::string("#version ") + OPENGL_GLSL_VERSION + "\n\n";
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);
if (not program->link()) {
Logs::warning() << "[OpenGL] Error while compiling shader " << name << std::endl
<< program->log().toStdString() << std::endl;
} else if (program->log().length() > 0) {
Logs::debug() << "[OpenGL] Shader " << name << " compilation output:" << std::endl
<< program->log().toStdString() << std::endl;
} else {
Logs::debug() << "[OpenGL] Shader " << name << " compiled" << std::endl;
2013-12-21 22:48:54 +00:00
}
}
2015-12-07 22:32:55 +00:00
bool OpenGLShaderProgram::bind(OpenGLSharedState *state) {
if (not compiled) {
compile();
compiled = true;
}
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);
}
return true;
} else {
return false;
}
2013-12-21 22:48:54 +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
release();
}
2013-12-21 22:48:54 +00:00
}