forked from 15-466/15-466-f22-base4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_compile_program.cpp
62 lines (54 loc) · 2.2 KB
/
gl_compile_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
#include "gl_compile_program.hpp"
#include <vector>
#include <string>
#include <stdexcept>
#include <iostream>
static GLuint gl_compile_shader(GLenum type, std::string const &source) {
GLuint shader = glCreateShader(type);
GLchar const *str = source.c_str();
GLint str_length = GLint(source.size());
glShaderSource(shader, 1, &str, &str_length);
glCompileShader(shader);
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
std::cerr << "Failed to compile shader." << std::endl;
GLint info_log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetShaderInfoLog(shader, GLint(info_log.size()), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
glDeleteShader(shader);
throw std::runtime_error("Failed to compile shader.");
}
return shader;
}
GLuint gl_compile_program(
std::string const &vertex_shader_source,
std::string const &fragment_shader_source
) {
GLuint vertex_shader = gl_compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
GLuint fragment_shader = gl_compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
//shaders are reference counted so this makes sure they are freed after program is deleted:
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
//link the shader program and throw errors if linking fails:
glLinkProgram(program);
GLint link_status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &link_status);
if (link_status != GL_TRUE) {
std::cerr << "Failed to link shader program." << std::endl;
GLint info_log_length = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetProgramInfoLog(program, GLint(info_log.size()), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
throw std::runtime_error("failed to link program");
}
return program;
}