GLuint compile const char path GLenum type char infoLog std ifstream s

 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
GLuint compile(const char *path, GLenum type, char *infoLog) {
std::ifstream source(path);
std::ostringstream oss;
oss << source.rdbuf();
std::string str = oss.str();
const char* charStr = str.c_str();
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &charStr, nullptr);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
cout << "ERROR::SHADER::" << (type == GL_VERTEX_SHADER ? "VERTEX" : "FRAGMENT");
cout << "::COMPILATION_FAILED\n" << infoLog << std::endl;
}
return shader;
}
GLuint createShader(const char *vertexSh, const char *fragmentSh) {
char infoLog[512];
GLuint vertexShader = compile(vertexSh, GL_VERTEX_SHADER, infoLog);
GLuint fragmentShader = compile(fragmentSh, GL_FRAGMENT_SHADER, infoLog);
GLuint id = glCreateProgram();
glAttachShader(id, vertexShader);
glAttachShader(id, fragmentShader);
glLinkProgram(id);
int success;
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(id, 512, nullptr, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return id;
}