diff --git a/examples/gazebo_scene_viewer/CMakeLists.txt b/examples/gazebo_scene_viewer/CMakeLists.txt index 9ff4b937a..1dbbded56 100644 --- a/examples/gazebo_scene_viewer/CMakeLists.txt +++ b/examples/gazebo_scene_viewer/CMakeLists.txt @@ -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") @@ -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 @@ -48,5 +49,5 @@ target_link_libraries(gazebo_scene_viewer2_demo ${GLEW_LIBRARIES} ${GAZEBO_LIBRARIES} ${IGNITION-RENDERING_LIBRARIES} - stdc++fs + ${STD_CXX_FS_LIBRARIES} ) diff --git a/examples/gazebo_scene_viewer/CameraWindow.cc b/examples/gazebo_scene_viewer/CameraWindow.cc index 0a662990b..2164a753b 100644 --- a/examples/gazebo_scene_viewer/CameraWindow.cc +++ b/examples/gazebo_scene_viewer/CameraWindow.cc @@ -18,6 +18,7 @@ #if __APPLE__ #include + #include #include #else #include @@ -30,6 +31,7 @@ #endif #include +#include #include #include @@ -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; @@ -66,7 +72,10 @@ double g_offset = 0.0; ////////////////////////////////////////////////// void GlutRun(std::vector _cameras) { -#if not (__APPLE__ || _WIN32) +#if __APPLE__ + g_context = CGLGetCurrentContext(); +#elif _WIN32 +#else g_context = glXGetCurrentContext(); g_display = glXGetCurrentDisplay(); g_drawable = glXGetCurrentDrawable(); @@ -77,7 +86,10 @@ void GlutRun(std::vector _cameras) GlutInitContext(); GlutPrintUsage(); -#if not (__APPLE__ || _WIN32) +#if __APPLE__ + g_glutContext = CGLGetCurrentContext(); +#elif _WIN32 +#else g_glutDisplay = glXGetCurrentDisplay(); g_glutDrawable = glXGetCurrentDrawable(); g_glutContext = glXGetCurrentContext(); @@ -89,7 +101,10 @@ void GlutRun(std::vector _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); @@ -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 @@ -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); @@ -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 @@ -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) @@ -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); diff --git a/examples/gazebo_scene_viewer/GazeboDemo.cc b/examples/gazebo_scene_viewer/GazeboDemo.cc index 1ddd0bfce..752628425 100644 --- a/examples/gazebo_scene_viewer/GazeboDemo.cc +++ b/examples/gazebo_scene_viewer/GazeboDemo.cc @@ -14,6 +14,16 @@ * limitations under the License. * */ + +#if defined(__APPLE__) + #include + #include +#elif not defined(_WIN32) + #include + #include + #include +#endif + #include #include #include @@ -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 cameras; std::vector engineNames; - engineNames.push_back("ogre"); + engineNames.push_back(ogreEngine); engineNames.push_back("optix"); for (auto engineName : engineNames) diff --git a/examples/gazebo_scene_viewer/GazeboWorldDemo.cc b/examples/gazebo_scene_viewer/GazeboWorldDemo.cc index d792062d1..e498e2358 100644 --- a/examples/gazebo_scene_viewer/GazeboWorldDemo.cc +++ b/examples/gazebo_scene_viewer/GazeboWorldDemo.cc @@ -14,6 +14,16 @@ * limitations under the License. * */ + +#if defined(__APPLE__) + #include + #include +#elif not defined(_WIN32) + #include + #include + #include +#endif + #include #include #include @@ -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 cameras; std::vector engineNames; - engineNames.push_back("ogre"); + engineNames.push_back(ogreEngine); engineNames.push_back("optix"); for (auto engineName : engineNames) diff --git a/examples/gazebo_scene_viewer/SceneManager.cc b/examples/gazebo_scene_viewer/SceneManager.cc index 9847e1050..088cea420 100644 --- a/examples/gazebo_scene_viewer/SceneManager.cc +++ b/examples/gazebo_scene_viewer/SceneManager.cc @@ -40,6 +40,7 @@ SceneManager::SceneManager() : ////////////////////////////////////////////////// SceneManager::~SceneManager() { + this->Fini(); delete this->pimpl; } @@ -157,8 +158,8 @@ SceneManagerPrivate::SceneManagerPrivate() : ////////////////////////////////////////////////// SceneManagerPrivate::~SceneManagerPrivate() { - delete currentSceneManager; - delete newSceneManager; + newSceneManager.reset(); + currentSceneManager.reset(); } ////////////////////////////////////////////////// @@ -224,7 +225,11 @@ void SceneManagerPrivate::Init() ////////////////////////////////////////////////// void SceneManagerPrivate::Fini() { - // TODO(anyone): disconnect + // block all message receival during cleanup + std::lock_guard generalLock(this->generalMutex); + std::lock_guard poseLock(this->poseMutex); + + this->transportNode->Fini(); } ////////////////////////////////////////////////// @@ -456,6 +461,11 @@ void SceneManagerPrivate::OnLightUpdate(::ConstLightPtr &_lightMsg) // wait for update unlock before adding message std::lock_guard lock(this->generalMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnLightUpdate(_lightMsg); this->newSceneManager->OnLightUpdate(_lightMsg); } @@ -466,6 +476,11 @@ void SceneManagerPrivate::OnModelUpdate(::ConstModelPtr &_modelMsg) // wait for update unlock before adding message std::lock_guard lock(this->generalMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnModelUpdate(_modelMsg); this->newSceneManager->OnModelUpdate(_modelMsg); } @@ -476,6 +491,11 @@ void SceneManagerPrivate::OnJointUpdate(::ConstJointPtr &_jointMsg) // wait for update unlock before adding message std::lock_guard lock(this->generalMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnJointUpdate(_jointMsg); this->newSceneManager->OnJointUpdate(_jointMsg); } @@ -486,6 +506,11 @@ void SceneManagerPrivate::OnVisualUpdate(::ConstVisualPtr &_visualMsg) // wait for update unlock before adding message std::lock_guard lock(this->generalMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnVisualUpdate(_visualMsg); this->newSceneManager->OnVisualUpdate(_visualMsg); } @@ -496,6 +521,11 @@ void SceneManagerPrivate::OnSensorUpdate(::ConstSensorPtr &_sensorMsg) // wait for update unlock before adding message std::lock_guard lock(this->generalMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnSensorUpdate(_sensorMsg); this->newSceneManager->OnSensorUpdate(_sensorMsg); } @@ -506,6 +536,11 @@ void SceneManagerPrivate::OnPoseUpdate(::ConstPosesStampedPtr &_posesMsg) // wait for update unlock before adding message std::lock_guard lock(this->poseMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnPoseUpdate(_posesMsg); this->newSceneManager->OnPoseUpdate(_posesMsg); } @@ -516,6 +551,11 @@ void SceneManagerPrivate::OnRemovalUpdate(const std::string &_name) // wait for update unlock before adding message std::lock_guard lock(this->poseMutex); + if (this->currentSceneManager == nullptr || this->newSceneManager == nullptr) + { + return; + } + this->currentSceneManager->OnRemovalUpdate(_name); this->newSceneManager->OnRemovalUpdate(_name); } diff --git a/examples/gazebo_scene_viewer/SceneManagerPrivate.hh b/examples/gazebo_scene_viewer/SceneManagerPrivate.hh index 5b1dbc3d8..f1e85c217 100644 --- a/examples/gazebo_scene_viewer/SceneManagerPrivate.hh +++ b/examples/gazebo_scene_viewer/SceneManagerPrivate.hh @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -110,9 +111,9 @@ namespace ignition private: void DemoteCurrentScenes(); - private: CurrentSceneManager *currentSceneManager; + private: std::unique_ptr currentSceneManager; - private: NewSceneManager *newSceneManager; + private: std::unique_ptr newSceneManager; private: gazebo::transport::NodePtr transportNode;