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

Fix gazebo_scene_viewer for macOS and ensure exits cleanly #259

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/gazebo_scene_viewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (NOT APPLE)
find_package(GLEW REQUIRED)
include_directories(SYSTEM ${GLEW_INCLUDE_DIRS})
link_directories(${GLEW_LIBRARY_DIRS})
set(STD_CXX_FS_LIBRARIES "stdc++fs")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
Expand All @@ -33,7 +34,7 @@ target_link_libraries(gazebo_scene_viewer
${GLEW_LIBRARIES}
${GAZEBO_LIBRARIES}
${IGNITION-RENDERING_LIBRARIES}
stdc++fs
${STD_CXX_FS_LIBRARIES}
)

add_executable(gazebo_scene_viewer2_demo
Expand All @@ -48,5 +49,5 @@ target_link_libraries(gazebo_scene_viewer2_demo
${GLEW_LIBRARIES}
${GAZEBO_LIBRARIES}
${IGNITION-RENDERING_LIBRARIES}
stdc++fs
${STD_CXX_FS_LIBRARIES}
)
44 changes: 34 additions & 10 deletions examples/gazebo_scene_viewer/CameraWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#if __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
Expand All @@ -30,6 +31,7 @@
#endif

#include <gazebo/common/Console.hh>
#include <gazebo/transport/TransportIface.hh>

#include <ignition/rendering/Camera.hh>
#include <ignition/rendering/Image.hh>
Expand All @@ -52,7 +54,11 @@ gz::ImagePtr g_image;

bool g_initContext = false;

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLContextObj g_context;
CGLContextObj g_glutContext;
#elif _WIN32
#else
GLXContext g_context;
Display *g_display;
GLXDrawable g_drawable;
Expand All @@ -66,7 +72,10 @@ double g_offset = 0.0;
//////////////////////////////////////////////////
void GlutRun(std::vector<gz::CameraPtr> _cameras)
{
#if not (__APPLE__ || _WIN32)
#if __APPLE__
g_context = CGLGetCurrentContext();
#elif _WIN32
#else
g_context = glXGetCurrentContext();
g_display = glXGetCurrentDisplay();
g_drawable = glXGetCurrentDrawable();
Expand All @@ -77,7 +86,10 @@ void GlutRun(std::vector<gz::CameraPtr> _cameras)
GlutInitContext();
GlutPrintUsage();

#if not (__APPLE__ || _WIN32)
#if __APPLE__
g_glutContext = CGLGetCurrentContext();
#elif _WIN32
#else
g_glutDisplay = glXGetCurrentDisplay();
g_glutDrawable = glXGetCurrentDrawable();
g_glutContext = glXGetCurrentContext();
Expand All @@ -89,7 +101,10 @@ void GlutRun(std::vector<gz::CameraPtr> _cameras)
//////////////////////////////////////////////////
void GlutDisplay()
{
#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_context);
#elif _WIN32
#else
if (g_display)
{
glXMakeCurrent(g_display, g_drawable, g_context);
Expand All @@ -98,7 +113,10 @@ void GlutDisplay()

g_cameras[g_cameraIndex]->Capture(*g_image);

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_glutContext);
#elif _WIN32
#else
glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext);
#endif

Expand All @@ -116,7 +134,10 @@ void GlutDisplay()
//////////////////////////////////////////////////
void GlutIdle()
{
#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_context);
#elif _WIN32
#else
if (g_display)
{
glXMakeCurrent(g_display, g_drawable, g_context);
Expand All @@ -128,7 +149,10 @@ void GlutIdle()

manager->UpdateScenes();

#if not (__APPLE__ || _WIN32)
#if __APPLE__
CGLSetCurrentContext(g_glutContext);
#elif _WIN32
#else
glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext);
#endif

Expand All @@ -140,6 +164,9 @@ void GlutKeyboard(unsigned char _key, int, int)
{
if (_key == KEY_ESC || _key == 'q' || _key == 'Q')
{
// stop transport
gazebo::transport::stop();
gazebo::transport::fini();
exit(0);
}
else if (_key == KEY_TAB)
Expand Down Expand Up @@ -167,9 +194,6 @@ void GlutInitCamera(gz::CameraPtr _camera)
//////////////////////////////////////////////////
void GlutInitContext()
{
int argc = 0;
char **argv = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(imgw, imgh);
Expand Down
24 changes: 22 additions & 2 deletions examples/gazebo_scene_viewer/GazeboDemo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
* limitations under the License.
*
*/

#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#elif not defined(_WIN32)
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif

#include <iostream>
#include <gazebo/common/Console.hh>
#include <gazebo/transport/TransportIface.hh>
Expand Down Expand Up @@ -76,13 +86,23 @@ CameraPtr CreateCamera(const std::string &_engine)
return camera;
}

int main(int, char**)
int main(int _argc, char** _argv)
{
glutInit(&_argc, _argv);

// Expose engine name to command line because we can't instantiate both
// ogre and ogre2 at the same time
std::string ogreEngine("ogre");
if (_argc > 1)
{
ogreEngine = _argv[1];
}

Connect();
std::vector<CameraPtr> cameras;
std::vector<std::string> engineNames;

engineNames.push_back("ogre");
engineNames.push_back(ogreEngine);
engineNames.push_back("optix");

for (auto engineName : engineNames)
Expand Down
24 changes: 22 additions & 2 deletions examples/gazebo_scene_viewer/GazeboWorldDemo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
* limitations under the License.
*
*/

#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#elif not defined(_WIN32)
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif

#include <iostream>
#include <ignition/common/Console.hh>
#include <gazebo/transport/TransportIface.hh>
Expand Down Expand Up @@ -76,13 +86,23 @@ CameraPtr CreateCamera(const std::string &_engine)
return camera;
}

int main(int, char**)
int main(int _argc, char** _argv)
{
glutInit(&_argc, _argv);

// Expose engine name to command line because we can't instantiate both
// ogre and ogre2 at the same time
std::string ogreEngine("ogre");
if (_argc > 1)
{
ogreEngine = _argv[1];
}

Connect();
std::vector<CameraPtr> cameras;
std::vector<std::string> engineNames;

engineNames.push_back("ogre");
engineNames.push_back(ogreEngine);
engineNames.push_back("optix");

for (auto engineName : engineNames)
Expand Down
46 changes: 43 additions & 3 deletions examples/gazebo_scene_viewer/SceneManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SceneManager::SceneManager() :
//////////////////////////////////////////////////
SceneManager::~SceneManager()
{
this->Fini();
delete this->pimpl;
}

Expand Down Expand Up @@ -157,8 +158,8 @@ SceneManagerPrivate::SceneManagerPrivate() :
//////////////////////////////////////////////////
SceneManagerPrivate::~SceneManagerPrivate()
{
delete currentSceneManager;
delete newSceneManager;
newSceneManager.reset();
currentSceneManager.reset();
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -224,7 +225,11 @@ void SceneManagerPrivate::Init()
//////////////////////////////////////////////////
void SceneManagerPrivate::Fini()
{
// TODO(anyone): disconnect
// block all message receival during cleanup
std::lock_guard<std::mutex> generalLock(this->generalMutex);
std::lock_guard<std::mutex> poseLock(this->poseMutex);

this->transportNode->Fini();
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -456,6 +461,11 @@ void SceneManagerPrivate::OnLightUpdate(::ConstLightPtr &_lightMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->generalMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnLightUpdate(_lightMsg);
this->newSceneManager->OnLightUpdate(_lightMsg);
}
Expand All @@ -466,6 +476,11 @@ void SceneManagerPrivate::OnModelUpdate(::ConstModelPtr &_modelMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->generalMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnModelUpdate(_modelMsg);
this->newSceneManager->OnModelUpdate(_modelMsg);
}
Expand All @@ -476,6 +491,11 @@ void SceneManagerPrivate::OnJointUpdate(::ConstJointPtr &_jointMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->generalMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnJointUpdate(_jointMsg);
this->newSceneManager->OnJointUpdate(_jointMsg);
}
Expand All @@ -486,6 +506,11 @@ void SceneManagerPrivate::OnVisualUpdate(::ConstVisualPtr &_visualMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->generalMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnVisualUpdate(_visualMsg);
this->newSceneManager->OnVisualUpdate(_visualMsg);
}
Expand All @@ -496,6 +521,11 @@ void SceneManagerPrivate::OnSensorUpdate(::ConstSensorPtr &_sensorMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->generalMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnSensorUpdate(_sensorMsg);
this->newSceneManager->OnSensorUpdate(_sensorMsg);
}
Expand All @@ -506,6 +536,11 @@ void SceneManagerPrivate::OnPoseUpdate(::ConstPosesStampedPtr &_posesMsg)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->poseMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnPoseUpdate(_posesMsg);
this->newSceneManager->OnPoseUpdate(_posesMsg);
}
Expand All @@ -516,6 +551,11 @@ void SceneManagerPrivate::OnRemovalUpdate(const std::string &_name)
// wait for update unlock before adding message
std::lock_guard<std::mutex> lock(this->poseMutex);

if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr)
{
return;
}

this->currentSceneManager->OnRemovalUpdate(_name);
this->newSceneManager->OnRemovalUpdate(_name);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/gazebo_scene_viewer/SceneManagerPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <map>
#include <string>
#include <vector>
#include <memory>

#include <ignition/common/Time.hh>

Expand Down Expand Up @@ -110,9 +111,9 @@ namespace ignition

private: void DemoteCurrentScenes();

private: CurrentSceneManager *currentSceneManager;
private: std::unique_ptr<CurrentSceneManager> currentSceneManager;

private: NewSceneManager *newSceneManager;
private: std::unique_ptr<NewSceneManager> newSceneManager;

private: gazebo::transport::NodePtr transportNode;

Expand Down