-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshader.cpp
82 lines (69 loc) · 1.94 KB
/
shader.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "shader.h"
static const char* vertexShaderSource =
"attribute highp vec3 aPos;\n"
"attribute highp vec3 iPos;\n"
"attribute mediump vec2 aUV;\n"
"uniform highp mat4 uModelMatrix;\n"
"uniform highp mat4 uViewMatrix;\n"
"uniform highp mat4 uProjMatrix;\n"
"varying mediump vec2 texCoords;\n"
"void main() {\n"
" texCoords = aUV;"
" gl_Position = uProjMatrix * uViewMatrix * uModelMatrix * vec4(aPos + iPos, 1.0);\n"
"}\n";
static const char* fragmentShaderSource =
"uniform sampler2D texture;\n"
"varying mediump vec2 texCoords;\n"
"void main() {\n"
" gl_FragColor = texture2D(texture, texCoords);\n"
"}\n";
Shader::Shader(QString name, QString vertPath, QString fragPath)
{
this->name = name;
bool result, result2, result3;
program = new QOpenGLShaderProgram();
result = program->addShaderFromSourceFile(QOpenGLShader::Vertex, vertPath);
if(!result)
Logger::Log(program->log().toStdString());
result2 = program->addShaderFromSourceFile(QOpenGLShader::Fragment, fragPath);
if(!result2)
Logger::Log(program->log().toStdString());
//result = program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
//result = program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
result3 = program->link();
if(!result || !result2 || !result3) //fix
{
qWarning() << "Shader could not be initialised";
exit(1);
}
}
Shader::~Shader()
{
delete program;
}
void Shader::Bind()
{
program->bind();
}
void Shader::Release()
{
program->release();
}
GLuint Shader::GetAttribLoc(const QString name)
{
int id = program->attributeLocation(name);
attributes.push_back(std::pair<QString, int>(name, id));
return id;
}
GLuint Shader::GetUniformLoc(const QString name)
{
return program->uniformLocation(name);
}
void Shader::SetUniform(int id, const QMatrix4x4 matrix)
{
program->setUniformValue(id, matrix);
}
void Shader::SetUniform(const char *name, const QMatrix4x4 matrix)
{
program->setUniformValue(name, matrix);
}