Viewer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Main class of the Viewer
  2. #ifndef IGL_VIEWER_H
  3. #define IGL_VIEWER_H
  4. #include <AntTweakBar.h>
  5. #include <vector>
  6. #include <string>
  7. #include <cstdint>
  8. #define IGL_MOD_SHIFT 0x0001
  9. #define IGL_MOD_CONTROL 0x0002
  10. #define IGL_MOD_ALT 0x0004
  11. #define IGL_MOD_SUPER 0x0008
  12. #ifdef ENABLE_XML_SERIALIZATION
  13. #include <igl/xml/XMLSerializer.h>
  14. #include <igl/xml/XMLSerialization.h>
  15. #endif
  16. #include <Eigen/Core>
  17. #include <igl/viewer/OpenGL_shader.h>
  18. #include <igl/viewer/ViewerData.h>
  19. #include <igl/viewer/OpenGL_state.h>
  20. #include <igl/viewer/ViewerPlugin.h>
  21. #include <igl/viewer/ViewerCore.h>
  22. namespace igl
  23. {
  24. class Viewer
  25. {
  26. public:
  27. int launch(std::string filename = "");
  28. void init();
  29. // Stores all the viewing options
  30. igl::ViewerCore options;
  31. // Stores all the data that should be visualized
  32. igl::ViewerData data;
  33. // Stores the vbos indices and opengl related settings
  34. igl::OpenGL_state opengl;
  35. // List of registered plugins
  36. std::vector<ViewerPlugin*> plugins;
  37. void init_plugins();
  38. void shutdown_plugins();
  39. // Temporary data stored when the mouse button is pressed
  40. Eigen::Vector4f down_rotation;
  41. int current_mouse_x;
  42. int current_mouse_y;
  43. int down_mouse_x;
  44. int down_mouse_y;
  45. float down_mouse_z;
  46. Eigen::Vector3f down_translation;
  47. bool down;
  48. bool hack_never_moved;
  49. // Anttweak bar
  50. TwBar* bar;
  51. // Keep track of the global position of the scrollwheel
  52. float scroll_position;
  53. // Wrapper for ViewerData::compute_normals()
  54. void compute_normals();
  55. void clear(); // Clear the mesh data
  56. // Change the visualization mode, invalidating the cache if necessary
  57. void set_face_based(bool newvalue);
  58. // Helpers that can draw the most common meshes
  59. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  60. void set_vertices(const Eigen::MatrixXd& V);
  61. void set_normals(const Eigen::MatrixXd& N);
  62. void set_colors(const Eigen::MatrixXd &C);
  63. void set_uv(const Eigen::MatrixXd& UV);
  64. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  65. void set_texture(
  66. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  67. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  68. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  69. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  70. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  71. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  72. void add_label (const Eigen::VectorXd& P, const std::string& str);
  73. // Eigen::Vector4f viewport;
  74. // UI Enumerations
  75. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  76. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  77. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  78. Viewer();
  79. ~Viewer();
  80. // Mesh IO
  81. bool load_mesh_from_file(const char* mesh_file_name);
  82. bool save_mesh_to_file(const char* mesh_file_name);
  83. // Callbacks
  84. bool key_down(unsigned char key, int modifier);
  85. bool key_up(unsigned char key, int modifier);
  86. bool mouse_down(MouseButton button, int modifier);
  87. bool mouse_up(MouseButton button, int modifier);
  88. bool mouse_move(int mouse_x, int mouse_y);
  89. bool mouse_scroll(float delta_y);
  90. // Scene IO
  91. bool load_scene();
  92. bool save_scene();
  93. // Draw everything
  94. void draw();
  95. // OpenGL context resize
  96. void resize(int w, int h);
  97. // C-style callbacks
  98. bool (*callback_pre_draw)(Viewer& viewer);
  99. bool (*callback_post_draw)(Viewer& viewer);
  100. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  101. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  102. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  103. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  104. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  105. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  106. // Pointers to per-callback data
  107. void* callback_pre_draw_data;
  108. void* callback_post_draw_data;
  109. void* callback_mouse_down_data;
  110. void* callback_mouse_up_data;
  111. void* callback_mouse_move_data;
  112. void* callback_mouse_scroll_data;
  113. void* callback_key_down_data;
  114. void* callback_key_up_data;
  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. public:
  126. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  127. };
  128. } // end namespace
  129. #ifndef IGL_STATIC_LIBRARY
  130. # include "Viewer.cpp"
  131. #endif
  132. #endif