From ec8dd7ac1e4024c34c93b74b5ed3d078dcc9b4cb Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 7 Nov 2021 19:57:42 +0200 Subject: [PATCH] materials are now created by specifying shaders shader uniforms are added to materials --- driverSample.cpp | 18 ++-- keyframeChannel.h | 4 + light.cpp | 10 +- lightSample.cpp | 22 +++-- material.cpp | 170 ++++++++++++--------------------- material.h | 181 +++++++++++++++++++++++++++++------- mesh.cpp | 11 ++- morphAnimationSample.cpp | 16 ++-- root.cpp | 31 +++--- shader.cpp | 14 ++- shader.h | 6 +- shadowSample.cpp | 18 ++-- skeletalAnimationSample.cpp | 10 +- skybox.frag | 11 ++- textSample.cpp | 6 +- texture.cpp | 3 +- texture.h | 3 +- 17 files changed, 321 insertions(+), 213 deletions(-) diff --git a/driverSample.cpp b/driverSample.cpp index 9188df0..3e9ed2e 100644 --- a/driverSample.cpp +++ b/driverSample.cpp @@ -41,18 +41,18 @@ int main(){ */ Box *driverBox = new Box(Vector3(1, 1, 1) * .25); Node *driver = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); - Material *driverMat = new Material(); - driverMat->setTexturingEnabled(false); - driverMat->setDiffuseColor(Vector4(0, 1, 0, 1)); + Material *driverMat = new Material(PATH + "texture"); + driverMat->addVariable("texturingEnabled", false); + driverMat->addVariable("diffuseColor", Vector4(0, 1, 0, 1)); driverBox->setMaterial(driverMat); driver->attachMesh(driverBox); rootNode->attachChild(driver); Box *leftBox = new Box(Vector3(1, 1, 1) * .25); Node *leftNode = new Node(); - Material *leftMat = new Material(); - leftMat->setTexturingEnabled(false); - leftMat->setDiffuseColor(Vector4(1, 0, 0, 1)); + Material *leftMat = new Material(PATH + "texture"); + leftMat->addVariable("texturingEnabled", false); + leftMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1)); leftBox->setMaterial(leftMat); leftNode->attachMesh(leftBox); rootNode->attachChild(leftNode); @@ -60,9 +60,9 @@ int main(){ Box *rightBox = new Box(Vector3(1, 1, 1) * .25); Node *rightNode = new Node(); - Material *rightMat = new Material(); - rightMat->setTexturingEnabled(false); - rightMat->setDiffuseColor(Vector4(0, 0, 1, 1)); + Material *rightMat = new Material(PATH + "texture"); + rightMat->addVariable("texturingEnabled", false); + rightMat->addVariable("diffuseColor", Vector4(0, 0, 1, 1)); rightBox->setMaterial(rightMat); rightNode->attachMesh(rightBox); rootNode->attachChild(rightNode); diff --git a/keyframeChannel.h b/keyframeChannel.h index 72ab67d..07f475a 100644 --- a/keyframeChannel.h +++ b/keyframeChannel.h @@ -19,6 +19,10 @@ namespace vb01{ SCALE_X, SCALE_Y, SCALE_Z, + UNIFORM_1, + UNIFORM_2, + UNIFORM_3, + UNIFORM_4, SHAPE_KEY_MIN, SHAPE_KEY_VALUE, SHAPE_KEY_MAX, diff --git a/light.cpp b/light.cpp index 7e22ea2..5a70c8b 100755 --- a/light.cpp +++ b/light.cpp @@ -55,20 +55,24 @@ namespace vb01{ glGenFramebuffers(1, &depthmapFBO); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); + if(type == POINT){ depthMap = new Texture(depthMapSize); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, *(depthMap->getTexture()), 0); - depthMapShader = new Shader(basePath + "vert", basePath + "frag", basePath + "geo"); + depthMapShader = new Shader(basePath, true); } else{ depthMap = new Texture(depthMapSize, depthMapSize, true); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *(depthMap->getTexture()), 0); - depthMapShader = new Shader(basePath + "vert", basePath + "frag"); + depthMapShader = new Shader(basePath); } + glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) cout << "Not complete\n"; + glBindFramebuffer(GL_FRAMEBUFFER, *(Root::getSingleton()->getFBO())); } @@ -129,7 +133,7 @@ namespace vb01{ vector meshes = n->getMeshes(); for(Mesh *mesh : meshes){ Material *mat = mesh->getMaterial(); - if(mesh->isCastShadow() && n->isVisible() && mat && mat->isLightingEnabled()){ + if(mesh->isCastShadow() && n->isVisible() && mat && ((Material::BoolUniform*)mat->getUniform("lightingEnabled"))->value){ Vector3 pos = n->localToGlobalPosition(Vector3::VEC_ZERO); Quaternion rot = n->localToGlobalOrientation(Quaternion::QUAT_W); Vector3 rotAxis = rot.norm().getAxis(); diff --git a/lightSample.cpp b/lightSample.cpp index cc50ac2..ee1105b 100644 --- a/lightSample.cpp +++ b/lightSample.cpp @@ -7,6 +7,8 @@ #include "animation.h" #include "animationController.h" #include "animationChannel.h" +#include +#include FT_FREETYPE_H using namespace std; using namespace vb01; @@ -43,27 +45,27 @@ int main(){ /* * Creates a material with diffuse, normal, and specular maps attached. */ - Material *mat = new Material(); - mat->setTexturingEnabled(true); - mat->setLightingEnabled(true); - mat->setNormalMapEnabled(true); - mat->setSpecularMapEnabled(true); + Material *mat = new Material(PATH + "texture"); + mat->addVariable("texturingEnabled", true); + mat->addVariable("lightingEnabled", true); + mat->addVariable("normalMapEnabled", true); + mat->addVariable("specularMapEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; Texture *diffuseTex = new Texture(im0, 1); - mat->addDiffuseMap(diffuseTex); + mat->addVariable("textures[0]", diffuseTex); string im1[] = {TEX_PATH + "bricksNormal.jpg"}; Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); - mat->addNormalMap(normalTex); + mat->addVariable("textures[1]", normalTex); string im2[] = {TEX_PATH + "bricksSpecular.jpg"}; Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); - mat->addSpecularMap(specularTex); + mat->addVariable("textures[2]", specularTex); //Setting specular parameteres - mat->setSpecularStrength(1); - mat->setShinyness(32); + mat->addVariable("specularStrength", 1); + mat->addVariable("shinyness", 32); box->setMaterial(mat); rootNode->attachChild(box); diff --git a/material.cpp b/material.cpp index e631e82..8eef143 100755 --- a/material.cpp +++ b/material.cpp @@ -3,134 +3,80 @@ #include "root.h" using namespace std; +using namespace glm; namespace vb01{ - Material::Material(Type type){ - this->type = type; - - initShader(); + Material::Material(string shaderName, bool geometryShader){ + shader = new Shader(shaderName); } Material::~Material(){ delete shader; - for(Texture *t : diffuseMapTextures) - delete t; - for(Texture *t : normalMapTextures) - delete t; - for(Texture *t : specularMapTextures) - delete t; } - void Material::initShader(){ - string basePath = Root::getSingleton()->getLibPath(), shaderName; - switch(type){ - case MATERIAL_2D: - shaderName = "texture."; - break; - case MATERIAL_PARTICLE: - shaderName = "particle."; - break; - case MATERIAL_SKYBOX: - shaderName = "skybox."; - break; - case MATERIAL_GUI: - shaderName = "gui."; - break; - case MATERIAL_POST: - shaderName = "postProcess."; - break; - case MATERIAL_TEXT: - shaderName = "text."; - break; - } - /* - if(type==MATERIAL_2D) - //shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag"); - shader=new Shader(basePath+shaderName+"vert",basePath+shaderName+"frag",basePath+shaderName+"geo"); - else - */ - shader = new Shader(basePath + shaderName + "vert", basePath + shaderName + "frag"); - } + Material::Uniform* Material::getUniform(string name){ + Uniform *uni = nullptr; - void Material::animate(float value, KeyframeChannel keyframeChannel){ - switch(keyframeChannel.type){ - case KeyframeChannel::DIFFUSE_COLOR_W: - diffuseColor.w = value; - break; - case KeyframeChannel::DIFFUSE_COLOR_X: - diffuseColor.x = value; - break; - case KeyframeChannel::DIFFUSE_COLOR_Y: - diffuseColor.y = value; - break; - case KeyframeChannel::DIFFUSE_COLOR_Z: - diffuseColor.z = value; - break; - case KeyframeChannel::SPECULAR_COLOR_W: - specularColor.w = value; - break; - case KeyframeChannel::SPECULAR_COLOR_X: - specularColor.x = value; - break; - case KeyframeChannel::SPECULAR_COLOR_Y: - specularColor.y = value; - break; - case KeyframeChannel::SPECULAR_COLOR_Z: - specularColor.z = value; - break; - } + for(Uniform *u : uniforms) + if(u->name == name){ + uni = u; + break; + } + + return uni; } void Material::update(){ shader->use(); - shader->setBool(texturingEnabled, "texturingEnabled"); - const int TEXTURE_SLOTS[]{ - Texture::DIFFUSE * 2, - Texture::NORMAL * 2, - Texture::SPECULAR * 2, - Texture::PARALLAX * 2, - Texture::ENVIRONMENT * 2 - }; - if(type == MATERIAL_2D){ - shader->setBool(lightingEnabled, "lightingEnabled"); - shader->setBool(normalMapEnabled, "normalMapEnabled"); - shader->setBool(specularMapEnabled, "specularMapEnabled"); - shader->setInt(shinyness, "shinyness"); - shader->setFloat(specularStrength, "specularStrength"); - shader->setVec4(specularColor, "specularColor"); - for(int i = 0; i < 4; i++){ - shader->setInt(TEXTURE_SLOTS[i], "textures[" + to_string(i) + "].pastTexture"); - shader->setInt(TEXTURE_SLOTS[i] + 1, "textures[" + to_string(i) + "].nextTexture"); - } - shader->setInt(TEXTURE_SLOTS[4], "environmentMap"); - } - else if(type == MATERIAL_GUI){ - shader->setBool(diffuseColorEnabled, "diffuseColorEnabled"); - if(diffuseColorEnabled) - shader->setVec4(diffuseColor, "diffuseColor"); - } - else if(type == MATERIAL_TEXT){ - shader->setInt(0, "textures[0].pastTexture"); - shader->setInt(1, "textures[0].nextTexture"); - shader->setInt(2, "textures[1].pastTexture"); - } + int i = 0; - if(texturingEnabled){ - vector textures; - textures.insert(textures.end(), diffuseMapTextures.begin(), diffuseMapTextures.end()); - textures.insert(textures.end(), normalMapTextures.begin(), normalMapTextures.end()); - textures.insert(textures.end(), specularMapTextures.begin(), specularMapTextures.end()); - textures.insert(textures.end(), parallaxMapTextures.begin(), parallaxMapTextures.end()); + for(Uniform *uni : uniforms){ + switch(uni->type){ + case Uniform::INT:{ + IntUniform *u = (IntUniform*)uni; + shader->setInt(u->value, u->name); + break; + } + case Uniform::BOOL:{ + BoolUniform *u = (BoolUniform*)uni; + shader->setBool(u->value, u->name); + break; + } + case Uniform::FLOAT:{ + FloatUniform *u = (FloatUniform*)uni; + shader->setFloat(u->value, u->name); + break; + } + case Uniform::VECTOR_2:{ + Vector2Uniform *u = (Vector2Uniform*)uni; + shader->setVec2(u->value, u->name); + break; + } + case Uniform::VECTOR_3:{ + Vector3Uniform *u = (Vector3Uniform*)uni; + shader->setVec3(u->value, u->name); + break; + } + case Uniform::VECTOR_4:{ + Vector4Uniform *u = (Vector4Uniform*)uni; + shader->setVec4(u->value, u->name); + break; + } + case Uniform::TEXTURE:{ + TextureUniform *u = (TextureUniform*)uni; + shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : "")); - for(Texture *t : textures){ - int id = t->getTextureTypeId(); - shader->setBool(t->getNumFrames() > 1, "textures[" + to_string(id) + "].animated"); - shader->setFloat(t->getMixRatio(), "textures[" + to_string(id) + "].mixRatio"); - t->update(2 * id); - } + if(u->value->getNumFrames() > 1){ + shader->setInt(2 * i + 1, u->name + ".nextTexture"); + shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio"); + } + + break; + } + } + + ++i; } - else - shader->setVec4(diffuseColor, "diffuseColor"); } } diff --git a/material.h b/material.h index c94cf51..a45e94a 100755 --- a/material.h +++ b/material.h @@ -7,49 +7,164 @@ #include "animatable.h" #include +#include namespace vb01{ + class Texture; + class Material : public Animatable{ public: - enum Type{MATERIAL_2D, MATERIAL_PARTICLE, MATERIAL_SKYBOX, MATERIAL_GUI, MATERIAL_POST, MATERIAL_TEXT}; + struct Uniform : public Animatable{ + void animate(float val, KeyframeChannel channel){ + switch(channel.type){ + case KeyframeChannel::UNIFORM_1: + animateComponent1(val); + break; + case KeyframeChannel::UNIFORM_2: + animateComponent2(val); + break; + case KeyframeChannel::UNIFORM_3: + animateComponent3(val); + break; + case KeyframeChannel::UNIFORM_4: + animateComponent4(val); + break; + } + } - Material(Type = MATERIAL_2D); + enum Type{ + INT, + BOOL, + FLOAT, + VECTOR_2, + VECTOR_3, + VECTOR_4, + TEXTURE + }; + + std::string name; + Type type; + Material *material = nullptr; + + protected: + virtual void animateComponent1(float val){} + virtual void animateComponent2(float val){} + virtual void animateComponent3(float val){} + virtual void animateComponent4(float val){} + }; + + struct IntUniform : public Uniform{ + int value; + + IntUniform(std::string name, int value){ + this->name = name; + this->value = value; + this->type = Uniform::INT; + } + + void animateComponent1(float val){value = (int)val;} + }; + + struct BoolUniform : public Uniform{ + bool value; + + BoolUniform(std::string name, bool value){ + this->name = name; + this->value = value; + this->type = Uniform::BOOL; + } + + void animateComponent1(float val){value = (bool)val;} + }; + + struct FloatUniform : public Uniform{ + float value; + + FloatUniform(std::string name, float value){ + this->name = name; + this->value = value; + this->type = Uniform::FLOAT; + } + + void animateComponent1(float val){value = val;} + }; + + struct Vector2Uniform : public Uniform{ + Vector2 value; + + Vector2Uniform(std::string name, Vector2 value){ + this->name = name; + this->value = value; + this->type = Uniform::VECTOR_2; + } + + void animateComponent1(float val){value.x = val;} + + void animateComponent2(float val){value.y = val;} + }; + + struct Vector3Uniform : public Uniform{ + Vector3 value; + + Vector3Uniform(std::string name, Vector3 value){ + this->name = name; + this->value = value; + this->type = Uniform::VECTOR_3; + } + + void animateComponent1(float val){value.x = val;} + + void animateComponent2(float val){value.y = val;} + + void animateComponent3(float val){value.z = val;} + }; + + struct Vector4Uniform : public Uniform{ + Vector4 value; + + Vector4Uniform(std::string name, Vector4 value){ + this->name = name; + this->value = value; + this->type = Uniform::VECTOR_4; + } + + void animateComponent1(float val){value.x = val;} + + void animateComponent2(float val){value.y = val;} + + void animateComponent3(float val){value.z = val;} + + void animateComponent4(float val){value.w = val;} + }; + + struct TextureUniform : public Uniform{ + Texture *value = nullptr; + bool animatable = false; + + TextureUniform(std::string name, Texture *value, bool animatable){ + this->name = name; + this->value = value; + this->animatable = animatable; + this->type = Uniform::TEXTURE; + } + }; + + Material(std::string, bool = false); ~Material(); void update(); - inline void addDiffuseMap(Texture *texture){diffuseMapTextures.push_back(texture);} - inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));} - inline void addNormalMap(Texture *texture){normalMapTextures.push_back(texture);} - inline void addSpecularMap(Texture *texture){specularMapTextures.push_back(texture);} - inline void addParallaxMap(Texture *texture){parallaxMapTextures.push_back(texture);} - inline void setDiffuseColor(Vector4 diffuse){this->diffuseColor = diffuse;} - inline void setSpecularColor(Vector4 specular){this->specularColor = specular;} - inline void setLightingEnabled(bool lighting){this->lightingEnabled = lighting;} - inline void setTexturingEnabled(bool texturing){this->texturingEnabled = texturing;} - inline void setDiffuseColorEnabled(bool diffuseColor){this->diffuseColorEnabled = diffuseColor;} - inline void setNormalMapEnabled(bool enabled){this->normalMapEnabled = enabled;} - inline void setSpecularMapEnabled(bool enabled){this->specularMapEnabled = enabled;} - inline void setParallaxMapEnabled(bool enabled){this->parallaxMapEnabled = enabled;} - inline void setShinyness(int shinyness){this->shinyness = shinyness;} - inline void setSpecularStrength(float specStr){this->specularStrength = specStr;} - inline bool isLightingEnabled(){return lightingEnabled;} - inline bool isTexturingEnabled(){return texturingEnabled;} - inline bool isDiffuseColorEnabled(){return diffuseColorEnabled;} - inline Texture* getDiffuseMap(int i){return diffuseMapTextures[i];} - inline Texture* getNormalMap(int i){return normalMapTextures[i];} - inline Texture* getSpecularMap(int i){return specularMapTextures[i];} + Uniform* getUniform(std::string); + inline Uniform* getUniform(int i){return uniforms[i];} + inline void addVariable(std::string name, int value){uniforms.push_back(new IntUniform(name, value));} + inline void addVariable(std::string name, bool value){uniforms.push_back(new BoolUniform(name, value));} + inline void addVariable(std::string name, float value){uniforms.push_back(new BoolUniform(name, value));} + inline void addVariable(std::string name, Vector2 value){uniforms.push_back(new Vector2Uniform(name, value));} + inline void addVariable(std::string name, Vector3 value){uniforms.push_back(new Vector3Uniform(name, value));} + inline void addVariable(std::string name, Vector4 value){uniforms.push_back(new Vector4Uniform(name, value));} + inline void addVariable(std::string name, Texture *value, bool animatable){uniforms.push_back(new TextureUniform(name, value, animatable));} inline Shader* getShader(){return shader;} - inline Type getType(){return type;} private: - Type type; - int shinyness = 32; - float specularStrength = .5; - bool lightingEnabled = false, texturingEnabled = true, diffuseColorEnabled = false, normalMapEnabled = false, specularMapEnabled = false, parallaxMapEnabled = false; - std::vector diffuseMapTextures, normalMapTextures, specularMapTextures, parallaxMapTextures; - Vector4 diffuseColor = Vector4::VEC_IJKL, specularColor = Vector4::VEC_ZERO; + std::vector uniforms; Shader *shader = nullptr; - - void initShader(); - void animate(float, KeyframeChannel); }; } diff --git a/mesh.cpp b/mesh.cpp index e71c81a..0a72fc4 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -68,7 +68,7 @@ namespace vb01{ u32 RBO; string basePath = "../../vb01/depthMap."; - environmentShader = new Shader(basePath + "vert", basePath + "frag", basePath + "geo"); + environmentShader = new Shader(basePath); environmentMap = new Texture(width, false); glGenFramebuffers(1, &environmentBuffer); @@ -133,8 +133,10 @@ namespace vb01{ } Vector3 rotAxis = orient.getAxis(); + if(rotAxis == Vector3::VEC_ZERO) rotAxis = Vector3::VEC_I; + mat4 model = mat4(1.); model = translate(model, vec3(pos.x, pos.y, pos.z)); model = rotate(model, orient.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z)); @@ -151,8 +153,10 @@ namespace vb01{ if(reflect) updateReflection(shader, pos, width, height); + if(skeleton) updateSkeleton(shader); + if(numShapeKeys > 0) updateShapeKeys(shader); @@ -164,8 +168,9 @@ namespace vb01{ shader->setVec3(camPos, "camPos"); shader->setMat4(model, "model"); - if(material->getType() == Material::MATERIAL_GUI){ + if(shader->getName() == "gui"){ shader->setVec2(Vector2((float)width, (float)height), "screen"); + if(node) shader->setVec3(pos, "pos"); } @@ -280,7 +285,7 @@ namespace vb01{ glViewport(0, 0, root->getWidth(), root->getHeight()); shader->setBool(reflect, "environmentMapEnabled"); - root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4); + //root->getSkybox()->getMaterial()->getDiffuseMap(0)->select(4); environmentMap->select(4); } diff --git a/morphAnimationSample.cpp b/morphAnimationSample.cpp index dc2cc69..8588b9e 100644 --- a/morphAnimationSample.cpp +++ b/morphAnimationSample.cpp @@ -6,6 +6,8 @@ #include "animationController.h" #include "animationChannel.h" #include "box.h" +#include +#include FT_FREETYPE_H using namespace std; using namespace vb01; @@ -38,12 +40,12 @@ int main(){ cam->lookAt(Vector3(1, 0, 0).norm(), Vector3(0, 1, 0).norm()); Model *model = new Model(MODEL_PATH + "kek.vb"); - Material *mat = new Material(); - mat->setLightingEnabled(false); - mat->setTexturingEnabled(true); + Material *mat = new Material(PATH + "texture"); + mat->addVariable("lightingEnabled", false); + mat->addVariable("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addDiffuseMap(texture); + mat->addVariable("textures[0]", texture); model->setMaterial(mat); rootNode->attachChild(model); @@ -67,9 +69,9 @@ int main(){ * Creates a box attached to the bone controlling the shape keys. */ Box *box = new Box(Vector3(1, 1, 1) * .25); - Material *boxMat = new Material(); - boxMat->setTexturingEnabled(false); - boxMat->setDiffuseColor(Vector4(1, 0, 0, 1)); + Material *boxMat = new Material(PATH + "texture"); + boxMat->addVariable("texturingEnabled", false); + boxMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1)); box->setMaterial(boxMat); skeleton->getBone("bone")->attachMesh(box); diff --git a/root.cpp b/root.cpp index 5b9d8ee..bb88d2f 100755 --- a/root.cpp +++ b/root.cpp @@ -82,8 +82,10 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, FBO); u32 colorAttachments[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; + for(int i = 0; i < 2; i++) glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[i], GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0); + glDrawBuffers(2, colorAttachments); glGenRenderbuffers(1, &RBO); @@ -93,17 +95,19 @@ namespace vb01{ if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) cout << "Not complete\n"; + glBindFramebuffer(GL_FRAMEBUFFER, 0); } void Root::initBloomFramebuffer(){ - string basePath = libPath + "blur."; - blurShader = new Shader(basePath + "vert", basePath + "frag"); + blurShader = new Shader(libPath + "blur"); glGenFramebuffers(2, pingpongBuffers); + for(int i = 0; i < 2; i++){ glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[i]); pingPongTextures[i] = new Texture(width, height, false); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *(pingPongTextures[i]->getTexture()), 0); + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) cout << "Not complete\n"; } @@ -111,9 +115,9 @@ namespace vb01{ void Root::initGuiPlane(Texture *fragTexture, Texture *brightTexture){ guiPlane = new Quad(Vector3(width, height, -1), false); - Material *mat = new Material(Material::MATERIAL_POST); - mat->addDiffuseMap(fragTexture); - mat->addDiffuseMap(brightTexture); + Material *mat = new Material(libPath + "postProcess"); + mat->addVariable("frag", fragTexture, false); + mat->addVariable("bright", brightTexture, false); guiPlane->setMaterial(mat); } @@ -154,16 +158,20 @@ namespace vb01{ bool horizontal = false; blurShader->use(); blurShader->setVec2(Vector2(width, height), "screen"); + for(int i = 0; i < blurLevel; i++){ glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[horizontal]); blurShader->setBool(horizontal, "horizontal"); + if(i == 0) - guiPlane->getMaterial()->getDiffuseMap(1)->select(); + ((Material::TextureUniform*)guiPlane->getMaterial()->getUniform("bright"))->value->select(); else pingPongTextures[!horizontal]->select(); + guiPlane->render(); horizontal = !horizontal; } + glBindFramebuffer(GL_FRAMEBUFFER, 0); } @@ -180,15 +188,16 @@ namespace vb01{ shader->setInt(0, "frag"); shader->setInt(1, "bright"); - material->getDiffuseMap(0)->select(0); - material->getDiffuseMap(1)->select(1); + ((Material::TextureUniform*)material->getUniform("frag"))->value->select(0); + ((Material::TextureUniform*)material->getUniform("bright"))->value->select(1); guiPlane->render(); } - void Root::createSkybox(string textures[6]){ + void Root::createSkybox(string paths[6]){ skybox = new Box(Vector3(1, 1, 1) * 10); - Material *skyboxMat = new Material(Material::MATERIAL_SKYBOX); - skyboxMat->addDiffuseMap(textures); + Material *skyboxMat = new Material(libPath + "skybox"); + Texture *texture = new Texture(paths); + skyboxMat->addVariable("skybox", texture, true); skybox->setMaterial(skyboxMat); } diff --git a/shader.cpp b/shader.cpp index ac969ac..7a40292 100755 --- a/shader.cpp +++ b/shader.cpp @@ -12,16 +12,22 @@ using namespace std; using namespace glm; namespace vb01{ - Shader::Shader(string vertShaderPath, string fragShaderPath, string geoShaderPath){ - initShaders(vertShaderPath, fragShaderPath, geoShaderPath); + Shader::Shader(string shaderPath, bool geometry){ + this->geometry = geometry; + this->path = shaderPath; + + initShaders(shaderPath + ".vert", shaderPath + ".frag", shaderPath + ".geo"); loadShaders(); } Shader::~Shader(){} - void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ - geometry = (geoShaderPath != ""); + string Shader::getName(){ + int dirId = path.find_last_of('/'); + return (dirId != -1 ? path.substr(dirId) : path); + } + void Shader::initShaders(string vertShaderPath, string fragShaderPath, string geoShaderPath){ ifstream vertShaderFile, geoShaderFile, fragShaderFile; vertShaderFile.open(vertShaderPath); geoShaderFile.open(geoShaderPath); diff --git a/shader.h b/shader.h index 1224b2f..58e09c3 100755 --- a/shader.h +++ b/shader.h @@ -14,8 +14,9 @@ namespace vb01{ enum ShaderType{VERTEX_SHADER, FRAGMENT_SHADER, GEOMETRY_SHADER}; Shader(){} - Shader(std::string, std::string, std::string = ""); + Shader(std::string, bool = false); ~Shader(); + std::string getName(); void setNumLights(int); void use(); void setMat4(glm::mat4, std::string); @@ -26,6 +27,7 @@ namespace vb01{ void setBool(bool, std::string); void setInt(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); @@ -34,7 +36,7 @@ namespace vb01{ void checkCompileErrors(u32, ErrorType); u32 id; bool geometry = false; - std::string vString, fString, gString; + std::string path, vString, fString, gString; friend class ShaderTest; }; diff --git a/shadowSample.cpp b/shadowSample.cpp index e72d8d0..98a6690 100644 --- a/shadowSample.cpp +++ b/shadowSample.cpp @@ -7,6 +7,8 @@ #include "animation.h" #include "animationController.h" #include "animationChannel.h" +#include +#include FT_FREETYPE_H using namespace std; using namespace vb01; @@ -40,23 +42,23 @@ int main(){ Box *box = new Box(Vector3(40, .2, 40)); Node *boxNode = new Node(); - Material *boxMat = new Material(); - boxMat->setTexturingEnabled(true); - boxMat->setLightingEnabled(true); + Material *boxMat = new Material(PATH + "texture"); + boxMat->addVariable("texturingEnabled", true); + boxMat->addVariable("lightingEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; Texture *boxTex = new Texture(im0, 1); - boxMat->addDiffuseMap(boxTex); + boxMat->addVariable("textures[0]", boxTex); box->setMaterial(boxMat); boxNode->attachMesh(box); rootNode->attachChild(boxNode); Model *jet = new Model(MODEL_PATH + "jet00.obj"); - Material *jetMat = new Material(); - jetMat->setTexturingEnabled(true); - jetMat->setLightingEnabled(true); + Material *jetMat = new Material(PATH + "texture"); + jetMat->addVariable("texturingEnabled", true); + jetMat->addVariable("lightingEnabled", true); string im1[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *jetTex = new Texture(im1, 1); - jetMat->addDiffuseMap(jetTex); + jetMat->addVariable("textures[0]", jetTex); jet->setMaterial(jetMat); rootNode->attachChild(jet); jet->setCastShadow(true); diff --git a/skeletalAnimationSample.cpp b/skeletalAnimationSample.cpp index a6522da..335d479 100644 --- a/skeletalAnimationSample.cpp +++ b/skeletalAnimationSample.cpp @@ -5,6 +5,8 @@ #include "skeleton.h" #include "animationController.h" #include "animationChannel.h" +#include +#include FT_FREETYPE_H using namespace std; using namespace vb01; @@ -38,12 +40,12 @@ int main(){ cam->lookAt(Vector3(0, 0, -1).norm(), Vector3(0, 1, 0).norm()); Model *model = new Model(MODEL_PATH + "kek.vb"); - Material *mat = new Material(); - mat->setLightingEnabled(false); - mat->setTexturingEnabled(true); + Material *mat = new Material(PATH + "texture"); + mat->addVariable("lightingEnabled", false); + mat->addVariable("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addDiffuseMap(texture); + mat->addVariable("textures[0]", texture); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/skybox.frag b/skybox.frag index 6c1738d..c0be633 100755 --- a/skybox.frag +++ b/skybox.frag @@ -5,9 +5,16 @@ in vec3 texCoords; layout (location = 0) out vec4 FragColor; layout (location = 1) out vec4 BrightColor; -uniform samplerCube skybox; +struct Texture{ + float mixRatio; + samplerCube pastTexture, nextTexture; + bool animated; +}; + +uniform Texture tex; void main(){ BrightColor = vec4(0); - FragColor = texture(skybox, texCoords); + //vec4 texColor = (tex.animated ? mix(tex.pastTexture, tex.nextTexture, tex.mixRatio) : tex.nextTexture); + FragColor = texture(tex.pastTexture, texCoords); } diff --git a/textSample.cpp b/textSample.cpp index e218b35..d5d414b 100644 --- a/textSample.cpp +++ b/textSample.cpp @@ -51,12 +51,12 @@ int main(){ * Creates a material for text objects and * attaches a diffuse texture with 2 images */ - Material *textMat = new Material(Material::MATERIAL_TEXT); - textMat->setTexturingEnabled(true); + Material *textMat = new Material(PATH + "text"); + textMat->addVariable("texturingEnabled", true); string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(frames, 2); - textMat->addDiffuseMap(texture); + textMat->addVariable("textures[0]", texture); for(int i = 0; i < numTexts; i++){ Text *text = new Text(FONT_PATH + fonts[i], texts[i]); diff --git a/texture.cpp b/texture.cpp index e5e8c71..da1b846 100755 --- a/texture.cpp +++ b/texture.cpp @@ -1,12 +1,13 @@ #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" +#include +#include FT_FREETYPE_H #include "texture.h" #include "glad.h" #include -#include #include using namespace std; diff --git a/texture.h b/texture.h index 3e31330..77cafd1 100755 --- a/texture.h +++ b/texture.h @@ -2,10 +2,11 @@ #define TEXTURE_H #include -#include #include "util.h" #include "animatable.h" + +#include #include FT_FREETYPE_H namespace vb01{