From 4ca06fe336e2ad3f5400cb06ed6df9d140daee5c Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Thu, 7 Dec 2023 20:47:20 +0000 Subject: [PATCH 01/20] test script tag Signed-off-by: Dharini Dutia --- src/rendering/SceneManager.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/rendering/SceneManager.cc b/src/rendering/SceneManager.cc index ab5d0ee92b..fc8ef5cf28 100644 --- a/src/rendering/SceneManager.cc +++ b/src/rendering/SceneManager.cc @@ -360,6 +360,12 @@ rendering::VisualPtr SceneManager::CreateVisual(Entity _id, else if (_visual.Material()) { material = this->LoadMaterial(*_visual.Material()); + if (!_visual.Material()->ScriptName().empty()) + { + auto scriptElem = _visual.Material()->ScriptName(); + gzwarn << "Testing material " << scriptElem + << std::endl; + } } // Don't set a default material for meshes because they // may have their own From c6b08b851684651697cd7bf310d63ec903086a7d Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Sat, 9 Dec 2023 03:30:46 +0000 Subject: [PATCH 02/20] add dark grey before creating component Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 14 +++++++++++++- src/rendering/SceneManager.cc | 6 ------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 638797ba6f..e22126ec22 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -788,8 +788,20 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) // \todo(louise) Populate with default material if undefined if (_visual->Material()) { + sdf::Material mat = *_visual->Material(); + if (!_visual->Material()->ScriptName().empty()) + { + std::string scriptElem = mat.ScriptName(); + gzwarn << "Testing material " << scriptElem + << std::endl; + if((scriptElem == "Gazebo/DarkGrey") | (scriptElem == "Gazebo/DarkGray")) { + mat.SetAmbient(gz::math::Color(0.0f, 0.0f, 1.0f)); + mat.SetDiffuse(gz::math::Color(0.0f, 0.0f, 1.0f)); + mat.SetSpecular(gz::math::Color(0.1f, 0.1f, 0.1f, 1.0f)); + } + } this->dataPtr->ecm->CreateComponent(visualEntity, - components::Material(*_visual->Material())); + components::Material(mat)); } // store the plugin in a component diff --git a/src/rendering/SceneManager.cc b/src/rendering/SceneManager.cc index fc8ef5cf28..ab5d0ee92b 100644 --- a/src/rendering/SceneManager.cc +++ b/src/rendering/SceneManager.cc @@ -360,12 +360,6 @@ rendering::VisualPtr SceneManager::CreateVisual(Entity _id, else if (_visual.Material()) { material = this->LoadMaterial(*_visual.Material()); - if (!_visual.Material()->ScriptName().empty()) - { - auto scriptElem = _visual.Material()->ScriptName(); - gzwarn << "Testing material " << scriptElem - << std::endl; - } } // Don't set a default material for meshes because they // may have their own From 29dc2aabfe4de1f2a51547e52eaeddb11ca89339 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Thu, 14 Dec 2023 22:15:26 +0000 Subject: [PATCH 03/20] material parser Signed-off-by: Dharini Dutia --- src/CMakeLists.txt | 28 +- src/SdfEntityCreator.cc | 31 +- src/rendering/MaterialParser/ConfigLoader.cc | 423 ++++ src/rendering/MaterialParser/ConfigLoader.hh | 164 ++ .../MaterialParser/MaterialParser.cc | 102 + .../MaterialParser/MaterialParser.hh | 49 + src/rendering/MaterialParser/gazebo.material | 1707 +++++++++++++++++ 7 files changed, 2485 insertions(+), 19 deletions(-) create mode 100644 src/rendering/MaterialParser/ConfigLoader.cc create mode 100644 src/rendering/MaterialParser/ConfigLoader.hh create mode 100644 src/rendering/MaterialParser/MaterialParser.cc create mode 100644 src/rendering/MaterialParser/MaterialParser.hh create mode 100644 src/rendering/MaterialParser/gazebo.material diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6e666dafc9..a240011073 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,13 +35,9 @@ gz_msgs_generate_messages_impl( DEPENDENCY_DESCRIPTIONS ${msgs_desc_file} ) -set(network_sources - network/NetworkConfig.cc - network/NetworkManager.cc - network/NetworkManagerPrimary.cc - network/NetworkManagerSecondary.cc - network/PeerInfo.cc - network/PeerTracker.cc +set(cli_sources + gz.cc + cmd/ModelCommandAPI.cc ) set(comms_sources @@ -55,9 +51,18 @@ set(gui_sources PARENT_SCOPE ) -set(cli_sources - gz.cc - cmd/ModelCommandAPI.cc +set(material_sources + rendering/MaterialParser/MaterialParser.cc + rendering/MaterialParser/ConfigLoader.cc +) + +set(network_sources + network/NetworkConfig.cc + network/NetworkManager.cc + network/NetworkManagerPrimary.cc + network/NetworkManagerSecondary.cc + network/PeerInfo.cc + network/PeerTracker.cc ) set (sources @@ -89,9 +94,10 @@ set (sources Util.cc View.cc World.cc - ${network_sources} ${comms_sources} ${msgs_sources} + ${network_sources} + ${material_sources} ) set (gtest_sources diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index e22126ec22..29edd1f20f 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -84,6 +84,8 @@ #include "gz/sim/components/WindMode.hh" #include "gz/sim/components/World.hh" +#include "rendering/MaterialParser/MaterialParser.hh" + class gz::sim::SdfEntityCreatorPrivate { /// \brief Pointer to entity component manager. We don't assume ownership. @@ -788,20 +790,33 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) // \todo(louise) Populate with default material if undefined if (_visual->Material()) { - sdf::Material mat = *_visual->Material(); + sdf::Material visualMaterial = *_visual->Material(); + if (!_visual->Material()->ScriptUri().empty()) + { + std::string scriptUri = visualMaterial.ScriptUri(); + if (scriptUri != "file://media/materials/scripts/gazebo.material") { + gzwarn << "Custom material scripts are not supported." + << std::endl; + } + } if (!_visual->Material()->ScriptName().empty()) { - std::string scriptElem = mat.ScriptName(); - gzwarn << "Testing material " << scriptElem + std::string scriptName = visualMaterial.ScriptName(); + + if((scriptName.find("Gazebo/") != std::string::npos)) { + gzwarn << "Using internal gazebo.material to parse " << scriptName << std::endl; - if((scriptElem == "Gazebo/DarkGrey") | (scriptElem == "Gazebo/DarkGray")) { - mat.SetAmbient(gz::math::Color(0.0f, 0.0f, 1.0f)); - mat.SetDiffuse(gz::math::Color(0.0f, 0.0f, 1.0f)); - mat.SetSpecular(gz::math::Color(0.1f, 0.1f, 0.1f, 1.0f)); + MaterialParser* materialParser = new MaterialParser(); + materialParser->Load(); + std::vector> parsed = materialParser->GetMaterialValues(scriptName); + + visualMaterial.SetAmbient(gz::math::Color(parsed[0][0],parsed[0][1], parsed[0][2])); + visualMaterial.SetDiffuse(gz::math::Color(parsed[1][0],parsed[1][1], parsed[1][2])); + visualMaterial.SetSpecular(gz::math::Color(parsed[2][0],parsed[2][1], parsed[2][2], parsed[2][3])); } } this->dataPtr->ecm->CreateComponent(visualEntity, - components::Material(mat)); + components::Material(visualMaterial)); } // store the plugin in a component diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc new file mode 100644 index 0000000000..c3152d76d5 --- /dev/null +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -0,0 +1,423 @@ +#include "ConfigLoader.hh" + +#include +#include +#include +#include +#include + +using namespace gz; +using namespace sim; + +void ConfigLoader::loadAllFiles(ConfigLoader* c, const std::string& path) +{ + try + { + for ( std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir ) + { + std::filesystem::path p(*dir); + if(p.extension() == c->m_fileEnding) + { + std::ifstream in((*dir).path().string().c_str(), std::ios::binary); + c->parseScript(in); + } + } + } + catch (std::filesystem::filesystem_error &e) + { + std::cerr << e.what() << std::endl; + } +} + +ConfigLoader::ConfigLoader(const std::string& fileEnding) +{ + //Register as a ScriptLoader + m_fileEnding = fileEnding; +} + +ConfigLoader::~ConfigLoader() +{ + clearScriptList(); + +} + +void ConfigLoader::clearScriptList() +{ + std::map ::iterator i; + for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) + { + delete i->second; + } + m_scriptList.clear(); +} + +ConfigNode *ConfigLoader::getConfigScript(const std::string &name) +{ + std::map ::iterator i; + + std::string key = name; + i = m_scriptList.find(key); + + //If found.. + if (i != m_scriptList.end()) + { + return i->second; + } + else + { + return NULL; + } +} + +std::map ConfigLoader::getAllConfigScripts () +{ + return m_scriptList; +} + +void ConfigLoader::parseScript(std::ifstream &stream) +{ + //Get first token + _nextToken(stream); + if (tok == TOKEN_EOF) + { + stream.close(); + return; + } + + //Parse the script + _parseNodes(stream, 0); + + stream.close(); +} + +void ConfigLoader::_nextToken(std::ifstream &stream) +{ + lastTok = tok; + lastTokVal = tokVal; + + //EOF token + if (stream.eof()) + { + tok = TOKEN_EOF; + return; + } + + //(Get next character) + int ch = stream.get(); + if (ch == -1) + { + tok = TOKEN_EOF; + return; + } + while ((ch == ' ' || ch == 9) && !stream.eof()) + { //Skip leading spaces / tabs + ch = stream.get(); + } + + if (stream.eof()) + { + tok = TOKEN_EOF; + return; + } + + //Newline token + if (ch == '\r' || ch == '\n') + { + do + { + ch = stream.get(); + } while ((ch == '\r' || ch == '\n') && !stream.eof()); + + stream.unget(); + + tok = TOKEN_NewLine; + return; + } + + //Open brace token + else if (ch == '{') + { + tok = TOKEN_OpenBrace; + return; + } + + //Close brace token + else if (ch == '}') + { + tok = TOKEN_CloseBrace; + return; + } + + //Text token + if (ch < 32 || ch > 122) //Verify valid char + { + throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); + } + + tokVal = ""; + tok = TOKEN_Text; + do + { + //Skip comments + if (ch == '/') + { + int ch2 = stream.peek(); + + //C++ style comment (//) + if (ch2 == '/') + { + stream.get(); + do + { + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); + + tok = TOKEN_NewLine; + return; + } + else if (ch2 == '*') + { + stream.get(); + do + { + ch = stream.get(); + ch2 = stream.peek(); + } while (!(ch == '*' && ch2 == '/') && !stream.eof()); + stream.get(); + do + { + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); + continue; + } + } + + //Add valid char to tokVal + tokVal += (char)ch; + + //Next char + ch = stream.get(); + + } while (ch > 32 && ch <= 122 && !stream.eof()); + + stream.unget(); + + return; +} + +void ConfigLoader::_skipNewLines(std::ifstream &stream) +{ + while (tok == TOKEN_NewLine) + { + _nextToken(stream); + } +} + +void ConfigLoader::_parseNodes(std::ifstream &stream, ConfigNode *parent) +{ + typedef std::pair ScriptItem; + + while (true) + { + switch (tok) + { + //Node + case TOKEN_Text: + //Add the new node + ConfigNode *newNode; + if (parent) + { + newNode = parent->addChild(tokVal); + } + else + { + newNode = new ConfigNode(0, tokVal); + } + + //Get values + _nextToken(stream); + while (tok == TOKEN_Text) + { + newNode->addValue(tokVal); + _nextToken(stream); + } + + //Add root nodes to scriptList + if (!parent){ + std::string key; + + if (newNode->getValues().empty()) + { + key = newNode->getName() + ' '; + } + else + { + key = newNode->getName() + ' ' + newNode->getValues().front(); + } + + m_scriptList.insert(ScriptItem(key, newNode)); + } + + _skipNewLines(stream); + + //Add any sub-nodes + if (tok == TOKEN_OpenBrace) + { + //Parse nodes + _nextToken(stream); + _parseNodes(stream, newNode); + //Check for matching closing brace + if (tok != TOKEN_CloseBrace) + { + throw std::runtime_error("Parse Error: Expecting closing brace"); + } + _nextToken(stream); + _skipNewLines(stream); + } + + break; + + //Out of place brace + case TOKEN_OpenBrace: + throw std::runtime_error("Parse Error: Opening brace out of plane"); + break; + + //Return if end of nodes have been reached + case TOKEN_CloseBrace: + return; + + //Return if reached end of file + case TOKEN_EOF: + return; + + case TOKEN_NewLine: + _nextToken(stream); + break; + } + }; +} + +ConfigNode::ConfigNode(ConfigNode *parent, const std::string &name) +{ + m_name = name; + m_parent = parent; + _removeSelf = true; //For proper destruction + m_lastChildFound = -1; + + //Add self to parent's child list (unless this is the root node being created) + if (parent != NULL) + { + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); + } +} + +ConfigNode::~ConfigNode() +{ + //Delete all children + std::vector::iterator i; + for (i = m_children.begin(); i != m_children.end(); i++) + { + ConfigNode *node = *i; + node->_removeSelf = false; + delete node; + } + m_children.clear(); + + //Remove self from parent's child list + if (_removeSelf && m_parent != NULL) + { + m_parent->m_children.erase(_iter); + } +} + +ConfigNode *ConfigNode::addChild(const std::string &name, bool replaceExisting) +{ + if (replaceExisting) + { + ConfigNode *node = findChild(name, false); + if (node) + { + return node; + } + } + return new ConfigNode(this, name); +} + +ConfigNode *ConfigNode::findChild(const std::string &name, bool recursive) +{ + int indx, prevC, nextC; + int childCount = (int)m_children.size(); + + if (m_lastChildFound != -1) + { + //If possible, try checking the nodes neighboring the last successful search + //(often nodes searched for in sequence, so this will std search speeds). + prevC = m_lastChildFound-1; if (prevC < 0) prevC = 0; else if (prevC >= childCount) prevC = childCount-1; + nextC = m_lastChildFound+1; if (nextC < 0) nextC = 0; else if (nextC >= childCount) nextC = childCount-1; + for (indx = prevC; indx <= nextC; ++indx) + { + ConfigNode *node = m_children[indx]; + if (node->m_name == name) + { + m_lastChildFound = indx; + return node; + } + } + + //If not found that way, search for the node from start to finish, avoiding the + //already searched area above. + for (indx = nextC + 1; indx < childCount; ++indx) + { + ConfigNode *node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + for (indx = 0; indx < prevC; ++indx) + { + ConfigNode *node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } + else + { + //Search for the node from start to finish + for (indx = 0; indx < childCount; ++indx){ + ConfigNode *node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } + + //If not found, search child nodes (if recursive == true) + if (recursive) + { + for (indx = 0; indx < childCount; ++indx) + { + m_children[indx]->findChild(name, recursive); + } + } + + //Not found anywhere + return NULL; +} + +void ConfigNode::setParent(ConfigNode *newParent) +{ + //Remove self from current parent + m_parent->m_children.erase(_iter); + + //Set new parent + m_parent = newParent; + + //Add self to new parent + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); +} \ No newline at end of file diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh new file mode 100644 index 0000000000..993c86108c --- /dev/null +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2023 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GZ_SIM_CONFIG_LOADER_HH_ +#define GZ_SIM_CONFIG_LOADER_HH_ + +#include +#include +#include +#include +#include + + +namespace gz +{ +namespace sim +{ + class ConfigNode; + + class ConfigLoader + { + public: + static void loadAllFiles(ConfigLoader* c, const std::string& path); + + ConfigLoader(const std::string& fileEnding); + virtual ~ConfigLoader(); + + std::string m_fileEnding; + + // For a line like + // entity animals/dog + // { + // ... + // } + // The type is "entity" and the name is "animals/dog" + // Or if animal/dog was not there then name is "" + virtual ConfigNode *getConfigScript (const std::string &name); + + virtual std::map getAllConfigScripts (); + + virtual void parseScript(std::ifstream &stream); + + + protected: + + float m_LoadOrder; + // like "*.object" + + std::map m_scriptList; + + enum Token + { + TOKEN_Text, + TOKEN_NewLine, + TOKEN_OpenBrace, + TOKEN_CloseBrace, + TOKEN_EOF, + }; + + Token tok, lastTok; + std::string tokVal, lastTokVal; + + void _parseNodes(std::ifstream &stream, ConfigNode *parent); + void _nextToken(std::ifstream &stream); + void _skipNewLines(std::ifstream &stream); + + virtual void clearScriptList(); + }; + + class ConfigNode + { + public: + ConfigNode(ConfigNode *parent, const std::string &name = "untitled"); + ~ConfigNode(); + + inline void setName(const std::string &name) + { + this->m_name = name; + } + + inline std::string &getName() + { + return m_name; + } + + inline void addValue(const std::string &value) + { + m_values.push_back(value); + } + + inline void clearValues() + { + m_values.clear(); + } + + inline std::vector &getValues() + { + return m_values; + } + + inline void getValuesInFloat(std::vector &floatValues) + { + for (const auto& str : m_values) + { + floatValues.push_back(std::stof(str)); + } + } + + inline const std::string &getValue(unsigned int index = 0) + { + assert(index < m_values.size()); + return m_values[index]; + } + + ConfigNode *addChild(const std::string &name = "untitled", bool replaceExisting = false); + ConfigNode *findChild(const std::string &name, bool recursive = false); + + inline std::vector &getChildren() + { + return m_children; + } + + inline ConfigNode *getChild(unsigned int index = 0) + { + assert(index < m_children.size()); + return m_children[index]; + } + + void setParent(ConfigNode *newParent); + + inline ConfigNode *getParent() + { + return m_parent; + } + + private: + std::string m_name; + std::vector m_values; + std::vector m_children; + ConfigNode *m_parent; + + int m_lastChildFound; //The last child node's index found with a call to findChild() + + std::vector::iterator _iter; + bool _removeSelf; + }; +} +} + +#endif \ No newline at end of file diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc new file mode 100644 index 0000000000..92ab52f752 --- /dev/null +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2023 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +#include "ConfigLoader.hh" +#include "MaterialParser.hh" + +using namespace gz; +using namespace sim; + +///////////////////////////////////////////////// +MaterialParser::MaterialParser() +{ + this->configLoader = new ConfigLoader(".material"); +} + +///////////////////////////////////////////////// +MaterialParser::~MaterialParser() +{ + delete this->configLoader; +} + +///////////////////////////////////////////////// +void MaterialParser::Load(const std::string &_path) +{ + ConfigLoader::loadAllFiles(this->configLoader, _path); +} + +///////////////////////////////////////////////// +std::vector> MaterialParser::GetMaterialValues(std::string material) const +{ + std::vector> values; + std::map scripts = + this->configLoader->getAllConfigScripts(); + + std::map::iterator it; + + for (it = scripts.begin(); it != scripts.end(); ++it) + { + std::string name = it->first; + + if (name.find(material) != std::string::npos) + { + ConfigNode *node = it->second; + + ConfigNode *techniqueNode = node->findChild("technique"); + if (techniqueNode) + { + ConfigNode *passNode = techniqueNode->findChild("pass"); + if (passNode) + { + std::size_t index = name.rfind(" "); + if (index != std::string::npos) + { + name = name.substr(index+1); + } + + ConfigNode *ambientNode = passNode->findChild("ambient"); + if (ambientNode) + { + std::vector ambientValues; + ambientNode->getValuesInFloat(ambientValues); + values.push_back(ambientValues); + } + + ConfigNode *diffuseNode = passNode->findChild("diffuse"); + if (diffuseNode) + { + std::vector diffuseValues; + diffuseNode->getValuesInFloat(diffuseValues); + values.push_back(diffuseValues); + } + + ConfigNode *specularNode = passNode->findChild("specular"); + if (specularNode) + { + std::vector specularValues; + specularNode->getValuesInFloat(specularValues); + values.push_back(specularValues); + } + } + } + } + } + return values; +} \ No newline at end of file diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh new file mode 100644 index 0000000000..840cb0dc6e --- /dev/null +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2023 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef GZ_SIM_MATERIALPARSER_HH_ +#define GZ_SIM_MATERIALPARSER_HH_ + +#include +#include + + +namespace gz +{ +namespace sim +{ + class ConfigNode; + class ConfigLoader; + + class MaterialParser + { + public: MaterialParser(); + + public: virtual ~MaterialParser(); + + public: void Load(const std::string &_path); + + public: void Load() { Load(std::filesystem::current_path().string()); }; + + public: std::vector> GetMaterialValues(std::string material) const; + + private: ConfigLoader *configLoader = nullptr; + }; +} +} + +#endif \ No newline at end of file diff --git a/src/rendering/MaterialParser/gazebo.material b/src/rendering/MaterialParser/gazebo.material new file mode 100644 index 0000000000..16211d3512 --- /dev/null +++ b/src/rendering/MaterialParser/gazebo.material @@ -0,0 +1,1707 @@ +vertex_program grid_vp_glsl glsl +{ + source grid_vp.glsl +} + +fragment_program grid_fp_glsl glsl +{ + source grid_fp.glsl +} + +material grid +{ + technique + { + pass + { + vertex_program_ref grid_vp_glsl {} + fragment_program_ref grid_fp_glsl {} + } + } +} + +vertex_program Gazebo/DepthMapVS glsl +{ + source depth_map.vert + + default_params + { + param_named_auto texelOffsets texel_offsets + } +} + +fragment_program Gazebo/DepthMapFS glsl +{ + source depth_map.frag + + default_params + { + param_named_auto pNear near_clip_distance + param_named_auto pFar far_clip_distance + } +} + +material Gazebo/DepthMap +{ + technique + { + pass + { + vertex_program_ref Gazebo/DepthMapVS { } + fragment_program_ref Gazebo/DepthMapFS { } + } + } +} + +vertex_program Gazebo/XYZPointsVS glsl +{ + source depth_points_map.vert +} + +fragment_program Gazebo/XYZPointsFS glsl +{ + source depth_points_map.frag + + default_params + { + param_named_auto width viewport_width + param_named_auto height viewport_height + } +} + +material Gazebo/XYZPoints +{ + technique + { + pass pcd_tex + { + separate_scene_blend one zero one zero + + vertex_program_ref Gazebo/XYZPointsVS { } + fragment_program_ref Gazebo/XYZPointsFS { } + } + } +} + +material Gazebo/Reflectance +{ + technique + { + pass + { + lighting off + texture_unit + { + } + } + } +} + +vertex_program Gazebo/XYZNormalsVS glsl +{ + source depth_normals_map.vert +} + +fragment_program Gazebo/XYZNormalsFS glsl +{ + source depth_normals_map.frag +} + +material Gazebo/XYZNormals +{ + technique + { + pass normals_tex + { + separate_scene_blend one zero one zero + + vertex_program_ref Gazebo/XYZNormalsVS { } + fragment_program_ref Gazebo/XYZNormalsFS { } + } + } +} + +vertex_program Gazebo/LaserScan1stVS glsl +{ + source laser_1st_pass.vert +} + +fragment_program Gazebo/LaserScan1stFS glsl +{ + source laser_1st_pass.frag + + default_params + { + param_named retro float 0.0 + param_named_auto near near_clip_distance + param_named_auto far far_clip_distance + } +} + +material Gazebo/LaserScan1st +{ + technique + { + pass laser_tex + { + separate_scene_blend one zero one zero + + vertex_program_ref Gazebo/LaserScan1stVS { } + fragment_program_ref Gazebo/LaserScan1stFS { } + } + } +} + +vertex_program Gazebo/LaserScan2ndVS glsl +{ + source laser_2nd_pass.vert +} + +fragment_program Gazebo/LaserScan2ndFS glsl +{ + source laser_2nd_pass.frag + + default_params + { + param_named tex1 int 0 + param_named tex2 int 1 + param_named tex3 int 2 + param_named_auto texSize texture_size 0 + } +} + +material Gazebo/LaserScan2nd +{ + technique + { + pass laser_tex_2nd + { + separate_scene_blend one zero one zero + + vertex_program_ref Gazebo/LaserScan2ndVS { } + fragment_program_ref Gazebo/LaserScan2ndFS { } + } + } +} + +material Gazebo/Grey +{ + technique + { + pass main + { + ambient .3 .3 .3 1.0 + diffuse .7 .7 .7 1.0 + specular 0.01 0.01 0.01 1.000000 1.500000 + } + } +} +material Gazebo/Gray : Gazebo/Grey +{ +} + +material Gazebo/DarkGrey +{ + technique + { + pass main + { + ambient .175 .175 .175 1.0 + diffuse .175 .175 .175 1.0 + specular .175 .175 .175 1.000000 1.500000 + } + } +} + +material Gazebo/DarkGray : Gazebo/DarkGrey +{ +} + +material Gazebo/White +{ + technique + { + pass ambient + { + ambient 1 1 1 1 + } + + pass light + { + diffuse 1 1 1 1 + specular .1 .1 .1 128 + } + } +} + +material Gazebo/FlatBlack +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 + diffuse 0.1 0.1 0.1 + specular 0.01 0.01 0.01 1.0 1.0 + } + } +} + +material Gazebo/Black +{ + technique + { + pass + { + ambient 0 0 0 1 + diffuse 0 0 0 1 + specular 0.1 0.1 0.1 1 5.0 + } + } +} + + +material Gazebo/Red +{ + technique + { + pass ambient + { + ambient 1 0 0 + diffuse 1 0 0 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/RedBright +{ + technique + { + pass ambient + { + ambient 0.87 0.26 0.07 + diffuse 0.87 0.26 0.07 + specular 0.87 0.26 0.07 1 1 + } + } +} + +material Gazebo/Green +{ + technique + { + pass ambient + { + ambient 0 1 0 + diffuse 0 1 0 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/Blue +{ + technique + { + pass ambient + { + ambient 0 0 1 + diffuse 0 0 1 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/SkyBlue +{ + technique + { + pass ambient + { + ambient 0.13 0.44 0.70 + diffuse 0 0 1 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/Yellow +{ + technique + { + pass ambient + { + ambient 1 1 0 1 + diffuse 1 1 0 1 + specular 0 0 0 0 0 + } + } +} + +material Gazebo/ZincYellow +{ + technique + { + pass ambient + { + ambient 0.9725 0.9529 0.2078 1 + diffuse 0.9725 0.9529 0.2078 1 + specular 0.9725 0.9529 0.2078 1 1 + } + } +} + +material Gazebo/DarkYellow +{ + technique + { + pass ambient + { + ambient 0.7 0.7 0 1 + diffuse 0.7 0.7 0 1 + specular 0 0 0 0 0 + } + } +} + + +material Gazebo/Purple +{ + technique + { + pass ambient + { + ambient 1 0 1 + diffuse 1 0 1 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/Turquoise +{ + technique + { + pass ambient + { + ambient 0 1 1 + diffuse 0 1 1 + specular 0.1 0.1 0.1 1 1 + } + } +} + +material Gazebo/Orange +{ + technique + { + pass ambient + { + lighting on + + ambient 1 0.5088 0.0468 1 + diffuse 1 0.5088 0.0468 1 + specular 0.5 0.5 0.5 128 + } + } +} + +material Gazebo/Indigo +{ + technique + { + pass ambient + { + ambient 0.33 0.0 0.5 + diffuse 0.33 0.0 0.5 + specular 0.1 0.1 0.1 1 + } + } +} + +material Gazebo/WhiteGlow : Gazebo/White +{ + technique + { + pass light + { + emissive 1 1 1 + } + } +} + +material Gazebo/RedGlow +{ + technique + { + pass ambient + { + ambient 1 0 0 + diffuse 1 0 0 + emissive 1 0 0 + specular 0 0 0 128 + } + + pass light + { + ambient 1 0 0 + diffuse 1 0 0 + emissive 1 0 0 + specular 1 0 0 128 + } + } +} + + + +material Gazebo/GreenGlow : Gazebo/Green +{ + technique + { + pass ambient + { + emissive 0 1 0 + } + + pass light + { + emissive 0 1 0 + } + } +} + +material Gazebo/BlueGlow : Gazebo/Blue +{ + technique + { + pass light + { + emissive 0 0 1 + } + } +} + +material Gazebo/YellowGlow : Gazebo/Yellow +{ + technique + { + pass light + { + emissive 1 1 0 + } + } +} + +material Gazebo/PurpleGlow : Gazebo/Purple +{ + technique + { + pass light + { + emissive 1 0 1 + } + } +} + +material Gazebo/TurquoiseGlow : Gazebo/Turquoise +{ + technique + { + pass light + { + emissive 0 1 1 + } + } +} + +material Gazebo/TurquoiseGlowOutline +{ + technique + { + pass ambient + { + scene_blend alpha_blend + //lighting off + + diffuse 0 1 1 1 + specular .1 .1 .1 128 + } + + pass ambient2 + { + scene_blend alpha_blend + + diffuse 0 1 1 + specular .1 .1 .1 128 + emissive 0 1 1 + + polygon_mode wireframe + } + } +} + +material Gazebo/RedTransparentOverlay +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + depth_check off + + texture_unit + { + colour_op_ex source1 src_manual src_current 1 0 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/BlueTransparentOverlay +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + depth_check off + + texture_unit + { + colour_op_ex source1 src_manual src_current 0 0 1 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/GreenTransparentOverlay +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + depth_check off + + texture_unit + { + colour_op_ex source1 src_manual src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/OrangeTransparentOverlay +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + depth_check off + ambient 1 0.5088 0.0468 + + texture_unit + { + colour_op_ex source1 src_manual src_current 1 0.5088 0.0468 + alpha_op_ex source1 src_manual src_current 0.8 + } + } + } +} + +material Gazebo/DarkOrangeTransparentOverlay +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + depth_check off + ambient 0.6 0.305 0.028 + + texture_unit + { + colour_op_ex source1 src_manual src_current 0.6 0.305 0.028 + alpha_op_ex source1 src_manual src_current 0.8 + } + } + } +} + +material Gazebo/RedTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + lighting off + + texture_unit + { + colour_op_ex source1 src_manual src_current 1 0 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/GreenTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 0.0 1.0 0.0 1 + diffuse 0.0 1.0 0.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/BlueTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 0.0 0.0 1.0 1 + diffuse 0.0 0.0 1.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/DarkMagentaTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 0.6 0.0 0.6 1 + diffuse 0.6 0.0 0.6 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0.6 0 0.6 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/GreyTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 0.5 0.5 0.5 1 + diffuse 0.5 0.5 0.5 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/BlackTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 0.0 0.0 0.0 1 + diffuse 0.0 0.0 0.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/YellowTransparent +{ + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 1.0 1.0 0.0 1 + diffuse 1.0 1.0 0.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.5 + } + } + } +} + +material Gazebo/LightOn +{ + technique + { + pass ambient + { + diffuse 0 1 0 + ambient 0 1 0 + emissive 0 1 0 + } + } +} + +material Gazebo/LightOff +{ + technique + { + pass ambient + { + diffuse 1 0 0 + ambient 1 0 0 + emissive 1 0 0 + } + } +} + +material Gazebo/LightBlueLaser +{ + receive_shadows off + + technique + { + pass + { + scene_blend alpha_blend + depth_write off + cull_hardware none + + ambient 0.5 0.5 1.0 1 + diffuse 0.5 0.5 1.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.4 + } + + } + } +} + +material Gazebo/BlueLaser +{ + receive_shadows off + + technique + { + pass + { + scene_blend alpha_blend + depth_write off + cull_hardware none + + ambient 0.0 0.0 1.0 1 + diffuse 0.0 0.0 1.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.4 + } + + } + } +} + +material Gazebo/OrangeTransparent +{ + receive_shadows off + + technique + { + pass + { + scene_blend alpha_blend + depth_write off + + ambient 1.0 0.44 0.0 1 + diffuse 1.0 0.44 0.0 1 + + texture_unit + { + colour_op_ex source1 src_current src_current 0 1 0 + alpha_op_ex source1 src_manual src_current 0.4 + } + + } + } +} + + +material Gazebo/JointAnchor +{ + receive_shadows off + + technique + { + pass + { + ambient 1.000000 1.000000 1.000000 1.000000 + diffuse 1.000000 1.000000 1.000000 1.000000 + specular 1.000000 1.000000 1.000000 1.000000 + emissive 1.000000 1.000000 1.000000 1.000000 + lighting off + } + } +} + +material Gazebo/CoM +{ + technique + { + pass + { + ambient 0.5 0.5 0.5 1.000000 + + texture_unit + { + texture com.png + } + } + } +} + +material Gazebo/WoodFloor +{ + receive_shadows on + + technique + { + pass + { + ambient 0.5 0.5 0.5 1.000000 + + texture_unit + { + texture hardwood_floor.jpg + } + } + } +} + +material Gazebo/CeilingTiled +{ + receive_shadows on + + technique + { + pass + { + ambient 0.5 0.5 0.5 1.000000 + + texture_unit + { + texture ceiling_tiled.jpg + } + } + } +} + +material Gazebo/PaintedWall +{ + receive_shadows on + + technique + { + pass + { + ambient 1.0 1.0 1.0 1.000000 + + texture_unit + { + texture paintedWall.jpg + } + } + } +} + +material Gazebo/PioneerBody +{ + receive_shadows on + technique + { + pass Ambient + { + ambient 0.5 0 0 + + texture_unit + { + texture pioneerBody.jpg + filtering trilinear + } + } + pass DirectionalLight + { + ambient 0 0 0 + diffuse 1 0 0 1 + specular 0.5 0 0 1 10 + + texture_unit + { + texture pioneerBody.jpg + filtering trilinear + } + } + pass PointLight + { + ambient 0 0 0 + diffuse 1 0 0 1 + specular 0.5 0 0 1 10 + + texture_unit + { + texture pioneerBody.jpg + filtering trilinear + } + } + + } +} + +material Gazebo/Pioneer2Body +{ + receive_shadows on + technique + { + pass + { + //ambient 0.500000 0.500000 0.500000 1.000000 + ambient 0.481193 0.000123 0.000123 1.000000 + diffuse 0.681193 0.000923 0.000923 1.000000 + specular 0.500000 0.500000 0.500000 1.000000 12.500000 + emissive 0.000000 0.000000 0.000000 1.000000 + } + } +} + +material Gazebo/Gold +{ + receive_shadows on + technique + { + pass + { + ambient 0.400000 0.248690 0.020759 1.000000 + diffuse 0.800000 0.648690 0.120759 1.000000 + specular 0.400000 0.400000 0.400000 1.000000 12.500000 + } + } +} + +material Gazebo/GreyGradientSky +{ + technique + { + pass + { + depth_write off + lighting off + + texture_unit + { + texture grey_gradient.jpg + } + } + } +} + +material Gazebo/CloudySky +{ + technique + { + pass + { + depth_write off + lighting off + + texture_unit + { + texture clouds.jpg + scroll_anim 0.15 0 + } + } + } +} + +material Gazebo/WoodPallet +{ + technique + { + pass + { + ambient 0.5 0.5 0.5 1.0 + diffuse 1.0 1.0 1.0 1.0 + specular 0.0 0.0 0.0 1.0 0.5 + + texture_unit + { + texture WoodPallet.png + filtering trilinear + } + } + } + + /* + technique + { + scheme GBuffer + pass DeferredShading/GBuffer/Pass_16 + { + lighting off + + vertex_program_ref Gazebo/NateVS + { + } + + fragment_program_ref Gazebo/NateFS + { + } + } + }*/ +} + +material Gazebo/Wood +{ + technique + { + pass + { + ambient 1.0 1.0 1.0 1.0 + diffuse 1.0 1.0 1.0 1.0 + specular 0.2 0.2 0.2 1.0 12.5 + + texture_unit + { + texture wood.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Bricks +{ + technique + { + pass + { + ambient 1.0 1.0 1.0 1.0 + diffuse 1.0 1.0 1.0 1.0 + specular 0.2 0.2 0.2 1.0 12.5 + + texture_unit + { + texture bricks.png + filtering trilinear + } + } + } +} + +material Gazebo/Road +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture road1.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Residential +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture residential.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Tertiary +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture residential.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Pedestrian +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture sidewalk.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Footway +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture sidewalk.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Motorway +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture motorway.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Lanes_6 +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture motorway.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Trunk +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture trunk.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Lanes_4 +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture trunk.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Primary +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture primary.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Lanes_2 +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture primary.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Secondary +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture secondary.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Lane_1 +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture secondary.jpg + filtering trilinear + } + } + } +} + +material Gazebo/Steps +{ + technique + { + pass + { + ambient 0.1 0.1 0.1 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.01 0.01 0.01 1.0 2.0 + + texture_unit + { + texture steps.jpeg + filtering trilinear + } + } + } +} + +material drc/san_fauxcity_sign +{ + receive_shadows off + technique + { + pass + { + ambient 0.8 0.8 0.8 1.0 + diffuse 0.8 0.8 0.8 1.0 + specular 0.1 0.1 0.1 1.0 2.0 + + texture_unit + { + texture san_fauxcity.png + filtering trilinear + } + } + } +} + +vertex_program Gazebo/GaussianCameraNoiseVS glsl +{ + source camera_noise_gaussian_vs.glsl +} + +fragment_program Gazebo/GaussianCameraNoiseFS glsl +{ + source camera_noise_gaussian_fs.glsl + default_params + { + param_named RT int 0 + param_named mean float 0.0 + param_named stddev float 1.0 + param_named offsets float3 0.0 0.0 0.0 + } +} + +material Gazebo/GaussianCameraNoise +{ + technique + { + pass + { + vertex_program_ref Gazebo/GaussianCameraNoiseVS { } + fragment_program_ref Gazebo/GaussianCameraNoiseFS { } + + texture_unit RT + { + tex_coord_set 0 + tex_address_mode clamp + filtering linear linear linear + } + } + } +} + +vertex_program Gazebo/CameraDistortionMapVS glsl +{ + source camera_distortion_map_vs.glsl +} + +fragment_program Gazebo/CameraDistortionMapFS glsl +{ + source camera_distortion_map_fs.glsl + default_params + { + param_named RT int 0 + param_named distortionMap int 1 + param_named scale float3 1.0 1.0 1.0 + } +} + +material Gazebo/CameraDistortionMap +{ + technique + { + pass + { + vertex_program_ref Gazebo/CameraDistortionMapVS { } + fragment_program_ref Gazebo/CameraDistortionMapFS { } + + texture_unit RT + { + tex_coord_set 0 + tex_address_mode border + filtering linear linear linear + } + } + } +} + +vertex_program Gazebo/WideLensMapVS glsl +{ + source wide_lens_map_vs.glsl + default_params + { + param_named ratio float 1 + } +} + +fragment_program Gazebo/WideLensMapFS glsl +{ + source wide_lens_map_fp.glsl + default_params + { + param_named envMap int 0 + param_named c1 float 1 + param_named c2 float 1 + param_named c3 float 0 + param_named f float 1 + param_named fun float3 0 0 1 + param_named cutOffAngle float 3.14 + } +} + +material Gazebo/WideLensMap +{ + technique + { + pass + { + vertex_program_ref Gazebo/WideLensMapVS { } + fragment_program_ref Gazebo/WideLensMapFS { } + } + } +} + + +vertex_program Gazebo/CameraLensFlareVS glsl +{ + source camera_lens_flare_vs.glsl +} + +fragment_program Gazebo/CameraLensFlareFS glsl +{ + source camera_lens_flare_fs.glsl + + default_params + { + param_named RT int 0 + param_named viewport float3 0.0 0.0 0.0 + param_named lightPos float3 0 0 0 + param_named scale float 1.0 + param_named color float3 1.4 1.2 1.0 + } +} + +material Gazebo/CameraLensFlare +{ + technique + { + pass + { + vertex_program_ref Gazebo/CameraLensFlareVS { } + fragment_program_ref Gazebo/CameraLensFlareFS { } + + texture_unit RT + { + tex_coord_set 0 + tex_address_mode border + filtering linear linear linear + } + } + } +} + +material Gazebo/PointCloud +{ + technique + { + pass + { + diffuse vertexcolour + specular vertexcolour + ambient vertexcolour + point_size 3 + point_sprites off + point_size_attenuation off + } + } +} + +material Gazebo/PointHandle +{ + technique + { + pass + { + lighting off + scene_blend alpha_blend + depth_write off + } + } +} + +material Gazebo/BuildingFrame +{ + technique + { + pass + { + ambient 1.0 1.0 1.0 1.0 + diffuse 1.0 1.0 1.0 1.0 + specular 0.2 0.2 0.2 1.0 12.5 + + texture_unit + { + texture building_frame.png + filtering trilinear + } + } + } +} + +material Gazebo/Runway +{ + receive_shadows on + + technique + { + pass + { + ambient 0.5 0.5 0.5 1.000000 + + texture_unit + { + texture runway.png + } + } + } +} + +material Gazebo/Grass +{ + receive_shadows on + + technique + { + pass + { + ambient 0.5 0.5 0.5 1.000000 + + texture_unit + { + scale .02 .02 + texture grass.jpg + filtering anisotropic + max_anisotropy 16 + } + } + } +} + +material Gazebo/Editor +{ + technique + { + pass ambient + { + ambient 1.0 1.0 1.0 1.0 + diffuse 1.0 1.0 1.0 1 + specular .1 .1 .1 128 + } + } +} + +material Gazebo/EditorPlane +{ + technique + { + pass + { + scene_blend colour_blend + + ambient .3 .3 .3 1.0 + diffuse .7 .7 .7 1.0 + specular 0.01 0.01 0.01 1.000000 1.500000 + } + } +} \ No newline at end of file From 4d1b9c533d44c20fdbf89bed9967868634f83db6 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Wed, 20 Dec 2023 00:33:50 +0000 Subject: [PATCH 04/20] linters Signed-off-by: Dharini Dutia --- src/rendering/MaterialParser/ConfigLoader.cc | 674 +++++++++--------- src/rendering/MaterialParser/ConfigLoader.hh | 507 +++++++++---- .../MaterialParser/MaterialParser.cc | 60 +- .../MaterialParser/MaterialParser.hh | 43 +- 4 files changed, 744 insertions(+), 540 deletions(-) diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc index c3152d76d5..ca0201d23f 100644 --- a/src/rendering/MaterialParser/ConfigLoader.cc +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -1,423 +1,395 @@ +/* + * Copyright (C) 2023 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + #include "ConfigLoader.hh" -#include #include #include #include #include +#include +#include using namespace gz; using namespace sim; -void ConfigLoader::loadAllFiles(ConfigLoader* c, const std::string& path) +void ConfigLoader::loadAllFiles(ConfigLoader * c, const std::string & path) { try { - for ( std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir ) - { - std::filesystem::path p(*dir); - if(p.extension() == c->m_fileEnding) - { - std::ifstream in((*dir).path().string().c_str(), std::ios::binary); - c->parseScript(in); - } - } + for (std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir) { + std::filesystem::path p(* dir); + if (p.extension() == c->m_fileEnding) { + std::ifstream in((*dir).path().string().c_str(), std::ios::binary); + c->parseScript(in); + } + } } - catch (std::filesystem::filesystem_error &e) + catch(std::filesystem::filesystem_error & e) { std::cerr << e.what() << std::endl; } } -ConfigLoader::ConfigLoader(const std::string& fileEnding) +ConfigLoader::ConfigLoader(const std::string & fileEnding) { - //Register as a ScriptLoader - m_fileEnding = fileEnding; + // Register as a ScriptLoader + m_fileEnding = fileEnding; } ConfigLoader::~ConfigLoader() { - clearScriptList(); - + clearScriptList(); } void ConfigLoader::clearScriptList() { - std::map ::iterator i; - for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) - { - delete i->second; - } - m_scriptList.clear(); + std::map < std::string, ConfigNode * > ::iterator i; + for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) { + delete i->second; + } + m_scriptList.clear(); } -ConfigNode *ConfigLoader::getConfigScript(const std::string &name) +ConfigNode * ConfigLoader::getConfigScript(const std::string & name) { - std::map ::iterator i; - - std::string key = name; - i = m_scriptList.find(key); - - //If found.. - if (i != m_scriptList.end()) - { - return i->second; - } - else - { - return NULL; - } + std::map < std::string, ConfigNode * > ::iterator i; + + std::string key = name; + i = m_scriptList.find(key); + + // If found.. + if (i != m_scriptList.end()) { + return i->second; + } else { + return NULL; + } } -std::map ConfigLoader::getAllConfigScripts () +std::map < std::string, ConfigNode * > ConfigLoader::getAllConfigScripts() { - return m_scriptList; + return m_scriptList; } -void ConfigLoader::parseScript(std::ifstream &stream) +void ConfigLoader::parseScript(std::ifstream & stream) { - //Get first token - _nextToken(stream); - if (tok == TOKEN_EOF) - { - stream.close(); - return; - } - - //Parse the script - _parseNodes(stream, 0); - - stream.close(); + // Get first token + _nextToken(stream); + if (tok == TOKEN_EOF) { + stream.close(); + return; + } + + // Parse the script + _parseNodes(stream, 0); + + stream.close(); } -void ConfigLoader::_nextToken(std::ifstream &stream) +void ConfigLoader::_nextToken(std::ifstream & stream) { - lastTok = tok; - lastTokVal = tokVal; - - //EOF token - if (stream.eof()) - { - tok = TOKEN_EOF; - return; - } - - //(Get next character) - int ch = stream.get(); - if (ch == -1) - { - tok = TOKEN_EOF; - return; - } - while ((ch == ' ' || ch == 9) && !stream.eof()) - { //Skip leading spaces / tabs - ch = stream.get(); - } - - if (stream.eof()) - { - tok = TOKEN_EOF; - return; - } - - //Newline token - if (ch == '\r' || ch == '\n') - { - do - { - ch = stream.get(); - } while ((ch == '\r' || ch == '\n') && !stream.eof()); - - stream.unget(); - - tok = TOKEN_NewLine; - return; - } - - //Open brace token - else if (ch == '{') - { - tok = TOKEN_OpenBrace; - return; - } - - //Close brace token - else if (ch == '}') - { - tok = TOKEN_CloseBrace; - return; - } - - //Text token - if (ch < 32 || ch > 122) //Verify valid char - { - throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); - } - - tokVal = ""; - tok = TOKEN_Text; - do - { - //Skip comments - if (ch == '/') - { - int ch2 = stream.peek(); - - //C++ style comment (//) - if (ch2 == '/') - { - stream.get(); - do - { - ch = stream.get(); - } while (ch != '\r' && ch != '\n' && !stream.eof()); - - tok = TOKEN_NewLine; - return; - } - else if (ch2 == '*') - { + lastTok = tok; + lastTokVal = tokVal; + + // EOF token + if (stream.eof()) { + tok = TOKEN_EOF; + return; + } + + // Get next character + int ch = stream.get(); + if (ch == -1) { + tok = TOKEN_EOF; + return; + } + while ((ch == ' ' || ch == 9) && !stream.eof()) { // Skip leading spaces / tabs + ch = stream.get(); + } + + if (stream.eof()) { + tok = TOKEN_EOF; + return; + } + + // Newline token + if (ch == '\r' || ch == '\n') { + do{ + ch = stream.get(); + } while ((ch == '\r' || ch == '\n') && !stream.eof()); + + stream.unget(); + + tok = TOKEN_NewLine; + return; + } else if (ch == '{') { + // Open brace token + tok = TOKEN_OpenBrace; + return; + } else if (ch == '}') { + // Close brace token + tok = TOKEN_CloseBrace; + return; + } + + // Text token + if (ch < 32 || ch > 122) { // Verify valid char + throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); + } + + tokVal = ""; + tok = TOKEN_Text; + do{ + // Skip comments + if (ch == '/') { + int ch2 = stream.peek(); + + // C++ style comment (//) + if (ch2 == '/') { stream.get(); - do - { - ch = stream.get(); - ch2 = stream.peek(); - } while (!(ch == '*' && ch2 == '/') && !stream.eof()); + do{ + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); + + tok = TOKEN_NewLine; + return; + } else if (ch2 == '*') { + stream.get(); + do{ + ch = stream.get(); + ch2 = stream.peek(); + } while (!(ch == '*' && ch2 == '/') && !stream.eof()); stream.get(); - do - { - ch = stream.get(); - } while (ch != '\r' && ch != '\n' && !stream.eof()); + do{ + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); continue; } - } - - //Add valid char to tokVal - tokVal += (char)ch; + } - //Next char - ch = stream.get(); + // Add valid char to tokVal + tokVal += (char)ch; - } while (ch > 32 && ch <= 122 && !stream.eof()); + // Next char + ch = stream.get(); + } while (ch > 32 && ch <= 122 && !stream.eof()); - stream.unget(); + stream.unget(); - return; + return; } -void ConfigLoader::_skipNewLines(std::ifstream &stream) +void ConfigLoader::_skipNewLines(std::ifstream & stream) { - while (tok == TOKEN_NewLine) - { - _nextToken(stream); - } + while (tok == TOKEN_NewLine) { + _nextToken(stream); + } } -void ConfigLoader::_parseNodes(std::ifstream &stream, ConfigNode *parent) +void ConfigLoader::_parseNodes(std::ifstream & stream, ConfigNode * parent) { - typedef std::pair ScriptItem; - - while (true) - { - switch (tok) - { - //Node - case TOKEN_Text: - //Add the new node - ConfigNode *newNode; - if (parent) - { - newNode = parent->addChild(tokVal); - } - else - { - newNode = new ConfigNode(0, tokVal); - } - - //Get values - _nextToken(stream); - while (tok == TOKEN_Text) - { - newNode->addValue(tokVal); - _nextToken(stream); - } - - //Add root nodes to scriptList - if (!parent){ - std::string key; - - if (newNode->getValues().empty()) - { - key = newNode->getName() + ' '; - } - else - { - key = newNode->getName() + ' ' + newNode->getValues().front(); - } - - m_scriptList.insert(ScriptItem(key, newNode)); - } - - _skipNewLines(stream); - - //Add any sub-nodes - if (tok == TOKEN_OpenBrace) - { - //Parse nodes - _nextToken(stream); - _parseNodes(stream, newNode); - //Check for matching closing brace - if (tok != TOKEN_CloseBrace) - { - throw std::runtime_error("Parse Error: Expecting closing brace"); - } - _nextToken(stream); - _skipNewLines(stream); - } - - break; - - //Out of place brace - case TOKEN_OpenBrace: - throw std::runtime_error("Parse Error: Opening brace out of plane"); - break; - - //Return if end of nodes have been reached - case TOKEN_CloseBrace: - return; - - //Return if reached end of file - case TOKEN_EOF: - return; - - case TOKEN_NewLine: - _nextToken(stream); - break; - } - }; + typedef std::pair < std::string, ConfigNode * > ScriptItem; + + while (true) { + switch (tok) { + // Node + case TOKEN_Text: + // Add the new node + ConfigNode * newNode; + if (parent) { + newNode = parent->addChild(tokVal); + } else { + newNode = new ConfigNode(0, tokVal); + } + + // Get values + _nextToken(stream); + while (tok == TOKEN_Text) { + newNode->addValue(tokVal); + _nextToken(stream); + } + + // Add root nodes to scriptList + if (!parent) { + std::string key; + + if (newNode->getValues().empty()) { + key = newNode->getName() + ' '; + } else { + key = newNode->getName() + ' ' + newNode->getValues().front(); + } + + m_scriptList.insert(ScriptItem(key, newNode)); + } + + _skipNewLines(stream); + + // Add any sub-nodes + if (tok == TOKEN_OpenBrace) { + // Parse nodes + _nextToken(stream); + _parseNodes(stream, newNode); + // Check for matching closing brace + if (tok != TOKEN_CloseBrace) { + throw std::runtime_error("Parse Error: Expecting closing brace"); + } + _nextToken(stream); + _skipNewLines(stream); + } + + break; + + // Out of place brace + case TOKEN_OpenBrace: + throw std::runtime_error("Parse Error: Opening brace out of plane"); + break; + + // Return if end of nodes have been reached + case TOKEN_CloseBrace: + return; + + // Return if reached end of file + case TOKEN_EOF: + return; + + case TOKEN_NewLine: + _nextToken(stream); + break; + } + } } -ConfigNode::ConfigNode(ConfigNode *parent, const std::string &name) +ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name) { - m_name = name; - m_parent = parent; - _removeSelf = true; //For proper destruction - m_lastChildFound = -1; - - //Add self to parent's child list (unless this is the root node being created) - if (parent != NULL) - { - m_parent->m_children.push_back(this); - _iter = --(m_parent->m_children.end()); - } + m_name = name; + m_parent = parent; + _removeSelf = true; // For proper destruction + m_lastChildFound = -1; + + // Add self to parent's child list (unless this is the root node being created) + if (parent != NULL) { + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); + } } ConfigNode::~ConfigNode() { - //Delete all children - std::vector::iterator i; - for (i = m_children.begin(); i != m_children.end(); i++) - { - ConfigNode *node = *i; - node->_removeSelf = false; - delete node; - } - m_children.clear(); - - //Remove self from parent's child list - if (_removeSelf && m_parent != NULL) - { - m_parent->m_children.erase(_iter); - } + // Delete all children + std::vector < ConfigNode * > ::iterator i; + for (i = m_children.begin(); i != m_children.end(); i++) { + ConfigNode * node = *i; + node->_removeSelf = false; + delete node; + } + m_children.clear(); + + // Remove self from parent's child list + if (_removeSelf && m_parent != NULL) { + m_parent->m_children.erase(_iter); + } } -ConfigNode *ConfigNode::addChild(const std::string &name, bool replaceExisting) +ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting) { - if (replaceExisting) - { - ConfigNode *node = findChild(name, false); - if (node) - { - return node; - } - } - return new ConfigNode(this, name); + if (replaceExisting) { + ConfigNode * node = findChild(name, false); + if (node) { + return node; + } + } + return new ConfigNode(this, name); } -ConfigNode *ConfigNode::findChild(const std::string &name, bool recursive) +ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) { - int indx, prevC, nextC; - int childCount = (int)m_children.size(); - - if (m_lastChildFound != -1) - { - //If possible, try checking the nodes neighboring the last successful search - //(often nodes searched for in sequence, so this will std search speeds). - prevC = m_lastChildFound-1; if (prevC < 0) prevC = 0; else if (prevC >= childCount) prevC = childCount-1; - nextC = m_lastChildFound+1; if (nextC < 0) nextC = 0; else if (nextC >= childCount) nextC = childCount-1; - for (indx = prevC; indx <= nextC; ++indx) - { - ConfigNode *node = m_children[indx]; - if (node->m_name == name) - { - m_lastChildFound = indx; - return node; - } - } - - //If not found that way, search for the node from start to finish, avoiding the - //already searched area above. - for (indx = nextC + 1; indx < childCount; ++indx) - { - ConfigNode *node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - for (indx = 0; indx < prevC; ++indx) - { - ConfigNode *node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - } - else - { - //Search for the node from start to finish - for (indx = 0; indx < childCount; ++indx){ - ConfigNode *node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - } - - //If not found, search child nodes (if recursive == true) - if (recursive) - { - for (indx = 0; indx < childCount; ++indx) - { - m_children[indx]->findChild(name, recursive); - } - } - - //Not found anywhere - return NULL; + int indx, prevC, nextC; + int childCount = (int)m_children.size(); + + if (m_lastChildFound != -1) { + // If possible, try checking the nodes neighboring the last successful search + // (often nodes searched for in sequence, so this will std search speeds). + prevC = m_lastChildFound - 1; + if (prevC < 0) { + prevC = 0; + } else if (prevC >= childCount) { + prevC = childCount - 1; + } + nextC = m_lastChildFound + 1; + if (nextC < 0) { + nextC = 0; + } else if (nextC >= childCount) { + nextC = childCount - 1; + } + for (indx = prevC; indx <= nextC; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + + // If not found that way, search for the node from start to finish, avoiding the + // already searched area above. + for (indx = nextC + 1; indx < childCount; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + for (indx = 0; indx < prevC; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } else { + // Search for the node from start to finish + for (indx = 0; indx < childCount; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } + + // If not found, search child nodes (if recursive == true) + if (recursive) { + for (indx = 0; indx < childCount; ++indx) { + m_children[indx]->findChild(name, recursive); + } + } + + // Not found anywhere + return NULL; } -void ConfigNode::setParent(ConfigNode *newParent) +void ConfigNode::setParent(ConfigNode * newParent) { - //Remove self from current parent - m_parent->m_children.erase(_iter); + // Remove self from current parent + m_parent->m_children.erase(_iter); - //Set new parent - m_parent = newParent; + // Set new parent + m_parent = newParent; - //Add self to new parent - m_parent->m_children.push_back(this); - _iter = --(m_parent->m_children.end()); -} \ No newline at end of file + // Add self to new parent + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); +} diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh index 993c86108c..ca0201d23f 100644 --- a/src/rendering/MaterialParser/ConfigLoader.hh +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -14,151 +14,382 @@ * limitations under the License. * */ - -#ifndef GZ_SIM_CONFIG_LOADER_HH_ -#define GZ_SIM_CONFIG_LOADER_HH_ + +#include "ConfigLoader.hh" #include +#include +#include +#include +#include #include -#include -#include -#include +using namespace gz; +using namespace sim; + +void ConfigLoader::loadAllFiles(ConfigLoader * c, const std::string & path) +{ + try + { + for (std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir) { + std::filesystem::path p(* dir); + if (p.extension() == c->m_fileEnding) { + std::ifstream in((*dir).path().string().c_str(), std::ios::binary); + c->parseScript(in); + } + } + } + catch(std::filesystem::filesystem_error & e) + { + std::cerr << e.what() << std::endl; + } +} + +ConfigLoader::ConfigLoader(const std::string & fileEnding) +{ + // Register as a ScriptLoader + m_fileEnding = fileEnding; +} + +ConfigLoader::~ConfigLoader() +{ + clearScriptList(); +} + +void ConfigLoader::clearScriptList() +{ + std::map < std::string, ConfigNode * > ::iterator i; + for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) { + delete i->second; + } + m_scriptList.clear(); +} + +ConfigNode * ConfigLoader::getConfigScript(const std::string & name) +{ + std::map < std::string, ConfigNode * > ::iterator i; + + std::string key = name; + i = m_scriptList.find(key); + + // If found.. + if (i != m_scriptList.end()) { + return i->second; + } else { + return NULL; + } +} + +std::map < std::string, ConfigNode * > ConfigLoader::getAllConfigScripts() +{ + return m_scriptList; +} + +void ConfigLoader::parseScript(std::ifstream & stream) +{ + // Get first token + _nextToken(stream); + if (tok == TOKEN_EOF) { + stream.close(); + return; + } + + // Parse the script + _parseNodes(stream, 0); + + stream.close(); +} + +void ConfigLoader::_nextToken(std::ifstream & stream) +{ + lastTok = tok; + lastTokVal = tokVal; + + // EOF token + if (stream.eof()) { + tok = TOKEN_EOF; + return; + } + + // Get next character + int ch = stream.get(); + if (ch == -1) { + tok = TOKEN_EOF; + return; + } + while ((ch == ' ' || ch == 9) && !stream.eof()) { // Skip leading spaces / tabs + ch = stream.get(); + } + + if (stream.eof()) { + tok = TOKEN_EOF; + return; + } + + // Newline token + if (ch == '\r' || ch == '\n') { + do{ + ch = stream.get(); + } while ((ch == '\r' || ch == '\n') && !stream.eof()); + + stream.unget(); + + tok = TOKEN_NewLine; + return; + } else if (ch == '{') { + // Open brace token + tok = TOKEN_OpenBrace; + return; + } else if (ch == '}') { + // Close brace token + tok = TOKEN_CloseBrace; + return; + } + + // Text token + if (ch < 32 || ch > 122) { // Verify valid char + throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); + } + + tokVal = ""; + tok = TOKEN_Text; + do{ + // Skip comments + if (ch == '/') { + int ch2 = stream.peek(); + + // C++ style comment (//) + if (ch2 == '/') { + stream.get(); + do{ + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); + + tok = TOKEN_NewLine; + return; + } else if (ch2 == '*') { + stream.get(); + do{ + ch = stream.get(); + ch2 = stream.peek(); + } while (!(ch == '*' && ch2 == '/') && !stream.eof()); + stream.get(); + do{ + ch = stream.get(); + } while (ch != '\r' && ch != '\n' && !stream.eof()); + continue; + } + } + + // Add valid char to tokVal + tokVal += (char)ch; + + // Next char + ch = stream.get(); + } while (ch > 32 && ch <= 122 && !stream.eof()); + + stream.unget(); + + return; +} -namespace gz +void ConfigLoader::_skipNewLines(std::ifstream & stream) { -namespace sim + while (tok == TOKEN_NewLine) { + _nextToken(stream); + } +} + +void ConfigLoader::_parseNodes(std::ifstream & stream, ConfigNode * parent) { - class ConfigNode; - - class ConfigLoader - { - public: - static void loadAllFiles(ConfigLoader* c, const std::string& path); - - ConfigLoader(const std::string& fileEnding); - virtual ~ConfigLoader(); - - std::string m_fileEnding; - - // For a line like - // entity animals/dog - // { - // ... - // } - // The type is "entity" and the name is "animals/dog" - // Or if animal/dog was not there then name is "" - virtual ConfigNode *getConfigScript (const std::string &name); - - virtual std::map getAllConfigScripts (); - - virtual void parseScript(std::ifstream &stream); - - - protected: - - float m_LoadOrder; - // like "*.object" - - std::map m_scriptList; - - enum Token - { - TOKEN_Text, - TOKEN_NewLine, - TOKEN_OpenBrace, - TOKEN_CloseBrace, - TOKEN_EOF, - }; - - Token tok, lastTok; - std::string tokVal, lastTokVal; - - void _parseNodes(std::ifstream &stream, ConfigNode *parent); - void _nextToken(std::ifstream &stream); - void _skipNewLines(std::ifstream &stream); - - virtual void clearScriptList(); - }; - - class ConfigNode - { - public: - ConfigNode(ConfigNode *parent, const std::string &name = "untitled"); - ~ConfigNode(); - - inline void setName(const std::string &name) - { - this->m_name = name; - } - - inline std::string &getName() - { - return m_name; - } - - inline void addValue(const std::string &value) - { - m_values.push_back(value); - } - - inline void clearValues() - { - m_values.clear(); - } - - inline std::vector &getValues() - { - return m_values; - } - - inline void getValuesInFloat(std::vector &floatValues) - { - for (const auto& str : m_values) - { - floatValues.push_back(std::stof(str)); - } - } - - inline const std::string &getValue(unsigned int index = 0) - { - assert(index < m_values.size()); - return m_values[index]; - } - - ConfigNode *addChild(const std::string &name = "untitled", bool replaceExisting = false); - ConfigNode *findChild(const std::string &name, bool recursive = false); - - inline std::vector &getChildren() - { - return m_children; - } - - inline ConfigNode *getChild(unsigned int index = 0) - { - assert(index < m_children.size()); - return m_children[index]; - } - - void setParent(ConfigNode *newParent); - - inline ConfigNode *getParent() - { - return m_parent; - } - - private: - std::string m_name; - std::vector m_values; - std::vector m_children; - ConfigNode *m_parent; - - int m_lastChildFound; //The last child node's index found with a call to findChild() - - std::vector::iterator _iter; - bool _removeSelf; - }; + typedef std::pair < std::string, ConfigNode * > ScriptItem; + + while (true) { + switch (tok) { + // Node + case TOKEN_Text: + // Add the new node + ConfigNode * newNode; + if (parent) { + newNode = parent->addChild(tokVal); + } else { + newNode = new ConfigNode(0, tokVal); + } + + // Get values + _nextToken(stream); + while (tok == TOKEN_Text) { + newNode->addValue(tokVal); + _nextToken(stream); + } + + // Add root nodes to scriptList + if (!parent) { + std::string key; + + if (newNode->getValues().empty()) { + key = newNode->getName() + ' '; + } else { + key = newNode->getName() + ' ' + newNode->getValues().front(); + } + + m_scriptList.insert(ScriptItem(key, newNode)); + } + + _skipNewLines(stream); + + // Add any sub-nodes + if (tok == TOKEN_OpenBrace) { + // Parse nodes + _nextToken(stream); + _parseNodes(stream, newNode); + // Check for matching closing brace + if (tok != TOKEN_CloseBrace) { + throw std::runtime_error("Parse Error: Expecting closing brace"); + } + _nextToken(stream); + _skipNewLines(stream); + } + + break; + + // Out of place brace + case TOKEN_OpenBrace: + throw std::runtime_error("Parse Error: Opening brace out of plane"); + break; + + // Return if end of nodes have been reached + case TOKEN_CloseBrace: + return; + + // Return if reached end of file + case TOKEN_EOF: + return; + + case TOKEN_NewLine: + _nextToken(stream); + break; + } + } } + +ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name) +{ + m_name = name; + m_parent = parent; + _removeSelf = true; // For proper destruction + m_lastChildFound = -1; + + // Add self to parent's child list (unless this is the root node being created) + if (parent != NULL) { + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); + } } -#endif \ No newline at end of file +ConfigNode::~ConfigNode() +{ + // Delete all children + std::vector < ConfigNode * > ::iterator i; + for (i = m_children.begin(); i != m_children.end(); i++) { + ConfigNode * node = *i; + node->_removeSelf = false; + delete node; + } + m_children.clear(); + + // Remove self from parent's child list + if (_removeSelf && m_parent != NULL) { + m_parent->m_children.erase(_iter); + } +} + +ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting) +{ + if (replaceExisting) { + ConfigNode * node = findChild(name, false); + if (node) { + return node; + } + } + return new ConfigNode(this, name); +} + +ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) +{ + int indx, prevC, nextC; + int childCount = (int)m_children.size(); + + if (m_lastChildFound != -1) { + // If possible, try checking the nodes neighboring the last successful search + // (often nodes searched for in sequence, so this will std search speeds). + prevC = m_lastChildFound - 1; + if (prevC < 0) { + prevC = 0; + } else if (prevC >= childCount) { + prevC = childCount - 1; + } + nextC = m_lastChildFound + 1; + if (nextC < 0) { + nextC = 0; + } else if (nextC >= childCount) { + nextC = childCount - 1; + } + for (indx = prevC; indx <= nextC; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + + // If not found that way, search for the node from start to finish, avoiding the + // already searched area above. + for (indx = nextC + 1; indx < childCount; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + for (indx = 0; indx < prevC; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } else { + // Search for the node from start to finish + for (indx = 0; indx < childCount; ++indx) { + ConfigNode * node = m_children[indx]; + if (node->m_name == name) { + m_lastChildFound = indx; + return node; + } + } + } + + // If not found, search child nodes (if recursive == true) + if (recursive) { + for (indx = 0; indx < childCount; ++indx) { + m_children[indx]->findChild(name, recursive); + } + } + + // Not found anywhere + return NULL; +} + +void ConfigNode::setParent(ConfigNode * newParent) +{ + // Remove self from current parent + m_parent->m_children.erase(_iter); + + // Set new parent + m_parent = newParent; + + // Add self to new parent + m_parent->m_children.push_back(this); + _iter = --(m_parent->m_children.end()); +} diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 92ab52f752..671fead2f9 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -16,7 +16,9 @@ */ #include +#include #include +#include #include "ConfigLoader.hh" #include "MaterialParser.hh" @@ -37,60 +39,52 @@ MaterialParser::~MaterialParser() } ///////////////////////////////////////////////// -void MaterialParser::Load(const std::string &_path) +void MaterialParser::Load(const std::string & _path) { ConfigLoader::loadAllFiles(this->configLoader, _path); } ///////////////////////////////////////////////// -std::vector> MaterialParser::GetMaterialValues(std::string material) const +std::vector < std::vector < float >> MaterialParser::GetMaterialValues(std::string material) const { - std::vector> values; - std::map scripts = - this->configLoader->getAllConfigScripts(); + std::vector < std::vector < float >> values; + std::map < std::string, ConfigNode * > scripts = + this->configLoader->getAllConfigScripts(); - std::map::iterator it; + std::map < std::string, ConfigNode * > ::iterator it; - for (it = scripts.begin(); it != scripts.end(); ++it) - { + for (it = scripts.begin(); it != scripts.end(); ++it) { std::string name = it->first; - if (name.find(material) != std::string::npos) - { - ConfigNode *node = it->second; + if (name.find(material) != std::string::npos) { + ConfigNode * node = it->second; - ConfigNode *techniqueNode = node->findChild("technique"); - if (techniqueNode) - { - ConfigNode *passNode = techniqueNode->findChild("pass"); - if (passNode) - { + ConfigNode * techniqueNode = node->findChild("technique"); + if (techniqueNode) { + ConfigNode * passNode = techniqueNode->findChild("pass"); + if (passNode) { std::size_t index = name.rfind(" "); - if (index != std::string::npos) - { - name = name.substr(index+1); + if (index != std::string::npos) { + name = name.substr(index + 1); } - ConfigNode *ambientNode = passNode->findChild("ambient"); - if (ambientNode) - { - std::vector ambientValues; + ConfigNode * ambientNode = passNode->findChild("ambient"); + if (ambientNode) { + std::vector < float > ambientValues; ambientNode->getValuesInFloat(ambientValues); values.push_back(ambientValues); } - ConfigNode *diffuseNode = passNode->findChild("diffuse"); - if (diffuseNode) - { - std::vector diffuseValues; + ConfigNode * diffuseNode = passNode->findChild("diffuse"); + if (diffuseNode) { + std::vector < float > diffuseValues; diffuseNode->getValuesInFloat(diffuseValues); values.push_back(diffuseValues); } - ConfigNode *specularNode = passNode->findChild("specular"); - if (specularNode) - { - std::vector specularValues; + ConfigNode * specularNode = passNode->findChild("specular"); + if (specularNode) { + std::vector < float > specularValues; specularNode->getValuesInFloat(specularValues); values.push_back(specularValues); } @@ -99,4 +93,4 @@ std::vector> MaterialParser::GetMaterialValues(std::string ma } } return values; -} \ No newline at end of file +} diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 840cb0dc6e..071549a5a6 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -15,35 +15,42 @@ * */ -#ifndef GZ_SIM_MATERIALPARSER_HH_ -#define GZ_SIM_MATERIALPARSER_HH_ +#ifndef RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ +#define RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ #include #include +#include namespace gz { -namespace sim -{ - class ConfigNode; - class ConfigLoader; - - class MaterialParser + namespace sim { - public: MaterialParser(); + class ConfigNode; + class ConfigLoader; + + class MaterialParser + { +public: + MaterialParser(); - public: virtual ~MaterialParser(); +public: + ~MaterialParser(); - public: void Load(const std::string &_path); +public: + void Load(const std::string & _path); - public: void Load() { Load(std::filesystem::current_path().string()); }; +public: + void Load() {Load(std::filesystem::current_path().string());} - public: std::vector> GetMaterialValues(std::string material) const; +public: + std::vector < std::vector < float >> GetMaterialValues(std::string material) const; - private: ConfigLoader *configLoader = nullptr; - }; -} -} +private: + ConfigLoader * configLoader = nullptr; + }; + } // namespace sim +} // namespace gz -#endif \ No newline at end of file +#endif // RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ From c16552812a81c8dbf9c2071e1979d4faa97e207e Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Tue, 19 Dec 2023 17:11:36 -0800 Subject: [PATCH 05/20] Update src/rendering/MaterialParser/MaterialParser.cc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alejandro Hernández Cordero Signed-off-by: Dharini Dutia Signed-off-by: Dharini Dutia --- src/rendering/MaterialParser/MaterialParser.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 671fead2f9..c96eb17c24 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -17,7 +17,11 @@ #include #include +#include +#include #include +#include +#include #include #include "ConfigLoader.hh" From 7e333f5ef75b6791d2006225a622907cf33aa80f Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Wed, 20 Dec 2023 19:24:46 +0000 Subject: [PATCH 06/20] fix configLoader, material struct and feedback Signed-off-by: Dharini Dutia --- src/CMakeLists.txt | 26 +- src/SdfEntityCreator.cc | 16 +- src/rendering/MaterialParser/ConfigLoader.cc | 8 +- src/rendering/MaterialParser/ConfigLoader.hh | 446 +++++------------- .../MaterialParser/MaterialParser.cc | 28 +- .../MaterialParser/MaterialParser.hh | 21 +- 6 files changed, 155 insertions(+), 390 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a240011073..03a8f6d93d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,9 +35,13 @@ gz_msgs_generate_messages_impl( DEPENDENCY_DESCRIPTIONS ${msgs_desc_file} ) -set(cli_sources - gz.cc - cmd/ModelCommandAPI.cc +set(network_sources + network/NetworkConfig.cc + network/NetworkManager.cc + network/NetworkManagerPrimary.cc + network/NetworkManagerSecondary.cc + network/PeerInfo.cc + network/PeerTracker.cc ) set(comms_sources @@ -51,20 +55,16 @@ set(gui_sources PARENT_SCOPE ) +set(cli_sources + gz.cc + cmd/ModelCommandAPI.cc +) + set(material_sources rendering/MaterialParser/MaterialParser.cc rendering/MaterialParser/ConfigLoader.cc ) -set(network_sources - network/NetworkConfig.cc - network/NetworkManager.cc - network/NetworkManagerPrimary.cc - network/NetworkManagerSecondary.cc - network/PeerInfo.cc - network/PeerTracker.cc -) - set (sources Actor.cc Barrier.cc @@ -94,9 +94,9 @@ set (sources Util.cc View.cc World.cc + ${network_sources} ${comms_sources} ${msgs_sources} - ${network_sources} ${material_sources} ) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 29edd1f20f..734340ea67 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -105,6 +105,9 @@ class gz::sim::SdfEntityCreatorPrivate /// \brief Keep track of new visuals being added, so we load their plugins /// only after we have their scoped name. public: std::map newVisuals; + + /// \brief Parse Gazebo defined materials for visuals + public: MaterialParser materialParser; }; using namespace gz; @@ -804,15 +807,14 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) std::string scriptName = visualMaterial.ScriptName(); if((scriptName.find("Gazebo/") != std::string::npos)) { - gzwarn << "Using internal gazebo.material to parse " << scriptName + gzwarn << "Using an internal gazebo.material to parse " << scriptName << std::endl; - MaterialParser* materialParser = new MaterialParser(); - materialParser->Load(); - std::vector> parsed = materialParser->GetMaterialValues(scriptName); + this->dataPtr->materialParser.Load(); + MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - visualMaterial.SetAmbient(gz::math::Color(parsed[0][0],parsed[0][1], parsed[0][2])); - visualMaterial.SetDiffuse(gz::math::Color(parsed[1][0],parsed[1][1], parsed[1][2])); - visualMaterial.SetSpecular(gz::math::Color(parsed[2][0],parsed[2][1], parsed[2][2], parsed[2][3])); + visualMaterial.SetAmbient(parsed.ambient.value()); + visualMaterial.SetDiffuse(parsed.diffuse.value()); + visualMaterial.SetSpecular(parsed.specular.value()); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc index ca0201d23f..e28d5d5fbf 100644 --- a/src/rendering/MaterialParser/ConfigLoader.cc +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -147,7 +147,7 @@ void ConfigLoader::_nextToken(std::ifstream & stream) } // Text token - if (ch < 32 || ch > 122) { // Verify valid char + if (ch < 32 || ch > 122) { // Verify valid char throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); } @@ -182,7 +182,7 @@ void ConfigLoader::_nextToken(std::ifstream & stream) } // Add valid char to tokVal - tokVal += (char)ch; + tokVal += static_cast(ch); // Next char ch = stream.get(); @@ -277,7 +277,7 @@ ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name) { m_name = name; m_parent = parent; - _removeSelf = true; // For proper destruction + _removeSelf = true; // For proper destruction m_lastChildFound = -1; // Add self to parent's child list (unless this is the root node being created) @@ -318,7 +318,7 @@ ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) { int indx, prevC, nextC; - int childCount = (int)m_children.size(); + int childCount = static_cast(m_children.size()); if (m_lastChildFound != -1) { // If possible, try checking the nodes neighboring the last successful search diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh index ca0201d23f..0a173ee9e1 100644 --- a/src/rendering/MaterialParser/ConfigLoader.hh +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -15,381 +15,147 @@ * */ -#include "ConfigLoader.hh" +#ifndef RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ +#define RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ #include -#include -#include -#include -#include +#include +#include +#include #include -using namespace gz; -using namespace sim; - -void ConfigLoader::loadAllFiles(ConfigLoader * c, const std::string & path) +namespace gz { - try - { - for (std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir) { - std::filesystem::path p(* dir); - if (p.extension() == c->m_fileEnding) { - std::ifstream in((*dir).path().string().c_str(), std::ios::binary); - c->parseScript(in); - } - } - } - catch(std::filesystem::filesystem_error & e) + namespace sim { - std::cerr << e.what() << std::endl; - } -} + class ConfigNode; -ConfigLoader::ConfigLoader(const std::string & fileEnding) -{ - // Register as a ScriptLoader - m_fileEnding = fileEnding; -} + class ConfigLoader + { +public: + static void loadAllFiles(ConfigLoader * c, const std::string & path); -ConfigLoader::~ConfigLoader() -{ - clearScriptList(); -} + ConfigLoader(const std::string & fileEnding); -void ConfigLoader::clearScriptList() -{ - std::map < std::string, ConfigNode * > ::iterator i; - for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) { - delete i->second; - } - m_scriptList.clear(); -} - -ConfigNode * ConfigLoader::getConfigScript(const std::string & name) -{ - std::map < std::string, ConfigNode * > ::iterator i; + ~ConfigLoader(); - std::string key = name; - i = m_scriptList.find(key); + std::string m_fileEnding; - // If found.. - if (i != m_scriptList.end()) { - return i->second; - } else { - return NULL; - } -} - -std::map < std::string, ConfigNode * > ConfigLoader::getAllConfigScripts() -{ - return m_scriptList; -} + // For a line like + // entity animals/dog + // { + // ... + // } + // The type is "entity" and the name is "animals/dog" + // Or if animal/dog was not there then name is "" + virtual ConfigNode * getConfigScript(const std::string & name); -void ConfigLoader::parseScript(std::ifstream & stream) -{ - // Get first token - _nextToken(stream); - if (tok == TOKEN_EOF) { - stream.close(); - return; - } + virtual std::map < std::string, ConfigNode * > getAllConfigScripts(); - // Parse the script - _parseNodes(stream, 0); + virtual void parseScript(std::ifstream & stream); - stream.close(); -} +protected: + float m_LoadOrder; + // like "*.object" -void ConfigLoader::_nextToken(std::ifstream & stream) -{ - lastTok = tok; - lastTokVal = tokVal; - - // EOF token - if (stream.eof()) { - tok = TOKEN_EOF; - return; - } - - // Get next character - int ch = stream.get(); - if (ch == -1) { - tok = TOKEN_EOF; - return; - } - while ((ch == ' ' || ch == 9) && !stream.eof()) { // Skip leading spaces / tabs - ch = stream.get(); - } - - if (stream.eof()) { - tok = TOKEN_EOF; - return; - } - - // Newline token - if (ch == '\r' || ch == '\n') { - do{ - ch = stream.get(); - } while ((ch == '\r' || ch == '\n') && !stream.eof()); - - stream.unget(); - - tok = TOKEN_NewLine; - return; - } else if (ch == '{') { - // Open brace token - tok = TOKEN_OpenBrace; - return; - } else if (ch == '}') { - // Close brace token - tok = TOKEN_CloseBrace; - return; - } - - // Text token - if (ch < 32 || ch > 122) { // Verify valid char - throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); - } - - tokVal = ""; - tok = TOKEN_Text; - do{ - // Skip comments - if (ch == '/') { - int ch2 = stream.peek(); - - // C++ style comment (//) - if (ch2 == '/') { - stream.get(); - do{ - ch = stream.get(); - } while (ch != '\r' && ch != '\n' && !stream.eof()); - - tok = TOKEN_NewLine; - return; - } else if (ch2 == '*') { - stream.get(); - do{ - ch = stream.get(); - ch2 = stream.peek(); - } while (!(ch == '*' && ch2 == '/') && !stream.eof()); - stream.get(); - do{ - ch = stream.get(); - } while (ch != '\r' && ch != '\n' && !stream.eof()); - continue; - } - } + std::map < std::string, ConfigNode * > m_scriptList; - // Add valid char to tokVal - tokVal += (char)ch; + enum Token + { + TOKEN_Text, + TOKEN_NewLine, + TOKEN_OpenBrace, + TOKEN_CloseBrace, + TOKEN_EOF, + }; - // Next char - ch = stream.get(); - } while (ch > 32 && ch <= 122 && !stream.eof()); + Token tok, lastTok; + std::string tokVal, lastTokVal; - stream.unget(); + void _parseNodes(std::ifstream & stream, ConfigNode * parent); + void _nextToken(std::ifstream & stream); + void _skipNewLines(std::ifstream & stream); - return; -} + virtual void clearScriptList(); + }; -void ConfigLoader::_skipNewLines(std::ifstream & stream) -{ - while (tok == TOKEN_NewLine) { - _nextToken(stream); - } -} + class ConfigNode + { +public: + ConfigNode(ConfigNode * parent, const std::string & name = "untitled"); + ~ConfigNode(); -void ConfigLoader::_parseNodes(std::ifstream & stream, ConfigNode * parent) -{ - typedef std::pair < std::string, ConfigNode * > ScriptItem; - - while (true) { - switch (tok) { - // Node - case TOKEN_Text: - // Add the new node - ConfigNode * newNode; - if (parent) { - newNode = parent->addChild(tokVal); - } else { - newNode = new ConfigNode(0, tokVal); - } + inline void setName(const std::string & name) + { + this->m_name = name; + } - // Get values - _nextToken(stream); - while (tok == TOKEN_Text) { - newNode->addValue(tokVal); - _nextToken(stream); - } + inline std::string & getName() + { + return m_name; + } - // Add root nodes to scriptList - if (!parent) { - std::string key; + inline void addValue(const std::string & value) + { + m_values.push_back(value); + } - if (newNode->getValues().empty()) { - key = newNode->getName() + ' '; - } else { - key = newNode->getName() + ' ' + newNode->getValues().front(); - } + inline void clearValues() + { + m_values.clear(); + } - m_scriptList.insert(ScriptItem(key, newNode)); - } + inline std::vector < std::string > & getValues() + { + return m_values; + } - _skipNewLines(stream); - - // Add any sub-nodes - if (tok == TOKEN_OpenBrace) { - // Parse nodes - _nextToken(stream); - _parseNodes(stream, newNode); - // Check for matching closing brace - if (tok != TOKEN_CloseBrace) { - throw std::runtime_error("Parse Error: Expecting closing brace"); - } - _nextToken(stream); - _skipNewLines(stream); + inline void getValuesInFloat(std::vector < float > & floatValues) + { + for (const auto & str : m_values) { + floatValues.push_back(std::stof(str)); } + } - break; + inline const std::string & getValue(unsigned int index = 0) + { + assert(index < m_values.size()); + return m_values[index]; + } - // Out of place brace - case TOKEN_OpenBrace: - throw std::runtime_error("Parse Error: Opening brace out of plane"); - break; + ConfigNode * addChild(const std::string & name = "untitled", bool replaceExisting = false); + ConfigNode * findChild(const std::string & name, bool recursive = false); - // Return if end of nodes have been reached - case TOKEN_CloseBrace: - return; + inline std::vector < ConfigNode * > & getChildren() + { + return m_children; + } - // Return if reached end of file - case TOKEN_EOF: - return; + inline ConfigNode * getChild(unsigned int index = 0) + { + assert(index < m_children.size()); + return m_children[index]; + } - case TOKEN_NewLine: - _nextToken(stream); - break; - } - } -} + void setParent(ConfigNode * newParent); -ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name) -{ - m_name = name; - m_parent = parent; - _removeSelf = true; // For proper destruction - m_lastChildFound = -1; - - // Add self to parent's child list (unless this is the root node being created) - if (parent != NULL) { - m_parent->m_children.push_back(this); - _iter = --(m_parent->m_children.end()); - } -} - -ConfigNode::~ConfigNode() -{ - // Delete all children - std::vector < ConfigNode * > ::iterator i; - for (i = m_children.begin(); i != m_children.end(); i++) { - ConfigNode * node = *i; - node->_removeSelf = false; - delete node; - } - m_children.clear(); - - // Remove self from parent's child list - if (_removeSelf && m_parent != NULL) { - m_parent->m_children.erase(_iter); - } -} - -ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting) -{ - if (replaceExisting) { - ConfigNode * node = findChild(name, false); - if (node) { - return node; - } - } - return new ConfigNode(this, name); -} - -ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) -{ - int indx, prevC, nextC; - int childCount = (int)m_children.size(); - - if (m_lastChildFound != -1) { - // If possible, try checking the nodes neighboring the last successful search - // (often nodes searched for in sequence, so this will std search speeds). - prevC = m_lastChildFound - 1; - if (prevC < 0) { - prevC = 0; - } else if (prevC >= childCount) { - prevC = childCount - 1; - } - nextC = m_lastChildFound + 1; - if (nextC < 0) { - nextC = 0; - } else if (nextC >= childCount) { - nextC = childCount - 1; - } - for (indx = prevC; indx <= nextC; ++indx) { - ConfigNode * node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - - // If not found that way, search for the node from start to finish, avoiding the - // already searched area above. - for (indx = nextC + 1; indx < childCount; ++indx) { - ConfigNode * node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; + inline ConfigNode * getParent() + { + return m_parent; } - } - for (indx = 0; indx < prevC; ++indx) { - ConfigNode * node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - } else { - // Search for the node from start to finish - for (indx = 0; indx < childCount; ++indx) { - ConfigNode * node = m_children[indx]; - if (node->m_name == name) { - m_lastChildFound = indx; - return node; - } - } - } - // If not found, search child nodes (if recursive == true) - if (recursive) { - for (indx = 0; indx < childCount; ++indx) { - m_children[indx]->findChild(name, recursive); - } - } +private: + std::string m_name; + std::vector < std::string > m_values; + std::vector < ConfigNode * > m_children; + ConfigNode * m_parent; - // Not found anywhere - return NULL; -} - -void ConfigNode::setParent(ConfigNode * newParent) -{ - // Remove self from current parent - m_parent->m_children.erase(_iter); + int m_lastChildFound; // The last child node's index found with a call to findChild() - // Set new parent - m_parent = newParent; + std::vector < ConfigNode * > ::iterator _iter; + bool _removeSelf; + }; + } // namespace sim +} // namespace gz - // Add self to new parent - m_parent->m_children.push_back(this); - _iter = --(m_parent->m_children.end()); -} +#endif // RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index c96eb17c24..a6a63cbb9f 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -18,13 +18,9 @@ #include #include #include -#include -#include #include #include -#include -#include "ConfigLoader.hh" #include "MaterialParser.hh" using namespace gz; @@ -32,28 +28,22 @@ using namespace sim; ///////////////////////////////////////////////// MaterialParser::MaterialParser() + : configLoader(".material") { - this->configLoader = new ConfigLoader(".material"); -} - -///////////////////////////////////////////////// -MaterialParser::~MaterialParser() -{ - delete this->configLoader; } ///////////////////////////////////////////////// void MaterialParser::Load(const std::string & _path) { - ConfigLoader::loadAllFiles(this->configLoader, _path); + ConfigLoader::loadAllFiles(&this->configLoader, _path); } ///////////////////////////////////////////////// -std::vector < std::vector < float >> MaterialParser::GetMaterialValues(std::string material) const +MaterialParser::MaterialValues MaterialParser::GetMaterialValues(std::string material) { - std::vector < std::vector < float >> values; + MaterialValues values; std::map < std::string, ConfigNode * > scripts = - this->configLoader->getAllConfigScripts(); + this->configLoader.getAllConfigScripts(); std::map < std::string, ConfigNode * > ::iterator it; @@ -76,21 +66,23 @@ std::vector < std::vector < float >> MaterialParser::GetMaterialValues(std::stri if (ambientNode) { std::vector < float > ambientValues; ambientNode->getValuesInFloat(ambientValues); - values.push_back(ambientValues); + values.ambient = gz::math::Color(ambientValues[0], ambientValues[1], ambientValues[2]); } ConfigNode * diffuseNode = passNode->findChild("diffuse"); if (diffuseNode) { std::vector < float > diffuseValues; diffuseNode->getValuesInFloat(diffuseValues); - values.push_back(diffuseValues); + values.diffuse = gz::math::Color(diffuseValues[0], diffuseValues[1], diffuseValues[2]); } ConfigNode * specularNode = passNode->findChild("specular"); if (specularNode) { std::vector < float > specularValues; specularNode->getValuesInFloat(specularValues); - values.push_back(specularValues); + // Using first four values for specular as Gazebo doesn't support shininess + values.specular = gz::math::Color + (specularValues[0], specularValues[1], specularValues[2], specularValues[3]); } } } diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 071549a5a6..98f1d8bf98 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -20,23 +20,28 @@ #include #include +#include #include +#include + +#include "ConfigLoader.hh" namespace gz { namespace sim { - class ConfigNode; - class ConfigLoader; - class MaterialParser { public: - MaterialParser(); - + struct MaterialValues + { + std::optional ambient; + std::optional diffuse; + std::optional specular; + }; public: - ~MaterialParser(); + MaterialParser(); public: void Load(const std::string & _path); @@ -45,10 +50,10 @@ public: void Load() {Load(std::filesystem::current_path().string());} public: - std::vector < std::vector < float >> GetMaterialValues(std::string material) const; + MaterialValues GetMaterialValues(std::string material); private: - ConfigLoader * configLoader = nullptr; + ConfigLoader configLoader; }; } // namespace sim } // namespace gz From 4158a72da0bfb3635ec3da7ff9f6e3df16419892 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Wed, 20 Dec 2023 23:57:59 +0000 Subject: [PATCH 07/20] default color and todos Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 7 ++++--- src/rendering/MaterialParser/MaterialParser.cc | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 734340ea67..461e0f2639 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -807,14 +807,15 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) std::string scriptName = visualMaterial.ScriptName(); if((scriptName.find("Gazebo/") != std::string::npos)) { + // \todo Add documentation page with warning gzwarn << "Using an internal gazebo.material to parse " << scriptName << std::endl; this->dataPtr->materialParser.Load(); MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - visualMaterial.SetAmbient(parsed.ambient.value()); - visualMaterial.SetDiffuse(parsed.diffuse.value()); - visualMaterial.SetSpecular(parsed.specular.value()); + visualMaterial.SetAmbient(parsed.ambient.value_or(math::Color::Black)); + visualMaterial.SetDiffuse(parsed.diffuse.value_or(math::Color::Black)); + visualMaterial.SetSpecular(parsed.specular.value_or(math::Color::Black)); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index a6a63cbb9f..477c4d035a 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -49,7 +49,6 @@ MaterialParser::MaterialValues MaterialParser::GetMaterialValues(std::string mat for (it = scripts.begin(); it != scripts.end(); ++it) { std::string name = it->first; - if (name.find(material) != std::string::npos) { ConfigNode * node = it->second; @@ -86,6 +85,7 @@ MaterialParser::MaterialValues MaterialParser::GetMaterialValues(std::string mat } } } + // \todo Handle dependent materials } } return values; From 930e89f7c0b038db0b09baaa4787c9c56d3aac25 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Thu, 21 Dec 2023 20:40:54 +0000 Subject: [PATCH 08/20] install/load one file, range based loop, hardcode dependent solid colors Signed-off-by: Dharini Dutia --- include/gz/sim/InstallationDirectories.hh | 3 ++ src/CMakeLists.txt | 4 ++ src/InstallationDirectories.cc | 6 +++ src/SdfEntityCreator.cc | 6 +-- src/rendering/MaterialParser/ConfigLoader.cc | 38 +++++++++---------- src/rendering/MaterialParser/ConfigLoader.hh | 2 +- .../MaterialParser/MaterialParser.cc | 4 +- .../MaterialParser/MaterialParser.hh | 5 +-- src/rendering/MaterialParser/gazebo.material | 18 +++++++++ 9 files changed, 56 insertions(+), 30 deletions(-) diff --git a/include/gz/sim/InstallationDirectories.hh b/include/gz/sim/InstallationDirectories.hh index 08e3b2b29e..a34478376b 100644 --- a/include/gz/sim/InstallationDirectories.hh +++ b/include/gz/sim/InstallationDirectories.hh @@ -50,6 +50,9 @@ namespace gz /// \brief getWorldInstallDir return the world install dir GZ_SIM_VISIBLE std::string getWorldInstallDir(); + + /// \brief getMediaInstallDir return the media install dir + GZ_SIM_VISIBLE std::string getMediaInstallDir(); } } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03a8f6d93d..0aeec12078 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -206,7 +206,11 @@ set_property( GZ_SIM_PLUGIN_RELATIVE_INSTALL_DIR="${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins" GZ_SIM_GUI_PLUGIN_RELATIVE_INSTALL_DIR="${GZ_LIB_INSTALL_DIR}/gz-${GZ_DESIGNATION}-${PROJECT_VERSION_MAJOR}/plugins/gui" GZ_SIM_WORLD_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/worlds" + GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/media" ) +file(GLOB material_files "*.material") +install(FILES ${material_files} + DESTINATION ${GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR}) target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PUBLIC diff --git a/src/InstallationDirectories.cc b/src/InstallationDirectories.cc index 2aa64e334d..f7b22f7cf6 100644 --- a/src/InstallationDirectories.cc +++ b/src/InstallationDirectories.cc @@ -62,6 +62,12 @@ std::string getWorldInstallDir() getInstallPrefix(), GZ_SIM_WORLD_RELATIVE_INSTALL_DIR); } +std::string getMediaInstallDir() +{ + return gz::common::joinPaths( + getInstallPrefix(), GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR); +} + } } } diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 461e0f2639..483c2b2d05 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -813,9 +813,9 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) this->dataPtr->materialParser.Load(); MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - visualMaterial.SetAmbient(parsed.ambient.value_or(math::Color::Black)); - visualMaterial.SetDiffuse(parsed.diffuse.value_or(math::Color::Black)); - visualMaterial.SetSpecular(parsed.specular.value_or(math::Color::Black)); + visualMaterial.SetAmbient(parsed.ambient.value_or(visualMaterial.Ambient())); + visualMaterial.SetDiffuse(parsed.diffuse.value_or(visualMaterial.Diffuse())); + visualMaterial.SetSpecular(parsed.specular.value_or(visualMaterial.Specular())); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc index e28d5d5fbf..2a4ca197dd 100644 --- a/src/rendering/MaterialParser/ConfigLoader.cc +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -15,8 +15,6 @@ * */ -#include "ConfigLoader.hh" - #include #include #include @@ -24,24 +22,29 @@ #include #include +#include +#include +#include "gz/sim/InstallationDirectories.hh" + +#include "ConfigLoader.hh" + using namespace gz; using namespace sim; -void ConfigLoader::loadAllFiles(ConfigLoader * c, const std::string & path) +void ConfigLoader::loadMaterialFiles(ConfigLoader * c) { try { - for (std::filesystem::recursive_directory_iterator end, dir(path); dir != end; ++dir) { - std::filesystem::path p(* dir); - if (p.extension() == c->m_fileEnding) { - std::ifstream in((*dir).path().string().c_str(), std::ios::binary); - c->parseScript(in); - } - } + std::string installedConfig = common::joinPaths( + gz::sim::getMediaInstallDir(), + "gazebo.material"); + std::ifstream in(installedConfig, std::ios::binary); + c->parseScript(in); + } catch(std::filesystem::filesystem_error & e) { - std::cerr << e.what() << std::endl; + gzerr << e.what() << std::endl; } } @@ -58,25 +61,20 @@ ConfigLoader::~ConfigLoader() void ConfigLoader::clearScriptList() { - std::map < std::string, ConfigNode * > ::iterator i; - for (i = m_scriptList.begin(); i != m_scriptList.end(); i++) { - delete i->second; + for (auto i : m_scriptList) { + delete i.second; } m_scriptList.clear(); } ConfigNode * ConfigLoader::getConfigScript(const std::string & name) { - std::map < std::string, ConfigNode * > ::iterator i; - - std::string key = name; - i = m_scriptList.find(key); - + auto i = m_scriptList.find(name); // If found.. if (i != m_scriptList.end()) { return i->second; } else { - return NULL; + return nullptr; } } diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh index 0a173ee9e1..a3d74fe599 100644 --- a/src/rendering/MaterialParser/ConfigLoader.hh +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -33,7 +33,7 @@ namespace gz class ConfigLoader { public: - static void loadAllFiles(ConfigLoader * c, const std::string & path); + static void loadMaterialFiles(ConfigLoader * c); ConfigLoader(const std::string & fileEnding); diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 477c4d035a..14bd5157a6 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -33,9 +33,9 @@ MaterialParser::MaterialParser() } ///////////////////////////////////////////////// -void MaterialParser::Load(const std::string & _path) +void MaterialParser::Load() { - ConfigLoader::loadAllFiles(&this->configLoader, _path); + ConfigLoader::loadMaterialFiles(&this->configLoader); } ///////////////////////////////////////////////// diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 98f1d8bf98..5a281e953c 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -44,10 +44,7 @@ public: MaterialParser(); public: - void Load(const std::string & _path); - -public: - void Load() {Load(std::filesystem::current_path().string());} + void Load(); public: MaterialValues GetMaterialValues(std::string material); diff --git a/src/rendering/MaterialParser/gazebo.material b/src/rendering/MaterialParser/gazebo.material index 16211d3512..5e26a937ea 100644 --- a/src/rendering/MaterialParser/gazebo.material +++ b/src/rendering/MaterialParser/gazebo.material @@ -198,6 +198,15 @@ material Gazebo/Grey } material Gazebo/Gray : Gazebo/Grey { + technique + { + pass main + { + ambient .3 .3 .3 1.0 + diffuse .7 .7 .7 1.0 + specular 0.01 0.01 0.01 1.000000 1.500000 + } + } } material Gazebo/DarkGrey @@ -215,6 +224,15 @@ material Gazebo/DarkGrey material Gazebo/DarkGray : Gazebo/DarkGrey { + technique + { + pass main + { + ambient .175 .175 .175 1.0 + diffuse .175 .175 .175 1.0 + specular .175 .175 .175 1.000000 1.500000 + } + } } material Gazebo/White From ee844d9f8e0f8c75c38a1656bd03adaaed06faed Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Thu, 21 Dec 2023 22:44:03 +0000 Subject: [PATCH 09/20] fix install_dir property Signed-off-by: Dharini Dutia --- src/CMakeLists.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0aeec12078..c8bb1e1519 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -208,9 +208,7 @@ set_property( GZ_SIM_WORLD_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/worlds" GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR="${GZ_DATA_INSTALL_DIR}/media" ) -file(GLOB material_files "*.material") -install(FILES ${material_files} - DESTINATION ${GZ_SIM_MEDIA_RELATIVE_INSTALL_DIR}) +install(FILES "rendering/MaterialParser/gazebo.material" DESTINATION ${GZ_DATA_INSTALL_DIR}/media) target_link_libraries(${PROJECT_LIBRARY_TARGET_NAME} PUBLIC From 083c2dd8c97ff6291d6e98a14e60a8f98db4ea0e Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Fri, 22 Dec 2023 00:35:36 +0000 Subject: [PATCH 10/20] credits, initializing cleanup Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 4 ++-- src/rendering/MaterialParser/ConfigLoader.cc | 5 ++--- src/rendering/MaterialParser/ConfigLoader.hh | 3 ++- src/rendering/MaterialParser/MaterialParser.cc | 4 ++-- src/rendering/MaterialParser/MaterialParser.hh | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 483c2b2d05..4d808804bc 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -198,6 +198,7 @@ SdfEntityCreator::SdfEntityCreator(EntityComponentManager &_ecm, { this->dataPtr->ecm = &_ecm; this->dataPtr->eventManager = &_eventManager; + this->dataPtr->materialParser.Load(); } ///////////////////////////////////////////////// @@ -806,11 +807,10 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) { std::string scriptName = visualMaterial.ScriptName(); - if((scriptName.find("Gazebo/") != std::string::npos)) { + if ((scriptName.find("Gazebo/") == 0u)) { // \todo Add documentation page with warning gzwarn << "Using an internal gazebo.material to parse " << scriptName << std::endl; - this->dataPtr->materialParser.Load(); MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); visualMaterial.SetAmbient(parsed.ambient.value_or(visualMaterial.Ambient())); diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc index 2a4ca197dd..0830cb64f9 100644 --- a/src/rendering/MaterialParser/ConfigLoader.cc +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -14,6 +14,7 @@ * limitations under the License. * */ +// This code is adapted from https://wiki.ogre3d.org/All-purpose+script+parser #include #include @@ -48,10 +49,8 @@ void ConfigLoader::loadMaterialFiles(ConfigLoader * c) } } -ConfigLoader::ConfigLoader(const std::string & fileEnding) +ConfigLoader::ConfigLoader() { - // Register as a ScriptLoader - m_fileEnding = fileEnding; } ConfigLoader::~ConfigLoader() diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh index a3d74fe599..9160181625 100644 --- a/src/rendering/MaterialParser/ConfigLoader.hh +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -14,6 +14,7 @@ * limitations under the License. * */ +// This code is adapted from https://wiki.ogre3d.org/All-purpose+script+parser #ifndef RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ #define RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ @@ -35,7 +36,7 @@ namespace gz public: static void loadMaterialFiles(ConfigLoader * c); - ConfigLoader(const std::string & fileEnding); + ConfigLoader(); ~ConfigLoader(); diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 14bd5157a6..e98040a570 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -28,7 +28,7 @@ using namespace sim; ///////////////////////////////////////////////// MaterialParser::MaterialParser() - : configLoader(".material") + : configLoader() { } @@ -39,7 +39,7 @@ void MaterialParser::Load() } ///////////////////////////////////////////////// -MaterialParser::MaterialValues MaterialParser::GetMaterialValues(std::string material) +MaterialParser::MaterialValues MaterialParser::GetMaterialValues(const std::string& material) { MaterialValues values; std::map < std::string, ConfigNode * > scripts = diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 5a281e953c..2478b52de7 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -47,7 +47,7 @@ public: void Load(); public: - MaterialValues GetMaterialValues(std::string material); + MaterialValues GetMaterialValues(const std::string& material); private: ConfigLoader configLoader; From a83e86c4856879d479a87ece3721f5a5758e799b Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Fri, 22 Dec 2023 02:36:40 +0000 Subject: [PATCH 11/20] eof Signed-off-by: Dharini Dutia --- src/rendering/MaterialParser/gazebo.material | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rendering/MaterialParser/gazebo.material b/src/rendering/MaterialParser/gazebo.material index 5e26a937ea..cca4f4ceff 100644 --- a/src/rendering/MaterialParser/gazebo.material +++ b/src/rendering/MaterialParser/gazebo.material @@ -1722,4 +1722,4 @@ material Gazebo/EditorPlane specular 0.01 0.01 0.01 1.000000 1.500000 } } -} \ No newline at end of file +} From 07415de2ad0531b71553eabfb5eb7a0e1c042a96 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Wed, 27 Dec 2023 20:52:25 +0000 Subject: [PATCH 12/20] reformat Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 17 +- src/rendering/MaterialParser/ConfigLoader.cc | 23 +- src/rendering/MaterialParser/ConfigLoader.hh | 231 +++++++++--------- .../MaterialParser/MaterialParser.cc | 28 ++- .../MaterialParser/MaterialParser.hh | 46 ++-- 5 files changed, 181 insertions(+), 164 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 4d808804bc..6fe1a31f82 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -807,15 +807,20 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) { std::string scriptName = visualMaterial.ScriptName(); - if ((scriptName.find("Gazebo/") == 0u)) { + if ((scriptName.find("Gazebo/") == 0u)) + { // \todo Add documentation page with warning gzwarn << "Using an internal gazebo.material to parse " << scriptName << std::endl; - MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - - visualMaterial.SetAmbient(parsed.ambient.value_or(visualMaterial.Ambient())); - visualMaterial.SetDiffuse(parsed.diffuse.value_or(visualMaterial.Diffuse())); - visualMaterial.SetSpecular(parsed.specular.value_or(visualMaterial.Specular())); + MaterialParser::MaterialValues parsed = + this->dataPtr->materialParser.GetMaterialValues(scriptName); + + visualMaterial.SetAmbient + (parsed.ambient.value_or(visualMaterial.Ambient())); + visualMaterial.SetDiffuse + (parsed.diffuse.value_or(visualMaterial.Diffuse())); + visualMaterial.SetSpecular + (parsed.specular.value_or(visualMaterial.Specular())); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/ConfigLoader.cc b/src/rendering/MaterialParser/ConfigLoader.cc index 0830cb64f9..36038d466b 100644 --- a/src/rendering/MaterialParser/ConfigLoader.cc +++ b/src/rendering/MaterialParser/ConfigLoader.cc @@ -41,7 +41,6 @@ void ConfigLoader::loadMaterialFiles(ConfigLoader * c) "gazebo.material"); std::ifstream in(installedConfig, std::ios::binary); c->parseScript(in); - } catch(std::filesystem::filesystem_error & e) { @@ -60,7 +59,7 @@ ConfigLoader::~ConfigLoader() void ConfigLoader::clearScriptList() { - for (auto i : m_scriptList) { + for (auto & i : m_scriptList) { delete i.second; } m_scriptList.clear(); @@ -77,7 +76,7 @@ ConfigNode * ConfigLoader::getConfigScript(const std::string & name) } } -std::map < std::string, ConfigNode * > ConfigLoader::getAllConfigScripts() +std::map ConfigLoader::getAllConfigScripts() { return m_scriptList; } @@ -114,7 +113,8 @@ void ConfigLoader::_nextToken(std::ifstream & stream) tok = TOKEN_EOF; return; } - while ((ch == ' ' || ch == 9) && !stream.eof()) { // Skip leading spaces / tabs + // Skip leading spaces / tabs + while ((ch == ' ' || ch == 9) && !stream.eof()) { ch = stream.get(); } @@ -145,7 +145,8 @@ void ConfigLoader::_nextToken(std::ifstream & stream) // Text token if (ch < 32 || ch > 122) { // Verify valid char - throw std::runtime_error("Parse Error: Invalid character, ConfigLoader::load()"); + throw std::runtime_error( + "Parse Error: Invalid character, ConfigLoader::load()"); } tokVal = ""; @@ -277,7 +278,8 @@ ConfigNode::ConfigNode(ConfigNode * parent, const std::string & name) _removeSelf = true; // For proper destruction m_lastChildFound = -1; - // Add self to parent's child list (unless this is the root node being created) + // Add self to parent's child list + // (unless this is the root node being created) if (parent != NULL) { m_parent->m_children.push_back(this); _iter = --(m_parent->m_children.end()); @@ -301,7 +303,8 @@ ConfigNode::~ConfigNode() } } -ConfigNode * ConfigNode::addChild(const std::string & name, bool replaceExisting) +ConfigNode * ConfigNode::addChild( + const std::string & name, bool replaceExisting) { if (replaceExisting) { ConfigNode * node = findChild(name, false); @@ -318,7 +321,7 @@ ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) int childCount = static_cast(m_children.size()); if (m_lastChildFound != -1) { - // If possible, try checking the nodes neighboring the last successful search + // If possible, try checking nodes neighboring the last successful search // (often nodes searched for in sequence, so this will std search speeds). prevC = m_lastChildFound - 1; if (prevC < 0) { @@ -340,8 +343,8 @@ ConfigNode * ConfigNode::findChild(const std::string & name, bool recursive) } } - // If not found that way, search for the node from start to finish, avoiding the - // already searched area above. + // If not found that way, search for the node from start to finish, + // avoiding the already searched area above. for (indx = nextC + 1; indx < childCount; ++indx) { ConfigNode * node = m_children[indx]; if (node->m_name == name) { diff --git a/src/rendering/MaterialParser/ConfigLoader.hh b/src/rendering/MaterialParser/ConfigLoader.hh index 9160181625..b6c846e1e1 100644 --- a/src/rendering/MaterialParser/ConfigLoader.hh +++ b/src/rendering/MaterialParser/ConfigLoader.hh @@ -19,144 +19,153 @@ #ifndef RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ #define RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ -#include #include -#include #include +#include +#include #include namespace gz { - namespace sim - { - class ConfigNode; +namespace sim +{ +class ConfigNode; - class ConfigLoader - { +class ConfigLoader +{ public: - static void loadMaterialFiles(ConfigLoader * c); + static void loadMaterialFiles(ConfigLoader * c); - ConfigLoader(); + ConfigLoader(); - ~ConfigLoader(); + ~ConfigLoader(); - std::string m_fileEnding; + std::string m_fileEnding; - // For a line like - // entity animals/dog - // { - // ... - // } - // The type is "entity" and the name is "animals/dog" - // Or if animal/dog was not there then name is "" - virtual ConfigNode * getConfigScript(const std::string & name); + // For a line like + // entity animals/dog + // { + // ... + // } + // The type is "entity" and the name is "animals/dog" + // Or if animal/dog was not there then name is "" + virtual ConfigNode * getConfigScript(const std::string & name); - virtual std::map < std::string, ConfigNode * > getAllConfigScripts(); + virtual std::map getAllConfigScripts(); - virtual void parseScript(std::ifstream & stream); + virtual void parseScript(std::ifstream & stream); protected: - float m_LoadOrder; - // like "*.object" + float m_LoadOrder; + // like "*.object" - std::map < std::string, ConfigNode * > m_scriptList; + std::map m_scriptList; - enum Token - { - TOKEN_Text, - TOKEN_NewLine, - TOKEN_OpenBrace, - TOKEN_CloseBrace, - TOKEN_EOF, - }; + enum Token + { + TOKEN_Text, + TOKEN_NewLine, + TOKEN_OpenBrace, + TOKEN_CloseBrace, + TOKEN_EOF, + }; - Token tok, lastTok; - std::string tokVal, lastTokVal; + Token tok, lastTok; + std::string tokVal, lastTokVal; - void _parseNodes(std::ifstream & stream, ConfigNode * parent); - void _nextToken(std::ifstream & stream); - void _skipNewLines(std::ifstream & stream); + void _parseNodes(std::ifstream & stream, ConfigNode * parent); + void _nextToken(std::ifstream & stream); + void _skipNewLines(std::ifstream & stream); - virtual void clearScriptList(); - }; + virtual void clearScriptList(); +}; - class ConfigNode - { +class ConfigNode +{ public: - ConfigNode(ConfigNode * parent, const std::string & name = "untitled"); - ~ConfigNode(); - - inline void setName(const std::string & name) - { - this->m_name = name; - } - - inline std::string & getName() - { - return m_name; - } - - inline void addValue(const std::string & value) - { - m_values.push_back(value); - } - - inline void clearValues() - { - m_values.clear(); - } - - inline std::vector < std::string > & getValues() - { - return m_values; - } - - inline void getValuesInFloat(std::vector < float > & floatValues) - { - for (const auto & str : m_values) { - floatValues.push_back(std::stof(str)); - } - } - - inline const std::string & getValue(unsigned int index = 0) - { - assert(index < m_values.size()); - return m_values[index]; - } - - ConfigNode * addChild(const std::string & name = "untitled", bool replaceExisting = false); - ConfigNode * findChild(const std::string & name, bool recursive = false); - - inline std::vector < ConfigNode * > & getChildren() - { - return m_children; - } - - inline ConfigNode * getChild(unsigned int index = 0) - { - assert(index < m_children.size()); - return m_children[index]; - } - - void setParent(ConfigNode * newParent); - - inline ConfigNode * getParent() - { - return m_parent; - } + explicit ConfigNode(ConfigNode * parent, + const std::string & name = "untitled"); + + ~ConfigNode(); + + inline void setName(const std::string & name) + { + this->m_name = name; + } + + inline std::string & getName() + { + return m_name; + } + + inline void addValue(const std::string & value) + { + m_values.push_back(value); + } + + inline void clearValues() + { + m_values.clear(); + } + + inline std::vector & getValues() + { + return m_values; + } + + inline void getValuesInFloat(std::vector & floatValues) + { + for (const auto & str : m_values) { + floatValues.push_back(std::stof(str)); + } + } + + inline const std::string & getValue(unsigned int index = 0) + { + assert(index < m_values.size()); + return m_values[index]; + } + + ConfigNode * addChild( + const std::string & name = "untitled", bool replaceExisting = false); + + ConfigNode * findChild(const std::string & name, bool recursive = false); + + inline std::vector & getChildren() + { + return m_children; + } + + inline ConfigNode * getChild(unsigned int index = 0) + { + assert(index < m_children.size()); + return m_children[index]; + } + + void setParent(ConfigNode * newParent); + + inline ConfigNode * getParent() + { + return m_parent; + } private: - std::string m_name; - std::vector < std::string > m_values; - std::vector < ConfigNode * > m_children; - ConfigNode * m_parent; + std::string m_name; + + std::vector m_values; + + std::vector m_children; + + ConfigNode * m_parent; + + // The last child node's index found with a call to findChild() + int m_lastChildFound; - int m_lastChildFound; // The last child node's index found with a call to findChild() + std::vector ::iterator _iter; - std::vector < ConfigNode * > ::iterator _iter; - bool _removeSelf; - }; - } // namespace sim + bool _removeSelf; +}; +} // namespace sim } // namespace gz #endif // RENDERING__MATERIALPARSER__CONFIGLOADER_HH_ diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index e98040a570..6983544adf 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -15,9 +15,9 @@ * */ +#include #include #include -#include #include #include @@ -39,13 +39,14 @@ void MaterialParser::Load() } ///////////////////////////////////////////////// -MaterialParser::MaterialValues MaterialParser::GetMaterialValues(const std::string& material) +MaterialParser::MaterialValues MaterialParser::GetMaterialValues( + const std::string& material) { MaterialValues values; - std::map < std::string, ConfigNode * > scripts = + std::map scripts = this->configLoader.getAllConfigScripts(); - std::map < std::string, ConfigNode * > ::iterator it; + std::map ::iterator it; for (it = scripts.begin(); it != scripts.end(); ++it) { std::string name = it->first; @@ -63,25 +64,28 @@ MaterialParser::MaterialValues MaterialParser::GetMaterialValues(const std::stri ConfigNode * ambientNode = passNode->findChild("ambient"); if (ambientNode) { - std::vector < float > ambientValues; + std::vector ambientValues; ambientNode->getValuesInFloat(ambientValues); - values.ambient = gz::math::Color(ambientValues[0], ambientValues[1], ambientValues[2]); + values.ambient = gz::math::Color(ambientValues[0], + ambientValues[1], ambientValues[2]); } ConfigNode * diffuseNode = passNode->findChild("diffuse"); if (diffuseNode) { - std::vector < float > diffuseValues; + std::vector diffuseValues; diffuseNode->getValuesInFloat(diffuseValues); - values.diffuse = gz::math::Color(diffuseValues[0], diffuseValues[1], diffuseValues[2]); + values.diffuse = gz::math::Color(diffuseValues[0], + diffuseValues[1], diffuseValues[2]); } ConfigNode * specularNode = passNode->findChild("specular"); if (specularNode) { - std::vector < float > specularValues; + std::vector specularValues; specularNode->getValuesInFloat(specularValues); - // Using first four values for specular as Gazebo doesn't support shininess - values.specular = gz::math::Color - (specularValues[0], specularValues[1], specularValues[2], specularValues[3]); + // Using first four values for specular as + // Gazebo doesn't support shininess + values.specular = gz::math::Color(specularValues[0], + specularValues[1], specularValues[2], specularValues[3]); } } } diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 2478b52de7..5803c7ea99 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -18,10 +18,8 @@ #ifndef RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ #define RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ -#include -#include #include -#include +#include #include @@ -29,30 +27,28 @@ namespace gz { - namespace sim - { - class MaterialParser +namespace sim +{ +class MaterialParser +{ + public: + struct MaterialValues { -public: - struct MaterialValues - { - std::optional ambient; - std::optional diffuse; - std::optional specular; - }; -public: - MaterialParser(); - -public: - void Load(); - -public: - MaterialValues GetMaterialValues(const std::string& material); - -private: - ConfigLoader configLoader; + std::optional ambient; + std::optional diffuse; + std::optional specular; }; - } // namespace sim + + MaterialParser(); + + void Load(); + + MaterialValues GetMaterialValues(const std::string& material); + + private: + ConfigLoader configLoader; +}; +} // namespace sim } // namespace gz #endif // RENDERING__MATERIALPARSER__MATERIALPARSER_HH_ From f2e2f5f128d167f83a82d932957e3654adecc8a2 Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Mon, 1 Jan 2024 03:21:20 +0000 Subject: [PATCH 13/20] add integration test Signed-off-by: Dharini Dutia --- test/integration/CMakeLists.txt | 1 + test/integration/material.cc | 247 ++++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 test/integration/material.cc diff --git a/test/integration/CMakeLists.txt b/test/integration/CMakeLists.txt index 7ed4703dec..61f350bd73 100644 --- a/test/integration/CMakeLists.txt +++ b/test/integration/CMakeLists.txt @@ -48,6 +48,7 @@ set(tests logical_camera_system.cc logical_audio_sensor_plugin.cc magnetometer_system.cc + material.cc mesh_inertia_calculation.cc model.cc model_photo_shoot_default_joints.cc diff --git a/test/integration/material.cc b/test/integration/material.cc new file mode 100644 index 0000000000..55c51d3244 --- /dev/null +++ b/test/integration/material.cc @@ -0,0 +1,247 @@ +/* + * Copyright (C) 2023 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include + +#include +#include + +#include +#include + +#include + +#include "gz/sim/EntityComponentManager.hh" +#include "gz/sim/EventManager.hh" +#include "gz/sim/Model.hh" +#include "gz/sim/SdfEntityCreator.hh" +#include "gz/sim/Server.hh" +#include "gz/sim/SystemLoader.hh" +#include "gz/sim/Types.hh" +#include "test_config.hh" + +#include "gz/sim/components/Material.hh" +#include "gz/sim/components/Model.hh" +#include "gz/sim/components/Name.hh" +#include "gz/sim/components/Visual.hh" +#include "gz/sim/components/World.hh" + +#include "../helpers/Relay.hh" +#include "../helpers/EnvTestFixture.hh" + +using namespace gz; +using namespace sim; + +class MaterialTest : public InternalFixture<::testing::Test> +{ + public: ::testing::AssertionResult StartServer( + const gz::sim::ServerConfig &_serverConfig = + gz::sim::ServerConfig()) + { + this->relay = std::make_unique(); + this->server = std::make_unique(_serverConfig); + using namespace std::chrono_literals; + this->server->SetUpdatePeriod(0ns); + + EXPECT_FALSE(this->server->Running()); + EXPECT_FALSE(*this->server->Running(0)); + // A pointer to the ecm. This will be valid once we run the mock system + relay->OnPreUpdate( + [this](const UpdateInfo &, EntityComponentManager &_ecm) + { + this->ecm = &_ecm; + }); + + this->server->AddSystem(this->relay->systemPtr); + this->server->Run(true, 1, false); + if (nullptr == this->ecm) + { + return ::testing::AssertionFailure() + << "Failed to create EntityComponentManager"; + } + + this->creator = + std::make_unique(*this->ecm, this->dummyEventMgr); + return ::testing::AssertionSuccess(); + } + + public: void SpawnModelSDF(const std::string &_sdfString) + { + ASSERT_NE(this->server, nullptr); + ASSERT_NE(this->creator, nullptr); + + sdf::Root root; + sdf::Errors errors = root.LoadSdfString(_sdfString); + EXPECT_TRUE(errors.empty()); + ASSERT_NE(nullptr, root.Model()); + + Entity modelEntity = this->creator->CreateEntities(root.Model()); + Entity worldEntity = this->ecm->EntityByComponents(components::World()); + this->creator->SetParent(modelEntity, worldEntity); + } + + public: Model GetModel(const std::string &_name) + { + return Model(this->ecm->EntityByComponents( + components::Model(), components::Name(_name))); + } + + public: std::unique_ptr relay; + public: std::unique_ptr server; + public: EntityComponentManager *ecm {nullptr}; + public: EventManager dummyEventMgr; + public: std::unique_ptr creator; +}; + +TEST_F(MaterialTest, SolidColor) +{ + const std::string modelSdf = R"sdf( + + + 0 0 0.5 0 0 0 + + 0 -1.5 0 0 0 0 + + + + 1 1 1 + + + + + + + + + + )sdf"; + + ASSERT_TRUE(this->StartServer()); + this->SpawnModelSDF(modelSdf); + + auto model = this->GetModel("material_shapes"); + ASSERT_TRUE(model.Valid(*this->ecm)); + + auto boxVisualEntity = + this->ecm->EntityByComponents(components::Name("box_visual")); + ASSERT_NE(kNullEntity, boxVisualEntity); + + auto boxVisualComp = + this->ecm->Component(boxVisualEntity); + EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f), + boxVisualComp->Data().Ambient()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 1.0f, 1.0f), + boxVisualComp->Data().Diffuse()); + EXPECT_EQ(math::Color(0.1f, 0.1f, 0.1f, 1.0f), + boxVisualComp->Data().Specular()); +} + +TEST_F(MaterialTest, OtherColor) +{ + const std::string modelSdf = R"sdf( + + + 0 0 0.5 0 0 0 + + 0 -1.5 0 0 0 0 + + + + 1 1 1 + + + + + + + + + + )sdf"; + + ASSERT_TRUE(this->StartServer()); + this->SpawnModelSDF(modelSdf); + + auto model = this->GetModel("material_shapes"); + ASSERT_TRUE(model.Valid(*this->ecm)); + + auto boxVisualEntity = + this->ecm->EntityByComponents(components::Name("box_visual")); + ASSERT_NE(kNullEntity, boxVisualEntity); + + // Default to black color + auto boxVisualComp = + this->ecm->Component(boxVisualEntity); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Ambient()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Diffuse()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Specular()); +} + +TEST_F(MaterialTest, CustomScript) +{ + const std::string modelSdf = R"sdf( + + + 0 0 0.5 0 0 0 + + 0 -1.5 0 0 0 0 + + + + 1 1 1 + + + + + + + + + + )sdf"; + + ASSERT_TRUE(this->StartServer()); + this->SpawnModelSDF(modelSdf); + + auto model = this->GetModel("material_shapes"); + ASSERT_TRUE(model.Valid(*this->ecm)); + + auto boxVisualEntity = + this->ecm->EntityByComponents(components::Name("box_visual")); + ASSERT_NE(kNullEntity, boxVisualEntity); + + // Default to black color + auto boxVisualComp = + this->ecm->Component(boxVisualEntity); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Ambient()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Diffuse()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Specular()); +} From 7b08ebf844a62946bec0a2668554464eaa0d367d Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Tue, 2 Jan 2024 09:38:42 +0000 Subject: [PATCH 14/20] migration note Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 8 +++++--- tutorials/migration_sdf.md | 13 ++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 6fe1a31f82..ae45b94aba 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -797,6 +797,9 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) sdf::Material visualMaterial = *_visual->Material(); if (!_visual->Material()->ScriptUri().empty()) { + gzwarn << "Gazebo does not support Ogre material scripts. See " << + "https://gazebosim.org/api/sim/8/migrationsdf.html#:~:text=Materials " << + "for details." << std::endl; std::string scriptUri = visualMaterial.ScriptUri(); if (scriptUri != "file://media/materials/scripts/gazebo.material") { gzwarn << "Custom material scripts are not supported." @@ -809,9 +812,8 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) if ((scriptName.find("Gazebo/") == 0u)) { - // \todo Add documentation page with warning - gzwarn << "Using an internal gazebo.material to parse " << scriptName - << std::endl; + gzwarn << "Using an internal gazebo.material to parse " + << scriptName << std::endl; MaterialParser::MaterialValues parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); diff --git a/tutorials/migration_sdf.md b/tutorials/migration_sdf.md index 1909bd5b8c..82ff7316ed 100644 --- a/tutorials/migration_sdf.md +++ b/tutorials/migration_sdf.md @@ -269,8 +269,8 @@ that worked on Gazebo classic may need more plugins on Gazebo. ## Materials Gazebo does not support Ogre material files like Classic does, because Gazebo -Gazebo can be used with multiple rendering engines. Therefore, materials defined -within a ` + + + + + + )sdf"; + + ASSERT_TRUE(this->StartServer()); + this->SpawnModelSDF(modelSdf); + + auto model = this->GetModel("material_shapes"); + ASSERT_TRUE(model.Valid(*this->ecm)); + + auto boxVisualEntity = + this->ecm->EntityByComponents(components::Name("box_visual")); + ASSERT_NE(kNullEntity, boxVisualEntity); + + // Default to black color + auto boxVisualComp = + this->ecm->Component(boxVisualEntity); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Ambient()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Diffuse()); + EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f), + boxVisualComp->Data().Specular()); +} From 26c61ca8ddb3ae01be55bb5aaa0359a18d9ff4fb Mon Sep 17 00:00:00 2001 From: Dharini Dutia Date: Fri, 5 Jan 2024 19:44:08 +0000 Subject: [PATCH 16/20] optional materialValues, typo Signed-off-by: Dharini Dutia --- src/SdfEntityCreator.cc | 12 ++++++------ src/rendering/MaterialParser/MaterialParser.cc | 2 +- src/rendering/MaterialParser/MaterialParser.hh | 2 +- tutorials/migration_sdf.md | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/SdfEntityCreator.cc b/src/SdfEntityCreator.cc index 0dfe866f2e..ef60860691 100644 --- a/src/SdfEntityCreator.cc +++ b/src/SdfEntityCreator.cc @@ -814,20 +814,20 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual) { gzwarn << "Using an internal gazebo.material to parse " << scriptName << std::endl; - MaterialParser::MaterialValues parsed = + std::optional parsed = this->dataPtr->materialParser.GetMaterialValues(scriptName); - if(!parsed.ambient.has_value()) { + if(!parsed.has_value()) { gzwarn << "Material " << scriptName << - " not recognized, using default."<< std::endl; + " not recognized, using default." << std::endl; } visualMaterial.SetAmbient - (parsed.ambient.value_or(visualMaterial.Ambient())); + (parsed->ambient.value_or(visualMaterial.Ambient())); visualMaterial.SetDiffuse - (parsed.diffuse.value_or(visualMaterial.Diffuse())); + (parsed->diffuse.value_or(visualMaterial.Diffuse())); visualMaterial.SetSpecular - (parsed.specular.value_or(visualMaterial.Specular())); + (parsed->specular.value_or(visualMaterial.Specular())); } } this->dataPtr->ecm->CreateComponent(visualEntity, diff --git a/src/rendering/MaterialParser/MaterialParser.cc b/src/rendering/MaterialParser/MaterialParser.cc index 6983544adf..36bf006306 100644 --- a/src/rendering/MaterialParser/MaterialParser.cc +++ b/src/rendering/MaterialParser/MaterialParser.cc @@ -39,7 +39,7 @@ void MaterialParser::Load() } ///////////////////////////////////////////////// -MaterialParser::MaterialValues MaterialParser::GetMaterialValues( +std::optional MaterialParser::GetMaterialValues( const std::string& material) { MaterialValues values; diff --git a/src/rendering/MaterialParser/MaterialParser.hh b/src/rendering/MaterialParser/MaterialParser.hh index 5803c7ea99..17b542ba0b 100644 --- a/src/rendering/MaterialParser/MaterialParser.hh +++ b/src/rendering/MaterialParser/MaterialParser.hh @@ -43,7 +43,7 @@ class MaterialParser void Load(); - MaterialValues GetMaterialValues(const std::string& material); + std::optional GetMaterialValues(const std::string& material); private: ConfigLoader configLoader; diff --git a/tutorials/migration_sdf.md b/tutorials/migration_sdf.md index 82ff7316ed..4a875ec40b 100644 --- a/tutorials/migration_sdf.md +++ b/tutorials/migration_sdf.md @@ -269,7 +269,7 @@ that worked on Gazebo classic may need more plugins on Gazebo. ## Materials Gazebo does not support Ogre material files like Classic does, because Gazebo -Gazebo can be used with multiple rendering engines. For example, materials defined +can be used with multiple rendering engines. For example, materials defined within a `