Viewer.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_OPENGL_GLFW_VIEWER_H
  9. #define IGL_OPENGL_GLFW_VIEWER_H
  10. #ifndef IGL_OPENGL_4
  11. #define IGL_OPENGL_4
  12. #endif
  13. #include "../../igl_inline.h"
  14. #include "../State.h"
  15. #include "../ViewerCore.h"
  16. #include "../../ViewerData.h"
  17. #include "ViewerPlugin.h"
  18. #include <Eigen/Core>
  19. #include <Eigen/Geometry>
  20. #include <vector>
  21. #include <string>
  22. #include <cstdint>
  23. #define IGL_MOD_SHIFT 0x0001
  24. #define IGL_MOD_CONTROL 0x0002
  25. #define IGL_MOD_ALT 0x0004
  26. #define IGL_MOD_SUPER 0x0008
  27. #ifdef IGL_VIEWER_WITH_NANOGUI
  28. namespace nanogui { class FormHelper; class Screen; }
  29. #endif
  30. struct GLFWwindow;
  31. namespace igl
  32. {
  33. namespace opengl
  34. {
  35. namespace glfw
  36. {
  37. // GLFW-based mesh viewer
  38. class Viewer
  39. {
  40. public:
  41. // UI Enumerations
  42. enum class MouseButton {Left, Middle, Right};
  43. enum class MouseMode { None, Rotation, Zoom, Pan, Translation} mouse_mode;
  44. IGL_INLINE int launch(bool resizable = true,bool fullscreen = false);
  45. IGL_INLINE int launch_init(bool resizable = true,bool fullscreen = false);
  46. IGL_INLINE bool launch_rendering(bool loop = true);
  47. IGL_INLINE void launch_shut();
  48. IGL_INLINE void init();
  49. IGL_INLINE void init_plugins();
  50. IGL_INLINE void shutdown_plugins();
  51. Viewer();
  52. ~Viewer();
  53. // Mesh IO
  54. IGL_INLINE bool load_mesh_from_file(const std::string & mesh_file_name);
  55. IGL_INLINE bool save_mesh_to_file(const std::string & mesh_file_name);
  56. // Callbacks
  57. IGL_INLINE bool key_pressed(unsigned int unicode_key,int modifier);
  58. IGL_INLINE bool key_down(int key,int modifier);
  59. IGL_INLINE bool key_up(int key,int modifier);
  60. IGL_INLINE bool mouse_down(MouseButton button,int modifier);
  61. IGL_INLINE bool mouse_up(MouseButton button,int modifier);
  62. IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
  63. IGL_INLINE bool mouse_scroll(float delta_y);
  64. // Scene IO
  65. IGL_INLINE bool load_scene();
  66. IGL_INLINE bool load_scene(std::string fname);
  67. IGL_INLINE bool save_scene();
  68. // Draw everything
  69. IGL_INLINE void draw();
  70. // OpenGL context resize
  71. IGL_INLINE void resize(int w,int h); // explicitly set window size
  72. IGL_INLINE void post_resize(int w,int h); // external resize due to user interaction
  73. // Helper functions
  74. IGL_INLINE void snap_to_canonical_quaternion();
  75. IGL_INLINE void open_dialog_load_mesh();
  76. IGL_INLINE void open_dialog_save_mesh();
  77. IGL_INLINE ViewerData& data();
  78. //IGL_INLINE const ViewerData& const_data() const;
  79. IGL_INLINE State& selected_opengl_state();
  80. // Append a new "slot" for a mesh (i.e., create empty entires at the end of
  81. // the data_list and opengl_state_list.
  82. //
  83. // Returns number of meshes (always >= 1)
  84. //
  85. // Side Effects:
  86. // selected_data_index is set this newly created, last entry (i.e.,
  87. // #meshes-1)
  88. IGL_INLINE void append_mesh();
  89. // Erase a mesh (i.e., its corresponding data and state entires in data_list
  90. // and opengl_state_list)
  91. //
  92. // Inputs:
  93. // index index of mesh to erase
  94. // Returns whether erasure was successful <=> cannot erase last mesh
  95. //
  96. // Side Effects:
  97. // If selected_data_index is greater than or equal to index then it is
  98. // decremented
  99. // Example:
  100. // // Erase all mesh slots except first and clear remaining mesh
  101. // viewer.selected_data_index = viewer.data_list.size()-1;
  102. // while(viewer.erase_mesh(viewer.selected_data_index)){};
  103. // viewer.data().clear();
  104. //
  105. IGL_INLINE bool erase_mesh(const size_t index);
  106. private:
  107. // Alec: I call this data_list instead of just data to avoid confusion with
  108. // old "data" variable.
  109. // Stores all the data that should be visualized
  110. std::vector<ViewerData> data_list;
  111. // Stores the vbos indices and opengl related settings
  112. std::vector<State> opengl_state_list;
  113. public:
  114. size_t selected_data_index;
  115. GLFWwindow* window;
  116. // Stores all the viewing options
  117. ViewerCore core;
  118. // List of registered plugins
  119. std::vector<ViewerPlugin*> plugins;
  120. // Temporary data stored when the mouse button is pressed
  121. Eigen::Quaternionf down_rotation;
  122. int current_mouse_x;
  123. int current_mouse_y;
  124. int down_mouse_x;
  125. int down_mouse_y;
  126. float down_mouse_z;
  127. Eigen::Vector3f down_translation;
  128. bool down;
  129. bool hack_never_moved;
  130. #ifdef IGL_VIEWER_WITH_NANOGUI
  131. nanogui::FormHelper* ngui;
  132. nanogui::Screen* screen;
  133. #endif
  134. // Keep track of the global position of the scrollwheel
  135. float scroll_position;
  136. // C++-style functions
  137. //
  138. // Returns **true** if action should be cancelled.
  139. std::function<bool(Viewer& viewer)> callback_init;
  140. std::function<bool(Viewer& viewer)> callback_pre_draw;
  141. std::function<bool(Viewer& viewer)> callback_post_draw;
  142. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  143. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  144. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  145. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  146. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
  147. // THESE SHOULD BE DEPRECATED:
  148. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
  149. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
  150. // Pointers to per-callback data
  151. void* callback_init_data;
  152. void* callback_pre_draw_data;
  153. void* callback_post_draw_data;
  154. void* callback_mouse_down_data;
  155. void* callback_mouse_up_data;
  156. void* callback_mouse_move_data;
  157. void* callback_mouse_scroll_data;
  158. void* callback_key_pressed_data;
  159. void* callback_key_down_data;
  160. void* callback_key_up_data;
  161. public:
  162. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  163. };
  164. } // end namespace
  165. } // end namespace
  166. } // end namespace
  167. #ifndef IGL_STATIC_LIBRARY
  168. # include "Viewer.cpp"
  169. #endif
  170. #endif