Viewer.h 4.7 KB

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