Skip to content

Commit

Permalink
Visualize wireframes (gazebosim#314)
Browse files Browse the repository at this point in the history
* Add wireframe mode

Signed-off-by: Atharva Pusalkar <[email protected]>

* Fix visual test

Signed-off-by: Atharva Pusalkar <[email protected]>

* Change scene name

Signed-off-by: Atharva Pusalkar <[email protected]>
Signed-off-by: William Lew <[email protected]>
  • Loading branch information
atharva-18 authored and WilliamLewww committed Dec 7, 2021
1 parent 8ca2a40 commit 1c7359e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/ignition/rendering/Visual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ namespace ignition
/// material will be returned.
public: virtual MaterialPtr Material() = 0;

/// \brief Enable or disable wireframe
/// \param[in] _show True to enable wireframe
public: virtual void SetWireframe(bool _show) = 0;

/// \brief Get whether wireframe is enabled for this visual.
/// \return True if wireframe is enabled for this visual.
public: virtual bool Wireframe() const = 0;

/// \brief Specify if this visual is visible
/// \param[in] _visible True if this visual should be made visible
public: virtual void SetVisible(bool _visible) = 0;
Expand Down
25 changes: 25 additions & 0 deletions include/ignition/rendering/base/BaseVisual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ namespace ignition
// Documentation inherited.
public: virtual MaterialPtr Material() override;

// Documentation inherited.
public: virtual void SetWireframe(bool _show) override;

// Documentation inherited.
public: virtual bool Wireframe() const override;

// Documentation inherited.
public: virtual void SetVisible(bool _visible) override;

Expand Down Expand Up @@ -136,6 +142,9 @@ namespace ignition

/// \brief The bounding box of the visual
protected: ignition::math::AxisAlignedBox boundingBox;

/// \brief True if wireframe mode is enabled else false
protected: bool wireframe = false;
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -349,6 +358,22 @@ namespace ignition
}
}

//////////////////////////////////////////////////
template <class T>
bool BaseVisual<T>::Wireframe() const
{
return this->wireframe;
}

//////////////////////////////////////////////////
template <class T>
void BaseVisual<T>::SetWireframe(bool _show)
{
ignerr << "SetWireframe(" << _show << ") not supported for "
<< "render engine: " << this->Scene()->Engine()->Name()
<< std::endl;
}

//////////////////////////////////////////////////
template <class T>
void BaseVisual<T>::SetVisible(bool _visible)
Expand Down
13 changes: 13 additions & 0 deletions ogre/include/ignition/rendering/ogre/OgreVisual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef IGNITION_RENDERING_OGRE_OGREVISUAL_HH_
#define IGNITION_RENDERING_OGRE_OGREVISUAL_HH_

#include <memory>
#include <vector>

#include "ignition/rendering/base/BaseVisual.hh"
Expand All @@ -29,13 +30,22 @@ namespace ignition
{
inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
//
// forward declaration
class OgreVisualPrivate;

class IGNITION_RENDERING_OGRE_VISIBLE OgreVisual :
public BaseVisual<OgreNode>
{
protected: OgreVisual();

public: virtual ~OgreVisual();

// Documentation inherited.
public: virtual void SetWireframe(bool _show) override;

// Documentation inherited.
public: virtual bool Wireframe() const override;

// Documentation inherited.
public: virtual void SetVisible(bool _visible) override;

Expand Down Expand Up @@ -83,6 +93,9 @@ namespace ignition

private: OgreVisualPtr SharedThis();

/// \brief Pointer to private data class
private: std::unique_ptr<OgreVisualPrivate> dataPtr;

private: friend class OgreScene;
};
}
Expand Down
63 changes: 63 additions & 0 deletions ogre/src/OgreVisual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,79 @@
using namespace ignition;
using namespace rendering;

/// \brief Private data for the Ogre2Visual class
class ignition::rendering::OgreVisualPrivate
{
/// \brief True if wireframe mode is enabled
public: bool wireframe;
};

//////////////////////////////////////////////////
OgreVisual::OgreVisual()
: dataPtr(new OgreVisualPrivate)
{
this->dataPtr->wireframe = false;
}

//////////////////////////////////////////////////
OgreVisual::~OgreVisual()
{
}

//////////////////////////////////////////////////
void OgreVisual::SetWireframe(bool _show)
{
if (this->dataPtr->wireframe == _show)
return;

this->dataPtr->wireframe = _show;
for (unsigned int i = 0; i < this->ogreNode->numAttachedObjects();
i++)
{
Ogre::Entity *entity = nullptr;
Ogre::MovableObject *obj = this->ogreNode->getAttachedObject(i);

entity = dynamic_cast<Ogre::Entity *>(obj);

if (!entity)
continue;

for (unsigned int j = 0; j < entity->getNumSubEntities(); j++)
{
Ogre::SubEntity *subEntity = entity->getSubEntity(j);
Ogre::MaterialPtr entityMaterial = subEntity->getMaterial();
if (entityMaterial.isNull())
continue;

unsigned int techniqueCount, passCount;
Ogre::Technique *technique;
Ogre::Pass *pass;

for (techniqueCount = 0;
techniqueCount < entityMaterial->getNumTechniques();
++techniqueCount)
{
technique = entityMaterial->getTechnique(techniqueCount);

for (passCount = 0; passCount < technique->getNumPasses(); passCount++)
{
pass = technique->getPass(passCount);
if (_show)
pass->setPolygonMode(Ogre::PM_WIREFRAME);
else
pass->setPolygonMode(Ogre::PM_SOLID);
}
}
}
}
}

//////////////////////////////////////////////////
bool OgreVisual::Wireframe() const
{
return this->dataPtr->wireframe;
}

//////////////////////////////////////////////////
void OgreVisual::SetVisible(bool _visible)
{
Expand Down
6 changes: 6 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2Visual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ namespace ignition
/// \brief Destructor
public: virtual ~Ogre2Visual();

// Documentation inherited
public: virtual void SetWireframe(bool _show) override;

// Documentation inherited
public: virtual bool Wireframe() const override;

// Documentation inherited.
public: virtual void SetVisible(bool _visible) override;

Expand Down
51 changes: 51 additions & 0 deletions ogre2/src/Ogre2Visual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,76 @@
#include "ignition/rendering/ogre2/Ogre2WireBox.hh"
#include "ignition/rendering/Utils.hh"

#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <OgreItem.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

using namespace ignition;
using namespace rendering;

/// \brief Private data for the Ogre2Visual class
class ignition::rendering::Ogre2VisualPrivate
{
/// \brief True if wireframe mode is enabled
public: bool wireframe;
};

//////////////////////////////////////////////////
Ogre2Visual::Ogre2Visual()
: dataPtr(new Ogre2VisualPrivate)
{
this->dataPtr->wireframe = false;
}

//////////////////////////////////////////////////
Ogre2Visual::~Ogre2Visual()
{
}

//////////////////////////////////////////////////
void Ogre2Visual::SetWireframe(bool _show)
{
if (this->dataPtr->wireframe == _show)
return;

this->dataPtr->wireframe = _show;
for (unsigned int i = 0; i < this->ogreNode->numAttachedObjects();
i++)
{
Ogre::Item *item = nullptr;
Ogre::MovableObject *obj = this->ogreNode->getAttachedObject(i);

item = dynamic_cast<Ogre::Item *>(obj);

if (!item)
continue;

for (unsigned int j = 0; j < item->getNumSubItems(); j++)
{
Ogre::SubItem *subItem = item->getSubItem(j);
auto datablock = subItem->getDatablock();
auto macroblock = *(datablock->getMacroblock());

if (_show)
macroblock.mPolygonMode = Ogre::PM_WIREFRAME;
else
macroblock.mPolygonMode = Ogre::PM_SOLID;

datablock->setMacroblock(macroblock);
}
}
}

//////////////////////////////////////////////////
bool Ogre2Visual::Wireframe() const
{
return this->dataPtr->wireframe;
}

//////////////////////////////////////////////////
void Ogre2Visual::SetVisible(bool _visible)
{
Expand Down
32 changes: 32 additions & 0 deletions src/Visual_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class VisualTest : public testing::Test,

/// \brief Test getting setting bounding boxes
public: void BoundingBox(const std::string &_renderEngine);

/// \brief Test changing to wireframe
public: void Wireframe(const std::string &_renderEngine);
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -548,6 +551,35 @@ TEST_P(VisualTest, BoundingBox)
BoundingBox(GetParam());
}

/////////////////////////////////////////////////
void VisualTest::Wireframe(const std::string &_renderEngine)
{
RenderEngine *engine = rendering::engine(_renderEngine);
if (!engine)
{
igndbg << "Engine '" << _renderEngine
<< "' is not supported" << std::endl;
return;
}

ScenePtr scene = engine->CreateScene("scene7");

// create visual
VisualPtr visual = scene->CreateVisual();
ASSERT_NE(nullptr, visual);
EXPECT_EQ(false, visual->Wireframe());

// set wireframe
visual->SetWireframe(true);
EXPECT_EQ(true, visual->Wireframe());
}

/////////////////////////////////////////////////
TEST_P(VisualTest, Wireframe)
{
Wireframe(GetParam());
}


INSTANTIATE_TEST_CASE_P(Visual, VisualTest,
RENDER_ENGINE_VALUES,
Expand Down

0 comments on commit 1c7359e

Please sign in to comment.