charcoal/OpenGLEngine/Shader.cpp
2018-09-12 17:03:46 -04:00

54 lines
1.2 KiB
C++

#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)
{
char buffer[1024];
GLsizei log_length;
glGetShaderInfoLog(m_shader, 1023, &log_length, buffer);
buffer[log_length] = '\0'; // Add null terminator
OutputDebugString("Error Compiling Shader:\n");
OutputDebugString(buffer);
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;
}
}