2021-12-22 23:45:24 +00:00
|
|
|
#include <lol/Shader.hpp>
|
2021-12-22 23:25:50 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
#include <glad/glad.h>
|
|
|
|
|
|
|
|
#define IMPLEMENT_UNIFORM_FUNCTION(type, func) \
|
|
|
|
inline
|
|
|
|
|
2021-12-22 23:51:59 +00:00
|
|
|
namespace lol
|
2021-12-22 23:25:50 +00:00
|
|
|
{
|
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
Shader::Shader(const std::string& vertexShader, const std::string& fragmentShader) :
|
2021-12-22 23:51:59 +00:00
|
|
|
id(0)
|
2021-12-22 23:25:50 +00:00
|
|
|
{
|
2021-12-22 23:51:59 +00:00
|
|
|
GLint success;
|
|
|
|
GLchar infoLog[512];
|
|
|
|
|
|
|
|
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
const char* vertexShaderSource = vertexShader.c_str();
|
|
|
|
glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL);
|
|
|
|
glCompileShader(vertexShaderID);
|
|
|
|
|
|
|
|
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog);
|
|
|
|
std::cerr << "Vertex shader creation failed: \n" << infoLog << std::endl;
|
|
|
|
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
const char* fragmentShaderSource = fragmentShader.c_str();
|
|
|
|
glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, NULL);
|
|
|
|
glCompileShader(fragmentShaderID);
|
|
|
|
|
|
|
|
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &success);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
glGetShaderInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
|
|
|
std::cerr << "Fragment shader creation failed: \n" << infoLog << std::endl;
|
|
|
|
|
|
|
|
glDeleteShader(fragmentShaderID);
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = glCreateProgram();
|
|
|
|
glAttachShader(id, vertexShaderID);
|
|
|
|
glAttachShader(id, fragmentShaderID);
|
|
|
|
glLinkProgram(id);
|
|
|
|
|
|
|
|
glGetProgramiv(id, GL_LINK_STATUS, &success);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
glGetProgramInfoLog(fragmentShaderID, 512, NULL, infoLog);
|
|
|
|
std::cerr << "Shader program linking failed: \n" << infoLog << std::endl;
|
|
|
|
|
|
|
|
glDeleteShader(fragmentShaderID);
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
|
|
|
|
id = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-12-22 23:25:50 +00:00
|
|
|
|
2021-12-22 23:51:59 +00:00
|
|
|
glDeleteShader(fragmentShaderID);
|
2021-12-22 23:25:50 +00:00
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
}
|
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
Shader::~Shader()
|
2021-12-22 23:25:50 +00:00
|
|
|
{
|
2021-12-22 23:51:59 +00:00
|
|
|
glDeleteProgram(id);
|
2021-12-22 23:25:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
void Shader::Bind()
|
2021-12-22 23:25:50 +00:00
|
|
|
{
|
2021-12-22 23:51:59 +00:00
|
|
|
glUseProgram(id);
|
2021-12-22 23:25:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
void Shader::Unbind()
|
|
|
|
{
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
2021-12-25 14:01:08 +00:00
|
|
|
void Shader::SetUniform(const std::string& name, float value)
|
|
|
|
{
|
|
|
|
GLint location = glGetUniformLocation(id, name.c_str());
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glUniform1f(location, value);
|
|
|
|
}
|
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
void Shader::SetUniform(const std::string& name, const glm::mat4& value)
|
2021-12-22 23:51:59 +00:00
|
|
|
{
|
|
|
|
GLint location = glGetUniformLocation(id, name.c_str());
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
2021-12-22 23:25:50 +00:00
|
|
|
|
2021-12-22 23:51:59 +00:00
|
|
|
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
|
|
|
|
}
|
2021-12-22 23:25:50 +00:00
|
|
|
|
2021-12-24 13:20:53 +00:00
|
|
|
void Shader::SetUniform(const std::string& name, const glm::vec4& value)
|
2021-12-22 23:51:59 +00:00
|
|
|
{
|
|
|
|
GLint location = glGetUniformLocation(id, name.c_str());
|
|
|
|
if (location == -1)
|
|
|
|
return;
|
2021-12-22 23:25:50 +00:00
|
|
|
|
2021-12-22 23:51:59 +00:00
|
|
|
glUniform4fv(location, 1, glm::value_ptr(value));
|
|
|
|
}
|
2021-12-22 23:25:50 +00:00
|
|
|
|
|
|
|
}
|