Viewer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_VIEWER_VIEWER_H
  9. #define IGL_VIEWER_VIEWER_H
  10. #ifndef IGL_OPENGL_4
  11. #define IGL_OPENGL_4
  12. #endif
  13. #include <vector>
  14. #include <string>
  15. #include <cstdint>
  16. #include <Eigen/Core>
  17. #include <Eigen/Geometry>
  18. #include <igl/igl_inline.h>
  19. #include "OpenGL_shader.h"
  20. #include "OpenGL_state.h"
  21. #include "ViewerCore.h"
  22. #include "ViewerData.h"
  23. #include "ViewerPlugin.h"
  24. #define IGL_MOD_SHIFT 0x0001
  25. #define IGL_MOD_CONTROL 0x0002
  26. #define IGL_MOD_ALT 0x0004
  27. #define IGL_MOD_SUPER 0x0008
  28. #ifdef IGL_VIEWER_WITH_NANOGUI
  29. namespace nanogui { class FormHelper; class Screen; }
  30. #endif
  31. class GLFWwindow;
  32. namespace igl
  33. {
  34. namespace viewer
  35. {
  36. // GLFW-based mesh viewer
  37. class Viewer
  38. {
  39. public:
  40. GLFWwindow* window;
  41. IGL_INLINE int launch(bool resizable = true,bool fullscreen = false);
  42. IGL_INLINE int launch_init(bool resizable = true,bool fullscreen = false);
  43. IGL_INLINE bool launch_rendering(bool loop = true);
  44. IGL_INLINE void launch_shut();
  45. IGL_INLINE void init();
  46. // Stores all the viewing options
  47. ViewerCore core;
  48. // Stores all the data that should be visualized
  49. ViewerData data;
  50. // Stores the vbos indices and opengl related settings
  51. OpenGL_state opengl;
  52. // List of registered plugins
  53. std::vector<ViewerPlugin*> plugins;
  54. IGL_INLINE void init_plugins();
  55. IGL_INLINE void shutdown_plugins();
  56. // Temporary data stored when the mouse button is pressed
  57. Eigen::Quaternionf down_rotation;
  58. int current_mouse_x;
  59. int current_mouse_y;
  60. int down_mouse_x;
  61. int down_mouse_y;
  62. float down_mouse_z;
  63. Eigen::Vector3f down_translation;
  64. bool down;
  65. bool hack_never_moved;
  66. #ifdef IGL_VIEWER_WITH_NANOGUI
  67. nanogui::FormHelper* ngui;
  68. nanogui::Screen* screen;
  69. #endif
  70. // Keep track of the global position of the scrollwheel
  71. float scroll_position;
  72. // UI Enumerations
  73. enum class MouseButton {Left, Middle, Right};
  74. enum class MouseMode { None, Rotation, Zoom, Pan, Translation} mouse_mode;
  75. Viewer();
  76. ~Viewer();
  77. // Mesh IO
  78. IGL_INLINE bool load_mesh_from_file(const char* mesh_file_name);
  79. IGL_INLINE bool save_mesh_to_file(const char* mesh_file_name);
  80. // Callbacks
  81. IGL_INLINE bool key_pressed(unsigned int unicode_key,int modifier);
  82. IGL_INLINE bool key_down(int key,int modifier);
  83. IGL_INLINE bool key_up(int key,int modifier);
  84. IGL_INLINE bool mouse_down(MouseButton button,int modifier);
  85. IGL_INLINE bool mouse_up(MouseButton button,int modifier);
  86. IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
  87. IGL_INLINE bool mouse_scroll(float delta_y);
  88. // Scene IO
  89. IGL_INLINE bool load_scene();
  90. IGL_INLINE bool load_scene(std::string fname);
  91. IGL_INLINE bool save_scene();
  92. // Draw everything
  93. IGL_INLINE void draw();
  94. // OpenGL context resize
  95. IGL_INLINE void resize(int w,int h);
  96. // Helper functions
  97. IGL_INLINE void snap_to_canonical_quaternion();
  98. IGL_INLINE void open_dialog_load_mesh();
  99. IGL_INLINE void open_dialog_save_mesh();
  100. // C++-style functions
  101. //
  102. // Returns **true** if action should be cancelled.
  103. std::function<bool(Viewer& viewer)> callback_init;
  104. std::function<bool(Viewer& viewer)> callback_pre_draw;
  105. std::function<bool(Viewer& viewer)> callback_post_draw;
  106. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  107. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  108. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  109. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  110. std::function<bool(Viewer& viewer, unsigned int key, int modifiers)> callback_key_pressed;
  111. // THESE SHOULD BE DEPRECATED:
  112. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
  113. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
  114. // Pointers to per-callback data
  115. void* callback_init_data;
  116. void* callback_pre_draw_data;
  117. void* callback_post_draw_data;
  118. void* callback_mouse_down_data;
  119. void* callback_mouse_up_data;
  120. void* callback_mouse_move_data;
  121. void* callback_mouse_scroll_data;
  122. void* callback_key_pressed_data;
  123. void* callback_key_down_data;
  124. void* callback_key_up_data;
  125. public:
  126. static IGL_INLINE bool igl_with_nanogui_defined_at_compile();
  127. static IGL_INLINE bool igl_with_nanogui_defined_consistently();
  128. public:
  129. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  130. };
  131. bool igl_with_nanogui_defined_at_include();
  132. #ifndef IGL_VIEWER_VIEWER_CPP
  133. bool igl_with_nanogui_defined_at_include()
  134. {
  135. // this must be inlined here.
  136. #ifdef IGL_VIEWER_WITH_NANOGUI
  137. return true;
  138. #else
  139. return false;
  140. #endif
  141. }
  142. #endif
  143. } // end namespace
  144. } // end namespace
  145. #ifndef IGL_STATIC_LIBRARY
  146. # include "Viewer.cpp"
  147. #endif
  148. #endif