first commit
This commit is contained in:
86
src/Shader.cpp
Normal file
86
src/Shader.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#include <print>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "glad/glad.h"
|
||||
|
||||
#include "Shader.hpp"
|
||||
#include "quit.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
Shader::Shader(std::filesystem::path& vertex_path, std::filesystem::path& fragment_path)
|
||||
: id{0} {
|
||||
|
||||
std::string vertex_code { read_entire_file(vertex_path) };
|
||||
std::string fragment_code { read_entire_file(fragment_path) };
|
||||
if (vertex_code == "" || fragment_code == "") {
|
||||
quit(1);
|
||||
}
|
||||
|
||||
const char* vertex_code_c_str { vertex_code.c_str() };
|
||||
const char* fragment_code_c_str { fragment_code.c_str() };
|
||||
|
||||
unsigned int vertex_shader { glCreateShader(GL_VERTEX_SHADER) };
|
||||
if (vertex_shader == 0) {
|
||||
std::println(stderr, "Failed to create vertex shader.");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glShaderSource(vertex_shader, 1, &vertex_code_c_str, nullptr);
|
||||
glCompileShader(vertex_shader);
|
||||
check_shader_compile_error(vertex_shader);
|
||||
|
||||
unsigned int fragment_shader { glCreateShader(GL_FRAGMENT_SHADER) };
|
||||
if (fragment_shader == 0) {
|
||||
std::println(stderr, "Failed to create fragment shader.");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glShaderSource(fragment_shader, 1, &fragment_code_c_str, nullptr);
|
||||
glCompileShader(fragment_shader);
|
||||
check_shader_compile_error(fragment_shader);
|
||||
|
||||
this->id = glCreateProgram();
|
||||
if (this->id == 0) {
|
||||
std::println(stderr, "Failed to create shader program");
|
||||
quit(1);
|
||||
}
|
||||
|
||||
glAttachShader(this->id, vertex_shader);
|
||||
glAttachShader(this->id, fragment_shader);
|
||||
|
||||
glLinkProgram(this->id);
|
||||
check_shader_program_link_error(this->id);
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
}
|
||||
|
||||
unsigned int Shader::get_id() const {
|
||||
return this->id;
|
||||
}
|
||||
|
||||
void Shader::use() const {
|
||||
glUseProgram(this->id);
|
||||
}
|
||||
|
||||
void Shader::set_mat4(const std::string_view& name, const glm::mat4& value) {
|
||||
const int uniform { glGetUniformLocation(this->id, name.data()) };
|
||||
if (uniform == -1) {
|
||||
std::println(stderr, "Could not find uniform '{}'.", name);
|
||||
quit(1);
|
||||
}
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void Shader::set_vec4(const std::string_view& name, const glm::vec4& value) {
|
||||
const int uniform { glGetUniformLocation(this->id, name.data()) };
|
||||
if (uniform == -1) {
|
||||
std::println(stderr, "Could not find uniform '{}'.", name);
|
||||
quit(1);
|
||||
}
|
||||
glUniform4fv(uniform, 1, glm::value_ptr(value));
|
||||
}
|
||||
Reference in New Issue
Block a user