#include "Shader.h" #include "Exception.h" namespace charcoal { Shader::Shader(const std::string& source, ShaderType type) { GLenum gl_shader_type; switch (type) { case VERTEX_SHADER: gl_shader_type = GL_VERTEX_SHADER; break; case FRAGMENT_SHADER: gl_shader_type = GL_FRAGMENT_SHADER; break; default: throw "Invalid Shader Type Specified"; } m_shader = glCreateShader(gl_shader_type); const char* c_str = source.c_str(); glShaderSource(m_shader, 1, &c_str, NULL); glCompileShader(m_shader); // Make sure that the shader has been compiled successfully GLint compiled; glGetShaderiv(m_shader, GL_COMPILE_STATUS, &compiled); if (compiled != GL_TRUE) { GLsizei log_length = 0; GLchar message[1024]; glGetShaderInfoLog(m_shader, 1023, &log_length, message); message[log_length] = '\0'; // Add null terminator OutputDebugString("Error Compiling Shader:\n"); OutputDebugString(message); OutputDebugString("\nSource:\n"); OutputDebugString(source.c_str()); OutputDebugString("\n"); m_shader = 0; throw EXCEPTION("Error compiling shader."); } } Shader::~Shader() {} GLuint Shader::get_shader() const { return m_shader; } }