Skip to content

Commit

Permalink
get color values
Browse files Browse the repository at this point in the history
Signed-off-by: Dharini Dutia <[email protected]>
  • Loading branch information
quarkytale committed Jan 10, 2024
1 parent d768c85 commit ccad6bd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
21 changes: 21 additions & 0 deletions src/rendering/MaterialParser/ConfigLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <utility>
#include <vector>

#include <gz/common/Console.hh>
#include <gz/common/Filesystem.hh>
#include <gz/math/Color.hh>
#include "gz/sim/InstallationDirectories.hh"

#include "ConfigLoader.hh"
Expand Down Expand Up @@ -305,6 +307,25 @@ ConfigNode::~ConfigNode()
}
}

void ConfigNode::getColorValues(gz::math::Color & colorValues,
unsigned int size)
{
std::vector<float> floatValues;
ConfigNode::getValuesInFloat(floatValues);
if (floatValues.size() < size) {
gzerr << "Bad material file." << std::endl;
floatValues.resize(size);

Check warning on line 317 in src/rendering/MaterialParser/ConfigLoader.cc

View check run for this annotation

Codecov / codecov/patch

src/rendering/MaterialParser/ConfigLoader.cc#L316-L317

Added lines #L316 - L317 were not covered by tests
}
if (size == 3) {
colorValues = gz::math::Color(floatValues[0], floatValues[1],
floatValues[2]);
}
if (size == 4) {
colorValues = gz::math::Color(floatValues[0], floatValues[1],
floatValues[2], floatValues[3]);
}
}

ConfigNode * ConfigNode::addChild(
const std::string & name, bool replaceExisting)
{
Expand Down
24 changes: 7 additions & 17 deletions src/rendering/MaterialParser/ConfigLoader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <string>
#include <vector>

#include <gz/common/Console.hh>

namespace gz
{
namespace sim
Expand Down Expand Up @@ -115,28 +113,20 @@ public:
return m_values;
}

inline void getValuesInFloat(std::vector<float> & floatValues)
inline const std::string & getValue(unsigned int index = 0)
{
for (const auto & str : m_values) {
floatValues.push_back(std::stof(str));
}
assert(index < m_values.size());
return m_values[index];
}

inline void getColorValues(std::vector<float> & colorValues,
unsigned int size)
inline void getValuesInFloat(std::vector<float> & floatValues)
{
getValuesInFloat(colorValues);
if (colorValues.size() < size) {
gzerr << "Bad material file." << std::endl;
colorValues.resize(size);
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];
}
void getColorValues(math::Color & colorValues, unsigned int size);

ConfigNode * addChild(
const std::string & name = "untitled", bool replaceExisting = false);
Expand Down
15 changes: 6 additions & 9 deletions src/rendering/MaterialParser/MaterialParser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,25 @@ std::optional<MaterialParser::MaterialValues> MaterialParser::GetMaterialValues(
if (passNode) {
ConfigNode * ambientNode = passNode->findChild("ambient");
if (ambientNode) {
std::vector<float> ambientValues;
gz::math::Color ambientValues;
ambientNode->getColorValues(ambientValues, 3);
values->ambient.emplace(gz::math::Color(ambientValues[0],
ambientValues[1], ambientValues[2]));
values->ambient.emplace(ambientValues);
}

ConfigNode * diffuseNode = passNode->findChild("diffuse");
if (diffuseNode) {
std::vector<float> diffuseValues;
gz::math::Color diffuseValues;
diffuseNode->getColorValues(diffuseValues, 3);
values->diffuse.emplace(gz::math::Color(diffuseValues[0],
diffuseValues[1], diffuseValues[2]));
values->diffuse.emplace(diffuseValues);
}

ConfigNode * specularNode = passNode->findChild("specular");
if (specularNode) {
std::vector<float> specularValues;
gz::math::Color specularValues;
specularNode->getColorValues(specularValues, 4);
// Using first four values for specular as
// Gazebo doesn't support shininess
values->specular.emplace(gz::math::Color(specularValues[0],
specularValues[1], specularValues[2], specularValues[3]));
values->specular.emplace(specularValues);
}
}
}
Expand Down

0 comments on commit ccad6bd

Please sign in to comment.