mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 12:02:47 -05:00
load shaders from files.
This commit is contained in:
@@ -1,10 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <cerrno>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include <sstream>
|
||||
class gl_error : public std::domain_error {
|
||||
public:
|
||||
explicit gl_error(const std::string &arg) : domain_error(arg) {}
|
||||
explicit gl_error(const char *string) : domain_error(string) {}
|
||||
};
|
||||
|
||||
class shader_error : public gl_error {
|
||||
public:
|
||||
explicit shader_error(const std::string &arg) : gl_error(arg) {}
|
||||
explicit shader_error(const char *string) : gl_error(string) {}
|
||||
};
|
||||
|
||||
class program_error : public gl_error {
|
||||
public:
|
||||
explicit program_error(const std::string &arg) : gl_error(arg) {}
|
||||
explicit program_error(const char *string) : gl_error(string) {}
|
||||
};
|
||||
|
||||
void utilShaderSource(GLuint shader, const std::vector<std::string> &sources) {
|
||||
char const *ptrs[sources.size()];
|
||||
@@ -39,4 +59,47 @@ std::string utilInfo() {
|
||||
<< " OpenGL version: " << glGetString(GL_VERSION) << std::endl
|
||||
<< " Shading version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string utilReadFile(const std::string &filename) {
|
||||
std::ifstream in(filename, std::ios::in | std::ios::binary);
|
||||
if (in) {
|
||||
std::ostringstream contents;
|
||||
contents << in.rdbuf();
|
||||
in.close();
|
||||
return (contents.str());
|
||||
}
|
||||
throw std::system_error(errno, std::generic_category());
|
||||
}
|
||||
|
||||
GLuint utilCompileFiles(const GLenum type, const std::vector<std::string> &files) {
|
||||
std::vector<std::string> sources;
|
||||
sources.reserve(files.size());
|
||||
for (const auto &file : files) {
|
||||
sources.push_back(utilReadFile(file));
|
||||
}
|
||||
|
||||
GLuint shader = glCreateShader(type);
|
||||
utilShaderSource(shader, sources);
|
||||
glCompileShader(shader);
|
||||
|
||||
int success;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (success) return shader;
|
||||
|
||||
throw shader_error(utilShaderInfoLog(shader));
|
||||
}
|
||||
|
||||
GLuint utilLinkProgram(const std::vector<GLuint> &shaders) {
|
||||
GLuint program = glCreateProgram();
|
||||
for (const auto &shader : shaders) {
|
||||
glAttachShader(program, shader);
|
||||
}
|
||||
glLinkProgram(program);
|
||||
|
||||
int success;
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &success);
|
||||
if (success)return program;
|
||||
|
||||
throw program_error(utilProgramInfoLog(program));
|
||||
}
|
||||
Reference in New Issue
Block a user