Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lightmap to material #132

Merged
merged 14 commits into from
Dec 21, 2020
15 changes: 15 additions & 0 deletions graphics/include/ignition/common/Pbr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ namespace common
/// \param[in] _map Filename of the emissive map.
public: void SetEmissiveMap(const std::string &_map);

/// \brief Get the light map filename. This will be an empty string
/// if an light map has not been set.
/// \return Filename of the light map, or empty string if a light
/// map has not been specified.
public: std::string LightMap() const;

/// \brief Set the light map filename.
/// \param[in] _map Filename of the light map.
/// \param[in] _uvSet Index of the texture coordinate set
public: void SetLightMap(const std::string &_map, unsigned int _uvSet = 0u);

/// \brief Get the light map texture coordinate set.
/// \return Index of the light map texture coordinate set
public: unsigned int LightMapTexCoordSet() const;

/// \brief Get the metalness value of the material for metal workflow
/// \return metalness value of the material
public: double Metalness() const;
Expand Down
60 changes: 60 additions & 0 deletions graphics/include/ignition/common/SubMesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ namespace ignition
/// \param[in] _uv The texture coordinate
public: void AddTexCoord(const ignition::math::Vector2d &_uv);

/// \brief Add a texture coord to a texture coordinate set of the mesh
/// \param[in] _u Position along u
/// \param[in] _v Position along v
/// \param[in] _setIndex Texture coordinate set index
public: void AddTexCoordBySet(double _u, double _v,
unsigned int _setIndex);

/// \brief Add a texture coord to a texture coordinate set of the mesh
/// \param[in] _u Position along u
/// \param[in] _v Position along v
/// \param[in] _setIndex Texture coordinate set index
public: void AddTexCoordBySet(const ignition::math::Vector2d &_uv,
unsigned int _setIndex);

/// \brief Add a vertex - skeleton node assignment
/// \param[in] _vertex The vertex index
/// \param[in] _node The node index
Expand Down Expand Up @@ -165,12 +179,31 @@ namespace ignition
public: ignition::math::Vector2d TexCoord(
const unsigned int _index) const;

/// \brief Get a texture coordinate for a texture coordinate set
/// \param[in] _index the texture index
/// \return The texture coordinate or ignition::math::Vector2d::Zero
/// if index is out of bounds.
/// \param[in] _setIndex Texture coordinate set index
/// \sa bool HasTexCoordBySet(unsigned int _index, unsigned int _setIndex)
/// const
public: ignition::math::Vector2d TexCoordBySet(
unsigned int _index,
unsigned int _setIndex) const;

/// \brief Set a texture coordinate
/// \param[in] _index Index of the texture coordinate that will be set.
/// \param[in] _uv The new texture coordinate
public: void SetTexCoord(const unsigned int _index,
const ignition::math::Vector2d &_uv);

/// \brief Set a texture coordinate for a texture coordinate set
/// \param[in] _index Index of the texture coordinate that will be set.
/// \param[in] _uv The new texture coordinate
/// \param[in] _setIndex Texture coordinate set index
public: void SetTexCoordBySet(unsigned int _index,
const ignition::math::Vector2d &_uv,
unsigned int _setIdex);

/// \brief Get an index value from the index array
/// \param[in] _index Array index.
/// \return The index, or -1 if the _index is out of bounds.
Expand Down Expand Up @@ -215,6 +248,16 @@ namespace ignition
/// \return The number of texture coordinates.
public: unsigned int TexCoordCount() const;

/// \brief Return the number of texture coordinates for a texture
/// coordinate set
/// \param[in] _setIndex Texture coordinate set index
/// \return The number of texture coordinates.
public: unsigned int TexCoordCountBySet(unsigned int _setIndex) const;

/// \brief Return the number of texture coordinate sets
/// \return The number of texture coordinates sets.
public: unsigned int TexCoordSetCount() const;

/// \brief Get the number of vertex-skeleton node assignments
/// \return The number of vertex-skeleton node assignments
public: unsigned int NodeAssignmentsCount() const;
Expand Down Expand Up @@ -258,6 +301,15 @@ namespace ignition
/// the given _index.
public: bool HasTexCoord(const unsigned int _index) const;

/// \brief Return true if this submesh has the texture coordinate with
/// the given index in a texture coordinate set
/// \param[in] _index Texture coordinate index
/// \param[in] _setIndex Texture coordinate set index
/// \return Return true if this submesh has the texture coordinate with
/// the given _index.
public: bool HasTexCoordBySet(unsigned int _index, unsigned int _setIndex)
const;

/// \brief Return true if this submesh has the node assignment with
/// the given index
/// \param[in] _index Node assignment index
Expand All @@ -284,6 +336,14 @@ namespace ignition
public: void GenSphericalTexCoord(
const ignition::math::Vector3d &_center);

/// \brief Generate texture coordinates for a texture coordinate set
/// using spherical projection from center
/// \param[in] _center Center of the projection.
/// \param[in] _setIndex Texture coordinate set index
public: void GenSphericalTexCoordBySet(
const ignition::math::Vector3d &_center,
unsigned int _setIndex);

/// \brief Scale all vertices by _factor
/// \param[in] _factor Scaling factor
public: void Scale(const ignition::math::Vector3d &_factor);
Expand Down
152 changes: 108 additions & 44 deletions graphics/src/ColladaLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ namespace ignition
/// \brief Index of a normal in the collada <p> element
public: unsigned int normalIndex;

/// \brief Index of a texture coordinate in the collada <p> element
public: unsigned int texcoordIndex;
/// \brief A map of texture coordinate set index to index of a texture
/// coordinate in the collada <p> element
public: std::map<unsigned int, unsigned int> texcoordIndex;

/// \brief Index of a vertex in the Gazebo mesh
public: unsigned int mappedIndex;
Expand Down Expand Up @@ -1992,15 +1993,16 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml,

std::vector<ignition::math::Vector3d> verts;
std::vector<ignition::math::Vector3d> norms;
std::vector<ignition::math::Vector2d> texcoords;
std::map<unsigned int, std::vector<ignition::math::Vector2d>> texcoords;
std::map<unsigned int, unsigned int> texcoordsOffsetToSet;

const unsigned int VERTEX = 0;
const unsigned int NORMAL = 1;
const unsigned int TEXCOORD = 2;
unsigned int otherSemantics = TEXCOORD + 1;

// look up table of position/normal/texcoord duplicate indices
std::map<unsigned int, unsigned int> texDupMap;
std::map<unsigned int, std::map<unsigned int, unsigned int>> texDupMap;
std::map<unsigned int, unsigned int> normalDupMap;
std::map<unsigned int, unsigned int> positionDupMap;

Expand Down Expand Up @@ -2034,8 +2036,14 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml,
}
else if (semantic == "TEXCOORD")
{
this->LoadTexCoords(source, texcoords, texDupMap);
inputs[TEXCOORD].insert(ignition::math::parseInt(offset));
int offsetInt = ignition::math::parseInt(offset);
if (inputs[TEXCOORD].find(offsetInt) == inputs[TEXCOORD].end())
{
int set = ignition::math::parseInt(polylistInputXml->Attribute("set"));
this->LoadTexCoords(source, texcoords[set], texDupMap[set]);
inputs[TEXCOORD].insert(offsetInt);
texcoordsOffsetToSet[offsetInt] = set;
}
}
else
{
Expand Down Expand Up @@ -2153,16 +2161,26 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml,

if (!inputs[TEXCOORD].empty())
{
// Get the vertex texcoord index value. If the texcoord is a
// duplicate then reset the index to the first instance of the
// duplicated texcoord
unsigned int remappedTexcoordIndex =
values[*inputs[TEXCOORD].begin()];

if (texDupMap.find(remappedTexcoordIndex) != texDupMap.end())
remappedTexcoordIndex = texDupMap[remappedTexcoordIndex];

texEqual = iv.texcoordIndex == remappedTexcoordIndex;
texEqual = true;
for (auto offset : inputs[TEXCOORD])
{
int set = texcoordsOffsetToSet[offset];
// Get the vertex texcoord index value. If the texcoord is a
// duplicate then reset the index to the first instance of the
// duplicated texcoord
unsigned int remappedTexcoordIndex =
values[offset];
auto &texDupMapSet = texDupMap[set];
auto texDupMapSetIt = texDupMapSet.find(
remappedTexcoordIndex);
if (texDupMapSetIt != texDupMapSet.end())
remappedTexcoordIndex = texDupMapSetIt->second;
if (iv.texcoordIndex[set] != remappedTexcoordIndex)
{
texEqual = false;
break;
}
}
}

// if the vertex has matching normal and texcoord index values
Expand Down Expand Up @@ -2228,17 +2246,24 @@ void ColladaLoaderPrivate::LoadPolylist(tinyxml2::XMLElement *_polylistXml,

if (!inputs[TEXCOORD].empty())
{
unsigned int inputRemappedTexcoordIndex =
values[*inputs[TEXCOORD].begin()];

if (texDupMap.find(inputRemappedTexcoordIndex) != texDupMap.end())
for (auto offset : inputs[TEXCOORD])
{
inputRemappedTexcoordIndex =
texDupMap[inputRemappedTexcoordIndex];
unsigned int inputRemappedTexcoordIndex =
values[offset];

int set = texcoordsOffsetToSet[offset];

auto &texDupMapSet = texDupMap[set];
auto texDupMapSetIt = texDupMapSet.find(
inputRemappedTexcoordIndex);
if (texDupMapSetIt != texDupMapSet.end())
inputRemappedTexcoordIndex = texDupMapSetIt->second;
auto &texcoordsSet = texcoords[set];
subMesh->AddTexCoordBySet(
texcoordsSet[inputRemappedTexcoordIndex].X(),
texcoordsSet[inputRemappedTexcoordIndex].Y(), set);
input.texcoordIndex[set] = inputRemappedTexcoordIndex;
}
subMesh->AddTexCoord(texcoords[inputRemappedTexcoordIndex].X(),
texcoords[inputRemappedTexcoordIndex].Y());
input.texcoordIndex = inputRemappedTexcoordIndex;
}

// add the new ignition submesh vertex index to the map
Expand Down Expand Up @@ -2293,7 +2318,8 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml,

std::vector<ignition::math::Vector3d> verts;
std::vector<ignition::math::Vector3d> norms;
std::vector<ignition::math::Vector2d> texcoords;
std::map<unsigned int, std::vector<ignition::math::Vector2d>> texcoords;
std::map<unsigned int, unsigned int> texcoordsOffsetToSet;

const unsigned int VERTEX = 0;
const unsigned int NORMAL = 1;
Expand All @@ -2309,7 +2335,7 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml,
std::map<const unsigned int, std::set<int>> inputs;

// look up table of position/normal/texcoord duplicate indices
std::map<unsigned int, unsigned int> texDupMap;
std::map<unsigned int, std::map<unsigned int, unsigned int>> texDupMap;
std::map<unsigned int, unsigned int> normalDupMap;
std::map<unsigned int, unsigned int> positionDupMap;

Expand Down Expand Up @@ -2337,9 +2363,15 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml,
}
else if (semantic == "TEXCOORD")
{
// we currently only support one set of UVs
this->LoadTexCoords(source, texcoords, texDupMap);
inputs[TEXCOORD].insert(ignition::math::parseInt(offset));
int offsetInt = ignition::math::parseInt(offset);
if (inputs[TEXCOORD].find(offsetInt) == inputs[TEXCOORD].end())
{
unsigned int set = ignition::math::parseInt(
trianglesInputXml->Attribute("set"));
this->LoadTexCoords(source, texcoords[set], texDupMap[set]);
inputs[TEXCOORD].insert(offsetInt);
texcoordsOffsetToSet[offsetInt] = set;
}
hasTexcoords = true;
}
else
Expand Down Expand Up @@ -2454,13 +2486,36 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml,
// Get the vertex texcoord index value. If the texcoord is a
// duplicate then reset the index to the first instance of the
// duplicated texcoord
unsigned int remappedTexcoordIndex =
values.at(*inputs[TEXCOORD].begin());
if (texDupMap.find(remappedTexcoordIndex) != texDupMap.end())
remappedTexcoordIndex = texDupMap[remappedTexcoordIndex];

if (iv.texcoordIndex == remappedTexcoordIndex)
texEqual = true;
// unsigned int offset = *inputs[TEXCOORD].begin();
// unsigned int remappedTexcoordIndex =
// values.at(offset);
// int set = texcoordsOffsetToSet[offset];
// auto &texDupMapSet = texDupMap[set];
// auto texDupMapSetIt = texDupMapSet.find(remappedTexcoordIndex);
// if (texDupMapSetIt != texDupMapSet.end())
// remappedTexcoordIndex = texDupMapSetIt->second;
// texEqual = iv.texcoordIndex[set] == remappedTexcoordIndex;

texEqual = true;
for (auto offset : inputs[TEXCOORD])
{
// Get the vertex texcoord index value. If the texcoord is a
// duplicate then reset the index to the first instance of the
// duplicated texcoord
unsigned int remappedTexcoordIndex =
values.at(offset);
int set = texcoordsOffsetToSet[offset];
auto &texDupMapSet = texDupMap[set];
auto texDupMapSetIt = texDupMapSet.find(remappedTexcoordIndex);
if (texDupMapSetIt != texDupMapSet.end())
remappedTexcoordIndex = texDupMapSetIt->second;

if (iv.texcoordIndex[set] != remappedTexcoordIndex)
{
texEqual = false;
break;
}
}
}

// if the vertex has matching normal and texcoord index values then
Expand Down Expand Up @@ -2524,13 +2579,22 @@ void ColladaLoaderPrivate::LoadTriangles(tinyxml2::XMLElement *_trianglesXml,
}
if (hasTexcoords)
{
unsigned int inputRemappedTexcoordIndex =
values.at(*inputs[TEXCOORD].begin());
if (texDupMap.find(inputRemappedTexcoordIndex) != texDupMap.end())
inputRemappedTexcoordIndex = texDupMap[inputRemappedTexcoordIndex];
subMesh->AddTexCoord(texcoords[inputRemappedTexcoordIndex].X(),
texcoords[inputRemappedTexcoordIndex].Y());
input.texcoordIndex = inputRemappedTexcoordIndex;
for (auto offset : inputs[TEXCOORD])
{
unsigned int inputRemappedTexcoordIndex =
values.at(offset);

int set = texcoordsOffsetToSet[offset];
auto &texDupMapSet = texDupMap[set];
auto texDupMapSetIt = texDupMapSet.find(inputRemappedTexcoordIndex);
if (texDupMapSetIt != texDupMapSet.end())
inputRemappedTexcoordIndex = texDupMapSetIt->second;
auto &texcoordsSet = texcoords[set];
subMesh->AddTexCoordBySet(
texcoordsSet[inputRemappedTexcoordIndex].X(),
texcoordsSet[inputRemappedTexcoordIndex].Y(), set);
input.texcoordIndex[set] = inputRemappedTexcoordIndex;
}
}

// add the new ignition submesh vertex index to the map
Expand Down
Loading