25ShaderProgram::load_shader_file (GLuint& shader_id, GLuint shader_type,
26 std::string
const& filename)
28 std::string shader_code;
33 this->load_shader_code(shader_id, shader_type, shader_code);
42ShaderProgram::load_shader_code (GLuint& shader_id, GLuint shader_type,
43 std::string
const& code)
47 shader_id = glCreateShader(shader_type);
50 glAttachShader(this->prog_id, shader_id);
54 this->compile_shader(shader_id, code);
55 this->need_to_link =
true;
61ShaderProgram::unload_shader (GLuint& shader_id)
65 glDetachShader(this->prog_id, shader_id);
67 glDeleteShader(shader_id);
76ShaderProgram::compile_shader (GLuint shader_id, std::string
const& code)
79 char const* data[1] = { code.c_str() };
80 glShaderSource(shader_id, 1, data,
nullptr);
84 glCompileShader(shader_id);
86 if (this->get_shader_property(shader_id, GL_COMPILE_STATUS) == GL_FALSE)
88 GLint log_size = this->get_shader_property(shader_id, GL_INFO_LOG_LENGTH);
93 log.append(log_size + 1,
'\0');
94 glGetShaderInfoLog(shader_id, log_size + 1,
nullptr, &log[0]);
100ShaderProgram::ensure_linked (
void)
102 if (this->need_to_link)
104 glLinkProgram(this->prog_id);
106 if (this->get_program_property(GL_LINK_STATUS) == GL_FALSE)
108 GLint log_size = this->get_program_property(GL_INFO_LOG_LENGTH);
113 log.append(log_size + 1,
'\0');
114 glGetProgramInfoLog(this->prog_id, log_size + 1,
nullptr, &log[0]);
117 this->need_to_link =
false;
124ShaderProgram::try_load_all (std::string
const& basename)
126 std::string vert_filename = basename +
".vert";
127 std::string geom_filename = basename +
".geom";
128 std::string frag_filename = basename +
".frag";
133 std::cerr <<
"Skipping shaders from " << basename <<
".*" << std::endl;
137 std::cerr <<
"Loading shaders from " << basename <<
".*" << std::endl;
139 this->load_vert_file(vert_filename);
142 this->load_geom_file(geom_filename);
144 this->load_frag_file(frag_filename);
Universal, simple exception class.
bool file_exists(char const *pathname)
Determines if the given path is a file.
void read_file_to_string(std::string const &filename, std::string *data)
Reads the whole file into a string.
#define OGL_NAMESPACE_END
#define OGL_NAMESPACE_BEGIN