Skip to content

Commit

Permalink
Helper function to get a scene (#320)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
chapulina authored May 15, 2021
1 parent e3d56ad commit 55a35a7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
12 changes: 12 additions & 0 deletions include/ignition/rendering/RenderingIface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "ignition/rendering/config.hh"
#include "ignition/rendering/Export.hh"
#include "ignition/rendering/RenderTypes.hh"

namespace ignition
{
Expand Down Expand Up @@ -131,6 +132,17 @@ namespace ignition
/// \param[in] _paths The list of the plugin paths
IGNITION_RENDERING_VISIBLE
void setPluginPaths(const std::list<std::string> &_paths);

/// \brief Most applications will only have one rendering engine loaded
/// at a time, and only one scene within that. This helper function gets
/// the first scene that can be found in the first loaded rendering engine.
///
/// It's not recommended to call this function when there's more than one
/// engine or scene.
///
/// \return Pointer to a scene that was found, null if no scene is loaded.
IGNITION_RENDERING_VISIBLE
ScenePtr sceneFromFirstRenderEngine();
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions ogre/src/OgreRenderEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,6 @@ std::string OgreRenderEngine::CreateRenderWindow(const std::string &_handle,
// Hide window if dimensions are less than or equal to one.
params["border"] = "none";

std::ostringstream stream;
stream << "OgreWindow(0)" << "_" << _handle;

// Needed for retina displays
params["contentScalingFactor"] = std::to_string(_ratio);

Expand All @@ -660,9 +657,12 @@ std::string OgreRenderEngine::CreateRenderWindow(const std::string &_handle,
params["currentGLContext"] = "true";
}

std::ostringstream stream;
int attempts = 0;
while (window == nullptr && (attempts++) < 10)
while (window == nullptr && attempts < 10)
{
stream.str("");
stream << "OgreWindow(" << attempts << ")" << "_" << _handle;
try
{
window = this->ogreRoot->createRenderWindow(
Expand All @@ -674,6 +674,7 @@ std::string OgreRenderEngine::CreateRenderWindow(const std::string &_handle,
<< "]. Exception [" << _e.what() << "]" << std::endl;
window = nullptr;
}
attempts++;
}

if (attempts >= 10)
Expand Down
58 changes: 58 additions & 0 deletions src/RenderingIface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
* limitations under the License.
*
*/

#include "ignition/common/Console.hh"

#include "ignition/rendering/RenderEngine.hh"
#include "ignition/rendering/RenderingIface.hh"
#include "ignition/rendering/RenderEngineManager.hh"
#include "ignition/rendering/Scene.hh"

namespace ignition
{
Expand Down Expand Up @@ -120,6 +125,59 @@ void setPluginPaths(const std::list<std::string> &_paths)
{
RenderEngineManager::Instance()->SetPluginPaths(_paths);
}

//////////////////////////////////////////////////
ScenePtr sceneFromFirstRenderEngine()
{
auto loadedEngNames = ignition::rendering::loadedEngines();
if (loadedEngNames.empty())
{
igndbg << "No rendering engine is loaded yet" << std::endl;
return nullptr;
}

auto engineName = loadedEngNames[0];
if (loadedEngNames.size() > 1)
{
ignwarn << "More than one engine is available. "
<< "Using engine [" << engineName << "]" << std::endl;
}

auto engine = ignition::rendering::engine(engineName);
if (!engine)
{
ignerr << "Internal error: failed to load engine [" << engineName
<< "]." << std::endl;
return nullptr;
}

if (engine->SceneCount() == 0)
{
igndbg << "No scene has been created yet" << std::endl;
return nullptr;
}

auto scene = engine->SceneByIndex(0);
if (nullptr == scene)
{
ignerr << "Internal error: scene is null." << std::endl;
return nullptr;
}

if (engine->SceneCount() > 1)
{
ignwarn << "More than one scene is available. "
<< "Using scene [" << scene->Name() << "]" << std::endl;
}

if (!scene->IsInitialized() || nullptr == scene->RootVisual())
{
igndbg << "Scene is not initialized yet" << std::endl;
return nullptr;
}

return scene;
}
}
}
}
19 changes: 19 additions & 0 deletions src/RenderingIface_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "test_config.h" // NOLINT(build/include)

#include <ignition/common/Console.hh>

#include "ignition/rendering/config.hh"
#include "ignition/rendering/RenderEngine.hh"
#include "ignition/rendering/RenderingIface.hh"
Expand Down Expand Up @@ -46,7 +48,10 @@ unsigned int defaultEnginesForTest()
/////////////////////////////////////////////////
TEST(RenderingIfaceTest, GetEngine)
{
common::Console::SetVerbosity(4);

unsigned int count = defaultEnginesForTest();
EXPECT_GT(count, 0u);

EXPECT_EQ(count, engineCount());

Expand All @@ -55,6 +60,7 @@ TEST(RenderingIfaceTest, GetEngine)
EXPECT_FALSE(isEngineLoaded("ogre2"));
EXPECT_FALSE(isEngineLoaded("optix"));
EXPECT_FALSE(isEngineLoaded("no_such_engine"));
EXPECT_EQ(nullptr, sceneFromFirstRenderEngine());

// check get engine
for (unsigned int i = 0; i < count; ++i)
Expand All @@ -73,6 +79,16 @@ TEST(RenderingIfaceTest, GetEngine)
++i;
#endif

#ifndef _WIN32
// Windows CI fails with
// Ogre::RenderingAPIException::RenderingAPIException: OpenGL 1.5 is not
// supported in GLRenderSystem::initialiseContext
auto scene = eng->CreateScene("scene");
EXPECT_NE(nullptr, scene);

EXPECT_EQ(scene, sceneFromFirstRenderEngine());
#endif

ASSERT_EQ(1u, loadedEngines().size());
EXPECT_EQ(eng->Name(), loadedEngines()[0]);

Expand All @@ -81,6 +97,7 @@ TEST(RenderingIfaceTest, GetEngine)
}

EXPECT_TRUE(loadedEngines().empty());
EXPECT_EQ(nullptr, sceneFromFirstRenderEngine());

// non-existent engine
EXPECT_EQ(nullptr, engine("no_such_engine"));
Expand All @@ -90,6 +107,8 @@ TEST(RenderingIfaceTest, GetEngine)
/////////////////////////////////////////////////
TEST(RenderingIfaceTest, RegisterEngine)
{
common::Console::SetVerbosity(4);

unsigned int count = defaultEnginesForTest();

if (count == 0)
Expand Down

0 comments on commit 55a35a7

Please sign in to comment.