Shader loading error text fix

This commit is contained in:
MihailRis 2023-12-02 17:53:53 +03:00
parent 8c261efbd4
commit 54178eafc1
4 changed files with 50 additions and 58 deletions

View File

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
#include "Assets.h" #include "Assets.h"
#include "../files/files.h"
#include "../coders/png.h" #include "../coders/png.h"
#include "../graphics/Shader.h" #include "../graphics/Shader.h"
#include "../graphics/Texture.h" #include "../graphics/Texture.h"
@ -17,8 +18,8 @@ using std::filesystem::path;
namespace fs = std::filesystem; namespace fs = std::filesystem;
bool assetload::texture(Assets* assets, bool assetload::texture(Assets* assets,
const path& filename, const path filename,
const string& name) { const string name) {
Texture* texture = png::load_texture(filename.string()); Texture* texture = png::load_texture(filename.string());
if (texture == nullptr) { if (texture == nullptr) {
std::cerr << "failed to load texture '" << name << "'" << std::endl; std::cerr << "failed to load texture '" << name << "'" << std::endl;
@ -29,10 +30,18 @@ bool assetload::texture(Assets* assets,
} }
bool assetload::shader(Assets* assets, bool assetload::shader(Assets* assets,
const path& filename, const path filename,
const string& name) { const string& name) {
Shader* shader = Shader::loadShader(filename.string() + ".glslv", path vertexFile = filename;
filename.string() + ".glslf"); path fragmentFile = filename;
vertexFile.append(".glslv");
fragmentFile.append(".glslf");
string vertexSource = files::read_string(vertexFile);
string fragmentSource = files::read_string(fragmentFile);
Shader* shader = Shader::loadShader(vertexFile, fragmentFile,
vertexSource, fragmentSource);
if (shader == nullptr) { if (shader == nullptr) {
std::cerr << "failed to load shader '" << name << "'" << std::endl; std::cerr << "failed to load shader '" << name << "'" << std::endl;
return false; return false;
@ -42,8 +51,8 @@ bool assetload::shader(Assets* assets,
} }
bool assetload::atlas(Assets* assets, bool assetload::atlas(Assets* assets,
const path& directory, const path directory,
const string& name) { const string name) {
AtlasBuilder builder; AtlasBuilder builder;
for (const auto& entry : fs::directory_iterator(directory)) { for (const auto& entry : fs::directory_iterator(directory)) {
path file = entry.path(); path file = entry.path();
@ -60,8 +69,8 @@ bool assetload::atlas(Assets* assets,
} }
bool assetload::font(Assets* assets, bool assetload::font(Assets* assets,
const path& filename, const path filename,
const string& name) { const string name) {
vector<Texture*> pages; vector<Texture*> pages;
for (size_t i = 0; i <= 4; i++) { for (size_t i = 0; i <= 4; i++) {
string name = filename.string() + "_" + std::to_string(i) + ".png"; string name = filename.string() + "_" + std::to_string(i) + ".png";

View File

@ -8,17 +8,17 @@ class Assets;
namespace assetload { namespace assetload {
bool texture(Assets* assets, bool texture(Assets* assets,
const std::filesystem::path& filename, const std::filesystem::path filename,
const std::string& name); const std::string name);
bool shader(Assets* assets, bool shader(Assets* assets,
const std::filesystem::path& filename, const std::filesystem::path filename,
const std::string& name); const std::string name);
bool atlas(Assets* assets, bool atlas(Assets* assets,
const std::filesystem::path& directory, const std::filesystem::path directory,
const std::string& name); const std::string name);
bool font(Assets* assets, bool font(Assets* assets,
const std::filesystem::path& filename, const std::filesystem::path filename,
const std::string& name); const std::string name);
} }
#endif // ASSETS_ASSET_LOADERS_H_ #endif // ASSETS_ASSET_LOADERS_H_

View File

@ -12,6 +12,11 @@
#include "../coders/GLSLExtension.h" #include "../coders/GLSLExtension.h"
using std::cerr;
using std::endl;
using glm::vec2;
using glm::vec3;
using std::string;
using std::filesystem::path; using std::filesystem::path;
GLSLExtension* Shader::preprocessor = new GLSLExtension(); GLSLExtension* Shader::preprocessor = new GLSLExtension();
@ -27,69 +32,46 @@ void Shader::use(){
glUseProgram(id); glUseProgram(id);
} }
void Shader::uniformMatrix(std::string name, glm::mat4 matrix){ void Shader::uniformMatrix(string name, glm::mat4 matrix){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(matrix)); glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(matrix));
} }
void Shader::uniform1i(std::string name, int x){ void Shader::uniform1i(string name, int x){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform1i(transformLoc, x); glUniform1i(transformLoc, x);
} }
void Shader::uniform1f(std::string name, float x){ void Shader::uniform1f(string name, float x){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform1f(transformLoc, x); glUniform1f(transformLoc, x);
} }
void Shader::uniform2f(std::string name, float x, float y){ void Shader::uniform2f(string name, float x, float y){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform2f(transformLoc, x, y); glUniform2f(transformLoc, x, y);
} }
void Shader::uniform2f(std::string name, glm::vec2 xy){ void Shader::uniform2f(string name, vec2 xy){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform2f(transformLoc, xy.x, xy.y); glUniform2f(transformLoc, xy.x, xy.y);
} }
void Shader::uniform3f(std::string name, float x, float y, float z){ void Shader::uniform3f(string name, float x, float y, float z){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform3f(transformLoc, x,y,z); glUniform3f(transformLoc, x,y,z);
} }
void Shader::uniform3f(std::string name, glm::vec3 xyz){ void Shader::uniform3f(string name, vec3 xyz){
GLuint transformLoc = glGetUniformLocation(id, name.c_str()); GLuint transformLoc = glGetUniformLocation(id, name.c_str());
glUniform3f(transformLoc, xyz.x, xyz.y, xyz.z); glUniform3f(transformLoc, xyz.x, xyz.y, xyz.z);
} }
Shader* Shader::loadShader(std::string vertexFile, std::string fragmentFile) { Shader* Shader::loadShader(string vertexFile,
// Reading Files string fragmentFile,
std::string vertexCode; string vertexCode,
std::string fragmentCode; string fragmentCode) {
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions(std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::badbit);
try {
vShaderFile.open(vertexFile);
fShaderFile.open(fragmentFile);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch(std::ifstream::failure& e) {
std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
return nullptr;
}
vertexCode = preprocessor->process(path(vertexFile), vertexCode); vertexCode = preprocessor->process(path(vertexFile), vertexCode);
fragmentCode = preprocessor->process(path(fragmentFile), fragmentCode); fragmentCode = preprocessor->process(path(fragmentFile), fragmentCode);
@ -107,8 +89,8 @@ Shader* Shader::loadShader(std::string vertexFile, std::string fragmentFile) {
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success){ if (!success){
glGetShaderInfoLog(vertex, 512, nullptr, infoLog); glGetShaderInfoLog(vertex, 512, nullptr, infoLog);
std::cerr << "SHADER::VERTEX: compilation failed: " << vertexFile << std::endl; cerr << "SHADER::VERTEX: compilation failed: " << vertexFile << endl;
std::cerr << infoLog << std::endl; cerr << infoLog << endl;
return nullptr; return nullptr;
} }
@ -119,8 +101,8 @@ Shader* Shader::loadShader(std::string vertexFile, std::string fragmentFile) {
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success){ if (!success){
glGetShaderInfoLog(fragment, 512, nullptr, infoLog); glGetShaderInfoLog(fragment, 512, nullptr, infoLog);
std::cerr << "SHADER::FRAGMENT: compilation failed: " << vertexFile << std::endl; cerr << "SHADER::FRAGMENT: compilation failed: " << vertexFile << endl;
std::cerr << infoLog << std::endl; cerr << infoLog << endl;
return nullptr; return nullptr;
} }
@ -133,8 +115,8 @@ Shader* Shader::loadShader(std::string vertexFile, std::string fragmentFile) {
glGetProgramiv(id, GL_LINK_STATUS, &success); glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success){ if (!success){
glGetProgramInfoLog(id, 512, nullptr, infoLog); glGetProgramInfoLog(id, 512, nullptr, infoLog);
std::cerr << "SHADER::PROGRAM: linking failed" << std::endl; cerr << "SHADER::PROGRAM: linking failed" << endl;
std::cerr << infoLog << std::endl; cerr << infoLog << endl;
glDeleteShader(vertex); glDeleteShader(vertex);
glDeleteShader(fragment); glDeleteShader(fragment);

View File

@ -23,7 +23,8 @@ public:
void uniform3f(std::string name, float x, float y, float z); void uniform3f(std::string name, float x, float y, float z);
void uniform3f(std::string name, glm::vec3 xyz); void uniform3f(std::string name, glm::vec3 xyz);
static Shader* loadShader(std::string vertexFile, std::string fragmentFile); static Shader* loadShader(std::string vertexFile, std::string fragmentFile,
std::string vertexSource, std::string fragmentSource);
}; };
#endif /* GRAPHICS_SHADER_H_ */ #endif /* GRAPHICS_SHADER_H_ */