Ver código fonte

Update ImGui bindings.

Warnings: potentially breaking changes on the ImGui, may require user to
adapt their UI code.

Former-commit-id: ce723d8c0047378e0a49cd2850ec6f04db977a4f
Jérémie Dumas 6 anos atrás
pai
commit
209b81e56c

+ 1 - 1
include/igl/opengl/ViewerCore.cpp

@@ -112,7 +112,7 @@ IGL_INLINE void igl::opengl::ViewerCore::draw(
   /* Bind and potentially refresh mesh/line/point data */
   if (data.dirty)
   {
-    data.updateGL(data, data.invert_normals,data.meshgl);
+    data.updateGL(data, data.invert_normals, data.meshgl);
     data.dirty = MeshGL::DIRTY_NONE;
   }
   data.meshgl.bind_mesh();

+ 49 - 17
include/igl/opengl/glfw/imgui/ImGuiHelpers.h

@@ -9,11 +9,13 @@
 #define IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
 
 ////////////////////////////////////////////////////////////////////////////////
+#include "ImGuiTraits.h"
 #include <imgui/imgui.h>
 #include <vector>
 #include <string>
 #include <algorithm>
 #include <functional>
+#include <cstddef>
 ////////////////////////////////////////////////////////////////////////////////
 
 // Extend ImGui by populating its namespace directly
@@ -25,35 +27,35 @@ namespace ImGui
 
 static auto vector_getter = [](void* vec, int idx, const char** out_text)
 {
-	auto& vector = *static_cast<std::vector<std::string>*>(vec);
-	if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
-	*out_text = vector.at(idx).c_str();
-	return true;
+  auto& vector = *static_cast<std::vector<std::string>*>(vec);
+  if (idx < 0 || idx >= static_cast<int>(vector.size())) { return false; }
+  *out_text = vector.at(idx).c_str();
+  return true;
 };
 
 inline bool Combo(const char* label, int* idx, std::vector<std::string>& values)
 {
-	if (values.empty()) { return false; }
-	return Combo(label, idx, vector_getter,
-		static_cast<void*>(&values), values.size());
+  if (values.empty()) { return false; }
+  return Combo(label, idx, vector_getter,
+    static_cast<void*>(&values), values.size());
 }
 
 inline bool Combo(const char* label, int* idx, std::function<const char *(int)> getter, int items_count)
 {
-	auto func = [](void* data, int i, const char** out_text) {
-		auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
-		const char *s = getter(i);
-		if (s) { *out_text = s; return true; }
-		else { return false; }
-	};
-	return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
+  auto func = [](void* data, int i, const char** out_text) {
+    auto &getter = *reinterpret_cast<std::function<const char *(int)> *>(data);
+    const char *s = getter(i);
+    if (s) { *out_text = s; return true; }
+    else { return false; }
+  };
+  return Combo(label, idx, func, reinterpret_cast<void *>(&getter), items_count);
 }
 
 inline bool ListBox(const char* label, int* idx, std::vector<std::string>& values)
 {
-	if (values.empty()) { return false; }
-	return ListBox(label, idx, vector_getter,
-		static_cast<void*>(&values), values.size());
+  if (values.empty()) { return false; }
+  return ListBox(label, idx, vector_getter,
+    static_cast<void*>(&values), values.size());
 }
 
 inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL)
@@ -69,6 +71,36 @@ inline bool InputText(const char* label, std::string &str, ImGuiInputTextFlags f
   return false;
 }
 
+// template<typename T>
+// inline bool DragScalar(const char *label, T* value, void* v, float v_speed, const void* v_min = NULL, const void* v_max = NULL, const char* format = NULL, float power = 1.0f)
+// {
+//   const char *fmt = format;
+//   if (format == nullptr) {
+//     fmt = ImGuiDataTypeTraits<T>::format;
+//   }
+//   return DragScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
+// }
+
+// template<typename T>
+// inline bool InputScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = nulltptr)
+// {
+//   const char *fmt = format;
+//   if (format == nullptr) {
+//     fmt = ImGuiDataTypeTraits<T>::format;
+//   }
+//   return InputScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
+// }
+
+template<typename T>
+inline bool SliderScalar(const char *label, T* value, T min = 0, T max = 0, const char* format = "")
+{
+  const char *fmt = format;
+  if (format == nullptr) {
+    fmt = ImGuiDataTypeTraits<T>::format;
+  }
+  return SliderScalar(label, ImGuiDataTypeTraits<T>::value, value, &min, &max, fmt);
+}
+
 } // namespace ImGui
 
 #endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H

+ 24 - 14
include/igl/opengl/glfw/imgui/ImGuiMenu.cpp

@@ -9,7 +9,8 @@
 #include "ImGuiMenu.h"
 #include <igl/project.h>
 #include <imgui/imgui.h>
-#include <imgui_impl_glfw_gl3.h>
+#include <imgui_impl_glfw.h>
+#include <imgui_impl_opengl3.h>
 #include <imgui_fonts_droid_sans.h>
 #include <GLFW/glfw3.h>
 #include <iostream>
@@ -30,11 +31,16 @@ IGL_INLINE void ImGuiMenu::init(igl::opengl::glfw::Viewer *_viewer)
   // Setup ImGui binding
   if (_viewer)
   {
-    if (context_ == nullptr)
+    IMGUI_CHECKVERSION();
+    if (!context_)
     {
-      context_ = ImGui::CreateContext();
+      // Single global context by default, but can be overridden by the user
+      static ImGuiContext * __global_context = ImGui::CreateContext();
+      context_ = __global_context;
     }
-    ImGui_ImplGlfwGL3_Init(viewer->window, false);
+    const char* glsl_version = "#version 150";
+    ImGui_ImplGlfw_InitForOpenGL(viewer->window, false);
+    ImGui_ImplOpenGL3_Init(glsl_version);
     ImGui::GetIO().IniFilename = nullptr;
     ImGui::StyleColorsDark();
     ImGuiStyle& style = ImGui::GetStyle();
@@ -57,9 +63,10 @@ IGL_INLINE void ImGuiMenu::reload_font(int font_size)
 IGL_INLINE void ImGuiMenu::shutdown()
 {
   // Cleanup
-  ImGui_ImplGlfwGL3_Shutdown();
-  ImGui::DestroyContext(context_);
-  context_ = nullptr;
+  ImGui_ImplOpenGL3_Shutdown();
+  ImGui_ImplGlfw_Shutdown();
+  // User is responsible for destroying context if a custom context is given
+  // ImGui::DestroyContext(*context_);
 }
 
 IGL_INLINE bool ImGuiMenu::pre_draw()
@@ -71,10 +78,12 @@ IGL_INLINE bool ImGuiMenu::pre_draw()
   if (std::abs(scaling - hidpi_scaling_) > 1e-5)
   {
     reload_font();
-    ImGui_ImplGlfwGL3_InvalidateDeviceObjects();
+    ImGui_ImplOpenGL3_DestroyDeviceObjects();
   }
 
-  ImGui_ImplGlfwGL3_NewFrame();
+  ImGui_ImplOpenGL3_NewFrame();
+  ImGui_ImplGlfw_NewFrame();
+  ImGui::NewFrame();
   return false;
 }
 
@@ -82,6 +91,7 @@ IGL_INLINE bool ImGuiMenu::post_draw()
 {
   draw_menu();
   ImGui::Render();
+  ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
   return false;
 }
 
@@ -97,7 +107,7 @@ IGL_INLINE void ImGuiMenu::post_resize(int width, int height)
 // Mouse IO
 IGL_INLINE bool ImGuiMenu::mouse_down(int button, int modifier)
 {
-  ImGui_ImplGlfwGL3_MouseButtonCallback(viewer->window, button, GLFW_PRESS, modifier);
+  ImGui_ImplGlfw_MouseButtonCallback(viewer->window, button, GLFW_PRESS, modifier);
   return ImGui::GetIO().WantCaptureMouse;
 }
 
@@ -113,26 +123,26 @@ IGL_INLINE bool ImGuiMenu::mouse_move(int mouse_x, int mouse_y)
 
 IGL_INLINE bool ImGuiMenu::mouse_scroll(float delta_y)
 {
-  ImGui_ImplGlfwGL3_ScrollCallback(viewer->window, 0.f, delta_y);
+  ImGui_ImplGlfw_ScrollCallback(viewer->window, 0.f, delta_y);
   return ImGui::GetIO().WantCaptureMouse;
 }
 
 // Keyboard IO
 IGL_INLINE bool ImGuiMenu::key_pressed(unsigned int key, int modifiers)
 {
-  ImGui_ImplGlfwGL3_CharCallback(nullptr, key);
+  ImGui_ImplGlfw_CharCallback(nullptr, key);
   return ImGui::GetIO().WantCaptureKeyboard;
 }
 
 IGL_INLINE bool ImGuiMenu::key_down(int key, int modifiers)
 {
-  ImGui_ImplGlfwGL3_KeyCallback(viewer->window, key, 0, GLFW_PRESS, modifiers);
+  ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_PRESS, modifiers);
   return ImGui::GetIO().WantCaptureKeyboard;
 }
 
 IGL_INLINE bool ImGuiMenu::key_up(int key, int modifiers)
 {
-  ImGui_ImplGlfwGL3_KeyCallback(viewer->window, key, 0, GLFW_RELEASE, modifiers);
+  ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_RELEASE, modifiers);
   return ImGui::GetIO().WantCaptureKeyboard;
 }
 

+ 1 - 0
include/igl/opengl/glfw/imgui/ImGuiMenu.h

@@ -12,6 +12,7 @@
 #include <igl/opengl/glfw/Viewer.h>
 #include <igl/opengl/glfw/ViewerPlugin.h>
 #include <igl/igl_inline.h>
+#include <memory>
 ////////////////////////////////////////////////////////////////////////////////
 
 // Forward declarations

+ 69 - 0
include/igl/opengl/glfw/imgui/ImGuiTraits.h

@@ -0,0 +1,69 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+//
+// Copyright (C) 2018 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_OPENGL_GLFW_IMGUI_IMGUITRAITS_H
+#define IGL_OPENGL_GLFW_IMGUI_IMGUITRAITS_H
+
+#include <imgui/imgui.h>
+
+// Extend ImGui by populating its namespace directly
+namespace ImGui
+{
+
+// Infer ImGuiDataType enum based on actual type
+template<typename T>
+class ImGuiDataTypeTraits
+{
+	static const ImGuiDataType value; // link error
+	static const char * format;
+};
+
+template<>
+class ImGuiDataTypeTraits<int>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_S32;
+	static constexpr char * format = "%d";
+};
+
+template<>
+class ImGuiDataTypeTraits<unsigned int>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_U32;
+	static constexpr char * format = "%u";
+};
+
+template<>
+class ImGuiDataTypeTraits<long long>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_S64;
+	static constexpr char * format = "%lld";
+};
+
+template<>
+class ImGuiDataTypeTraits<unsigned long long>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_U64;
+	static constexpr char * format = "%llu";
+};
+
+template<>
+class ImGuiDataTypeTraits<float>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_Float;
+	static constexpr char * format = "%.3f";
+};
+
+template<>
+class ImGuiDataTypeTraits<double>
+{
+	static constexpr ImGuiDataType value = ImGuiDataType_Double;
+	static constexpr char * format = "%.6f";
+};
+
+} // namespace ImGui
+
+#endif // IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H

+ 2 - 2
shared/cmake/libigl.cmake

@@ -11,7 +11,7 @@ if(APPLE)
     message(STATUS "Found libtool - ${CMAKE_LIBTOOL}")
     get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
     foreach(lang ${languages})
-      # Added -c 
+      # Added -c
       set(CMAKE_${lang}_CREATE_STATIC_LIBRARY
         "${CMAKE_LIBTOOL} -c -static -o <TARGET> <LINK_FLAGS> <OBJECTS> ")
     endforeach()
@@ -42,7 +42,7 @@ option(LIBIGL_WITH_MATLAB            "Use Matlab"         "${Matlab_FOUND}")
 option(LIBIGL_WITH_MOSEK             "Use MOSEK"          "${MOSEK_FOUND}")
 option(LIBIGL_WITH_OPENGL            "Use OpenGL"         "${OPENGL_FOUND}")
 option(LIBIGL_WITH_OPENGL_GLFW       "Use GLFW"           "${OPENGL_FOUND}")
-option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui"          OFF)
+option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui"          ON)
 option(LIBIGL_WITH_PNG               "Use PNG"            ON)
 option(LIBIGL_WITH_TETGEN            "Use Tetgen"         ON)
 option(LIBIGL_WITH_TRIANGLE          "Use Triangle"       ON)

+ 4 - 4
tutorial/106_ViewerMenu/main.cpp

@@ -6,7 +6,6 @@
 #include <iostream>
 #include "tutorial_shared_path.h"
 
-
 int main(int argc, char *argv[])
 {
   Eigen::MatrixXd V;
@@ -23,7 +22,7 @@ int main(int argc, char *argv[])
   viewer.plugins.push_back(&menu);
 
   // Customize the menu
-  float floatVariable = 0.1f; // Shared between two menus
+  double doubleVariable = 0.1f; // Shared between two menus
 
   // Add content to the default menu window
   menu.callback_draw_viewer_menu = [&]()
@@ -35,7 +34,7 @@ int main(int argc, char *argv[])
     if (ImGui::CollapsingHeader("New Group", ImGuiTreeNodeFlags_DefaultOpen))
     {
       // Expose variable directly ...
-      ImGui::InputFloat("float", &floatVariable, 0, 0, 3);
+      ImGui::InputDouble("double", &doubleVariable, 0, 0, "%.4f");
 
       // ... or using a custom callback
       static bool boolVariable = true;
@@ -87,9 +86,10 @@ int main(int argc, char *argv[])
         ImGuiWindowFlags_NoSavedSettings
     );
 
+
     // Expose the same variable directly ...
     ImGui::PushItemWidth(-80);
-    ImGui::DragFloat("float", &floatVariable, 0.0, 0.0, 3.0);
+    ImGui::DragScalar("double", ImGuiDataType_Double, &doubleVariable, 0.1, 0, 0, "%.4f");
     ImGui::PopItemWidth();
 
     static std::string str = "bunny";