소스 검색

Update multi-mesh tutorial + drop opengl2/ from CMake.

Former-commit-id: ce9fa48f5d54d7d7477044eb0121c91e96185382
Jérémie Dumas 7 년 전
부모
커밋
4a4b6d96dd

+ 7 - 11
include/igl/opengl/glfw/Viewer.cpp

@@ -6,8 +6,6 @@
 // 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/.
 
-// Must defined this before including Viewer.h
-#define IGL_VIEWER_VIEWER_CPP
 #include "Viewer.h"
 
 #include <chrono>
@@ -465,13 +463,10 @@ namespace glfw
   T,t     Toggle filled faces
   Z       Snap to canonical view
   [,]     Toggle between rotation control types (trackball, two-axis
-          valuator with fixed up, 2D mode with no rotation)
-  <,>     Toggle between models.)"
-#ifdef IGL_VIEWER_WITH_NANOGUI
-		R"(
+          valuator with fixed up, 2D mode with no rotation))
+  <,>     Toggle between models
   ;       Toggle vertex labels
   :       Toggle face labels)"
-#endif
 );
     std::cout<<usage<<std::endl;
 #endif
@@ -685,14 +680,12 @@ namespace glfw
           (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
         return true;
       }
-#ifdef IGL_VIEWER_WITH_NANOGUI
       case ';':
         data().show_vertid = !data().show_vertid;
         return true;
       case ':':
         data().show_faceid = !data().show_faceid;
         return true;
-#endif
       default: break;//do nothing
     }
     return false;
@@ -1063,15 +1056,18 @@ namespace glfw
     return data_list[selected_data_index];
   }
 
-  IGL_INLINE void Viewer::append_mesh()
+  IGL_INLINE size_t Viewer::append_mesh()
   {
     assert(data_list.size() >= 1);
 
     data_list.emplace_back();
     selected_data_index = data_list.size()-1;
+    return data_list.size();
   }
+
   IGL_INLINE bool Viewer::erase_mesh(const size_t index)
   {
+    assert((i >= 0 && i < data_list.size()) && "index should be in bounds");
     assert(data_list.size() >= 1);
     if(data_list.size() == 1)
     {
@@ -1079,7 +1075,7 @@ namespace glfw
       return false;
     }
     data_list[index].meshgl.free();
-    data_list.erase(data_list.begin()                 + index);
+    data_list.erase(data_list.begin() + index);
     if(selected_data_index >= index && selected_data_index>0)
     {
       selected_data_index--;

+ 1 - 1
include/igl/opengl/glfw/Viewer.h

@@ -91,7 +91,7 @@ namespace glfw
     // Side Effects:
     //   selected_data_index is set this newly created, last entry (i.e.,
     //   #meshes-1)
-    IGL_INLINE void append_mesh();
+    IGL_INLINE size_t append_mesh();
     // Erase a mesh (i.e., its corresponding data and state entires in data_list
     // and opengl_state_list)
     //

+ 7 - 0
include/igl/opengl/glfw/imgui/ImGuiHelpers.h

@@ -1,3 +1,10 @@
+// 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_IMGUIHELPERS_H
 #define IGL_OPENGL_GLFW_IMGUI_IMGUIHELPERS_H
 

+ 11 - 1
include/igl/opengl/glfw/imgui/ImGuiMenu.cpp

@@ -1,3 +1,10 @@
+// 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/.
 ////////////////////////////////////////////////////////////////////////////////
 #include "ImGuiMenu.h"
 #include <igl/project.h>
@@ -285,7 +292,10 @@ IGL_INLINE void ImGuiMenu::draw_labels_menu()
       | ImGuiWindowFlags_NoCollapse
       | ImGuiWindowFlags_NoSavedSettings
       | ImGuiWindowFlags_NoInputs);
-  draw_labels(viewer->data());
+  for (const auto & data : viewer->data_list)
+  {
+    draw_labels(data);
+  }
   ImGui::End();
   ImGui::PopStyleColor();
 }

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

@@ -1,3 +1,10 @@
+// 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_IMGUIMENU_H
 #define IGL_OPENGL_GLFW_IMGUI_IMGUIMENU_H
 

+ 20 - 1
include/igl/opengl2/gl.h

@@ -20,6 +20,25 @@
 //     #include <GL/gl.h>
 //
 
-#include <glad/glad.h>
+// For now this includes glu, glew and glext (perhaps these should be
+// separated)
+#ifdef _WIN32
+#    define NOMINMAX
+#    include <Windows.h>
+#    undef DrawText
+#    undef NOMINMAX
+#endif
+
+#ifndef __APPLE__
+#  define GLEW_STATIC
+#  include <GL/glew.h>
+#endif
+
+#ifdef __APPLE__
+#  include <OpenGL/gl.h>
+#  define __gl_h_ /* Prevent inclusion of the old gl.h */
+#else
+#  include <GL/gl.h>
+#endif
 
 #endif

+ 0 - 4
shared/cmake/libigl.cmake

@@ -272,18 +272,14 @@ if(LIBIGL_WITH_OPENGL)
   # OpenGL module
   find_package(OpenGL REQUIRED)
   compile_igl_module("opengl")
-  # compile_igl_module("opengl2")
   target_link_libraries(igl_opengl ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
-  # target_link_libraries(igl_opengl2 ${IGL_SCOPE} ${OPENGL_gl_LIBRARY})
   target_include_directories(igl_opengl SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
-  # target_include_directories(igl_opengl2 SYSTEM ${IGL_SCOPE} ${OPENGL_INCLUDE_DIR})
 
   # glad module
   if(NOT TARGET glad)
     add_subdirectory(${LIBIGL_EXTERNAL}/glad glad)
   endif()
   target_link_libraries(igl_opengl ${IGL_SCOPE} glad)
-  # target_link_libraries(igl_opengl2 ${IGL_SCOPE} glad)
 endif()
 
 ################################################################################

+ 1 - 1
tutorial/107_MultipleMeshes/CMakeLists.txt

@@ -2,4 +2,4 @@ cmake_minimum_required(VERSION 2.8.12)
 project(107_MultipleMeshes)
 
 add_executable(${PROJECT_NAME}_bin main.cpp)
-target_link_libraries(${PROJECT_NAME}_bin igl::core igl::opengl igl::opengl_glfw tutorials)
+target_link_libraries(${PROJECT_NAME}_bin igl::core igl::opengl igl::opengl_glfw igl::opengl_glfw_imgui tutorials)

+ 50 - 14
tutorial/107_MultipleMeshes/main.cpp

@@ -1,35 +1,71 @@
 #include "tutorial_shared_path.h"
 #include <igl/opengl/glfw/Viewer.h>
+#include <igl/opengl/glfw/imgui/ImGuiMenu.h>
+#include <igl/opengl/glfw/imgui/ImGuiHelpers.h>
 #include <string>
+#include <map>
 #include <iostream>
 
+std::vector<std::string> names = {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
+std::map<std::string, Eigen::RowVector3d> colors;
+int last_colored_index = -1;
+
+// Set colors of each mesh
+void update_colors(igl::opengl::glfw::Viewer &viewer)
+{
+  for (int i = 0; i < (int) viewer.data_list.size(); ++i)
+  {
+    viewer.data_list[i].set_colors(colors[names[i]]);
+  }
+  viewer.data_list[viewer.selected_data_index].set_colors(Eigen::RowVector3d(0.9,0.1,0.1));
+  last_colored_index = viewer.selected_data_index;
+}
+
+// Menu for selecting a mesh
+class MyMenu : public igl::opengl::glfw::imgui::ImGuiMenu
+{
+  virtual void draw_viewer_menu() override
+  {
+    if (ImGui::Combo("Selected Mesh", (int *) &viewer->selected_data_index, names))
+    {
+      update_colors(*viewer);
+    }
+    if (last_colored_index != viewer->selected_data_index)
+    {
+      update_colors(*viewer);
+    }
+  }
+};
+
 int main(int argc, char * argv[])
 {
   igl::opengl::glfw::Viewer viewer;
-  const auto names = 
-    {"cube.obj","sphere.obj","xcylinder.obj","ycylinder.obj","zcylinder.obj"};
   for(const auto & name : names)
   {
     viewer.load_mesh_from_file(std::string(TUTORIAL_SHARED_PATH) + "/" + name);
+    colors[name] = Eigen::RowVector3d::Random();
   }
 
-  // Set colors of each mesh by selecting its index first
-  viewer.selected_data_index = 0;
-  viewer.data().set_colors(Eigen::RowVector3d(0.8,0.47,0.22));
-  viewer.selected_data_index = 1;
-  viewer.data().set_colors(Eigen::RowVector3d(0.6,0.01,0.11));
-  viewer.selected_data_index = 2;
-  viewer.data().set_colors(Eigen::RowVector3d(0.37,0.06,0.25));
-  viewer.selected_data_index = 3;
-  viewer.data().set_colors(Eigen::RowVector3d(1,1,1));
-
-  viewer.callback_key_down = 
+  // Attach a custom menu
+  MyMenu menu;
+  viewer.core.is_animating = true;
+  viewer.plugins.push_back(&menu);
+
+  // Color each mesh differently
+  update_colors(viewer);
+
+  viewer.callback_key_down =
     [&](igl::opengl::glfw::Viewer &, unsigned int key, int mod)
   {
     // Delete
     if(key == '3')
     {
-      viewer.erase_mesh(viewer.selected_data_index);
+      int old_index = viewer.selected_data_index;
+      if (viewer.erase_mesh(viewer.selected_data_index))
+      {
+        names.erase(names.begin() + old_index);
+        update_colors(viewer);
+      }
       return true;
     }
     return false;