From 52c59d68791eb357e4f1f22e69364b569bc1e0aa Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 1 Apr 2020 10:31:22 +0300 Subject: [PATCH] removable lights and skybox --- node.cpp | 65 +++++++++++++++++++++++++------------------------------- node.h | 3 +++ root.cpp | 6 ++++++ root.h | 1 + vector.h | 2 ++ 5 files changed, 41 insertions(+), 36 deletions(-) diff --git a/node.cpp b/node.cpp index 8f27ced..ba74222 100755 --- a/node.cpp +++ b/node.cpp @@ -26,9 +26,7 @@ namespace vb01{ for(Text *t : texts) delete t; for(Node *c : children){ - Node *par=c->getParent(); - if(par) - par->dettachChild(c); + //dettachChild(c); delete c; } } @@ -49,27 +47,11 @@ namespace vb01{ } void Node::attachChild(Node *child){ - /* - Node *parent=getParent(); - while(parent){ - parent->getDescendants().push_back(child); - parent=parent->getParent(); - } - descendants.push_back(child); - */ child->setParent(this); children.push_back(child); } void Node::dettachChild(Node *child){ - /* - Node *parent=getParent(); - while(parent){ - parent->getDescendants().push_back(child); - parent=parent->getParent(); - } - descendants.push_back(child); - */ child->setParent(nullptr); int id=-1; for(int i=0;inumLights++; lights.push_back(light); light->setNode(this); - Root *root=Root::getSingleton(); + updateShaders(); + } - Node *rootNode=Root::getSingleton()->getRootNode(); - vector descendants; - rootNode->getDescendants(rootNode,descendants); - descendants.push_back(rootNode); - root->numLights++; - for(Node *n : descendants){ - vector meshes=n->getMeshes(); - for(Mesh *m : meshes){ - Material *mat=m->getMaterial(); - string str=to_string(root->numLights>0?root->numLights:1); - if(mat){ - //mat->getShader()->editShader(true,'=',';',str); - mat->getShader()->editShader(false,'=',';',str); - } - } - } + void Node::removeLight(int id){ + Root::getSingleton()->numLights--; + Light *light=lights[id]; + light->setNode(nullptr); + lights.erase(lights.begin()+id); + updateShaders(); } void Node::addText(Text *text){ @@ -242,4 +216,23 @@ namespace vb01{ return p; } + + void Node::updateShaders(){ + Root *root=Root::getSingleton(); + Node *rootNode=Root::getSingleton()->getRootNode(); + vector descendants; + rootNode->getDescendants(rootNode,descendants); + descendants.push_back(rootNode); + for(Node *n : descendants){ + vector meshes=n->getMeshes(); + for(Mesh *m : meshes){ + Material *mat=m->getMaterial(); + string str=to_string(root->numLights>0?root->numLights:1); + if(mat){ + //mat->getShader()->editShader(true,'=',';',str); + mat->getShader()->editShader(false,'=',';',str); + } + } + } + } } diff --git a/node.h b/node.h index 36da0a3..977a5e7 100755 --- a/node.h +++ b/node.h @@ -27,6 +27,7 @@ namespace vb01{ void attachChild(Node*); void dettachChild(Node*); void addLight(Light*); + void removeLight(int); void addText(Text*); void lookAt(Vector3,Vector3); Transform getWorldTransform(); @@ -57,6 +58,8 @@ namespace vb01{ inline void setVisible(bool v){this->visible=v;} void getDescendants(Node*,std::vector&); protected: + void updateShaders(); + bool visible=true; Vector3 pos,scale; Quaternion orientation; diff --git a/root.cpp b/root.cpp index 9aee6aa..2f660c7 100755 --- a/root.cpp +++ b/root.cpp @@ -162,4 +162,10 @@ namespace vb01{ skyboxMat->addDiffuseMap(textures); skybox->setMaterial(skyboxMat); } + + void Root::removeSkybox(){ + Material *mat=skybox->getMaterial(); + delete skybox; + skybox=nullptr; + } } diff --git a/root.h b/root.h index 6fc1b42..84f10b0 100755 --- a/root.h +++ b/root.h @@ -35,6 +35,7 @@ namespace vb01{ inline void setBlurLevel(bool level){this->blurLevel=level;} inline Box* getSkybox(){return skybox;} void createSkybox(std::string[6]); + void removeSkybox(); static Root* getSingleton(); int numLights=0; private: diff --git a/vector.h b/vector.h index 9735de0..b56f544 100755 --- a/vector.h +++ b/vector.h @@ -2,6 +2,7 @@ #define VECTOR_H #include +#include namespace vb01{ struct Vector4{ @@ -67,6 +68,7 @@ namespace vb01{ float getLength(){return std::sqrt(getLengthSq());} float dot(Vector2 v){return x*v.x+y*v.y;} float getAngleBetween(Vector2 v){return std::acos(dot(v));} + float getDistanceFrom(Vector2 v){return (v-*this).getLengthSq();} Vector2 norm(){return *this/getLength();} float x,y;