Viewer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 char* mesh_file_name);
  55. IGL_INLINE bool save_mesh_to_file(const char* 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);
  72. // Helper functions
  73. IGL_INLINE void snap_to_canonical_quaternion();
  74. IGL_INLINE void open_dialog_load_mesh();
  75. IGL_INLINE void open_dialog_save_mesh();
  76. IGL_INLINE ViewerData& selected_data();
  77. //IGL_INLINE const ViewerData& const_selected_data() const;
  78. IGL_INLINE State& selected_opengl();
  79. private:
  80. // Alec: I call this data_list instead of just data to avoid confusion with
  81. // old "data" variable.
  82. // Stores all the data that should be visualized
  83. std::vector<ViewerData> data_list;
  84. // Stores the vbos indices and opengl related settings
  85. std::vector<State> opengl_list;
  86. public:
  87. size_t selected_data_index;
  88. GLFWwindow* window;
  89. // Stores all the viewing options
  90. ViewerCore core;
  91. // List of registered plugins
  92. std::vector<ViewerPlugin*> plugins;
  93. // Temporary data stored when the mouse button is pressed
  94. Eigen::Quaternionf down_rotation;
  95. int current_mouse_x;
  96. int current_mouse_y;
  97. int down_mouse_x;
  98. int down_mouse_y;
  99. float down_mouse_z;
  100. Eigen::Vector3f down_translation;
  101. bool down;
  102. bool hack_never_moved;
  103. #ifdef IGL_VIEWER_WITH_NANOGUI
  104. nanogui::FormHelper* ngui;
  105. nanogui::Screen* screen;
  106. #endif
  107. // Keep track of the global position of the scrollwheel
  108. float scroll_position;
  109. // C++-style functions
  110. //
  111. // Returns **true** if action should be cancelled.
  112. std::function<bool(Viewer& viewer)> callback_init;
  113. std::function<bool(Viewer& viewer)> callback_pre_draw;
  114. std::function<bool(Viewer& viewer)> callback_post_draw;
  115. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  116. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  117. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  118. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  119. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
  120. // THESE SHOULD BE DEPRECATED:
  121. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
  122. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
  123. // Pointers to per-callback data
  124. void* callback_init_data;
  125. void* callback_pre_draw_data;
  126. void* callback_post_draw_data;
  127. void* callback_mouse_down_data;
  128. void* callback_mouse_up_data;
  129. void* callback_mouse_move_data;
  130. void* callback_mouse_scroll_data;
  131. void* callback_key_pressed_data;
  132. void* callback_key_down_data;
  133. void* callback_key_up_data;
  134. public:
  135. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  136. };
  137. } // end namespace
  138. } // end namespace
  139. } // end namespace
  140. #ifndef IGL_STATIC_LIBRARY
  141. # include "Viewer.cpp"
  142. #endif
  143. #endif