From 37b791342abd722c13621220c461bc5dffbe84ab Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 7 Sep 2024 11:34:14 +0300 Subject: [PATCH 1/5] lighting calculation bugfix --- texture.frag | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/texture.frag b/texture.frag index e0b6333..ae5330c 100755 --- a/texture.frag +++ b/texture.frag @@ -133,9 +133,9 @@ void main(){ float shadow = getShadow(i); diffuseCol *= (1.0 - shadow); } - - finalColor *= vec4(diffuseCol + specularCol, 1); } + + finalColor *= vec4(diffuseCol + specularCol, 1); } float brightness = dot(finalColor.rgb, vec3(0.2126, 0.7152, 0.0722)); From 1f5d1bc94d7ee0df2e9fba63bd877568a4e6faf0 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 8 Sep 2024 14:23:23 +0300 Subject: [PATCH 2/5] reader shaders as assets --- CMakeLists.txt | 4 +-- assetManager.cpp | 34 ++++++++++++++++++ assetManager.h | 1 + shader.cpp | 94 +++++++++--------------------------------------- shader.h | 10 +++--- shaderAsset.h | 17 +++++++++ shaderReader.cpp | 30 ++++++++++++++++ shaderReader.h | 16 +++++++++ 8 files changed, 122 insertions(+), 84 deletions(-) create mode 100644 shaderAsset.h create mode 100644 shaderReader.cpp create mode 100644 shaderReader.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ef4915..5800646 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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(ANIM animationController.cpp animationChannel.cpp animation.cpp animatable.cpp keyframeChannel.cpp driver.cpp) set(ARMATURE skeleton.cpp bone.cpp ikSolver.cpp) -set(ASSET_MANAGER assetManager.cpp textAsset.h imageAsset.h modelAsset.h fontAsset.h) -set(ASSET_READERS imageReader.cpp fontReader.cpp modelReader.cpp xmlModelReader.cpp) +set(ASSET_MANAGER assetManager.cpp shaderAsset.h textAsset.h imageAsset.h modelAsset.h fontAsset.h) +set(ASSET_READERS shaderReader.cpp imageReader.cpp fontReader.cpp modelReader.cpp xmlModelReader.cpp) if(BUILD_WITH_ASSIMP) set(ASSET_READERS ${ASSET_READERS} assimpModelReader.cpp) diff --git a/assetManager.cpp b/assetManager.cpp index 63b106b..8bdfdf8 100644 --- a/assetManager.cpp +++ b/assetManager.cpp @@ -1,8 +1,13 @@ #include "assetManager.h" #include "abstractAssetReader.h" #include "util.h" +#include "root.h" +#include "node.h" +#include "mesh.h" +#include "material.h" #include "imageReader.h" #include "fontReader.h" +#include "shaderReader.h" #include "xmlModelReader.h" #include @@ -71,6 +76,11 @@ namespace vb01{ if(find(modelFormats.begin(), modelFormats.end(), format) != modelFormats.end()) assetReader = XmlModelReader::getSingleton(); + vector shaderFormats = vector{"vert", "frag", "geo"}; + + if(find(shaderFormats.begin(), shaderFormats.end(), format) != shaderFormats.end()) + assetReader = ShaderReader::getSingleton(); + if(assetReader) assets.push_back(assetReader->readAsset(file)); else @@ -85,4 +95,28 @@ namespace vb01{ 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 descendants; + root->getRootNode()->getDescendants(descendants); + root->getGuiNode()->getDescendants(descendants); + + for(Node *desc : descendants){ + vector meshes = desc->getMeshes(); + + for(Mesh *mesh : meshes){ + Material *mat = mesh->getMaterial(); + + if(mat) + mat->getShader()->loadShaders(); + } + } + } + } } diff --git a/assetManager.h b/assetManager.h index 262e9c1..d84e4f7 100644 --- a/assetManager.h +++ b/assetManager.h @@ -12,6 +12,7 @@ namespace vb01{ public: static AssetManager* getSingleton(); void load(std::string, bool = false); + void editAsset(std::string, Asset&); Asset* getAsset(std::string); inline Asset* getAsset(int i){return assets[i];} inline std::vector getAssets(){return assets;} diff --git a/shader.cpp b/shader.cpp index 79e6035..4a63f0c 100755 --- a/shader.cpp +++ b/shader.cpp @@ -1,20 +1,19 @@ #include "glad.h" #include #include -#include -#include -#include "shader.h" #include #include +#include "shader.h" +#include "assetManager.h" + using namespace std; using namespace glm; namespace vb01{ Shader::Shader(string shaderPath, bool geometry){ this->geometry = geometry; - this->path = shaderPath; initShaders(shaderPath + ".vert", shaderPath + ".frag", shaderPath + ".geo"); loadShaders(); @@ -22,7 +21,6 @@ namespace vb01{ Shader::Shader(string vertShader, string fragShader){ this->geometry = false; - this->path = vertShader; initShaders(vertShader, fragShader, ""); loadShaders(); @@ -30,88 +28,30 @@ namespace vb01{ Shader::Shader(string vertShader, string fragShader, string geoShader){ this->geometry = true; - this->path = vertShader; initShaders(vertShader, fragShader, geoShader); loadShaders(); } - Shader::~Shader(){} - string Shader::getName(){ - int dirId = path.find_last_of('/'); - string name = (dirId != -1 ? path.substr(dirId + 1) : path); + int dirId = vString->path.find_last_of('/'); + 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) - name = name.substr(0, dotId); + if(dotId != -1) + name = name.substr(0, dotId); - return name; + return name; } void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ - ifstream vertShaderFile, fragShaderFile; - vertShaderFile.open(vertShaderPath); - fragShaderFile.open(fragShaderPath); - - stringstream vertShaderStream, fragShaderStream; - vertShaderStream << vertShaderFile.rdbuf(); - fragShaderStream << fragShaderFile.rdbuf(); - - vertShaderFile.close(); - fragShaderFile.close(); - - vString = vertShaderStream.str(); - fString = fragShaderStream.str(); + AssetManager *am = AssetManager::getSingleton(); + vString = (ShaderAsset*)am->getAsset(vertShaderPath); + fString = (ShaderAsset*)am->getAsset(fragShaderPath); - if(geometry){ - ifstream geoShaderFile; - 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); + if(geometry) + gString = (ShaderAsset*)am->getAsset(geoShaderPath); } void Shader::pushShader(u32 &type, string &sString, int glType, ErrorType errorType){ @@ -124,11 +64,11 @@ namespace vb01{ void Shader::loadShaders(){ u32 vert, geo, frag; - pushShader(vert, vString, GL_VERTEX_SHADER, VERTEX_ERROR); - pushShader(frag, fString, GL_FRAGMENT_SHADER, FRAGMENT_ERROR); + pushShader(vert, vString->shaderString, GL_VERTEX_SHADER, VERTEX_ERROR); + pushShader(frag, fString->shaderString, GL_FRAGMENT_SHADER, FRAGMENT_ERROR); if(geometry) - pushShader(geo, gString, GL_GEOMETRY_SHADER, GEOMETRY_ERROR); + pushShader(geo, gString->shaderString, GL_GEOMETRY_SHADER, GEOMETRY_ERROR); id = glCreateProgram(); glAttachShader(id, vert); diff --git a/shader.h b/shader.h index 7092516..15ea0a0 100755 --- a/shader.h +++ b/shader.h @@ -4,6 +4,7 @@ #include #include +#include "shaderAsset.h" #include "vector.h" #include "util.h" @@ -17,10 +18,10 @@ namespace vb01{ Shader(std::string, bool = false); Shader(std::string, std::string); Shader(std::string, std::string, std::string); - ~Shader(); + ~Shader(){} std::string getName(); - void setNumLights(int); void use(); + void loadShaders(); void setMat4(glm::mat4, std::string); void setVec4(Vector4, std::string); void setVec3(Vector3, std::string); @@ -29,17 +30,16 @@ namespace vb01{ void setBool(bool, std::string); void setInt(int, std::string); void setUnsignedInt(u32, std::string); - void editShader(ShaderType, int, std::string); + void editShader(ShaderType, int, std::string){} inline bool isGeometry(){return geometry;} private: void initShaders(std::string, std::string, std::string); void replaceLine(ShaderType, int, std::string); - void loadShaders(); void pushShader(u32&, std::string&, int, ErrorType); void checkCompileErrors(u32, ErrorType); u32 id; bool geometry = false; - std::string path, vString, fString, gString; + ShaderAsset *vString = nullptr, *fString = nullptr, *gString = nullptr; friend class ShaderTest; }; diff --git a/shaderAsset.h b/shaderAsset.h new file mode 100644 index 0000000..75fa384 --- /dev/null +++ b/shaderAsset.h @@ -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 diff --git a/shaderReader.cpp b/shaderReader.cpp new file mode 100644 index 0000000..48a6682 --- /dev/null +++ b/shaderReader.cpp @@ -0,0 +1,30 @@ +#include "shaderReader.h" +#include "shaderAsset.h" + +#include +#include + +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()); + } +} diff --git a/shaderReader.h b/shaderReader.h new file mode 100644 index 0000000..379b0a4 --- /dev/null +++ b/shaderReader.h @@ -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 From 1719cdf98d96e23f79118bd41cc7d7afbff9cba1 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 8 Sep 2024 14:23:56 +0300 Subject: [PATCH 3/5] adding shader assets --- root.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/root.cpp b/root.cpp index 01bb698..0c0c1d5 100755 --- a/root.cpp +++ b/root.cpp @@ -5,6 +5,7 @@ #include "box.h" #include "quad.h" #include "lineRenderer.h" +#include "assetManager.h" #include "animationController.h" #include "glad.h" @@ -22,6 +23,7 @@ namespace vb01{ Root* Root::getSingleton(){ if(!root) root = new Root(); + return root; } @@ -47,6 +49,8 @@ namespace vb01{ this->height = height; this->libPath = libPath; + AssetManager::getSingleton()->load(Root::getSingleton()->getLibPath()); + initWindow(name); brdfLutPlane = new Quad(Vector3(1, 1, 1) * 2); @@ -59,7 +63,7 @@ namespace vb01{ initBloomFramebuffer(); initGuiPlane(fragTexture, brightTexture); - shader = new Shader(Root::getSingleton()->getLibPath() + "line3D"); + shader = new Shader(Root::getSingleton()->getLibPath() + "line3D"); } void Root::initWindow(string name){ From 63429dacbe02917ece2cc3d01e84fb44011ba98d Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 8 Sep 2024 14:24:17 +0300 Subject: [PATCH 4/5] fixed normal calculation --- xmlModelReader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmlModelReader.cpp b/xmlModelReader.cpp index 25eeb38..f268ab7 100644 --- a/xmlModelReader.cpp +++ b/xmlModelReader.cpp @@ -132,7 +132,7 @@ namespace vb01{ float biTanZ = atof(vertEl->Attribute("bz")); Vector3 biTan = Vector3(biTanX, biTanY, biTanZ); - normals[i] = tan.cross(biTan); + normals[i] = biTan.cross(tan); MeshData::Vertex vertex; vertex.pos = &vertPos[id]; From 39dc1afd25c12290e8e49b1df56a4e474c13ad47 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 8 Sep 2024 14:24:44 +0300 Subject: [PATCH 5/5] using new way to edit shaders --- node.cpp | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/node.cpp b/node.cpp index d57cd3e..3560121 100755 --- a/node.cpp +++ b/node.cpp @@ -1,19 +1,24 @@ #include "root.h" #include "bone.h" #include "mesh.h" -#include "particleEmitter.h" -#include "light.h" #include "text.h" -#include "material.h" +#include "light.h" #include "matrix.h" +#include "material.h" #include "skeleton.h" +#include "shaderAsset.h" +#include "particleEmitter.h" +#include "assetManager.h" #include "animationController.h" +#include + #include #include using namespace std; using namespace glm; +using namespace boost; namespace vb01{ Node::Node(Vector3 pos, Quaternion orientation, Vector3 scale, string name, Animatable::Type type) : Animatable(type, name){ @@ -245,6 +250,7 @@ namespace vb01{ lights.push_back(light); light->onAttached(this); updateShaders(); + } void Node::removeLight(int id){ @@ -439,27 +445,23 @@ namespace vb01{ void Node::updateShaders(){ Root *root = Root::getSingleton(); + AssetManager *am = AssetManager::getSingleton(); + ShaderAsset *sa = (ShaderAsset*)am->getAsset(root->getLibPath() + "texture.frag"); + string shaderStr = sa->shaderString; + int numLights = root->getNumLights(); - Node *rootNode = root->getRootNode(); - vector descendants; - rootNode->getDescendants(descendants); - descendants.push_back(rootNode); + string str1 = "const int numLights = " + to_string(numLights > 0 ? numLights : 1) + ";"; + int a1 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 0).begin()); + int a2 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 1).begin()); + shaderStr.replace(a1 + 1, a2 - a1 - 1, str1); - for(Node *n : descendants){ - vector meshes = n->getMeshes(); + string str2 = "const bool checkLights = " + string(numLights > 0 ? "true" : "false") + ";"; + a1 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 1).begin()); + a2 = std::distance(shaderStr.begin(), find_nth(shaderStr, "\n", 2).begin()); + shaderStr.replace(a1 + 1, a2 - a1 - 1, str2); - for(Mesh *m : meshes){ - Material *mat = m->getMaterial(); - - if(mat){ - int numLights = root->getNumLights(); - string str1 = "const int numLights = " + to_string(numLights > 0 ? numLights : 1) + ";"; - mat->getShader()->editShader(Shader::FRAGMENT_SHADER, 1, str1); - string str2 = "const bool checkLights = " + string(numLights > 0 ? "true" : "false") + ";"; - mat->getShader()->editShader(Shader::FRAGMENT_SHADER, 2, str2); - } - } - } + ShaderAsset sa2(sa->path, shaderStr); + am->editAsset(sa->path, sa2); } void Node::setOrientation(Quaternion q){