diff --git a/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.cpp b/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.cpp index 54931804369..0e50ca0d8f2 100644 --- a/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.cpp +++ b/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.cpp @@ -62,17 +62,30 @@ VisualStyle::VisualStyle() displayFlags.setOriginalData(&d_displayFlags); } -void VisualStyle::fwdDraw(VisualParams* vparams) +void VisualStyle::updateVisualFlags(VisualParams* vparams) { backupFlags = vparams->displayFlags(); vparams->displayFlags() = sofa::core::visual::merge_displayFlags(backupFlags, d_displayFlags.getValue()); } -void VisualStyle::bwdDraw(VisualParams* vparams) +void VisualStyle::applyBackupFlags(VisualParams* vparams) { vparams->displayFlags() = backupFlags; } + +bool VisualStyle::insertInNode( sofa::core::objectmodel::BaseNode* node ) +{ + node->addVisualStyle(this); + return true; +} + +bool VisualStyle::removeInNode( sofa::core::objectmodel::BaseNode* node ) +{ + node->removeVisualStyle(this); + return true; +} + helper::WriteAccessor addVisualStyle( simulation::Node::SPtr node ) { const VisualStyle::SPtr visualStyle = New(); diff --git a/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.h b/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.h index eb43f2f5ebf..e566118cdab 100644 --- a/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.h +++ b/Sofa/Component/Visual/src/sofa/component/visual/VisualStyle.h @@ -21,8 +21,10 @@ ******************************************************************************/ #pragma once #include +#include #include +#include #include #include @@ -56,18 +58,21 @@ namespace sofa::component::visual * showNormals hideNormals * showWireframe hideWireframe */ -class SOFA_COMPONENT_VISUAL_API VisualStyle : public sofa::core::visual::VisualModel +class SOFA_COMPONENT_VISUAL_API VisualStyle : public sofa::core::visual::BaseVisualStyle { public: - SOFA_CLASS(VisualStyle,sofa::core::visual::VisualModel); + SOFA_CLASS(VisualStyle,sofa::core::visual::BaseVisualStyle); typedef sofa::core::visual::VisualParams VisualParams; typedef sofa::core::visual::DisplayFlags DisplayFlags; protected: VisualStyle(); public: - void fwdDraw(VisualParams* ) override; - void bwdDraw(VisualParams* ) override; + void updateVisualFlags(VisualParams* ) override; + void applyBackupFlags(VisualParams* ) override; + + bool insertInNode( sofa::core::objectmodel::BaseNode* node ); + bool removeInNode( sofa::core::objectmodel::BaseNode* node ); SOFA_ATTRIBUTE_DEPRECATED__RENAME_DATA_IN_VISUAL() sofa::core::objectmodel::RenamedData displayFlags; diff --git a/Sofa/framework/Core/CMakeLists.txt b/Sofa/framework/Core/CMakeLists.txt index 4910d364c28..003d978cb06 100644 --- a/Sofa/framework/Core/CMakeLists.txt +++ b/Sofa/framework/Core/CMakeLists.txt @@ -187,6 +187,7 @@ set(HEADER_FILES ${SRC_ROOT}/topology/TopologySubsetData.cpp ${SRC_ROOT}/topology/TopologySubsetIndices.h ${SRC_ROOT}/trait/DataTypes.h + ${SRC_ROOT}/visual/BaseVisualStyle.h ${SRC_ROOT}/visual/Data[DisplayFlags].h ${SRC_ROOT}/visual/DisplayFlags.h ${SRC_ROOT}/visual/FlagTreeItem.h diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.h b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.h index 79cbe8e255b..16d8e21ee3f 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.h +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseNode.h @@ -23,6 +23,7 @@ #include #include +#include namespace sofa::core::objectmodel { @@ -172,6 +173,7 @@ class SOFA_CORE_API BaseNode : public virtual Base BASENODE_ADD_SPECIAL_COMPONENT( core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting ) BASENODE_ADD_SPECIAL_COMPONENT( core::visual::Shader, Shader, shaders ) BASENODE_ADD_SPECIAL_COMPONENT( core::visual::VisualModel, VisualModel, visualModel ) + BASENODE_ADD_SPECIAL_COMPONENT( core::visual::BaseVisualStyle, VisualStyle, visualStyle ) BASENODE_ADD_SPECIAL_COMPONENT( core::visual::VisualManager, VisualManager, visualManager ) BASENODE_ADD_SPECIAL_COMPONENT( core::CollisionModel, CollisionModel, collisionModel ) BASENODE_ADD_SPECIAL_COMPONENT( core::collision::Pipeline, CollisionPipeline, collisionPipeline ) diff --git a/Sofa/framework/Core/src/sofa/core/visual/BaseVisualStyle.h b/Sofa/framework/Core/src/sofa/core/visual/BaseVisualStyle.h new file mode 100644 index 00000000000..bfeb50bdd67 --- /dev/null +++ b/Sofa/framework/Core/src/sofa/core/visual/BaseVisualStyle.h @@ -0,0 +1,51 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once +#include +#include +#include +#include + + +namespace sofa::core::visual +{ + +class SOFA_CORE_API BaseVisualStyle : public sofa::core::objectmodel::BaseObject +{ +public: + SOFA_CLASS(BaseVisualStyle,sofa::core::objectmodel::BaseObject); + + typedef sofa::core::visual::VisualParams VisualParams; + typedef sofa::core::visual::DisplayFlags DisplayFlags; + +protected: + BaseVisualStyle() { } + ~BaseVisualStyle() override { } + +public: + virtual void updateVisualFlags(VisualParams* ) { }; + virtual void applyBackupFlags(VisualParams* ) { }; + +}; + +} // namespace sofa::simulation::graph + diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp index f3c4f3d34ec..8032425b1dc 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.cpp @@ -94,6 +94,7 @@ Node::Node(const std::string& name) , configurationSetting(initLink("configurationSetting", "The ConfigurationSetting(s) attached to this node")) , shaders(initLink("shaders", "The shaders attached to this node")) , visualModel(initLink("visualModel", "The VisualModel(s) attached to this node")) + , visualStyle(initLink("visualStyle", "The VisualStyle(s) attached to this node")) , visualManager(initLink("visualManager", "The VisualManager(s) attached to this node")) , collisionModel(initLink("collisionModel", "The CollisionModel(s) attached to this node")) , unsorted(initLink("unsorted", "The remaining objects attached to this node")) @@ -930,6 +931,7 @@ void Node::printComponents() using core::objectmodel::ContextObject; using core::collision::Pipeline; using core::BaseState; + using core::visual::BaseVisualStyle; std::stringstream sstream; @@ -987,6 +989,9 @@ void Node::printComponents() sstream << "\n" << "VisualModel: "; for (NodeSequence::iterator i = visualModel.begin(), iend = visualModel.end(); i != iend; ++i) sstream << (*i)->getName() << " "; + sstream << "\n" << "BaseVisualStyle: "; + for (NodeSingle::iterator i = visualStyle.begin(), iend = visualStyle.end(); i != iend; ++i) + sstream << (*i)->getName() << " "; sstream << "\n" << "CollisionModel: "; for (NodeSequence::iterator i = collisionModel.begin(), iend = collisionModel.end(); i != iend; ++i) sstream << (*i)->getName() << " "; @@ -1045,6 +1050,7 @@ NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ContextObject, ContextOb NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting ) NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::Shader, Shader, shaders ) NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualModel, VisualModel, visualModel ) +NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::BaseVisualStyle, VisualStyle, visualStyle ) NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualManager, VisualManager, visualManager ) NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::CollisionModel, CollisionModel, collisionModel ) NODE_DEFINE_SEQUENCE_ACCESSOR( sofa::core::collision::Pipeline, CollisionPipeline, collisionPipeline ) @@ -1071,6 +1077,7 @@ template class NodeSequence; template class NodeSingle; template class NodeSingle; +template class NodeSingle; template class NodeSingle; template class NodeSingle; template class NodeSingle; diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h index 0f6d4aa7d54..2318c243d9f 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Node.h @@ -127,6 +127,7 @@ extern template class NodeSequence; extern template class NodeSingle; extern template class NodeSingle; +extern template class NodeSingle; extern template class NodeSingle; extern template class NodeSingle; extern template class NodeSingle; @@ -255,6 +256,7 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode, NodeSingle animationManager; NodeSingle visualLoop; + NodeSingle visualStyle; NodeSingle topology; NodeSingle meshTopology; NodeSingle state; @@ -599,6 +601,7 @@ class SOFA_SIMULATION_CORE_API Node : public sofa::core::objectmodel::BaseNode, NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::objectmodel::ConfigurationSetting, ConfigurationSetting, configurationSetting ) NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::Shader, Shader, shaders ) NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualModel, VisualModel, visualModel ) + NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::BaseVisualStyle, VisualStyle, visualStyle ) NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::visual::VisualManager, VisualManager, visualManager ) NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::CollisionModel, CollisionModel, collisionModel ) NODE_DECLARE_SEQUENCE_ACCESSOR( sofa::core::collision::Pipeline, CollisionPipeline, collisionPipeline ) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/Simulation.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/Simulation.cpp index 337ac2008ce..79579eae660 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/Simulation.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/Simulation.cpp @@ -230,7 +230,8 @@ void animate(Node* root, SReal dt) void updateVisual(Node* root) { SCOPED_TIMER("Simulation::updateVisual"); - + + sofa::core::visual::VisualParams* vparams = sofa::core::visual::visualparams::defaultInstance(); if (sofa::core::visual::VisualLoop* vloop = root->getVisualLoop()) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.cpp index cde01030ab4..e64d50dda39 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.cpp @@ -46,14 +46,32 @@ Visitor::Result VisualVisitor::processNodeTopDown(simulation::Node* node) } -Visitor::Result VisualDrawVisitor::processNodeTopDown(simulation::Node* node) +void VisualVisitor::fwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm) { + vm-> updateVisualFlags(vparams); +} + + +void VisualVisitor::bwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm) +{ + vm-> applyBackupFlags(vparams); +} + +Visitor::Result VisualDrawVisitor::processNodeTopDown(simulation::Node* node) +{ // NB: hasShader is only used when there are visual models and getShader does a graph search when there is no shader, // which will most probably be the case when there are no visual models, so we skip the search unless we have visual models. hasShader = !node->visualModel.empty() && (node->getShader()!=nullptr); + if(node->visualStyle.get()) + { + fwdProcessVisualStyle(node,node->visualStyle.get()); + } + + for_each(this, node, node->visualModel, &VisualDrawVisitor::fwdVisualModel); + this->VisualVisitor::processNodeTopDown(node); return RESULT_CONTINUE; @@ -65,6 +83,12 @@ void VisualDrawVisitor::processNodeBottomUp(simulation::Node* node) if (!vparams->displayFlags().getShowVisualModels()) return; + if(node->visualStyle.get()) + { + bwdProcessVisualStyle(node,node->visualStyle.get()); + } + + for_each(this, node, node->visualModel, &VisualDrawVisitor::bwdVisualModel); } @@ -149,11 +173,27 @@ void VisualDrawVisitor::processVisualModel(simulation::Node* node, core::visual: Visitor::Result VisualUpdateVisitor::processNodeTopDown(simulation::Node* node) { - for_each(this, node, node->visualModel, &VisualUpdateVisitor::processVisualModel); + //Necessary check for first draw + if(node->visualStyle.get()) + { + fwdProcessVisualStyle(node,node->visualStyle.get()); + } + + for_each(this, node, node->visualModel, &VisualUpdateVisitor::processVisualModel); return RESULT_CONTINUE; } +void VisualUpdateVisitor::processNodeBottomUp(simulation::Node* node) +{ + //Necessary check for first draw + if(node->visualStyle.get()) + { + bwdProcessVisualStyle(node,node->visualStyle.get()); + } +} + + void VisualUpdateVisitor::processVisualModel(simulation::Node*, core::visual::VisualModel* vm) { helper::ScopedAdvancedTimer timer("VisualUpdateVisitor process: " + vm->getName()); diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.h index 44ca10602b6..f44a98cef82 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/VisualVisitor.h @@ -24,6 +24,7 @@ #include #include +#include namespace sofa::simulation @@ -38,6 +39,8 @@ class SOFA_SIMULATION_CORE_API VisualVisitor : public Visitor {} virtual void processVisualModel(simulation::Node* node, core::visual::VisualModel* vm) = 0; + virtual void fwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm); + virtual void bwdProcessVisualStyle(simulation::Node* node, core::visual::BaseVisualStyle* vm); virtual void processObject(simulation::Node* /*node*/, core::objectmodel::BaseObject* /*o*/) {} Result processNodeTopDown(simulation::Node* node) override; @@ -54,14 +57,14 @@ class SOFA_SIMULATION_CORE_API VisualVisitor : public Visitor core::visual::VisualParams* vparams; }; + class SOFA_SIMULATION_CORE_API VisualDrawVisitor : public VisualVisitor { public: bool hasShader; VisualDrawVisitor(core::visual::VisualParams* params) - : VisualVisitor(params) - { - } + : VisualVisitor(params) + {}; Result processNodeTopDown(simulation::Node* node) override; void processNodeBottomUp(simulation::Node* node) override; virtual void fwdVisualModel(simulation::Node* node, core::visual::VisualModel* vm); @@ -83,11 +86,10 @@ class SOFA_SIMULATION_CORE_API VisualUpdateVisitor : public VisualVisitor virtual void processVisualModel(simulation::Node*, core::visual::VisualModel* vm) override; Result processNodeTopDown(simulation::Node* node) override; + void processNodeBottomUp(simulation::Node* node) override; const char* getClassName() const override { return "VisualUpdateVisitor"; } -protected: - core::visual::VisualParams* m_vparams; }; class SOFA_SIMULATION_CORE_API VisualInitVisitor : public VisualVisitor