2013-12-21 22:48:54 +00:00
|
|
|
#include "OpenGLShaderProgram.h"
|
|
|
|
|
|
|
|
#include <QOpenGLShaderProgram>
|
2013-12-21 23:10:18 +00:00
|
|
|
#include <QOpenGLFunctions_3_2_Core>
|
2013-12-21 22:48:54 +00:00
|
|
|
#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 14:04:33 +00:00
|
|
|
OpenGLShaderProgram::OpenGLShaderProgram(QString 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();
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLShaderProgram::~OpenGLShaderProgram()
|
|
|
|
{
|
|
|
|
delete program;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::addVertexSource(QString path)
|
|
|
|
{
|
|
|
|
program->addShaderFromSourceFile(QOpenGLShader::Vertex, QString(":/shaders/%1.vert").arg(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::addFragmentSource(QString path)
|
|
|
|
{
|
|
|
|
program->addShaderFromSourceFile(QOpenGLShader::Fragment, QString(":/shaders/%1.frag").arg(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::compile()
|
|
|
|
{
|
|
|
|
if (not program->link())
|
|
|
|
{
|
|
|
|
qWarning() << "Error while compiling shader " << name << "\n" << program->log() << "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Shader " << name << " compilation output:\n" << program->log() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::bind()
|
|
|
|
{
|
|
|
|
program->bind();
|
|
|
|
|
2013-12-22 17:05:11 +00:00
|
|
|
int texture_unit = 0;
|
|
|
|
renderer->getSharedState()->apply(this, texture_unit);
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::release()
|
|
|
|
{
|
|
|
|
program->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLShaderProgram::drawTriangles(float* vertices, int triangle_count)
|
|
|
|
{
|
|
|
|
bind();
|
|
|
|
|
|
|
|
GLuint vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(vertex);
|
|
|
|
|
2013-12-21 23:10:18 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
bind();
|
|
|
|
|
|
|
|
GLuint vertex = program->attributeLocation("vertex");
|
|
|
|
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
|
|
|
|
program->enableAttributeArray(vertex);
|
|
|
|
|
2013-12-21 23:10:18 +00:00
|
|
|
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
|
|
|
program->disableAttributeArray(vertex);
|
|
|
|
|
|
|
|
release();
|
|
|
|
}
|