Viewer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. namespace igl
  22. {
  23. class Viewer
  24. {
  25. public:
  26. int launch(std::string filename = "");
  27. void init();
  28. class Options
  29. #ifdef ENABLE_XML_SERIALIZATION
  30. : public ::igl::XMLSerialization
  31. #endif
  32. {
  33. public:
  34. Options()
  35. #ifdef ENABLE_XML_SERIALIZATION
  36. : XMLSerialization("Options")
  37. #endif
  38. {};
  39. void InitSerialization();
  40. // Shape material
  41. float shininess;
  42. // Colors
  43. Eigen::Vector3f background_color;
  44. Eigen::Vector3f line_color;
  45. // Lighting
  46. Eigen::Vector3f light_position;
  47. float lighting_factor;
  48. // Trackball angle (quaternion)
  49. Eigen::Vector4f trackball_angle;
  50. // Model viewing parameters
  51. float model_zoom;
  52. Eigen::Vector3f model_translation;
  53. // Model viewing paramters (uv coordinates)
  54. float model_zoom_uv;
  55. Eigen::Vector3f model_translation_uv;
  56. // Camera parameters
  57. float camera_zoom;
  58. bool orthographic;
  59. Eigen::Vector3f camera_eye;
  60. Eigen::Vector3f camera_up;
  61. Eigen::Vector3f camera_center;
  62. float camera_view_angle;
  63. float camera_dnear;
  64. float camera_dfar;
  65. // Visualization options
  66. bool show_overlay;
  67. bool show_overlay_depth;
  68. bool show_texture;
  69. bool show_faces;
  70. bool show_lines;
  71. bool show_vertid;
  72. bool show_faceid;
  73. bool invert_normals;
  74. // Point size / line width
  75. float point_size;
  76. float line_width;
  77. // Animation
  78. bool is_animating;
  79. double animation_max_fps;
  80. };
  81. // Stores all the viewing options
  82. Options options;
  83. // Stores all the data that should be visualized
  84. igl::ViewerData data;
  85. // Stores the vbos indices and opengl related settings
  86. igl::OpenGL_state opengl;
  87. // List of registered plugins
  88. std::vector<ViewerPlugin*> plugins;
  89. void init_plugins();
  90. void shutdown_plugins();
  91. // Temporary data stored when the mouse button is pressed
  92. Eigen::Vector4f down_rotation;
  93. int current_mouse_x;
  94. int current_mouse_y;
  95. int down_mouse_x;
  96. int down_mouse_y;
  97. float down_mouse_z;
  98. Eigen::Vector3f down_translation;
  99. bool down;
  100. bool hack_never_moved;
  101. // Anttweak bar
  102. TwBar* bar;
  103. // Window size
  104. int width;
  105. int height;
  106. // Keep track of the global position of the scrollwheel
  107. float scroll_position;
  108. void align_camera_center(); // Adjust the view to see the entire model
  109. void compute_normals();
  110. void clear(); // Clear the mesh data
  111. // Change the visualization mode, invalidating the cache if necessary
  112. void set_face_based(bool newvalue);
  113. // Helpers that can draw the most common meshes
  114. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  115. void set_vertices(const Eigen::MatrixXd& V);
  116. void set_normals(const Eigen::MatrixXd& N);
  117. // Set the color of the mesh
  118. //
  119. // Inputs:
  120. // C #V|#F|1 by 3 list of colors
  121. void set_colors(const Eigen::MatrixXd &C);
  122. void set_uv(const Eigen::MatrixXd& UV);
  123. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  124. void set_texture(
  125. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  126. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  127. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  128. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  129. // Sets edges given a list of edge vertices and edge indices. In constrast
  130. // to `add_edges` this will (purposefully) clober existing edges.
  131. //
  132. // Inputs:
  133. // P #P by 3 list of vertex positions
  134. // E #E by 2 list of edge indices into P
  135. // C #E|1 by 3 color(s)
  136. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  137. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  138. void add_label (const Eigen::VectorXd& P, const std::string& str);
  139. // Save the OpenGL transformation matrices used for the previous rendering pass
  140. Eigen::Matrix4f view;
  141. Eigen::Matrix4f model;
  142. Eigen::Matrix4f proj;
  143. Eigen::Vector4f viewport;
  144. // UI Enumerations
  145. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  146. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  147. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  148. Viewer();
  149. ~Viewer();
  150. // Mesh IO
  151. bool load_mesh_from_file(const char* mesh_file_name);
  152. bool save_mesh_to_file(const char* mesh_file_name);
  153. // Callbacks
  154. bool key_down(unsigned char key, int modifier);
  155. bool key_up(unsigned char key, int modifier);
  156. bool mouse_down(MouseButton button, int modifier);
  157. bool mouse_up(MouseButton button, int modifier);
  158. bool mouse_move(int mouse_x, int mouse_y);
  159. bool mouse_scroll(float delta_y);
  160. // Scene IO
  161. bool load_scene();
  162. bool save_scene();
  163. // Determines how much to zoom and shift such that the mesh fills the unit
  164. // box (centered at the origin)
  165. static void get_scale_and_shift_to_fit_mesh(
  166. const Eigen::MatrixXd& V,
  167. const Eigen::MatrixXi& F,
  168. float & zoom,
  169. Eigen::Vector3f& shift);
  170. // Draw everything
  171. void draw();
  172. // OpenGL context resize
  173. void resize(int w, int h);
  174. // C-style callbacks
  175. bool (*callback_pre_draw)(Viewer& viewer);
  176. bool (*callback_post_draw)(Viewer& viewer);
  177. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  178. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  179. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  180. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  181. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  182. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  183. // Pointers to per-callback data
  184. void* callback_pre_draw_data;
  185. void* callback_post_draw_data;
  186. void* callback_mouse_down_data;
  187. void* callback_mouse_up_data;
  188. void* callback_mouse_move_data;
  189. void* callback_mouse_scroll_data;
  190. void* callback_key_down_data;
  191. void* callback_key_up_data;
  192. /********* AntTweakBar callbacks *********/
  193. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  194. static void TW_CALL save_scene_cb(void *clientData);
  195. static void TW_CALL load_scene_cb(void *clientData);
  196. static void TW_CALL open_dialog_mesh(void *clientData);
  197. static void TW_CALL align_camera_center_cb(void *clientData);
  198. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  199. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  200. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  201. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  202. public:
  203. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  204. };
  205. } // end namespace
  206. #ifndef IGL_STATIC_LIBRARY
  207. # include "Viewer.cpp"
  208. #endif
  209. #endif