Viewer.h 5.5 KB

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