Viewer.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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 "../MeshGL.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. struct GLFWwindow;
  28. namespace igl
  29. {
  30. namespace opengl
  31. {
  32. namespace glfw
  33. {
  34. // GLFW-based mesh viewer
  35. class Viewer
  36. {
  37. public:
  38. // UI Enumerations
  39. enum class MouseButton {Left, Middle, Right};
  40. enum class MouseMode { None, Rotation, Zoom, Pan, Translation} mouse_mode;
  41. IGL_INLINE int launch (bool fullscreen = false, const std::string &name = "libigl viewer", int width = 0, int height = 0);
  42. IGL_INLINE int launch_init(bool fullscreen = false, const std::string &name = "libigl viewer", int width = 0, int height = 0);
  43. IGL_INLINE bool launch_rendering(bool loop = true);
  44. IGL_INLINE void launch_shut();
  45. IGL_INLINE void init();
  46. IGL_INLINE void init_plugins();
  47. IGL_INLINE void shutdown_plugins();
  48. Viewer();
  49. ~Viewer();
  50. // Mesh IO
  51. IGL_INLINE bool load_mesh_from_file(const std::string & mesh_file_name);
  52. IGL_INLINE bool save_mesh_to_file(const std::string & mesh_file_name);
  53. // Callbacks
  54. IGL_INLINE bool key_pressed(unsigned int unicode_key,int modifier);
  55. IGL_INLINE bool key_down(int key,int modifier);
  56. IGL_INLINE bool key_up(int key,int modifier);
  57. IGL_INLINE bool mouse_down(MouseButton button,int modifier);
  58. IGL_INLINE bool mouse_up(MouseButton button,int modifier);
  59. IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
  60. IGL_INLINE bool mouse_scroll(float delta_y);
  61. // Scene IO
  62. IGL_INLINE bool load_scene();
  63. IGL_INLINE bool load_scene(std::string fname);
  64. IGL_INLINE bool save_scene();
  65. IGL_INLINE bool save_scene(std::string fname);
  66. // Draw everything
  67. IGL_INLINE void draw();
  68. // Render given ViewerCore to a buffer. The width and height are determined
  69. // by non-zeros dimensions of R or – if both are zero — are set to this
  70. // core's viewport sizes. Other buffers are resized to fit if needed.
  71. //
  72. // Template:
  73. // T image storage type, e.g., unsigned char (values ∈ [0,255]), double
  74. // (values ∈ [0.0,1.0]).
  75. // Inputs:
  76. // data which ViewerData to draw
  77. // update_matrices whether to update view, proj, and norm matrices in
  78. // shaders
  79. // Outputs:
  80. // R width by height red pixel color values
  81. // G width by height green pixel color values
  82. // B width by height blue pixel color values
  83. // A width by height alpha pixel color values
  84. // D width by height depth pixel values. Depth values are _not_
  85. // anti-aliased like RGBA.
  86. //
  87. template <typename T>
  88. IGL_INLINE void draw_buffer(
  89. // can't be const because of writing in and out of `core.viewport`
  90. /*const*/ igl::opengl::ViewerCore & core,
  91. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & R,
  92. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & G,
  93. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B,
  94. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  95. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & D);
  96. // OpenGL context resize
  97. IGL_INLINE void resize(int w,int h); // explicitly set window size
  98. IGL_INLINE void post_resize(int w,int h); // external resize due to user interaction
  99. // Helper functions
  100. IGL_INLINE void snap_to_canonical_quaternion();
  101. IGL_INLINE void open_dialog_load_mesh();
  102. IGL_INLINE void open_dialog_save_mesh();
  103. ////////////////////////
  104. // Multi-mesh methods //
  105. ////////////////////////
  106. // Return the current mesh, or the mesh corresponding to a given unique identifier
  107. //
  108. // Inputs:
  109. // mesh_id unique identifier associated to the desired mesh (current mesh if -1)
  110. IGL_INLINE ViewerData& data(int mesh_id = -1);
  111. IGL_INLINE const ViewerData& data(int mesh_id = -1) const;
  112. // Append a new "slot" for a mesh (i.e., create empty entries at the end of
  113. // the data_list and opengl_state_list.
  114. //
  115. // Inputs:
  116. // visible If true, the new mesh is set to be visible on all existing viewports
  117. // Returns the id of the last appended mesh
  118. //
  119. // Side Effects:
  120. // selected_data_index is set this newly created, last entry (i.e.,
  121. // #meshes-1)
  122. IGL_INLINE int append_mesh(bool visible = true);
  123. // Erase a mesh (i.e., its corresponding data and state entires in data_list
  124. // and opengl_state_list)
  125. //
  126. // Inputs:
  127. // index index of mesh to erase
  128. // Returns whether erasure was successful <=> cannot erase last mesh
  129. //
  130. // Side Effects:
  131. // If selected_data_index is greater than or equal to index then it is
  132. // decremented
  133. // Example:
  134. // // Erase all mesh slots except first and clear remaining mesh
  135. // viewer.selected_data_index = viewer.data_list.size()-1;
  136. // while(viewer.erase_mesh(viewer.selected_data_index)){};
  137. // viewer.data().clear();
  138. //
  139. IGL_INLINE bool erase_mesh(const size_t index);
  140. // Retrieve mesh index from its unique identifier
  141. // Returns 0 if not found
  142. IGL_INLINE size_t mesh_index(const int id) const;
  143. ////////////////////////////
  144. // Multi-viewport methods //
  145. ////////////////////////////
  146. // Return the current viewport, or the viewport corresponding to a given unique identifier
  147. //
  148. // Inputs:
  149. // core_id unique identifier corresponding to the desired viewport (current viewport if 0)
  150. IGL_INLINE ViewerCore& core(unsigned core_id = 0);
  151. IGL_INLINE const ViewerCore& core(unsigned core_id = 0) const;
  152. // Append a new "slot" for a viewport (i.e., copy properties of the current viewport, only
  153. // changing the viewport size/position)
  154. //
  155. // Inputs:
  156. // viewport Vector specifying the viewport origin and size in screen coordinates.
  157. // append_empty If true, existing meshes are hidden on the new viewport.
  158. //
  159. // Returns the unique id of the newly inserted viewport. There can be a maximum of 31
  160. // viewports created in the same viewport. Erasing a viewport does not change the id of
  161. // other existing viewports
  162. IGL_INLINE int append_core(Eigen::Vector4f viewport, bool append_empty = false);
  163. // Erase a viewport
  164. //
  165. // Inputs:
  166. // index index of the viewport to erase
  167. IGL_INLINE bool erase_core(const size_t index);
  168. // Retrieve viewport index from its unique identifier
  169. // Returns 0 if not found
  170. IGL_INLINE size_t core_index(const int id) const;
  171. // Change selected_core_index to the viewport containing the mouse
  172. // (current_mouse_x, current_mouse_y)
  173. IGL_INLINE void select_hovered_core();
  174. public:
  175. //////////////////////
  176. // Member variables //
  177. //////////////////////
  178. // Alec: I call this data_list instead of just data to avoid confusion with
  179. // old "data" variable.
  180. // Stores all the data that should be visualized
  181. std::vector<ViewerData> data_list;
  182. size_t selected_data_index;
  183. int next_data_id;
  184. GLFWwindow* window;
  185. // Stores all the viewing options
  186. std::vector<ViewerCore> core_list;
  187. size_t selected_core_index;
  188. int next_core_id;
  189. // List of registered plugins
  190. std::vector<ViewerPlugin*> plugins;
  191. // Temporary data stored when the mouse button is pressed
  192. Eigen::Quaternionf down_rotation;
  193. int current_mouse_x;
  194. int current_mouse_y;
  195. int down_mouse_x;
  196. int down_mouse_y;
  197. float down_mouse_z;
  198. Eigen::Vector3f down_translation;
  199. bool down;
  200. bool hack_never_moved;
  201. // Keep track of the global position of the scrollwheel
  202. float scroll_position;
  203. // C++-style functions
  204. //
  205. // Returns **true** if action should be cancelled.
  206. std::function<bool(Viewer& viewer)> callback_init;
  207. std::function<bool(Viewer& viewer)> callback_pre_draw;
  208. std::function<bool(Viewer& viewer)> callback_post_draw;
  209. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  210. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  211. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  212. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  213. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
  214. std::function<bool(Viewer& viewer, int w, int h)> callback_post_resize;
  215. // THESE SHOULD BE DEPRECATED:
  216. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_down;
  217. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_up;
  218. // Pointers to per-callback data
  219. void* callback_init_data;
  220. void* callback_pre_draw_data;
  221. void* callback_post_draw_data;
  222. void* callback_mouse_down_data;
  223. void* callback_mouse_up_data;
  224. void* callback_mouse_move_data;
  225. void* callback_mouse_scroll_data;
  226. void* callback_key_pressed_data;
  227. void* callback_key_down_data;
  228. void* callback_key_up_data;
  229. public:
  230. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  231. };
  232. } // end namespace
  233. } // end namespace
  234. } // end namespace
  235. #ifndef IGL_STATIC_LIBRARY
  236. # include "Viewer.cpp"
  237. #endif
  238. #endif