reader shaders as assets

This commit is contained in:
devZoGok
2024-09-08 14:23:23 +03:00
parent 37b791342a
commit 1f5d1bc94d
8 changed files with 122 additions and 84 deletions
+2 -2
View File
@@ -18,8 +18,8 @@ set(MATH quaternion.cpp vector.cpp matrix.cpp rayCaster.cpp)
set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp meshData.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp) set(RENDER lineRenderer.cpp particleEmitter.cpp camera.cpp light.cpp material.cpp mesh.cpp meshData.cpp model.cpp node.cpp quad.cpp box.cpp root.cpp shader.cpp texture.cpp)
set(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp) set(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp)
set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp) set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp)
set(ASSET_MANAGER assetManager.cpp textAsset.h imageAsset.h modelAsset.h fontAsset.h) set(ASSET_MANAGER assetManager.cpp shaderAsset.h textAsset.h imageAsset.h modelAsset.h fontAsset.h)
set(ASSET_READERS imageReader.cpp fontReader.cpp modelReader.cpp xmlModelReader.cpp) set(ASSET_READERS shaderReader.cpp imageReader.cpp fontReader.cpp modelReader.cpp xmlModelReader.cpp)
if(BUILD_WITH_ASSIMP) if(BUILD_WITH_ASSIMP)
set(ASSET_READERS ${ASSET_READERS} assimpModelReader.cpp) set(ASSET_READERS ${ASSET_READERS} assimpModelReader.cpp)
+34
View File
@@ -1,8 +1,13 @@
#include "assetManager.h" #include "assetManager.h"
#include "abstractAssetReader.h" #include "abstractAssetReader.h"
#include "util.h" #include "util.h"
#include "root.h"
#include "node.h"
#include "mesh.h"
#include "material.h"
#include "imageReader.h" #include "imageReader.h"
#include "fontReader.h" #include "fontReader.h"
#include "shaderReader.h"
#include "xmlModelReader.h" #include "xmlModelReader.h"
#include <tinydir.h> #include <tinydir.h>
@@ -71,6 +76,11 @@ namespace vb01{
if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end()) if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end())
assetReader = XmlModelReader::getSingleton(); assetReader = XmlModelReader::getSingleton();
vector<string> shaderFormats = vector<string>{"vert", "frag", "geo"};
if(find(shaderFormats.begin(), shaderFormats.end(), format) != shaderFormats.end())
assetReader = ShaderReader::getSingleton();
if(assetReader) if(assetReader)
assets.push_back(assetReader->readAsset(file)); assets.push_back(assetReader->readAsset(file));
else else
@@ -85,4 +95,28 @@ namespace vb01{
return nullptr; return nullptr;
} }
void AssetManager::editAsset(string path, Asset &newAsset){
ShaderAsset *oldAsset = (ShaderAsset*)getAsset(path);
if(oldAsset){
oldAsset->shaderString = ((ShaderAsset&)newAsset).shaderString;
Root *root = Root::getSingleton();
vector<Node*> descendants;
root->getRootNode()->getDescendants(descendants);
root->getGuiNode()->getDescendants(descendants);
for(Node *desc : descendants){
vector<Mesh*> meshes = desc->getMeshes();
for(Mesh *mesh : meshes){
Material *mat = mesh->getMaterial();
if(mat)
mat->getShader()->loadShaders();
}
}
}
}
} }
+1
View File
@@ -12,6 +12,7 @@ namespace vb01{
public: public:
static AssetManager* getSingleton(); static AssetManager* getSingleton();
void load(std::string, bool = false); void load(std::string, bool = false);
void editAsset(std::string, Asset&);
Asset* getAsset(std::string); Asset* getAsset(std::string);
inline Asset* getAsset(int i){return assets[i];} inline Asset* getAsset(int i){return assets[i];}
inline std::vector<Asset*> getAssets(){return assets;} inline std::vector<Asset*> getAssets(){return assets;}
+17 -77
View File
@@ -1,20 +1,19 @@
#include "glad.h" #include "glad.h"
#include <glfw3.h> #include <glfw3.h>
#include <iostream> #include <iostream>
#include <sstream>
#include <fstream>
#include "shader.h"
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#include "assetManager.h"
using namespace std; using namespace std;
using namespace glm; using namespace glm;
namespace vb01{ namespace vb01{
Shader::Shader(string shaderPath, bool geometry){ Shader::Shader(string shaderPath, bool geometry){
this->geometry = geometry; this->geometry = geometry;
this->path = shaderPath;
initShaders(shaderPath + ".vert", shaderPath + ".frag", shaderPath + ".geo"); initShaders(shaderPath + ".vert", shaderPath + ".frag", shaderPath + ".geo");
loadShaders(); loadShaders();
@@ -22,7 +21,6 @@ namespace vb01{
Shader::Shader(string vertShader, string fragShader){ Shader::Shader(string vertShader, string fragShader){
this->geometry = false; this->geometry = false;
this->path = vertShader;
initShaders(vertShader, fragShader, ""); initShaders(vertShader, fragShader, "");
loadShaders(); loadShaders();
@@ -30,88 +28,30 @@ namespace vb01{
Shader::Shader(string vertShader, string fragShader, string geoShader){ Shader::Shader(string vertShader, string fragShader, string geoShader){
this->geometry = true; this->geometry = true;
this->path = vertShader;
initShaders(vertShader, fragShader, geoShader); initShaders(vertShader, fragShader, geoShader);
loadShaders(); loadShaders();
} }
Shader::~Shader(){}
string Shader::getName(){ string Shader::getName(){
int dirId = path.find_last_of('/'); int dirId = vString->path.find_last_of('/');
string name = (dirId != -1 ? path.substr(dirId + 1) : path); string name = (dirId != -1 ? vString->path.substr(dirId + 1) : vString->path);
int dotId = path.find_last_of('.'); int dotId = name.find_last_of('.');
if(dotId != -1) if(dotId != -1)
name = name.substr(0, dotId); name = name.substr(0, dotId);
return name; return name;
} }
void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){
ifstream vertShaderFile, fragShaderFile; AssetManager *am = AssetManager::getSingleton();
vertShaderFile.open(vertShaderPath); vString = (ShaderAsset*)am->getAsset(vertShaderPath);
fragShaderFile.open(fragShaderPath); fString = (ShaderAsset*)am->getAsset(fragShaderPath);
stringstream vertShaderStream, fragShaderStream;
vertShaderStream << vertShaderFile.rdbuf();
fragShaderStream << fragShaderFile.rdbuf();
vertShaderFile.close();
fragShaderFile.close();
vString = vertShaderStream.str();
fString = fragShaderStream.str();
if(geometry){ if(geometry)
ifstream geoShaderFile; gString = (ShaderAsset*)am->getAsset(geoShaderPath);
geoShaderFile.open(geoShaderPath);
stringstream geoShaderStream;
geoShaderStream << geoShaderFile.rdbuf();
geoShaderFile.close();
gString = geoShaderStream.str();
}
}
void Shader::editShader(ShaderType type, int line, string insertion){
replaceLine(type, line, insertion);
loadShaders();
}
void Shader::replaceLine(ShaderType type, int line, string insertion){
string *shaderString;
switch(type){
case VERTEX_SHADER:
shaderString = &vString;
break;
case FRAGMENT_SHADER:
shaderString = &fString;
break;
case GEOMETRY_SHADER:
shaderString = &gString;
break;
}
int numPassedLines = 0, lineStart = -1, lineEnd = -1;
for(int i = 0; i < shaderString->length(); i++)
if(shaderString[0][i] == '\n'){
if(numPassedLines == line - 1)
lineStart = i + 1;
if(numPassedLines == line){
lineEnd = i;
break;
}
numPassedLines++;
}
*shaderString = shaderString->substr(0, lineStart) + insertion + shaderString->substr(lineEnd);
} }
void Shader::pushShader(u32 &type, string &sString, int glType, ErrorType errorType){ void Shader::pushShader(u32 &type, string &sString, int glType, ErrorType errorType){
@@ -124,11 +64,11 @@ namespace vb01{
void Shader::loadShaders(){ void Shader::loadShaders(){
u32 vert, geo, frag; u32 vert, geo, frag;
pushShader(vert, vString, GL_VERTEX_SHADER, VERTEX_ERROR); pushShader(vert, vString->shaderString, GL_VERTEX_SHADER, VERTEX_ERROR);
pushShader(frag, fString, GL_FRAGMENT_SHADER, FRAGMENT_ERROR); pushShader(frag, fString->shaderString, GL_FRAGMENT_SHADER, FRAGMENT_ERROR);
if(geometry) if(geometry)
pushShader(geo, gString, GL_GEOMETRY_SHADER, GEOMETRY_ERROR); pushShader(geo, gString->shaderString, GL_GEOMETRY_SHADER, GEOMETRY_ERROR);
id = glCreateProgram(); id = glCreateProgram();
glAttachShader(id, vert); glAttachShader(id, vert);
+5 -5
View File
@@ -4,6 +4,7 @@
#include <string> #include <string>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include "shaderAsset.h"
#include "vector.h" #include "vector.h"
#include "util.h" #include "util.h"
@@ -17,10 +18,10 @@ namespace vb01{
Shader(std::string, bool = false); Shader(std::string, bool = false);
Shader(std::string, std::string); Shader(std::string, std::string);
Shader(std::string, std::string, std::string); Shader(std::string, std::string, std::string);
~Shader(); ~Shader(){}
std::string getName(); std::string getName();
void setNumLights(int);
void use(); void use();
void loadShaders();
void setMat4(glm::mat4, std::string); void setMat4(glm::mat4, std::string);
void setVec4(Vector4, std::string); void setVec4(Vector4, std::string);
void setVec3(Vector3, std::string); void setVec3(Vector3, std::string);
@@ -29,17 +30,16 @@ namespace vb01{
void setBool(bool, std::string); void setBool(bool, std::string);
void setInt(int, std::string); void setInt(int, std::string);
void setUnsignedInt(u32, std::string); void setUnsignedInt(u32, std::string);
void editShader(ShaderType, int, std::string); void editShader(ShaderType, int, std::string){}
inline bool isGeometry(){return geometry;} inline bool isGeometry(){return geometry;}
private: private:
void initShaders(std::string, std::string, std::string); void initShaders(std::string, std::string, std::string);
void replaceLine(ShaderType, int, std::string); void replaceLine(ShaderType, int, std::string);
void loadShaders();
void pushShader(u32&, std::string&, int, ErrorType); void pushShader(u32&, std::string&, int, ErrorType);
void checkCompileErrors(u32, ErrorType); void checkCompileErrors(u32, ErrorType);
u32 id; u32 id;
bool geometry = false; bool geometry = false;
std::string path, vString, fString, gString; ShaderAsset *vString = nullptr, *fString = nullptr, *gString = nullptr;
friend class ShaderTest; friend class ShaderTest;
}; };
+17
View File
@@ -0,0 +1,17 @@
#ifndef SHADER_ASSET_H
#define SHADER_ASSET_H
#include "asset.h"
namespace vb01{
struct ShaderAsset : public Asset{
std::string shaderString = "";
ShaderAsset(std::string p, std::string str){
path = p;
shaderString = str;
}
};
}
#endif
+30
View File
@@ -0,0 +1,30 @@
#include "shaderReader.h"
#include "shaderAsset.h"
#include <sstream>
#include <fstream>
namespace vb01{
using namespace std;
static ShaderReader *shaderReader = nullptr;
ShaderReader* ShaderReader::getSingleton(){
if(!shaderReader)
shaderReader = new ShaderReader();
return shaderReader;
}
Asset* ShaderReader::readAsset(string path){
ifstream shaderFile;
shaderFile.open(path);
stringstream shaderStream;
shaderStream << shaderFile.rdbuf();
shaderFile.close();
return new ShaderAsset(path, shaderStream.str());
}
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef SHADER_READER_H
#define SHADER_READER_H
#include "abstractAssetReader.h"
namespace vb01{
class ShaderReader : public AbstractAssetReader{
public:
static ShaderReader* getSingleton();
Asset* readAsset(std::string);
private:
ShaderReader(){}
};
}
#endif