std::stringGLuint LoadFileLoadShaders(const std::string const&vector<ShaderInfo>& nameshader_info) {
GLuint program {glCreateProgram()};
std::ifstreamvector<GLuint> file;detach;
file.exceptionsfor (std:auto info :ios_base shader_info) {
std::failbitifstream |file {info.file, std::ios_base::badbit)in};
file.open(name);
file.seekg(0, std::ios_base::end);
std::string contentsource(file.tellg(), '\0');
file.seekg(0, std::ios_base::beg);
file.read(&content[0], content.size());
file.closeread();
return content;
}
GLuint&source[0], LoadShaderssource.size(std::vector<ShaderInfo> const& shader_info) {
GLuint program {glCreateProgram()};
if (!program)
throw std::runtime_errorfile.close("cannot acquire id for shader program");
std::vector<GLuint> detach;
std::string log;
for (auto const& info : shader_info) {
GLuint shader {glCreateShader(info.type)};
if (glGetError()GLchar* ==source_ptr GL_INVALID_ENUM){&source[0]};
log += "[" + info.file + "] invalid glShaderSource(shader type\n";
, else1, if&source_ptr, (!shadernullptr);
log += "[" + info.file + "] cannot acquire id\n";
else try {glCompileShader(shader);
glAttachShader(program, shader);
glDeleteShader(shader);
detach.push_back(shader);
std::string source {LoadFile(info.file)};
GLchar* source_ptr(&source[0]);
glShaderSource(shader, 1, &source_ptr, nullptr);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
log += "[" + info.file + "]\n";
auto offset {log.size()};
std::string log.resize(offset + length, '\0');
glGetShaderInfoLog(shader, length, nullptr, &log[offset]&log[0]);
log.resize(log.sizeglDeleteProgram() - 1program);
}
}
catchthrow (std::ios_base::failure const&) {
runtime_error(log += "[" + info.file + "] cannot process file\n";);
}
}
if (log.size()) {
glDeleteProgramglLinkProgram(program);
for (auto throwshader std::runtime_error(log detach);
}
glLinkProgramglDetachShader(program, shader);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
GLint length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
std::string log.resize(length, '\0');
glGetProgramInfoLog(program, length, nullptr, &log[0]);
glDeleteProgram(program);
throw std::runtime_error(log);
}
for (auto shader : detach)
glDetachShader(program, shader);
return program;
}
`````
EDIT 1: Incorporated user673679 suggestions.
- Split reading source file into another function.
- Error on failure opening a file.
- Combine logs from multiple shader objects compilation attempts.
- Plus added some extra OpenGL error checks.
I tried my best to make the code not too messy to read.