paysages3d/src/render/opengl/OpenGLShaderProgram.cpp

102 lines
3 KiB
C++
Raw Normal View History

2013-12-21 22:48:54 +00:00
#include "OpenGLShaderProgram.h"
2013-12-23 09:26:29 +00:00
#include OPENGL_FUNCTIONS_INCLUDE
2013-12-21 22:48:54 +00:00
#include <QOpenGLShaderProgram>
#include <QDir>
#include "OpenGLRenderer.h"
#include "OpenGLSharedState.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();
state = new OpenGLSharedState();
compiled = false;
2013-12-21 22:48:54 +00:00
}
OpenGLShaderProgram::~OpenGLShaderProgram() {
2013-12-21 22:48:54 +00:00
delete program;
delete state;
2013-12-21 22:48:54 +00:00
}
void OpenGLShaderProgram::addVertexSource(QString path) {
QFile file(QString(":/shaders/%1.vert").arg(path));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
source_vertex += QString(file.readAll()).toStdString();
} else {
Logs::error() << "Can't open vertex file " << file.fileName().toStdString() << std::endl;
}
2013-12-21 22:48:54 +00:00
}
void OpenGLShaderProgram::addFragmentSource(QString path) {
QFile file(QString(":/shaders/%1.frag").arg(path));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
source_fragment += QString(file.readAll()).toStdString();
} else {
Logs::error() << "Can't open fragment file " << file.fileName().toStdString() << std::endl;
}
2013-12-21 22:48:54 +00:00
}
void OpenGLShaderProgram::compile() {
program->addShaderFromSourceCode(QOpenGLShader::Vertex, QString::fromStdString(source_vertex));
program->addShaderFromSourceCode(QOpenGLShader::Fragment, QString::fromStdString(source_fragment));
if (not program->link()) {
Logs::warning() << "Error while compiling shader " << name << std::endl
<< program->log().toStdString() << std::endl;
} else if (program->log().length() > 0) {
Logs::debug() << "Shader " << name << " compilation output:" << std::endl
<< program->log().toStdString() << std::endl;
2013-12-21 22:48:54 +00:00
}
}
void OpenGLShaderProgram::bind() {
if (not compiled) {
compile();
compiled = true;
}
2013-12-21 22:48:54 +00:00
program->bind();
2013-12-22 17:05:11 +00:00
int texture_unit = 0;
renderer->getSharedState()->apply(this, texture_unit);
state->apply(this, texture_unit);
2013-12-21 22:48:54 +00:00
}
void OpenGLShaderProgram::release() {
2013-12-21 22:48:54 +00:00
program->release();
}
void OpenGLShaderProgram::drawTriangles(float *vertices, int triangle_count) {
2013-12-21 22:48:54 +00:00
bind();
GLuint vertex = program->attributeLocation("vertex");
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
program->enableAttributeArray(vertex);
functions->glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
2013-12-21 22:48:54 +00:00
program->disableAttributeArray(vertex);
release();
}
void OpenGLShaderProgram::drawTriangleStrip(float *vertices, int vertex_count) {
2013-12-21 22:48:54 +00:00
bind();
GLuint vertex = program->attributeLocation("vertex");
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
program->enableAttributeArray(vertex);
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
2013-12-21 22:48:54 +00:00
program->disableAttributeArray(vertex);
release();
}