paysages3d/src/render/opengl/OpenGLShaderProgram.cpp

116 lines
2.8 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(QString 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()
{
delete program;
}
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
{
logError() << "Can't open vertex file " << file.fileName();
}
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
{
logError() << "Can't open fragment file " << file.fileName();
}
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));
2013-12-21 22:48:54 +00:00
if (not program->link())
{
qWarning() << "Error while compiling shader " << name << "\n" << program->log() << "\n";
}
else if (program->log().length() > 0)
2013-12-21 22:48:54 +00:00
{
qDebug() << "Shader " << name << " compilation output:\n" << program->log() << "\n";
}
}
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);
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);
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);
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
2013-12-21 22:48:54 +00:00
program->disableAttributeArray(vertex);
release();
}