Initial shader class & restructure
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 2m38s

This commit is contained in:
2024-08-23 12:25:46 -04:00
parent 6650af4fc4
commit f3c2fd5e93
12 changed files with 180 additions and 11 deletions

View File

@@ -17,10 +17,10 @@
#include <Color3.hpp>
#include <Color4.hpp>
#include <Colors.hpp>
#include <JGL/Texture.h>
#include <JGL/enums.h>
#include <JGL/FontCache.h>
#include <JGL/Font.h>
#include <JGL/types/Texture.h>
#include <JGL/types/enums.h>
#include <JGL/types/FontCache.h>
#include <JGL/types/Font.h>
#include <J3ML/LinearAlgebra.hpp>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <J3ML/LinearAlgebra/Vector3.hpp>

View File

@@ -0,0 +1,40 @@
#pragma once
#include <J3ML/J3ML.hpp>
#include <glad/glad.h>
namespace JGL {
class Shader;
enum class ShaderType : u8 {
VERTEX = 0,
FRAGMENT = 1,
COMBINED = 2,
};
}
class JGL::Shader {
private:
///Shader program.
GLuint shader_program_handle = 0;
///Shader type.
ShaderType shader_type = ShaderType::COMBINED;
///Individual shaders which are compiled into the shader program.
GLuint vertex_shader = 0;
GLuint fragment_shader = 0;
private:
void load(const char* file_path, ShaderType type);
void load(const std::string& shader_program_text, ShaderType type);
void link();
public:
[[nodiscard]] GLuint GetGLShaderProgramHandle() const;
[[nodiscard]] ShaderType GetShaderType() const;
void Erase() const;
public:
Shader(const char* vertex_shader_file, const char* fragment_shader_file);
Shader(const char* shader_file, ShaderType type);
Shader(const std::string& vertex_shader_text, const std::string& fragment_shader_text);
Shader(const std::string& shader_text, ShaderType type);
~Shader();
};

View File

@@ -4,7 +4,7 @@
#include <Colors.hpp>
#include <chrono>
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <JGL/Font.h>
#include <JGL/types/Font.h>
#include <jlog/jlog.hpp>
#include <ReTexture/Texture.h>

View File

@@ -13,8 +13,8 @@
#include FT_OUTLINE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.h>
#include <JGL/types/Font.h>
#include <JGL/types/FontCache.h>
#include <jlog/jlog.hpp>
namespace JGL {

View File

@@ -16,8 +16,8 @@
#include FT_OUTLINE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.h>
#include <JGL/types/Font.h>
#include <JGL/types/FontCache.h>
namespace JGL::Detail
{

View File

@@ -1,4 +1,4 @@
#include <JGL/FontCache.h>
#include <JGL/types/FontCache.h>
using namespace JGL;

129
src/types/Shader.cpp Normal file
View File

@@ -0,0 +1,129 @@
#include <JGL/types/Shader.h>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
void checkCompilationError(GLuint shader) {
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar info[512];
glGetShaderInfoLog(shader, sizeof(info), nullptr, info);
std::cerr << info << std::endl;
exit(-1);
}
}
void checkLinkingError(GLuint shader) {
GLint success;
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
GLchar info[512];
glGetProgramInfoLog(shader, sizeof(info), nullptr, info);
std::cerr << info << std::endl;
exit(-1);
}
}
GLuint JGL::Shader::GetGLShaderProgramHandle() const {
if (shader_program_handle == 0)
throw std::runtime_error("JGL::Shader::GetGLShaderProgramHandle: Returned the default shader?");
return shader_program_handle;
}
JGL::ShaderType JGL::Shader::GetShaderType() const {
return shader_type;
}
void JGL::Shader::Erase() const {
if (shader_program_handle == 0)
throw std::runtime_error("JGL::Shader::Erase: Deleted the default shader?");
glDeleteProgram(shader_program_handle);
}
JGL::Shader::~Shader() {
Shader::Erase();
}
void JGL::Shader::load(const char* file_path, const JGL::ShaderType type) {
std::ifstream file(file_path);
if (!file.is_open())
throw std::runtime_error("JGL::Shader::load: File not found.");
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
std::string shaderSource = buffer.str();
load(shaderSource, type);
}
void JGL::Shader::load(const std::string& shader_program_text, const JGL::ShaderType type) {
const GLchar* source = shader_program_text.c_str();
if (type == ShaderType::VERTEX) {
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &source, nullptr);
glCompileShader(vertex_shader);
checkCompilationError(vertex_shader);
}
if (type == ShaderType::FRAGMENT) {
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &source, nullptr);
glCompileShader(fragment_shader);
checkCompilationError(fragment_shader);
}
}
void JGL::Shader::link() {
if (shader_program_handle != 0 || vertex_shader == 0 && fragment_shader == 0)
throw std::runtime_error("JGL::Shader::link: Linking a shader with no shader source?");
shader_program_handle = glCreateProgram();
if (vertex_shader != 0)
glAttachShader(shader_program_handle, vertex_shader);
if (fragment_shader != 0)
glAttachShader(shader_program_handle, fragment_shader);
glLinkProgram(shader_program_handle);
checkLinkingError(shader_program_handle);
}
JGL::Shader::Shader(const char* vertex_shader_file, const char* fragment_shader_file) {
shader_type = ShaderType::COMBINED;
load(vertex_shader_file, ShaderType::VERTEX);
load(fragment_shader_file, ShaderType::FRAGMENT);
link();
}
JGL::Shader::Shader(const char* shader_file, const JGL::ShaderType type) {
shader_type = type;
load(shader_file, type);
link();
}
JGL::Shader::Shader(const std::string& vertex_shader_text, const std::string& fragment_shader_text) {
shader_type = ShaderType::COMBINED;
load(vertex_shader_text, ShaderType::VERTEX);
load(fragment_shader_text, ShaderType::FRAGMENT);
link();
}
JGL::Shader::Shader(const std::string& shader_text, const JGL::ShaderType type) {
shader_type = type;
load(shader_text, type);
link();
}

View File

@@ -1,4 +1,4 @@
#include <JGL/Texture.h>
#include <JGL/types/Texture.h>
#include <iostream>
using namespace ReTexture;