Skip to content

Commit

Permalink
copy MeshDescriptor and move Clone implementation to Mesh class
Browse files Browse the repository at this point in the history
Signed-off-by: Ashton Larkin <[email protected]>
  • Loading branch information
adlarkin committed Sep 16, 2021
1 parent 982b7a1 commit f5b07ea
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
7 changes: 7 additions & 0 deletions include/ignition/rendering/Mesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ignition/math/Matrix4.hh>
#include "ignition/rendering/config.hh"
#include "ignition/rendering/Geometry.hh"
#include "ignition/rendering/MeshDescriptor.hh"
#include "ignition/rendering/Object.hh"

namespace ignition
Expand Down Expand Up @@ -119,6 +120,12 @@ namespace ignition
/// \return The sub-mesh at the given index
public: virtual SubMeshPtr SubMeshByIndex(
unsigned int _index) const = 0;

/// \brief Copy an existing MeshDescriptor. Once copying is complete,
/// MeshDescriptor::Load needs to be called on the mesh's MeshDescriptor
/// before using it
/// \param[in] _copy The MeshDescriptor to copy
public: virtual void CopyMeshDescriptor(const MeshDescriptor &_copy) = 0;
};

/// \class SubMesh Mesh.hh ignition/rendering/Mesh.hh
Expand Down
23 changes: 1 addition & 22 deletions include/ignition/rendering/base/BaseGeometry.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ namespace ignition

// Documentation inherited
public: virtual void Destroy() override;

/// \brief Name of the mesh for this geometry
protected: std::string meshName;
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -101,24 +98,7 @@ namespace ignition
template <class T>
GeometryPtr BaseGeometry<T>::Clone() const
{
if (!this->Scene())
{
ignerr << "Cloning a geometry failed because the geometry to be "
<< "cloned does not belong to a scene.\n";
return nullptr;
}
else if (this->meshName.empty())
{
ignerr << "Cloning a geometry failed because the name of the mesh is "
<< "missing.\n";
return nullptr;
}

auto result = this->Scene()->CreateMesh(this->meshName);
if (result && this->Material())
result->SetMaterial(this->Material());

return result;
return nullptr;
}

//////////////////////////////////////////////////
Expand All @@ -127,7 +107,6 @@ namespace ignition
{
T::Destroy();
this->RemoveParent();
this->meshName = "";
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions include/ignition/rendering/base/BaseMesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ namespace ignition

public: virtual void PreRender() override;

public: virtual GeometryPtr Clone() const override;

public: virtual void CopyMeshDescriptor(
const MeshDescriptor &_copy) override;

// Documentation inherited
public: virtual void Destroy() override;

Expand All @@ -111,6 +116,9 @@ namespace ignition

/// \brief Pointer to currently assigned material
protected: MaterialPtr material;

/// \brief MeshDescriptor for this mesh
protected: MeshDescriptor meshDescriptor;
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -315,6 +323,58 @@ namespace ignition
T::PreRender();
}

//////////////////////////////////////////////////
template <class T>
GeometryPtr BaseMesh<T>::Clone() const
{
if (!this->Scene())
{
ignerr << "Cloning a geometry failed because the geometry to be "
<< "cloned does not belong to a scene.\n";
return nullptr;
}
else if (this->meshDescriptor.meshName.empty())
{
ignerr << "Cloning a geometry failed because the name of the mesh is "
<< "missing.\n";
return nullptr;
}

auto result = this->Scene()->CreateMesh(this->meshDescriptor.meshName);
if (result)
{
result->CopyMeshDescriptor(this->meshDescriptor);

if (this->Material())
{
// this call will set the material for the mesh and its submeshes
result->SetMaterial(this->Material());
}
else
{
// if the mesh doesn't have a material, clone any existing submesh
// materials
for (unsigned int i = 0; i < this->SubMeshCount(); ++i)
{
auto existingSubMeshMaterial = this->SubMeshByIndex(i)->Material();
if (existingSubMeshMaterial)
result->SubMeshByIndex(i)->SetMaterial(existingSubMeshMaterial);
}
}
}

return result;
}

//////////////////////////////////////////////////
template <class T>
void BaseMesh<T>::CopyMeshDescriptor(const MeshDescriptor &_copy)
{
this->meshDescriptor.meshName = _copy.meshName;
this->meshDescriptor.subMeshName = _copy.subMeshName;
this->meshDescriptor.centerSubMesh = _copy.centerSubMesh;
}

//////////////////////////////////////////////////
template <class T>
void BaseMesh<T>::Destroy()
Expand All @@ -324,6 +384,7 @@ namespace ignition
if (this->material && this->ownsMaterial)
this->Scene()->DestroyMaterial(this->material);
this->material.reset();
this->meshDescriptor = MeshDescriptor();
}

//////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion ogre/src/OgreMeshFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ OgreMeshPtr OgreMeshFactory::Create(const MeshDescriptor &_desc)
return nullptr;
}

mesh->meshName = _desc.meshName;
mesh->CopyMeshDescriptor(normDesc);

// create sub-mesh store
OgreSubMeshStoreFactory subMeshFactory(this->scene, mesh->ogreEntity);
Expand Down
2 changes: 1 addition & 1 deletion ogre2/src/Ogre2MeshFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Ogre2MeshPtr Ogre2MeshFactory::Create(const MeshDescriptor &_desc)
return nullptr;
}

mesh->meshName = _desc.meshName;
mesh->CopyMeshDescriptor(normDesc);

// create sub-mesh store
Ogre2SubMeshStoreFactory subMeshFactory(this->scene, mesh->ogreItem);
Expand Down

0 comments on commit f5b07ea

Please sign in to comment.