Skip to content

Commit

Permalink
Visualizer: improve GLTF context handling (fix isl-org#4960)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchretien committed Apr 1, 2022
1 parent b85de1d commit 0821d31
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
81 changes: 53 additions & 28 deletions cpp/open3d/visualization/visualizer/Visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "open3d/visualization/visualizer/Visualizer.h"

#include <memory>

#include "open3d/geometry/TriangleMesh.h"

#if defined(__APPLE__) && defined(BUILD_GUI)
Expand All @@ -37,51 +39,63 @@ void unbind();

namespace open3d {

namespace {
namespace visualization {

class GLFWEnvironmentSingleton {
/// \brief GLFW context, handled as a singleton.
class GLFWContext {
private:
GLFWEnvironmentSingleton() { utility::LogDebug("GLFW init."); }
GLFWEnvironmentSingleton(const GLFWEnvironmentSingleton &) = delete;
GLFWEnvironmentSingleton &operator=(const GLFWEnvironmentSingleton &) =
delete;

public:
~GLFWEnvironmentSingleton() {
glfwTerminate();
utility::LogDebug("GLFW destruct.");
}

public:
static GLFWEnvironmentSingleton &GetInstance() {
static GLFWEnvironmentSingleton singleton;
return singleton;
}
GLFWContext() {
utility::LogDebug("GLFW init.");

static int InitGLFW() {
GLFWEnvironmentSingleton::GetInstance();
#if defined(__APPLE__)
// On macOS, GLFW changes the directory to the resource directory,
// which will cause an unexpected change of directory if using a
// framework build version of Python.
glfwInitHint(GLFW_COCOA_CHDIR_RESOURCES, GLFW_FALSE);
#endif
return glfwInit();
init_status_ = glfwInit();
}

GLFWContext(const GLFWContext &) = delete;
GLFWContext &operator=(const GLFWContext &) = delete;

public:
~GLFWContext() {
if (init_status_ == GLFW_TRUE) {
glfwTerminate();
utility::LogDebug("GLFW destruct.");
}
}

/// \brief Get the glfwInit status.
inline int InitStatus() const { return init_status_; }

/// \brief Get a shared instance of the GLFW context.
static std::shared_ptr<GLFWContext> GetInstance() {
static std::weak_ptr<GLFWContext> singleton;

auto res = singleton.lock();
if (res == nullptr) {
res = std::shared_ptr<GLFWContext>(new GLFWContext());
singleton = res;
}
return res;
}

static void GLFWErrorCallback(int error, const char *description) {
utility::LogWarning("GLFW Error: {}", description);
}
};

} // unnamed namespace

namespace visualization {
private:
/// \brief Status of the glfwInit call.
int init_status_ = GLFW_FALSE;
};

Visualizer::Visualizer() {}

Visualizer::~Visualizer() {
glfwTerminate(); // to be safe
DestroyVisualizerWindow();

#if defined(__APPLE__) && defined(BUILD_GUI)
bluegl::unbind();
#endif
Expand All @@ -96,6 +110,7 @@ bool Visualizer::CreateVisualizerWindow(
const bool visible /* = true*/) {
window_name_ = window_name;
if (window_) { // window already created
utility::LogDebug("[Visualizer] Reusing window.");
UpdateWindowTitle();
glfwSetWindowPos(window_, left, top);
glfwSetWindowSize(window_, width, height);
Expand All @@ -110,8 +125,10 @@ bool Visualizer::CreateVisualizerWindow(
return true;
}

glfwSetErrorCallback(GLFWEnvironmentSingleton::GLFWErrorCallback);
if (!GLFWEnvironmentSingleton::InitGLFW()) {
utility::LogDebug("[Visualizer] Creating window.");
glfwSetErrorCallback(GLFWContext::GLFWErrorCallback);
glfw_context_ = GLFWContext::GetInstance();
if (glfw_context_->InitStatus() != GLFW_TRUE) {
utility::LogWarning("Failed to initialize GLFW");
return false;
}
Expand Down Expand Up @@ -220,9 +237,17 @@ bool Visualizer::CreateVisualizerWindow(
}

void Visualizer::DestroyVisualizerWindow() {
if (!is_initialized_) {
return;
}

utility::LogDebug("[Visualizer] Destroying window.");
is_initialized_ = false;
glDeleteVertexArrays(1, &vao_id_);
vao_id_ = 0;
glfwDestroyWindow(window_);
window_ = nullptr;
glfw_context_.reset();
}

void Visualizer::RegisterAnimationCallback(
Expand Down
6 changes: 6 additions & 0 deletions cpp/open3d/visualization/visualizer/Visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Image;

namespace visualization {

class GLFWContext;

/// \class Visualizer
///
/// \brief The main Visualizer class.
Expand Down Expand Up @@ -270,6 +272,10 @@ class Visualizer {
// window
GLFWwindow *window_ = NULL;
std::string window_name_ = "Open3D";

/// \brief Shared GLFW context.
std::shared_ptr<GLFWContext> glfw_context_ = nullptr;

Eigen::Vector2i saved_window_size_ = Eigen::Vector2i::Zero();
Eigen::Vector2i saved_window_pos_ = Eigen::Vector2i::Zero();
std::function<bool(Visualizer *)> animation_callback_func_ = nullptr;
Expand Down

0 comments on commit 0821d31

Please sign in to comment.