-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shader.cpp
113 lines (100 loc) · 2.92 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "Application.h"
#include "Shader.h"
Shader::Shader()
{
// build and compile our shader program
// ------------------------------------
// vertex shader
int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
LOGC("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s", infoLog);
}
// fragment shader
int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
LOGC("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s", infoLog);
}
// fragment shader
const char* f2 = fragmentShaderSource2.c_str();
int fragmentShader2 = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader2, 1, &f2, NULL);
glCompileShader(fragmentShader2);
// check for shader compile errors
glGetShaderiv(fragmentShader2, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader2, 512, NULL, infoLog);
LOGC("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s", infoLog);
}
// link shaders
ID = glCreateProgram();
glAttachShader(ID, vertexShader);
glAttachShader(ID, fragmentShader);
glLinkProgram(ID);
// check for linking errors
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(ID, 512, NULL, infoLog);
LOGC("ERROR::SHADER::PROGRAM::LINKING_FAILED\n %s", infoLog);
}
// link shaders
ID_N = glCreateProgram();
glAttachShader(ID_N, vertexShader);
glAttachShader(ID_N, fragmentShader2);
glLinkProgram(ID_N);
// check for linking errors
glGetProgramiv(ID_N, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(ID_N, 512, NULL, infoLog);
LOGC("ERROR::SHADER::PROGRAM::LINKING_FAILED\n %s", infoLog);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
void Shader::use(int i)
{
switch (i) {
case 0:
glUseProgram(ID);
break;
case 1:
glUseProgram(ID_N);
break;
}
glEnable(GL_BLEND);
}
void Shader::stop()
{
glUseProgram(0);
glDisable(GL_BLEND);
}
void Shader::setBool(const std::string &name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setInt(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
}
void Shader::setMat4(const std::string &name, const mat4x4 &mat) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat);
}