Last active
April 27, 2019 14:34
-
-
Save alexsr/a97a260532455682ea4f12ec8b1f7913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename... Ts> | |
void attach_shaders(GLuint program, Ts... shaders) { | |
(glAttachShader(program, shaders), ...); | |
} | |
template <typename... Ts> | |
void detach_shaders(GLuint program, Ts... shaders) { | |
(glDetachShader(program, shaders), ...); | |
} | |
template <typename... Ts> | |
void delete_shaders(Ts... shaders) { | |
(glDeleteShader(shaders), ...); | |
} | |
void link_program(GLuint program) { | |
glLinkProgram(program); | |
GLint link_status = 0; | |
glGetProgramiv(program, GL_LINK_STATUS, &link_status); | |
if (link_status == GL_FALSE) { | |
GLint length = 0; | |
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length); | |
std::string error_log; | |
error_log.resize(static_cast<unsigned long>(length)); | |
glGetProgramInfoLog(program, length, &length, &error_log[0]); | |
glDeleteProgram(program); | |
throw std::runtime_error{ | |
"Error while linking shader program " + std::to_string(program) + "\n" | |
+ "Error log: \n" | |
+ error_log | |
}; | |
} | |
} | |
template <typename... Args> | |
GLuint create_program(Args&&... shaders) { | |
const GLuint program = glCreateProgram(); | |
attach_shaders(program, shaders...); | |
link_program(program); | |
detach_shaders(program, shaders...); | |
delete_shaders(shaders...); | |
return program; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment