-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshaderprogram.cpp
More file actions
52 lines (49 loc) · 1.6 KB
/
Copy pathshaderprogram.cpp
File metadata and controls
52 lines (49 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "shaderprogram.h"
#include <QFile>
#include <QList>
#include <QPair>
#include <QMessageBox>
#include <QTextStream>
ShaderProgram::ShaderProgram() {
}
int ShaderProgram::init(const QStringList& prepend, const QString& fname) {
// Shader types with file extensions
const QVector<QPair<QOpenGLShader::ShaderTypeBit, QString>> shaderTypes = {
{QOpenGLShader::Vertex, ".vsh"},
{QOpenGLShader::Fragment, ".fsh"},
{QOpenGLShader::Geometry, ".gsh"},
{QOpenGLShader::TessellationControl, ".tcs"},
{QOpenGLShader::TessellationEvaluation, ".tes"}
};
// Shader "library" code
QString sharedCode;
for(const QString& prependFname : prepend) {
QFile f(prependFname);
if(!f.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open shader library file: " + prependFname;
return 5;
}
QTextStream in(&f);
sharedCode += in.readAll();
}
// Shaders stage code which contains main()
for(const auto& st : shaderTypes) {
const QString shaderFname = fname + st.second;
QFile f(shaderFname);
if(!f.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open shader stage file: " + shaderFname;
return 4;
}
QTextStream in(&f);
if(!shader.addShaderFromSourceCode(st.first, sharedCode + in.readAll())) {
qDebug() << "Shader compilation failed.";
return 1;
}
}
// Linking shaders into program
if(!shader.link()) {
qDebug() << "Shader linking failed.";
return 2;
}
return 0;
}