瀏覽代碼

Plugin shutdown routine; minor cleanups

 - Added a new plugin method that gets called just before shutdown

 - Made a few global internal variables static since they're not useful
   to external code

 - Minor comment cleanups etc.


Former-commit-id: c69fc7cd3c9ff908b5c1d989aa4f4a60e2dd58b5
Wenzel Jakob 11 年之前
父節點
當前提交
a580aa64b5
共有 2 個文件被更改,包括 37 次插入22 次删除
  1. 20 11
      include/igl/viewer/Viewer.cpp
  2. 17 11
      include/igl/viewer/Viewer.h

+ 20 - 11
include/igl/viewer/Viewer.cpp

@@ -185,14 +185,14 @@ Eigen::Matrix4f translate(
 #include <igl/snap_to_canonical_view_quat.h>
 #include <igl/snap_to_canonical_view_quat.h>
 #include <TwOpenGLCore.h>
 #include <TwOpenGLCore.h>
 
 
-// This is needed for glfw event handling
-
-// main.cpp
-igl::Viewer * __viewer;
+// Plugin manager (exported to other compilation units)
 igl::Plugin_manager igl_viewer_plugin_manager;
 igl::Plugin_manager igl_viewer_plugin_manager;
-double highdpi = 1;
-double scroll_x = 0;
-double scroll_y = 0;
+
+// Internal global variables used for glfw event handling
+static igl::Viewer * __viewer;
+static double highdpi = 1;
+static double scroll_x = 0;
+static double scroll_y = 0;
 
 
 /* This class extends the font rendering code in AntTweakBar
 /* This class extends the font rendering code in AntTweakBar
    so that it can be used to render text at arbitrary 3D positions */
    so that it can be used to render text at arbitrary 3D positions */
@@ -700,6 +700,13 @@ namespace igl
   {
   {
   }
   }
 
 
+  void Viewer::shutdown_plugins()
+  {
+    if (plugin_manager)
+      for (unsigned int i = 0; i<plugin_manager->plugin_list.size(); ++i)
+        plugin_manager->plugin_list[i]->shutdown();
+  }
+
   bool Viewer::load_mesh_from_file(const char* mesh_file_name)
   bool Viewer::load_mesh_from_file(const char* mesh_file_name)
   {
   {
     std::string mesh_file_name_string = std::string(mesh_file_name);
     std::string mesh_file_name_string = std::string(mesh_file_name);
@@ -2264,9 +2271,10 @@ namespace igl
     // Initialize AntTweakBar
     // Initialize AntTweakBar
     TwInit(TW_OPENGL_CORE, NULL);
     TwInit(TW_OPENGL_CORE, NULL);
 
 
-    //      // Initialize IGL viewer
+    // Initialize IGL viewer
     init(&igl_viewer_plugin_manager);
     init(&igl_viewer_plugin_manager);
     __viewer = this;
     __viewer = this;
+
     // Register callbacks
     // Register callbacks
     glfwSetKeyCallback(window, glfw_key_callback);
     glfwSetKeyCallback(window, glfw_key_callback);
     glfwSetCursorPosCallback(window,glfw_mouse_move);
     glfwSetCursorPosCallback(window,glfw_mouse_move);
@@ -2290,19 +2298,20 @@ namespace igl
 
 
     // Load the mesh passed as input
     // Load the mesh passed as input
     if (filename.size() > 0)
     if (filename.size() > 0)
-      __viewer->load_mesh_from_file(filename.c_str());
+      load_mesh_from_file(filename.c_str());
 
 
     // Rendering loop
     // Rendering loop
     while (!glfwWindowShouldClose(window))
     while (!glfwWindowShouldClose(window))
     {
     {
-      __viewer->draw();
+      draw();
 
 
       glfwSwapBuffers(window);
       glfwSwapBuffers(window);
       //glfwPollEvents();
       //glfwPollEvents();
       glfwWaitEvents();
       glfwWaitEvents();
     }
     }
 
 
-    __viewer->free_opengl();
+    free_opengl();
+    shutdown_plugins();
 
 
     glfwDestroyWindow(window);
     glfwDestroyWindow(window);
     glfwTerminate();
     glfwTerminate();

+ 17 - 11
include/igl/viewer/Viewer.h

@@ -315,6 +315,7 @@ namespace igl
     // Pointer to the plugin_manager (usually it will be a global variable)
     // Pointer to the plugin_manager (usually it will be a global variable)
     Plugin_manager* plugin_manager;
     Plugin_manager* plugin_manager;
     void init_plugins();
     void init_plugins();
+    void shutdown_plugins();
 
 
     // Temporary data stored when the mouse button is pressed
     // Temporary data stored when the mouse button is pressed
     Eigen::Vector4f down_rotation;
     Eigen::Vector4f down_rotation;
@@ -454,19 +455,24 @@ namespace igl
 
 
     ~Viewer_plugin(){};
     ~Viewer_plugin(){};
 
 
-    // It is called when the viewer is initialized (no mesh will be loaded at this stage)
+    // This function is called when the viewer is initialized (no mesh will be loaded at this stage)
     virtual void init(igl::Viewer *_viewer)
     virtual void init(igl::Viewer *_viewer)
     {
     {
       viewer = _viewer;
       viewer = _viewer;
     }
     }
 
 
-    // It is called before a mesh is loaded
+    // This function is called before shutdown
+    virtual void shutdown()
+    {
+    }
+
+    // This function is called before a mesh is loaded
     virtual bool load(std::string filename)
     virtual bool load(std::string filename)
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called before a mesh is saved
+    // This function is called before a mesh is saved
     virtual bool save(std::string filename)
     virtual bool save(std::string filename)
     {
     {
       return false;
       return false;
@@ -478,19 +484,19 @@ namespace igl
       return false;
       return false;
     }
     }
 
 
-    // It is called before the draw procedure of Preview3D
+    // This function is called before the draw procedure of Preview3D
     virtual bool pre_draw()
     virtual bool pre_draw()
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called after the draw procedure of Preview3D
+    // This function is called after the draw procedure of Preview3D
     virtual bool post_draw()
     virtual bool post_draw()
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called when the mouse button is pressed
+    // This function is called when the mouse button is pressed
     // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
     // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     virtual bool mouse_down(int button, int modifier)
     virtual bool mouse_down(int button, int modifier)
@@ -498,7 +504,7 @@ namespace igl
       return false;
       return false;
     }
     }
 
 
-    // It is called when the mouse button is released
+    // This function is called when the mouse button is released
     // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
     // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     virtual bool mouse_up(int button, int modifier)
     virtual bool mouse_up(int button, int modifier)
@@ -506,28 +512,28 @@ namespace igl
       return false;
       return false;
     }
     }
 
 
-    // It is called every time the mouse is moved
+    // This function is called every time the mouse is moved
     // - mouse_x and mouse_y are the new coordinates of the mouse pointer in screen coordinates
     // - mouse_x and mouse_y are the new coordinates of the mouse pointer in screen coordinates
     virtual bool mouse_move(int mouse_x, int mouse_y)
     virtual bool mouse_move(int mouse_x, int mouse_y)
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called every time the scroll wheel is moved
+    // This function is called every time the scroll wheel is moved
     // Note: this callback is not working with every glut implementation
     // Note: this callback is not working with every glut implementation
     virtual bool mouse_scroll(float delta_y)
     virtual bool mouse_scroll(float delta_y)
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called when a keyboard key is pressed
+    // This function is called when a keyboard key is pressed
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     virtual bool key_down(unsigned char key, int modifiers)
     virtual bool key_down(unsigned char key, int modifiers)
     {
     {
       return false;
       return false;
     }
     }
 
 
-    // It is called when a keyboard key is release
+    // This function is called when a keyboard key is release
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
     virtual bool key_up(unsigned char key, int modifiers)
     virtual bool key_up(unsigned char key, int modifiers)
     {
     {