49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#include "Shader.hpp"
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
|
|
using namespace pass;
|
|
|
|
Shader::Shader(GLenum type, const std::string& file_path, const std::vector<std::string>& flags) {
|
|
ShaderID = glCreateShader(type);
|
|
|
|
// Load code
|
|
std::string code;
|
|
std::ifstream CodeStream(file_path, std::ios::in);
|
|
CodeStream.exceptions(CodeStream.failbit);
|
|
|
|
std::stringstream sstr;
|
|
std::string line;
|
|
std::getline(CodeStream, line);
|
|
sstr << line << std::endl; // version
|
|
// Add flags
|
|
for(auto flag : flags) {
|
|
sstr << "#define " << flag << " 1" << std::endl;
|
|
}
|
|
sstr << CodeStream.rdbuf();
|
|
CodeStream.close();
|
|
code = sstr.str();
|
|
|
|
// Compile Shader
|
|
char const *codePtr = code.c_str();
|
|
glShaderSource(ShaderID, 1, &codePtr, NULL);
|
|
glCompileShader(ShaderID);
|
|
|
|
// Check Shader
|
|
GLint result = GL_FALSE;
|
|
int infoLogLength;
|
|
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &result);
|
|
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
|
|
if (infoLogLength > 0) {
|
|
std::vector<char> errorMessage(infoLogLength + 1);
|
|
glGetShaderInfoLog(ShaderID, infoLogLength, NULL, &errorMessage[0]);
|
|
std::cerr << "Compiling shader : " << file_path << "\n";
|
|
throw std::runtime_error(&errorMessage[0]);
|
|
}
|
|
}
|
|
Shader::~Shader() {
|
|
glDeleteShader(ShaderID);
|
|
}
|