-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.cc
111 lines (69 loc) · 1.73 KB
/
program.cc
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
#include <sstream>
#include <fstream>
#include <cstdlib>
#include "program.h"
#include "shader.h"
#include "debug.h"
#include "glm/gtc/type_ptr.hpp"
namespace gl {
Program::Program() :
program(glCreateProgram()) {}
Program::~Program() {
glDeleteProgram(program);
}
bool Program::attachShader(GLenum type, char const *src) {
Shader sh {type, src};
if (!sh)
return false;
glAttachShader(program, sh.shader);
return true;
}
bool Program::attachShaderFile(GLenum type, char const *file) {
std::ifstream fs {file};
if (!fs) {
fprintf(stderr, "%s: filed to open stream: ", file);
perror("");
return false;
}
std::string s;
// call std::basic_string<char>& std::basic_string<char>::operator=(std::basic_string<char> const&)
return attachShader(type, (s = (std::ostringstream {} << fs.rdbuf()).str()).c_str());
}
bool Program::link() {
glLinkProgram(program);
if (!getParam(GL_LINK_STATUS)) {
char *log = getInfoLog();
E_DEBUG("link program failed: %s", log);
delete[] log;
return false;
}
return true;
}
void Program::use() {
glUseProgram(program);
}
// Uniforms
// Name is damn long
#define uloc(n) glGetUniformLocation(program, n)
void Program::setUniform(char const *name, glm::mat4 const& mat) {
glUniformMatrix4fv(
uloc(name),
1,
GL_FALSE,
glm::value_ptr(mat)
);
}
void Program::setUniform(char const *name, glm::vec3 const& vec) {
glUniform3fv(uloc(name), 1, glm::value_ptr(vec));
}
void Program::setUniform(char const *name, float const& v) {
glUniform1f(uloc(name), v);
}
GLint Program::getParam(GLenum param) {
GLint data;
glGetProgramiv(program, param, &data);
return data;
}
char *Program::getInfoLog(GLint *length)
DEFAULT_GET_LOG(Program, program, length)
} /* namespace gl */