Last active
September 28, 2021 09:28
-
-
Save oxUnd/624a558f4f1b8dbeea9c1bce63251479 to your computer and use it in GitHub Desktop.
shader.h (base on glad)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author fansekey | |
*/ | |
#ifndef __GL_SHADER_H__ | |
#define __GL_SHADER_H__ | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <cstring> | |
#include <filesystem> | |
#include <vector> | |
#include <glad/glad.h> | |
//#define __ROOT__ "/GLSL" | |
#define SHADER_SOURCE_LENGTH 10240 | |
#define GLSL_ERROR_MESSAGE_LENGTH 512 | |
class Shader { | |
public: | |
Shader(){} | |
Shader(std::string vertexShaderFileName, std::string fragmentShaderFileName) { | |
std::filesystem::path root = std::filesystem::current_path(); | |
std::vector<std::string> vPathArr = { root, "src", "GLSL", vertexShaderFileName}; | |
std::vector<std::string> fPathArr = { root, "src", "GLSL", fragmentShaderFileName}; | |
std::string vertexShaderFileRealpath = shaderp(vPathArr), fragmentShaderFileRealpath = shaderp(fPathArr); | |
attach(vertexShaderFileRealpath, GL_VERTEX_SHADER); | |
attach(fragmentShaderFileRealpath, GL_FRAGMENT_SHADER); | |
link(); | |
} | |
void attach(std::string shaderPath, unsigned int type) { | |
char source[SHADER_SOURCE_LENGTH] = "\0"; | |
char *rSource = source; | |
loadSource(shaderPath, source); | |
unsigned int shaderID = glCreateShader(type); | |
glShaderSource(shaderID, 1, &rSource, 0); | |
glCompileShader(shaderID); | |
int ok = 0; | |
char errorMessage[GLSL_ERROR_MESSAGE_LENGTH]; | |
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &ok); | |
if (!ok) { | |
glGetShaderInfoLog(shaderID, GLSL_ERROR_MESSAGE_LENGTH, 0, errorMessage); | |
//@TODO | |
std::cout << "ERROR::SHADER::COMPILE_SHADER_FILE_FAILED" << " " << errorMessage << std::endl; | |
return; | |
} | |
shaderArray.push_back(shaderID); | |
} | |
void link() { | |
programID = glCreateProgram(); | |
for (int i = 0; i < shaderArray.size(); i++) { | |
glAttachShader(programID, shaderArray[i]); | |
glDeleteShader(shaderArray[i]); | |
} | |
glLinkProgram(programID); | |
int ok = 0; | |
char errorMessage[GLSL_ERROR_MESSAGE_LENGTH]; | |
glGetProgramiv(programID, GL_LINK_STATUS, &ok); | |
if (!ok) { | |
glGetProgramInfoLog(programID, GLSL_ERROR_MESSAGE_LENGTH, 0, errorMessage); | |
//@TODO | |
std::cout << "ERROR::SHADER::COMPILE_SHADER_FILE_FAILED" << " " << errorMessage << std::endl; | |
return; | |
} | |
} | |
void begin() { | |
glUseProgram(programID); | |
} | |
void end() { | |
glUseProgram(0); | |
} | |
private: | |
unsigned int programID; | |
std::vector<unsigned int> shaderArray; | |
void loadSource(std::string path, char *source) { | |
std::ifstream file; | |
file.exceptions(std::ifstream::failbit | std::ifstream::badbit); | |
try { | |
file.open(path.c_str()); | |
std::stringstream shaderSource; | |
shaderSource << file.rdbuf(); | |
strcpy(source, shaderSource.str().c_str()); | |
file.close(); | |
} catch(std::ifstream::failure e) { | |
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ " << e.what() << std::endl; | |
} | |
} | |
std::string join(std::vector<std::string> strings, const char * separator) { | |
std::string result(""); | |
for (std::size_t i = 0; i < strings.size(); i++) { | |
result.append(strings[i]); | |
if (i != strings.size() - 1) { | |
result.append(separator); | |
} | |
} | |
return result; | |
} | |
std::string shaderp(std::vector<std::string> strings) { | |
return join(strings, "/") + ".txt"; | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment