Viewer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace nanogui {
  29. class FormScreen;
  30. }
  31. namespace igl
  32. {
  33. namespace viewer
  34. {
  35. // GLFW-based mesh viewer
  36. class Viewer
  37. {
  38. public:
  39. IGL_INLINE int launch(bool resizable = true,bool fullscreen = false);
  40. IGL_INLINE void init();
  41. // Stores all the viewing options
  42. ViewerCore core;
  43. // Stores all the data that should be visualized
  44. ViewerData data;
  45. // Stores the vbos indices and opengl related settings
  46. OpenGL_state opengl;
  47. // List of registered plugins
  48. std::vector<ViewerPlugin*> plugins;
  49. IGL_INLINE void init_plugins();
  50. IGL_INLINE void shutdown_plugins();
  51. // Temporary data stored when the mouse button is pressed
  52. Eigen::Vector4f down_rotation;
  53. int current_mouse_x;
  54. int current_mouse_y;
  55. int down_mouse_x;
  56. int down_mouse_y;
  57. float down_mouse_z;
  58. Eigen::Vector3f down_translation;
  59. bool down;
  60. bool hack_never_moved;
  61. nanogui::FormScreen* ngui;
  62. // Keep track of the global position of the scrollwheel
  63. float scroll_position;
  64. // UI Enumerations
  65. enum class MouseButton {Left, Middle, Right};
  66. enum class MouseMode { None, Rotation, Zoom, Pan, Translation} mouse_mode;
  67. Viewer();
  68. ~Viewer();
  69. // Mesh IO
  70. IGL_INLINE bool load_mesh_from_file(const char* mesh_file_name);
  71. IGL_INLINE bool save_mesh_to_file(const char* mesh_file_name);
  72. // Callbacks
  73. IGL_INLINE bool key_down(int key,int modifier);
  74. IGL_INLINE bool key_up(int key,int modifier);
  75. IGL_INLINE bool mouse_down(MouseButton button,int modifier);
  76. IGL_INLINE bool mouse_up(MouseButton button,int modifier);
  77. IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
  78. IGL_INLINE bool mouse_scroll(float delta_y);
  79. // Scene IO
  80. IGL_INLINE bool load_scene();
  81. IGL_INLINE bool load_scene(std::string fname);
  82. IGL_INLINE bool save_scene();
  83. // Draw everything
  84. IGL_INLINE void draw();
  85. // OpenGL context resize
  86. IGL_INLINE void resize(int w,int h);
  87. // Helper functions
  88. IGL_INLINE void snap_to_canonical_quaternion();
  89. IGL_INLINE void open_dialog_load_mesh();
  90. IGL_INLINE void open_dialog_save_mesh();
  91. // C++-style functions
  92. std::function<bool(Viewer& viewer)> callback_init;
  93. std::function<bool(Viewer& viewer)> callback_pre_draw;
  94. std::function<bool(Viewer& viewer)> callback_post_draw;
  95. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  96. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  97. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  98. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  99. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
  100. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
  101. // Pointers to per-callback data
  102. void* callback_init_data;
  103. void* callback_pre_draw_data;
  104. void* callback_post_draw_data;
  105. void* callback_mouse_down_data;
  106. void* callback_mouse_up_data;
  107. void* callback_mouse_move_data;
  108. void* callback_mouse_scroll_data;
  109. void* callback_key_down_data;
  110. void* callback_key_up_data;
  111. public:
  112. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  113. };
  114. } // end namespace
  115. } // end namespace
  116. #ifndef IGL_STATIC_LIBRARY
  117. # include "Viewer.cpp"
  118. #endif
  119. #endif