-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
117 lines (95 loc) · 4.06 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
114
115
116
117
#include "Shader.h"
Shader::Shader(const char *vertPath, const char *fragPath) {
// Load vertex program source code
std::string vp = FileUtils::LoadTextFile(vertPath);
const char *source_vp = vp.c_str();
std::string fp = FileUtils::LoadTextFile(fragPath);
const char *source_fp = fp.c_str();
// Create a shader from vertex program source code
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &source_vp, NULL);
glCompileShader(vs);
// Check if shader compiled successfully
GLint status;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(vs, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error compiling vertex shader: ") + std::string(buffer)));
}
// Create a shader from the fragment program source code
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &source_fp, NULL);
glCompileShader(fs);
// Check if shader compiled successfully
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(fs, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error compiling fragment shader: ") + std::string(buffer)));
}
// Create a shader program linking both vertex and fragment shaders
// together
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vs);
glAttachShader(shaderProgram, fs);
glLinkProgram(shaderProgram);
// Check if shaders were linked successfully
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(shaderProgram, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error linking shaders: ") + std::string(buffer)));
}
// Delete memory used by shaders, since they were already compiled
// and linked
glDeleteShader(vs);
glDeleteShader(fs);
attributeBinding();
}
void Shader::attributeBinding() {
// Set attributes for shaders
// Should be consistent with how we created the buffers for the square
GLint vertex_att = glGetAttribLocation(shaderProgram, "vertex");
glVertexAttribPointer(vertex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(vertex_att);
GLint color_att = glGetAttribLocation(shaderProgram, "color");
glVertexAttribPointer(color_att, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(color_att);
GLint tex_att = glGetAttribLocation(shaderProgram, "uv");
glVertexAttribPointer(tex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void *)(5 * sizeof(GLfloat)));
glEnableVertexAttribArray(tex_att);
}
// Sets a uniform integer variable in your shader program to a value
void Shader::setUniform1i(const GLchar *name, int value) {
glUniform1i(glGetUniformLocation(shaderProgram, name), value);
}
// Sets a uniform float variable in your shader program to a value
void Shader::setUniform1f(const GLchar *name, float value) {
glUniform1f(glGetUniformLocation(shaderProgram, name), value);
}
// Sets a uniform vector2 variable in your shader program to a vector
void Shader::setUniform2f(const GLchar *name, const glm::vec2 &vector) {
glUniform2f(glGetUniformLocation(shaderProgram, name), vector.x, vector.y);
}
// Sets a uniform vector3 variable in your shader program to a vector
void Shader::setUniform3f(const GLchar *name, const glm::vec3 &vector) {
glUniform3f(glGetUniformLocation(shaderProgram, name), vector.x, vector.y, vector.z);
}
// Sets a uniform vector4 variable in your shader program to a vector
void Shader::setUniform4f(const GLchar *name, const glm::vec4 &vector) {
glUniform4f(glGetUniformLocation(shaderProgram, name), vector.x, vector.y, vector.z, vector.w);
}
// Sets a uniform matrix4x4 variable in your shader program to a matrix4x4
void Shader::setUniformMat4(const GLchar *name, const glm::mat4 &matrix) {
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, name), 1, GL_FALSE, glm::value_ptr(matrix));
}
Shader::~Shader() {
glDeleteProgram(shaderProgram);
}
void Shader::enable() {
glUseProgram(shaderProgram);
}
void Shader::disable() {
glUseProgram(0);
}