106 lines
2.7 KiB
C++
106 lines
2.7 KiB
C++
#include "Shader.h"
|
|
|
|
Shader::Shader(const std::string& vPath, const std::string& fPath)
|
|
{
|
|
std::string vCode;
|
|
std::string fCode;
|
|
std::ifstream vShaderFile;
|
|
std::ifstream fShaderFile;
|
|
|
|
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
|
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
|
try
|
|
{
|
|
vShaderFile.open(vPath);
|
|
fShaderFile.open(fPath);
|
|
std::stringstream vShaderStream, fShaderStream;
|
|
|
|
vShaderStream << vShaderFile.rdbuf();
|
|
fShaderStream << fShaderFile.rdbuf();
|
|
|
|
vShaderFile.close();
|
|
fShaderFile.close();
|
|
|
|
vCode = vShaderStream.str();
|
|
fCode = fShaderStream.str();
|
|
}
|
|
catch(std::ifstream::failure& e)
|
|
{
|
|
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ::" << e.what() << std::endl;
|
|
std::cerr << "VPATH::" << vPath << std::endl << "FPATH::" << fPath << std::endl;
|
|
}
|
|
const char* vShaderCode = vCode.c_str();
|
|
const char* fShaderCode = fCode.c_str();
|
|
|
|
unsigned int vertex, fragment;
|
|
int success;
|
|
char infoLog[512];
|
|
|
|
vertex = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(vertex, 1, &vShaderCode, nullptr);
|
|
glCompileShader(vertex);
|
|
|
|
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
|
|
if(!success)
|
|
{
|
|
glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
|
|
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
|
};
|
|
|
|
fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(fragment, 1, &fShaderCode, nullptr);
|
|
glCompileShader(fragment);
|
|
|
|
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
|
|
if(!success)
|
|
{
|
|
glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
|
|
std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
|
};
|
|
|
|
m_ID = glCreateProgram();
|
|
glAttachShader(m_ID, vertex);
|
|
glAttachShader(m_ID, fragment);
|
|
glLinkProgram(m_ID);
|
|
|
|
glGetProgramiv(m_ID, GL_LINK_STATUS, &success);
|
|
if(!success)
|
|
{
|
|
glGetProgramInfoLog(m_ID, 512, nullptr, infoLog);
|
|
std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
|
}
|
|
|
|
glDeleteShader(vertex);
|
|
glDeleteShader(fragment);
|
|
}
|
|
|
|
Shader::~Shader()
|
|
{
|
|
glDeleteProgram(m_ID);
|
|
}
|
|
|
|
Shader Shader::Create(const std::string& vPath, const std::string& fPath)
|
|
{
|
|
return Shader{vPath, fPath};
|
|
}
|
|
|
|
void Shader::Use() const
|
|
{
|
|
glUseProgram(m_ID);
|
|
}
|
|
|
|
void Shader::Shutdown() const
|
|
{
|
|
glDeleteProgram(m_ID);
|
|
}
|
|
|
|
int Shader::GetUniformLocation(const std::string& name)
|
|
{
|
|
if(m_Uniforms.find(name) == m_Uniforms.end())
|
|
{
|
|
m_Uniforms[name] = glGetUniformLocation(m_ID, name.c_str());
|
|
}
|
|
|
|
return m_Uniforms.at(name);
|
|
}
|