Created
March 2, 2015 19:31
-
-
Save feliwir/9db29a12efc003aa0254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Shader::LoadFromFile(const Type type, const std::string & filename) | |
{ | |
std::ifstream fin(filename, std::ios::in); | |
if (fin.fail()) | |
SAGELogCrash("Failed to open file " + filename); | |
fin.seekg(0, std::ios::end); | |
const uint32_t size = static_cast<uint32_t>(fin.tellg()); | |
fin.seekg(0, std::ios::beg); | |
char* buffer = new char[size]; | |
std::memset(buffer, 0, size); | |
fin.read(buffer, size); | |
LoadFromMemory(type, buffer); | |
} | |
void Shader::LoadFromMemory(const Type type, const std::string & content) | |
{ | |
if (m_shaders[type] != 0) | |
SAGELogCrash("Shader of type " + std::to_string(type) + " already defined in this program!"); | |
switch (type) | |
{ | |
case VERTEX_SHADER: | |
m_shaders[type] = glCreateShader(GL_VERTEX_SHADER); | |
break; | |
case CONTROL_SHADER: | |
m_shaders[type] = glCreateShader(GL_TESS_CONTROL_SHADER); | |
break; | |
case EVALUATION_SHADER: | |
m_shaders[type] = glCreateShader(GL_TESS_EVALUATION_SHADER); | |
break; | |
case GEOMETRY_SHADER: | |
m_shaders[type] = glCreateShader(GL_GEOMETRY_SHADER); | |
break; | |
case FRAGMENT_SHADER: | |
m_shaders[type] = glCreateShader(GL_FRAGMENT_SHADER); | |
break; | |
default: | |
SAGELogCrash("Invalid shader type: "+ std::to_string(type)); | |
} | |
const GLchar* src = reinterpret_cast<const GLchar*>(content.c_str()); | |
const GLint size = content.size(); | |
glShaderSource(m_shaders[type], 1, &src,&size); | |
glCompileShader(m_shaders[type]); | |
GLint status = 0; | |
glGetShaderiv(m_shaders[type], GL_COMPILE_STATUS, &status); | |
if (status == GL_FALSE) | |
{ | |
GLint logSize = 0; | |
glGetShaderiv(m_shaders[type], GL_INFO_LOG_LENGTH, &logSize); | |
std::vector<GLchar> compileLog(logSize); | |
glGetShaderInfoLog(m_shaders[type], logSize, &logSize, &compileLog[0]); | |
SAGELogCrash("Failed to compile shader:\n" + std::string(&compileLog[0])); | |
} | |
glAttachShader(m_program, m_shaders[type]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment