mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
feat: add shader class
This commit is contained in:
80
src/shader.cpp
Normal file
80
src/shader.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <Cubed/shader.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/cubed_hash.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/shader_tools.hpp>
|
||||
|
||||
Shader::Shader() {
|
||||
|
||||
}
|
||||
|
||||
Shader::Shader(const std::string& name, const std::string& v_shader_path, const std::string& f_shader_path) {
|
||||
m_program = Tools::create_shader_program(v_shader_path, f_shader_path);
|
||||
m_name = name;
|
||||
m_hash = HASH::str(name);
|
||||
}
|
||||
|
||||
Shader::Shader(Shader&& shader) :
|
||||
m_hash(shader.m_hash),
|
||||
m_program(shader.m_program),
|
||||
m_name(std::move(shader.m_name))
|
||||
{
|
||||
shader.m_hash = 0;
|
||||
shader.m_program = 0;
|
||||
}
|
||||
|
||||
Shader::~Shader() {
|
||||
if (m_program) {
|
||||
glDeleteProgram(m_program);
|
||||
}
|
||||
}
|
||||
|
||||
Shader& Shader::operator=(Shader&& shader) {
|
||||
m_hash = shader.m_hash;
|
||||
m_name = std::move(shader.m_name);
|
||||
m_program = shader.m_program;
|
||||
shader.m_hash = 0;
|
||||
shader.m_program = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Shader::create(const std::string& name, const std::string& v_shader_path, const std::string& f_shader_path) {
|
||||
if (!m_program) {
|
||||
LOG::warn("Shader has already created !");
|
||||
return;
|
||||
}
|
||||
m_program = Tools::create_shader_program(v_shader_path, f_shader_path);
|
||||
m_name = name;
|
||||
m_hash = HASH::str(name);
|
||||
}
|
||||
|
||||
std::size_t Shader::hash() const {
|
||||
if (!m_hash) {
|
||||
LOG::warn("Shader has already created !");
|
||||
return 0;
|
||||
}
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
GLuint Shader::loc(const std::string& loc) const {
|
||||
CUBED_ASSERT_MSG(m_program != 0, "Shader program not created");
|
||||
GLint pos = glGetUniformLocation(m_program, loc.c_str());
|
||||
if (pos == -1) {
|
||||
LOG::info("Shader name {}, loc name {}, pos {}", m_name, loc, pos);
|
||||
CUBED_ASSERT_MSG(pos == -1, "Can't find UniformLocation");
|
||||
}
|
||||
return static_cast<GLuint>(pos);
|
||||
}
|
||||
|
||||
const std::string& Shader::name() const {
|
||||
if (m_name == "-1") {
|
||||
LOG::warn("Shader has already created !");
|
||||
|
||||
}
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void Shader::use() const{
|
||||
CUBED_ASSERT_MSG(m_program, "Shader don't create !");
|
||||
glUseProgram(m_program);
|
||||
}
|
||||
Reference in New Issue
Block a user