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>
|
2013-12-22 14:04:33 +00:00
|
|
|
#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"
|
2013-12-22 17:47:24 +00:00
|
|
|
#include "Logs.h"
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2015-11-09 21:30:46 +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();
|
2013-12-22 14:04:33 +00:00
|
|
|
functions = renderer->getOpenGlFunctions();
|
2015-11-25 22:15:58 +00:00
|
|
|
state = new OpenGLSharedState();
|
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-11-25 22:15:58 +00:00
|
|
|
delete state;
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::addVertexSource(QString path) {
|
2013-12-22 17:47:24 +00:00
|
|
|
QFile file(QString(":/shaders/%1.vert").arg(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-08-12 22:33:16 +00:00
|
|
|
Logs::error() << "Can't open vertex file " << file.fileName().toStdString() << std::endl;
|
2013-12-22 17:47:24 +00:00
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::addFragmentSource(QString path) {
|
2013-12-22 17:47:24 +00:00
|
|
|
QFile file(QString(":/shaders/%1.frag").arg(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-08-12 22:33:16 +00:00
|
|
|
Logs::error() << "Can't open fragment file " << file.fileName().toStdString() << std::endl;
|
2013-12-22 17:47:24 +00:00
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::compile() {
|
2013-12-22 17:47:24 +00:00
|
|
|
program->addShaderFromSourceCode(QOpenGLShader::Vertex, QString::fromStdString(source_vertex));
|
|
|
|
program->addShaderFromSourceCode(QOpenGLShader::Fragment, QString::fromStdString(source_fragment));
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::bind() {
|
|
|
|
if (not compiled) {
|
2013-12-22 17:47:24 +00:00
|
|
|
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);
|
2015-11-25 22:15:58 +00:00
|
|
|
state->apply(this, texture_unit);
|
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-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::drawTriangles(float *vertices, int triangle_count) {
|
2013-12-21 22:48:54 +00:00
|
|
|
bind();
|
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
GLuint array_vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(array_vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(array_vertex);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2013-12-21 23:10:18 +00:00
|
|
|
functions->glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
program->disableAttributeArray(array_vertex);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void OpenGLShaderProgram::drawTriangleStrip(float *vertices, int vertex_count) {
|
2013-12-21 22:48:54 +00:00
|
|
|
bind();
|
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
GLuint array_vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(array_vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(array_vertex);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2013-12-21 23:10:18 +00:00
|
|
|
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2015-11-29 18:18:36 +00:00
|
|
|
program->disableAttributeArray(array_vertex);
|
|
|
|
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::drawTrianglesUV(float *vertices, float *uv, int triangle_count) {
|
|
|
|
bind();
|
|
|
|
|
|
|
|
GLuint array_vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(array_vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(array_vertex);
|
|
|
|
|
|
|
|
GLuint array_uv = program->attributeLocation("uv");
|
|
|
|
program->setAttributeArray(array_uv, GL_FLOAT, uv, 2);
|
|
|
|
program->enableAttributeArray(array_uv);
|
|
|
|
|
|
|
|
functions->glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
|
|
|
|
|
|
|
|
program->disableAttributeArray(array_vertex);
|
|
|
|
program->disableAttributeArray(array_uv);
|
|
|
|
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::drawTriangleStripUV(float *vertices, float *uv, int vertex_count) {
|
|
|
|
bind();
|
|
|
|
|
|
|
|
GLuint array_vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(array_vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(array_vertex);
|
|
|
|
|
|
|
|
GLuint array_uv = program->attributeLocation("uv");
|
|
|
|
program->setAttributeArray(array_uv, GL_FLOAT, uv, 2);
|
|
|
|
program->enableAttributeArray(array_uv);
|
|
|
|
|
|
|
|
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
|
|
|
|
|
|
|
|
program->disableAttributeArray(array_vertex);
|
|
|
|
program->disableAttributeArray(array_uv);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
|
|
|
release();
|
|
|
|
}
|