-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cpp
70 lines (55 loc) · 1.74 KB
/
Program.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
#include "Program.h"
Program::Program()
{
program = glCreateProgram();
}
Program::~Program()
{
}
void Program::AddShader(const char* path, GLenum type)
{
// Opens file
const char* src = FileAsString(path);
// Creates and compiles shader from the file
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
// Length of debug message log
GLint log_length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_length);
// Appropriately sized buffer
GLchar infolog[log_length] = "";
glGetShaderInfoLog(shader, log_length + 1, nullptr, infolog);
// Prints if it's not empty
if (strcmp(infolog, "") != 0) cout << infolog << endl;
// Exits if it didn't compile
GLint params = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, ¶ms);
if (params != GL_TRUE) exit(glGetError());
// Adds the compiled shader ID
shaders.push_back(shader);
}
void Program::Link()
{
// Attaches all the shaders to the program
for (size_t i = 0; i < shaders.size(); i++)
glAttachShader(program, shaders[i]);
// Links the OpenGL shaders in the program
glLinkProgram(program);
// Gets the length of the debug log
GLint log_length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_length);
// Appropriately sized buffer
GLchar infolog[log_length] = "";
glGetProgramInfoLog(program, log_length + 1, nullptr, infolog);
// Prints if it's not empty
if (strcmp(infolog, "") != 0) cout << infolog << endl;
// Exits completely if linking failed
GLint param = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, ¶m);
if (param != GL_TRUE) exit(glGetError());
}
GLuint Program::GetID()
{
return program;
}