From 003eb44938c121578627ade814fc3b04cef1823f Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 13 Nov 2021 10:34:37 +0200 Subject: [PATCH] improved shader uniform assignment --- driverSample.cpp | 12 ++++++------ lightSample.cpp | 22 +++++++++++----------- material.cpp | 12 ++++++++---- material.h | 14 +++++++------- morphAnimationSample.cpp | 10 +++++----- root.cpp | 6 +++--- shadowSample.cpp | 12 ++++++------ skeletalAnimationSample.cpp | 6 +++--- textSample.cpp | 4 ++-- texture.frag | 9 +++++++-- 10 files changed, 58 insertions(+), 49 deletions(-) diff --git a/driverSample.cpp b/driverSample.cpp index 83cc97b..9c01391 100644 --- a/driverSample.cpp +++ b/driverSample.cpp @@ -42,8 +42,8 @@ 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(PATH + "texture"); - driverMat->addVariable("texturingEnabled", false); - driverMat->addVariable("diffuseColor", Vector4(0, 1, 0, 1)); + driverMat->addBoolUniform("texturingEnabled", false); + driverMat->addVec4Uniform("diffuseColor", Vector4(0, 1, 0, 1)); driverBox->setMaterial(driverMat); driver->attachMesh(driverBox); rootNode->attachChild(driver); @@ -51,8 +51,8 @@ int main(){ Box *leftBox = new Box(Vector3(1, 1, 1) * .25); Node *leftNode = new Node(); Material *leftMat = new Material(PATH + "texture"); - leftMat->addVariable("texturingEnabled", false); - leftMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1)); + leftMat->addBoolUniform("texturingEnabled", false); + leftMat->addVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); leftBox->setMaterial(leftMat); leftNode->attachMesh(leftBox); rootNode->attachChild(leftNode); @@ -61,8 +61,8 @@ int main(){ Box *rightBox = new Box(Vector3(1, 1, 1) * .25); Node *rightNode = new Node(); Material *rightMat = new Material(PATH + "texture"); - rightMat->addVariable("texturingEnabled", false); - rightMat->addVariable("diffuseColor", Vector4(0, 0, 1, 1)); + rightMat->addBoolUniform("texturingEnabled", false); + rightMat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); rightBox->setMaterial(rightMat); rightNode->attachMesh(rightBox); rootNode->attachChild(rightNode); diff --git a/lightSample.cpp b/lightSample.cpp index 5b185be..3254be6 100644 --- a/lightSample.cpp +++ b/lightSample.cpp @@ -44,26 +44,26 @@ int main(){ * Creates a material with diffuse, normal, and specular maps attached. */ Material *mat = new Material(PATH + "texture"); - mat->addVariable("texturingEnabled", true); - mat->addVariable("lightingEnabled", true); - mat->addVariable("normalMapEnabled", true); - mat->addVariable("specularMapEnabled", false); + mat->addBoolUniform("texturingEnabled", true); + mat->addBoolUniform("lightingEnabled", true); + mat->addBoolUniform("normalMapEnabled", true); + mat->addBoolUniform("specularMapEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; Texture *diffuseTex = new Texture(im0, 1); - mat->addVariable("textures[0]", diffuseTex, false); + mat->addTexUniform("textures[0]", diffuseTex, true); string im1[] = {TEX_PATH + "bricksNormal.jpg"}; Texture *normalTex = new Texture(im1, 1, Texture::NORMAL); - mat->addVariable("textures[1]", normalTex, false); + mat->addTexUniform("textures[1]", normalTex, true); string im2[] = {TEX_PATH + "bricksSpecular.jpg"}; Texture *specularTex = new Texture(im2, 1, Texture::SPECULAR); - mat->addVariable("textures[2]", specularTex, false); + mat->addTexUniform("textures[2]", specularTex, true); //Setting specular parameteres - mat->addVariable("specularStrength", 1); - mat->addVariable("shinyness", 32); + mat->addFloatUniform("specularStrength", 1); + mat->addFloatUniform("shinyness", 2); box->setMaterial(mat); rootNode->attachChild(box); @@ -77,8 +77,8 @@ int main(){ Node *lightNode = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); lightNode->addLight(light); rootNode->attachChild(lightNode); - lightNode->setOrientation(Quaternion(.0, Vector3(1, 0, 0))); - lightNode->setPosition(Vector3(0, 5, -5)); + lightNode->setOrientation(Quaternion(.7, Vector3(1, 0, 0))); + lightNode->setPosition(Vector3(0, 5, -10)); /* * KeyframeChannel structs determine what object and how it will be animated. diff --git a/material.cpp b/material.cpp index f073baf..3fa95a9 100755 --- a/material.cpp +++ b/material.cpp @@ -66,11 +66,15 @@ namespace vb01{ case Uniform::TEXTURE:{ TextureUniform *u = (TextureUniform*)uni; shader->setInt(2 * i, u->name + (u->animatable ? ".pastTexture" : "")); - shader->setBool(u->animatable, u->name + ".animated"); - if(u->value->getNumFrames() > 1){ - shader->setInt(2 * i + 1, u->name + ".nextTexture"); - shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio"); + if(u->animatable) { + if(u->value->getNumFrames() > 1){ + shader->setInt(2 * i + 1, u->name + ".nextTexture"); + shader->setFloat(u->value->getMixRatio(), u->name + ".mixRatio"); + shader->setBool(true, u->name + ".animated"); + } + else + shader->setBool(false, u->name + ".animated"); } u->value->update(2 * i); diff --git a/material.h b/material.h index 4c0b010..6b814d0 100755 --- a/material.h +++ b/material.h @@ -153,13 +153,13 @@ namespace vb01{ void update(); 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 void addIntUniform(std::string name, int value){uniforms.push_back(new IntUniform(name, value));} + inline void addBoolUniform(std::string name, bool value){uniforms.push_back(new BoolUniform(name, value));} + inline void addFloatUniform(std::string name, float value){uniforms.push_back(new FloatUniform(name, value));} + inline void addVec2Uniform(std::string name, Vector2 value){uniforms.push_back(new Vector2Uniform(name, value));} + inline void addVec3Uniform(std::string name, Vector3 value){uniforms.push_back(new Vector3Uniform(name, value));} + inline void addVec4Uniform(std::string name, Vector4 value){uniforms.push_back(new Vector4Uniform(name, value));} + inline void addTexUniform(std::string name, Texture *value, bool animatable){uniforms.push_back(new TextureUniform(name, value, animatable));} inline Shader* getShader(){return shader;} private: std::vector uniforms; diff --git a/morphAnimationSample.cpp b/morphAnimationSample.cpp index 755bc10..6aecc38 100644 --- a/morphAnimationSample.cpp +++ b/morphAnimationSample.cpp @@ -41,11 +41,11 @@ int main(){ Model *model = new Model(MODEL_PATH + "kek.vb"); Material *mat = new Material(PATH + "texture"); - mat->addVariable("lightingEnabled", false); - mat->addVariable("texturingEnabled", true); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addVariable("textures[0]", texture, true); + mat->addTexUniform("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); @@ -70,8 +70,8 @@ int main(){ */ Box *box = new Box(Vector3(1, 1, 1) * .25); Material *boxMat = new Material(PATH + "texture"); - boxMat->addVariable("texturingEnabled", false); - boxMat->addVariable("diffuseColor", Vector4(1, 0, 0, 1)); + boxMat->addBoolUniform("texturingEnabled", false); + boxMat->addVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); box->setMaterial(boxMat); skeleton->getBone("bone")->attachMesh(box); diff --git a/root.cpp b/root.cpp index bb88d2f..1b74d6b 100755 --- a/root.cpp +++ b/root.cpp @@ -116,8 +116,8 @@ namespace vb01{ void Root::initGuiPlane(Texture *fragTexture, Texture *brightTexture){ guiPlane = new Quad(Vector3(width, height, -1), false); Material *mat = new Material(libPath + "postProcess"); - mat->addVariable("frag", fragTexture, false); - mat->addVariable("bright", brightTexture, false); + mat->addTexUniform("frag", fragTexture, false); + mat->addTexUniform("bright", brightTexture, false); guiPlane->setMaterial(mat); } @@ -197,7 +197,7 @@ namespace vb01{ skybox = new Box(Vector3(1, 1, 1) * 10); Material *skyboxMat = new Material(libPath + "skybox"); Texture *texture = new Texture(paths); - skyboxMat->addVariable("skybox", texture, true); + skyboxMat->addTexUniform("skybox", texture, true); skybox->setMaterial(skyboxMat); } diff --git a/shadowSample.cpp b/shadowSample.cpp index 98a6690..2a1b227 100644 --- a/shadowSample.cpp +++ b/shadowSample.cpp @@ -43,22 +43,22 @@ int main(){ Box *box = new Box(Vector3(40, .2, 40)); Node *boxNode = new Node(); Material *boxMat = new Material(PATH + "texture"); - boxMat->addVariable("texturingEnabled", true); - boxMat->addVariable("lightingEnabled", true); + boxMat->addBoolUniform("texturingEnabled", true); + boxMat->addBoolUniform("lightingEnabled", true); string im0[] = {TEX_PATH + "bricks.jpg"}; Texture *boxTex = new Texture(im0, 1); - boxMat->addVariable("textures[0]", boxTex); + boxMat->addTexUniform("textures[0]", boxTex, true); box->setMaterial(boxMat); boxNode->attachMesh(box); rootNode->attachChild(boxNode); Model *jet = new Model(MODEL_PATH + "jet00.obj"); Material *jetMat = new Material(PATH + "texture"); - jetMat->addVariable("texturingEnabled", true); - jetMat->addVariable("lightingEnabled", true); + jetMat->addBoolUniform("texturingEnabled", true); + jetMat->addBoolUniform("lightingEnabled", true); string im1[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *jetTex = new Texture(im1, 1); - jetMat->addVariable("textures[0]", jetTex); + jetMat->addTexUniform("textures[0]", jetTex, true); jet->setMaterial(jetMat); rootNode->attachChild(jet); jet->setCastShadow(true); diff --git a/skeletalAnimationSample.cpp b/skeletalAnimationSample.cpp index c043c8f..0dfc3b1 100644 --- a/skeletalAnimationSample.cpp +++ b/skeletalAnimationSample.cpp @@ -41,11 +41,11 @@ int main(){ Model *model = new Model(MODEL_PATH + "kek.vb"); Material *mat = new Material(PATH + "texture"); - mat->addVariable("lightingEnabled", false); - mat->addVariable("texturingEnabled", true); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", true); string images[] = {TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(images, 1); - mat->addVariable("textures[0]", texture, true); + mat->addTexUniform("textures[0]", texture, true); model->setMaterial(mat); rootNode->attachChild(model); diff --git a/textSample.cpp b/textSample.cpp index 288648f..1ee0ddf 100644 --- a/textSample.cpp +++ b/textSample.cpp @@ -52,11 +52,11 @@ int main(){ * attaches a diffuse texture with 2 images */ Material *textMat = new Material(PATH + "text"); - textMat->addVariable("texturingEnabled", true); + textMat->addBoolUniform("texturingEnabled", true); string frames[]{TEX_PATH + "bricks.jpg", TEX_PATH + "defaultTexture.jpg"}; Texture *texture = new Texture(frames, 2); - textMat->addVariable("textures[0]", texture, true); + textMat->addTexUniform("textures[0]", texture, true); for(int i = 0; i < numTexts; i++){ Text *text = new Text(FONT_PATH + fonts[i], texts[i], 0, 60000); diff --git a/texture.frag b/texture.frag index a29e3f4..38fb860 100755 --- a/texture.frag +++ b/texture.frag @@ -68,8 +68,8 @@ float getShadow(int id){ return shadow; } -void main(){ - vec4 finalColor = diffuseColor; +void main(){ vec4 finalColor = diffuseColor; + if(texturingEnabled){ vec4 textureColor = texture(textures[0].pastTexture, texCoords); vec4 mixedTexColor = mix(textureColor, texture(textures[0].nextTexture, texCoords), textures[0].mixRatio); @@ -77,11 +77,14 @@ void main(){ } vec3 normal = norm; + if(lightingEnabled){ if(normalMapEnabled){ vec3 n = vec3(texture(textures[1].pastTexture, texCoords)); + if(textures[1].animated) n = mix(n, texture(textures[1].nextTexture, texCoords).rgb, textures[1].mixRatio); + normal = mat3(tan, biTan, norm) * n; } @@ -127,7 +130,9 @@ void main(){ } float brightness = dot(finalColor.rgb, vec3(0.2126, 0.7152, 0.0722)); + if(brightness > 1) BrightColor = vec4(finalColor.rgb, 1); + FragColor = finalColor; }