-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_utils.cpp
105 lines (92 loc) · 2.89 KB
/
shader_utils.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
#include "shader_utils.h"
#include "gl.h"
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
std::string error_log(GLuint object) {
GLint log_length = 0;
if (glIsShader(object))
glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
else if (glIsProgram(object))
glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
else {
throw std::runtime_error("not a shader nor a program");
}
char *log = (char *)malloc(log_length);
if (glIsShader(object))
glGetShaderInfoLog(object, log_length, NULL, log);
else if (glIsProgram(object))
glGetProgramInfoLog(object, log_length, NULL, log);
std::string result(log, log_length);
free(log);
return result;
}
std::string file_read(const char *path) {
std::ifstream ifs(path);
if (ifs.fail()) {
std::stringstream ss;
ss << "file_read: " << path;
throw std::runtime_error(ss.str());
}
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
GLuint create_shader(const char *filename, GLenum type) {
const std::string source = file_read(filename);
GLuint res = glCreateShader(type);
const GLchar *sources[] = {"#version 120\n"
"#define lowp \n"
"#define mediump\n"
"#define highp \n",
source.data()};
glShaderSource(res, 2, sources, NULL);
glCompileShader(res);
GLint compile_ok = GL_FALSE;
glGetShaderiv(res, GL_COMPILE_STATUS, &compile_ok);
if (compile_ok == GL_FALSE) {
std::stringstream ss;
ss << "shader compilation: " << filename << error_log(res);
throw std::runtime_error(ss.str());
}
return res;
}
GLuint create_program(const char *vertexfile, const char *fragmentfile) {
GLuint program = glCreateProgram();
GLuint shader;
if (vertexfile) {
shader = create_shader(vertexfile, GL_VERTEX_SHADER);
glAttachShader(program, shader);
}
if (fragmentfile) {
shader = create_shader(fragmentfile, GL_FRAGMENT_SHADER);
glAttachShader(program, shader);
}
glLinkProgram(program);
GLint link_ok = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
std::stringstream ss;
ss << "glLinkProgram: (" << vertexfile << ", " << fragmentfile << ")";
throw std::runtime_error(ss.str());
}
return program;
}
GLint get_attrib(GLuint program, const char *name) {
GLint attribute = glGetAttribLocation(program, name);
if (attribute == -1) {
std::stringstream ss;
ss << "Could not bind attribute " << name;
throw std::runtime_error(ss.str());
}
return attribute;
}
GLint get_uniform(GLuint program, const char *name) {
GLint uniform = glGetUniformLocation(program, name);
if (uniform == -1) {
std::stringstream ss;
ss << "Could not bind uniform " << name;
throw std::runtime_error(ss.str());
}
return uniform;
}