From 6c51ff82e5f5083a77d9f62891110b3c00b75a05 Mon Sep 17 00:00:00 2001 From: Stefano Date: Tue, 26 Sep 2023 12:39:25 +0200 Subject: [PATCH 1/2] Avoid to use GLFW to create the Viz window on Linux. Attempt to fix https://github.com/robotology/idyntree/issues/1116 --- src/visualization/src/Visualizer.cpp | 70 ++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index 123ef00220..0c35986526 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -39,6 +39,10 @@ #include #endif +#if defined(_WIN32) || defined(__APPLE__) +#define IDYNTREE_USE_GLFW_WINDOW +#endif + #endif #include "DummyImplementations.h" @@ -126,6 +130,7 @@ struct Visualizer::VisualizerPimpl #ifdef IDYNTREE_USES_IRRLICHT +#ifdef IDYNTREE_USE_GLFW_WINDOW /** * Custom window object */ @@ -137,8 +142,7 @@ struct Visualizer::VisualizerPimpl HWND m_windowId; #elif defined(__APPLE__) id m_windowId; -#elif defined(__linux__) - Window m_windowId; +#endif #endif /** @@ -238,6 +242,7 @@ struct Visualizer::VisualizerPimpl m_palette["meshcat"].vector = irr::video::SColor(255,253,98,2); } +#ifdef IDYNTREE_USE_GLFW_WINDOW void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos) { if (window != m_window) @@ -375,6 +380,7 @@ struct Visualizer::VisualizerPimpl { static_cast(glfwGetWindowUserPointer(window))->scrollCallback(window, xoffset, yoffset); } +#endif #else DummyCamera m_camera; @@ -399,7 +405,7 @@ struct Visualizer::VisualizerPimpl } }; -#ifdef IDYNTREE_USES_IRRLICHT +#ifdef IDYNTREE_USE_GLFW_WINDOW unsigned int Visualizer::VisualizerPimpl::m_glfwInstances = 0; @@ -441,6 +447,9 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) // initialize the color palette pimpl->initializePalette(); + irr::SIrrlichtCreationParameters irrDevParams; + +#ifdef IDYNTREE_USE_GLFW_WINDOW if (pimpl->m_glfwInstances == 0) { if (!glfwInit()) { @@ -461,10 +470,14 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) glfwMakeContextCurrent(pimpl->m_window); glfwSwapInterval(1); - irr::SIrrlichtCreationParameters irrDevParams; +#if defined(_WIN32) + pimpl->m_windowId = glfwGetWin32Window(pimpl->m_window); + irrDevParams.WindowId = (void*)(pimpl->m_windowId); +#elif defined(__APPLE__) + pimpl->m_windowId = glfwGetCocoaWindow(pimpl->m_window); + irrDevParams.WindowId = (void*)(pimpl->m_windowId); +#endif -// If we are on Windows, only SDL works with the external window -#if defined(_WIN32) || defined(__APPLE__) #ifndef _IRR_COMPILE_WITH_SDL_DEVICE_ #error "On Windows and MacOS it is necessary to use Irrlicht with SDL" #endif @@ -476,18 +489,6 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) irrDevParams.WithAlphaChannel = true; irrDevParams.AntiAlias = 4; -#if defined(_WIN32) - pimpl->m_windowId = glfwGetWin32Window(pimpl->m_window); - irrDevParams.WindowId = (void*)(pimpl->m_windowId); -#elif defined(__APPLE__) - pimpl->m_windowId = glfwGetCocoaWindow(pimpl->m_window); - irrDevParams.WindowId = (void*)(pimpl->m_windowId); -#elif defined(__linux__) - pimpl->m_windowId = glfwGetX11Window(pimpl->m_window); - irrDevParams.WindowId = (void*)(pimpl->m_windowId); -#endif - - if( visualizerOptions.verbose ) { reportWarning("Visualizer","init","verbose flag found, enabling verbose output in Visualizer"); @@ -555,10 +556,12 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) pimpl->m_textures.init(pimpl->m_irrDriver, pimpl->m_irrSmgr); +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwSetWindowUserPointer(pimpl->m_window, pimpl); glfwSetCursorPosCallback(pimpl->m_window, VisualizerPimpl::cursor_position_callback); glfwSetMouseButtonCallback(pimpl->m_window, VisualizerPimpl::mouse_button_callback); glfwSetScrollCallback(pimpl->m_window, VisualizerPimpl::scroll_callback); +#endif pimpl->m_isInitialized = true; pimpl->lastFPS = -1; @@ -667,7 +670,9 @@ void Visualizer::draw() return; } +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwMakeContextCurrent(pimpl->m_window); +#endif pimpl->m_irrDriver->beginScene(true,true, pimpl->m_environment.m_backgroundColor.toSColor(), pimpl->m_irrVideoData); @@ -685,9 +690,11 @@ void Visualizer::draw() pimpl->m_irrDriver->endScene(); pimpl->m_subDrawStarted = false; +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwSwapBuffers(pimpl->m_window); glfwPollEvents(); +#endif int fps = pimpl->m_irrDriver->getFPS(); @@ -701,7 +708,9 @@ void Visualizer::draw() irr::core::stringc strc(str); pimpl->m_irrDevice->setWindowCaption(str.c_str()); +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwSetWindowTitle(pimpl->m_window, strc.c_str()); +#endif pimpl->lastFPS = fps; } @@ -739,7 +748,9 @@ void Visualizer::subDraw(int xOffsetFromTopLeft, int yOffsetFromTopLeft, int sub bool clearTextureBuffers = false; if (!pimpl->m_subDrawStarted) { +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwMakeContextCurrent(pimpl->m_window); +#endif pimpl->m_irrDriver->beginScene(true,true, pimpl->m_environment.m_backgroundColor.toSColor(), pimpl->m_irrVideoData); pimpl->m_subDrawStarted = true; clearTextureBuffers = true; @@ -907,11 +918,15 @@ int Visualizer::width() const reportError("Visualizer","width","Visualizer not initialized."); return 0; } - +#ifdef IDYNTREE_USE_GLFW_WINDOW GLint ww, wh; glfwGetWindowSize(pimpl->m_window, &ww, &wh); return ww; +#else + auto winDimensions = pimpl->m_irrDriver->getScreenSize(); + return winDimensions.Width; +#endif #else return 0; #endif @@ -926,9 +941,15 @@ int Visualizer::height() const return 0; } +#ifdef IDYNTREE_USE_GLFW_WINDOW GLint ww, wh; glfwGetWindowSize(pimpl->m_window, &ww, &wh); + return wh; +#else + auto winDimensions = pimpl->m_irrDriver->getScreenSize(); + return winDimensions.Height; +#endif #else return 0; #endif @@ -942,8 +963,13 @@ bool Visualizer::run() reportError("Visualizer","run","Impossible to run not initialized visualizer"); return false; } + bool shouldClose = false; + +#ifdef IDYNTREE_USE_GLFW_WINDOW + shouldClose = glfwWindowShouldClose(pimpl->m_window); +#endif - return pimpl->m_irrDevice->run() && !glfwWindowShouldClose(pimpl->m_window); + return pimpl->m_irrDevice->run() && !shouldClose; #else reportError("Visualizer","run","Impossible to use iDynTree::Visualizer, as iDynTree has been compiled without Irrlicht."); return false; @@ -957,7 +983,9 @@ void Visualizer::close() { return; } +#ifdef IDYNTREE_USE_GLFW_WINDOW glfwMakeContextCurrent(pimpl->m_window); +#endif pimpl->m_vectors.close(); pimpl->m_frames.close(); @@ -979,6 +1007,7 @@ void Visualizer::close() pimpl->m_modelViz.resize(0); +#ifdef IDYNTREE_USE_GLFW_WINDOW if (pimpl->m_window) { glfwMakeContextCurrent(pimpl->m_window); @@ -992,6 +1021,7 @@ void Visualizer::close() pimpl->m_window = nullptr; } +#endif return; #endif From 5e57ec933f7535688d555b78a296257a70f3600f Mon Sep 17 00:00:00 2001 From: Stefano Date: Tue, 26 Sep 2023 14:07:26 +0200 Subject: [PATCH 2/2] If irrlicht is compiled with SDL, use SDL also on Linux --- src/visualization/src/Visualizer.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/visualization/src/Visualizer.cpp b/src/visualization/src/Visualizer.cpp index 0c35986526..3e9e04c40b 100644 --- a/src/visualization/src/Visualizer.cpp +++ b/src/visualization/src/Visualizer.cpp @@ -39,7 +39,10 @@ #include #endif -#if defined(_WIN32) || defined(__APPLE__) +#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_) || defined(_WIN32) || defined(__APPLE__) +#if (defined(_WIN32) || defined(__APPLE__)) && !defined(_IRR_COMPILE_WITH_SDL_DEVICE_) +#error "On Windows and MacOS it is necessary to use Irrlicht with SDL" +#endif #define IDYNTREE_USE_GLFW_WINDOW #endif @@ -142,6 +145,8 @@ struct Visualizer::VisualizerPimpl HWND m_windowId; #elif defined(__APPLE__) id m_windowId; +#elif defined(__linux__) + Window m_windowId; #endif #endif @@ -476,11 +481,11 @@ bool Visualizer::init(const VisualizerOptions &visualizerOptions) #elif defined(__APPLE__) pimpl->m_windowId = glfwGetCocoaWindow(pimpl->m_window); irrDevParams.WindowId = (void*)(pimpl->m_windowId); +#elif defined(__linux__) + pimpl->m_windowId = glfwGetX11Window(pimpl->m_window); + irrDevParams.WindowId = (void*)(pimpl->m_windowId); #endif -#ifndef _IRR_COMPILE_WITH_SDL_DEVICE_ -#error "On Windows and MacOS it is necessary to use Irrlicht with SDL" -#endif irrDevParams.DeviceType = irr::EIDT_SDL; #endif