charcoal/OpenGLEngine/Shader.cpp

54 lines
1.2 KiB
C++
Raw Normal View History

2018-09-05 06:49:02 +00:00
#include "Shader.h"
#include "Exception.h"
2018-09-12 21:03:46 +00:00
namespace charcoal
2018-09-05 06:49:02 +00:00
{
2018-09-12 21:03:46 +00:00
Shader::Shader(const std::string& source, ShaderType type)
2018-09-05 06:49:02 +00:00
{
2018-09-12 21:03:46 +00:00
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
2018-09-12 21:03:46 +00:00
OutputDebugString("Error Compiling Shader:\n");
OutputDebugString(message);
2018-09-12 21:03:46 +00:00
OutputDebugString("\nSource:\n");
OutputDebugString(source.c_str());
OutputDebugString("\n");
m_shader = 0;
throw EXCEPTION("Error compiling shader.");
}
2018-09-05 06:49:02 +00:00
}
2018-09-12 21:03:46 +00:00
Shader::~Shader() {}
2018-09-05 06:49:02 +00:00
2018-09-12 21:03:46 +00:00
GLuint Shader::get_shader() const
2018-09-05 06:49:02 +00:00
{
2018-09-12 21:03:46 +00:00
return m_shader;
2018-09-05 06:49:02 +00:00
}
2018-09-12 21:03:46 +00:00
}