2021-05-27 19:54:05 +04:30
|
|
|
#include "ltpch.h"
|
|
|
|
#include "glShader.h"
|
|
|
|
|
2021-05-30 16:45:54 +04:30
|
|
|
#include <glad/glad.h>
|
2021-05-27 19:54:05 +04:30
|
|
|
|
|
|
|
namespace Light {
|
|
|
|
|
2021-05-31 19:09:27 +04:30
|
|
|
glShader::glShader(const std::string& vertexSource, const std::string& fragmentSource)
|
2021-05-27 19:54:05 +04:30
|
|
|
{
|
|
|
|
m_ShaderID = glCreateProgram();
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// create shaders
|
2021-05-27 19:54:05 +04:30
|
|
|
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
unsigned int pixelShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
2021-05-31 19:09:27 +04:30
|
|
|
// & (address of) needs an lvalue
|
|
|
|
const char* lVertexSource = vertexSource.c_str();
|
|
|
|
const char* lFragmentSource = fragmentSource.c_str();
|
2021-05-27 19:54:05 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// set shaders' sorce code
|
2021-05-31 19:09:27 +04:30
|
|
|
glShaderSource(vertexShader, 1, &lVertexSource, NULL);
|
|
|
|
glShaderSource(pixelShader, 1, &lFragmentSource, NULL);
|
2021-05-27 19:54:05 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// compile shaders
|
2021-05-27 19:54:05 +04:30
|
|
|
glCompileShader(vertexShader);
|
|
|
|
glCompileShader(pixelShader);
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
//* TEMP__ HANDLE SHADER COMPILE FAILURE __TEMP **//
|
2021-05-27 19:54:05 +04:30
|
|
|
int isCompiled = 0;
|
|
|
|
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
|
|
|
|
if (isCompiled == GL_FALSE)
|
|
|
|
{
|
|
|
|
GLint maxLength = 0;
|
|
|
|
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
std::vector<char> errorLog(maxLength);
|
|
|
|
glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &errorLog[0]);
|
|
|
|
|
2021-06-26 13:09:11 +04:30
|
|
|
for(int i = 0; i < errorLog.size() -1; i++)
|
|
|
|
std::cout << errorLog[i];
|
|
|
|
|
2021-05-27 19:54:05 +04:30
|
|
|
glDeleteShader(vertexShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
glGetShaderiv(pixelShader, GL_COMPILE_STATUS, &isCompiled);
|
|
|
|
if (isCompiled == GL_FALSE)
|
|
|
|
{
|
|
|
|
GLint maxLength = 0;
|
|
|
|
glGetShaderiv(pixelShader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
std::vector<char> errorLog(maxLength);
|
|
|
|
glGetShaderInfoLog(pixelShader, maxLength, &maxLength, &errorLog[0]);
|
|
|
|
|
|
|
|
glDeleteShader(pixelShader);
|
|
|
|
}
|
2021-06-19 15:12:42 +04:30
|
|
|
//* TEMP__ HANDLE SHADER COMPILE FAILURE __TEMP **//
|
2021-05-27 19:54:05 +04:30
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// attach and link shaders to the shader program
|
2021-05-27 19:54:05 +04:30
|
|
|
glAttachShader(m_ShaderID, vertexShader);
|
|
|
|
glAttachShader(m_ShaderID, pixelShader);
|
|
|
|
glLinkProgram(m_ShaderID);
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// delete shaders (free memory)
|
2021-05-27 19:54:05 +04:30
|
|
|
glDeleteShader(vertexShader);
|
|
|
|
glDeleteShader(pixelShader);
|
|
|
|
|
2021-06-19 15:12:42 +04:30
|
|
|
// #todo: validate program
|
2021-05-27 19:54:05 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
glShader::~glShader()
|
|
|
|
{
|
|
|
|
glDeleteProgram(m_ShaderID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void glShader::Bind()
|
|
|
|
{
|
|
|
|
glUseProgram(m_ShaderID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void glShader::UnBind()
|
|
|
|
{
|
|
|
|
glUseProgram(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|