charcoal/OpenGLEngine/Shader.cpp

54 lines
1.1 KiB
C++
Raw Normal View History

2018-09-05 06:49:02 +00:00
#include "Shader.h"
#include "Exception.h"
Shader::Shader(const std::string& source, ShaderType type)
2018-09-05 06:49:02 +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);
2018-09-05 06:49:02 +00:00
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");
2018-09-05 06:49:02 +00:00
m_shader = 0;
throw EXCEPTION("Error compiling shader.");
2018-09-05 06:49:02 +00:00
}
}
Shader::~Shader()
{
}
GLuint Shader::get_shader() const
{
return m_shader;
}