-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.cpp
72 lines (52 loc) · 1.86 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
#include <fstream>
#include <iostream>
#include <string>
#include <GL/glew.h>
class Shader {
//FIXME: Steal my usual shader.c
private:
GLuint program;
class ShaderCode {
public:
GLuint shader;
ShaderCode(GLuint type, std::string path) {
std::ifstream file(path);
std::string code((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
shader = glCreateShader(type);
const char* codeCString = code.c_str();
glShaderSource(shader, 1, &codeCString, NULL);
glCompileShader(shader);
}
};
public:
//FIXME: This is a bad idea as GL might not be ready in constructors..
Shader(std::string vertexPath, std::string fragmentPath) {
ShaderCode vs(GL_VERTEX_SHADER, vertexPath + ".vsh");
ShaderCode fs(GL_FRAGMENT_SHADER, fragmentPath + ".fsh");
program = glCreateProgram();
glAttachShader(program, vs.shader);
glAttachShader(program, fs.shader);
glBindAttribLocation(program, 0, "vertex");
glLinkProgram(program);
}
Shader(std::string path) : Shader(path, path) {}
void activate() {
//FIXME: Shitty API?
glUseProgram(program);
}
void set(std::string variable, glm::mat3 value) {
glUniformMatrix3fv(glGetUniformLocation(program, variable.c_str()), 1, GL_TRUE, glm::value_ptr(value));
}
void set(std::string variable, glm::mat4 value) {
glUniformMatrix4fv(glGetUniformLocation(program, variable.c_str()), 1, GL_TRUE, glm::value_ptr(value));
}
void set(std::string variable, glm::vec3 value) {
glUniform3fv(glGetUniformLocation(program, variable.c_str()), 1, glm::value_ptr(value));
}
void set(std::string variable, float value) {
glUniform1f(glGetUniformLocation(program, variable.c_str()), value);
}
void set(std::string variable, unsigned int value) {
glUniform1i(glGetUniformLocation(program, variable.c_str()), value);
}
};