Viewer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <AntTweakBar.h>
  19. #include <igl/igl_inline.h>
  20. #include "OpenGL_shader.h"
  21. #include "OpenGL_state.h"
  22. #include "ViewerCore.h"
  23. #include "ViewerData.h"
  24. #include "ViewerPlugin.h"
  25. #define IGL_MOD_SHIFT 0x0001
  26. #define IGL_MOD_CONTROL 0x0002
  27. #define IGL_MOD_ALT 0x0004
  28. #define IGL_MOD_SUPER 0x0008
  29. namespace igl
  30. {
  31. namespace viewer
  32. {
  33. // GLFW-based mesh viewer
  34. class Viewer
  35. {
  36. public:
  37. IGL_INLINE int launch(std::string filename = "");
  38. IGL_INLINE void init();
  39. // Stores command line arguments
  40. int argc;
  41. char **argv;
  42. // Stores all the viewing options
  43. ViewerCore core;
  44. // Stores all the data that should be visualized
  45. ViewerData data;
  46. // Stores the vbos indices and opengl related settings
  47. OpenGL_state opengl;
  48. // List of registered plugins
  49. std::vector<ViewerPlugin*> plugins;
  50. IGL_INLINE void init_plugins();
  51. IGL_INLINE void shutdown_plugins();
  52. // Temporary data stored when the mouse button is pressed
  53. Eigen::Vector4f down_rotation;
  54. int current_mouse_x;
  55. int current_mouse_y;
  56. int down_mouse_x;
  57. int down_mouse_y;
  58. float down_mouse_z;
  59. Eigen::Vector3f down_translation;
  60. bool down;
  61. bool hack_never_moved;
  62. // Anttweak bar
  63. TwBar* bar;
  64. // Keep track of the global position of the scrollwheel
  65. float scroll_position;
  66. // UI Enumerations
  67. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  68. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  69. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  70. Viewer();
  71. ~Viewer();
  72. // Mesh IO
  73. IGL_INLINE bool load_mesh_from_file(const char* mesh_file_name);
  74. IGL_INLINE bool save_mesh_to_file(const char* mesh_file_name);
  75. // Callbacks
  76. IGL_INLINE bool key_down(int key,int modifier);
  77. IGL_INLINE bool key_up(int key,int modifier);
  78. IGL_INLINE bool mouse_down(MouseButton button,int modifier);
  79. IGL_INLINE bool mouse_up(MouseButton button,int modifier);
  80. IGL_INLINE bool mouse_move(int mouse_x,int mouse_y);
  81. IGL_INLINE bool mouse_scroll(float delta_y);
  82. // Scene IO
  83. IGL_INLINE bool load_scene();
  84. IGL_INLINE bool save_scene();
  85. // Draw everything
  86. IGL_INLINE void draw();
  87. // OpenGL context resize
  88. IGL_INLINE void resize(int w,int h);
  89. // C++-style functions
  90. std::function<bool(Viewer& viewer)> callback_init;
  91. std::function<bool(Viewer& viewer)> callback_pre_draw;
  92. std::function<bool(Viewer& viewer)> callback_post_draw;
  93. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_down;
  94. std::function<bool(Viewer& viewer, int button, int modifier)> callback_mouse_up;
  95. std::function<bool(Viewer& viewer, int mouse_x, int mouse_y)> callback_mouse_move;
  96. std::function<bool(Viewer& viewer, float delta_y)> callback_mouse_scroll;
  97. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_down;
  98. std::function<bool(Viewer& viewer, unsigned char key, int modifiers)> callback_key_up;
  99. // Pointers to per-callback data
  100. void* callback_init_data;
  101. void* callback_pre_draw_data;
  102. void* callback_post_draw_data;
  103. void* callback_mouse_down_data;
  104. void* callback_mouse_up_data;
  105. void* callback_mouse_move_data;
  106. void* callback_mouse_scroll_data;
  107. void* callback_key_down_data;
  108. void* callback_key_up_data;
  109. /********* AntTweakBar callbacks *********/
  110. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  111. static void TW_CALL save_scene_cb(void *clientData);
  112. static void TW_CALL load_scene_cb(void *clientData);
  113. static void TW_CALL open_dialog_mesh(void *clientData);
  114. static void TW_CALL align_camera_center_cb(void *clientData);
  115. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  116. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  117. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  118. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  119. public:
  120. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  121. };
  122. } // end namespace
  123. } // end namespace
  124. #ifndef IGL_STATIC_LIBRARY
  125. # include "Viewer.cpp"
  126. #endif
  127. #endif