Skip to content

Commit

Permalink
Avoid to use GLFW to create the Viz window on Linux.
Browse files Browse the repository at this point in the history
Attempt to fix robotology#1116
  • Loading branch information
S-Dafarra authored and flferretti committed Dec 23, 2024
1 parent 5ef7283 commit 5ba0a7a
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions src/visualization/src/Visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <windows.h>
#endif

#if defined(_WIN32) || defined(__APPLE__)
#define IDYNTREE_USE_GLFW_WINDOW
#endif

#endif

#include "DummyImplementations.h"
Expand Down Expand Up @@ -126,6 +130,7 @@ struct Visualizer::VisualizerPimpl

#ifdef IDYNTREE_USES_IRRLICHT

#ifdef IDYNTREE_USE_GLFW_WINDOW
/**
* Custom window object
*/
Expand All @@ -137,8 +142,7 @@ struct Visualizer::VisualizerPimpl
HWND m_windowId;
#elif defined(__APPLE__)
id m_windowId;
#elif defined(__linux__)
Window m_windowId;
#endif
#endif

/**
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -375,6 +380,7 @@ struct Visualizer::VisualizerPimpl
{
static_cast<Visualizer::VisualizerPimpl*>(glfwGetWindowUserPointer(window))->scrollCallback(window, xoffset, yoffset);
}
#endif

#else
DummyCamera m_camera;
Expand All @@ -399,7 +405,7 @@ struct Visualizer::VisualizerPimpl
}
};

#ifdef IDYNTREE_USES_IRRLICHT
#ifdef IDYNTREE_USE_GLFW_WINDOW

unsigned int Visualizer::VisualizerPimpl::m_glfwInstances = 0;

Expand Down Expand Up @@ -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()) {
Expand All @@ -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
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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();

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -992,6 +1021,7 @@ void Visualizer::close()

pimpl->m_window = nullptr;
}
#endif

return;
#endif
Expand Down

0 comments on commit 5ba0a7a

Please sign in to comment.