-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
87 lines (75 loc) · 2.37 KB
/
util.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
#include <iostream>
#include <sstream>
#include <fstream>
#include "util.hpp"
using namespace std;
GLuint compileShader(GLenum type, string filename, string prepend) {
// Read the file
ifstream file(filename);
if (!file.is_open()) {
stringstream ss;
ss << "Could not open " << filename << "!" << endl;
throw runtime_error(ss.str());
}
stringstream buffer;
buffer << prepend << endl;
buffer << file.rdbuf();
string bufStr = buffer.str();
const char* bufCStr = bufStr.c_str();
GLint length = bufStr.length();
// Compile the shader
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &bufCStr, &length);
glCompileShader(shader);
// Make sure compilation succeeded
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
// Compilation failed, get the info log
GLint logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
vector<GLchar> logText(logLength);
glGetShaderInfoLog(shader, logLength, NULL, logText.data());
// Construct an error message with the compile log
stringstream ss;
string typeStr = "";
switch (type) {
case GL_VERTEX_SHADER:
typeStr = "vertex"; break;
case GL_FRAGMENT_SHADER:
typeStr = "fragment"; break;
}
ss << "Error compiling " + typeStr + " shader!" << endl << endl << logText.data() << endl;
// Cleanup shader and throw an exception
glDeleteShader(shader);
throw runtime_error(ss.str());
}
return shader;
}
GLuint linkProgram(vector<GLuint> shaders) {
GLuint program = glCreateProgram();
// Attach the shaders and link the program
for (auto it = shaders.begin(); it != shaders.end(); ++it)
glAttachShader(program, *it);
glLinkProgram(program);
// Detach shaders
for (auto it = shaders.begin(); it != shaders.end(); ++it)
glDetachShader(program, *it);
// Make sure link succeeded
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
// Link failed, get the info log
GLint logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
vector<GLchar> logText(logLength);
glGetProgramInfoLog(program, logLength, NULL, logText.data());
// Construct an error message with the compile log
stringstream ss;
ss << "Error linking program!" << endl << endl << logText.data() << endl;
// Cleanup program and throw an exception
glDeleteProgram(program);
throw runtime_error(ss.str());
}
return program;
}