Viewer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. // Enable per-face colors and normals
  78. bool face_based;
  79. // Animation
  80. bool is_animating;
  81. double animation_max_fps;
  82. };
  83. // Stores all the viewing options
  84. Options options;
  85. // Stores all the data that should be visualized
  86. igl::ViewerData data;
  87. // Stores the vbos indices and opengl related settings
  88. igl::OpenGL_state opengl;
  89. // List of registered plugins
  90. std::vector<ViewerPlugin*> plugins;
  91. void init_plugins();
  92. void shutdown_plugins();
  93. // Temporary data stored when the mouse button is pressed
  94. Eigen::Vector4f down_rotation;
  95. int current_mouse_x;
  96. int current_mouse_y;
  97. int down_mouse_x;
  98. int down_mouse_y;
  99. float down_mouse_z;
  100. Eigen::Vector3f down_translation;
  101. bool down;
  102. bool hack_never_moved;
  103. // Anttweak bar
  104. TwBar* bar;
  105. // Window size
  106. int width;
  107. int height;
  108. // Keep track of the global position of the scrollwheel
  109. float scroll_position;
  110. // Useful functions
  111. void compute_normals(); // Computes the normals of the mesh
  112. void uniform_colors(Eigen::Vector3d ambient, Eigen::Vector3d diffuse, Eigen::Vector3d specular); // assign uniform colors to all faces/vertices
  113. void grid_texture(); // Generate a default grid texture
  114. void clear_mesh(); // Clear the mesh data
  115. void align_camera_center(); // Adjust the view to see the entire model
  116. // Change the visualization mode, invalidating the cache if necessary
  117. void set_face_based(bool newvalue);
  118. // Helpers that can draw the most common meshes
  119. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  120. void set_vertices(const Eigen::MatrixXd& V);
  121. void set_normals(const Eigen::MatrixXd& N);
  122. // Set the color of the mesh
  123. //
  124. // Inputs:
  125. // C #V|#F|1 by 3 list of colors
  126. void set_colors(const Eigen::MatrixXd &C);
  127. void set_uv(const Eigen::MatrixXd& UV);
  128. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  129. void set_texture(
  130. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  131. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  132. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  133. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  134. // Sets edges given a list of edge vertices and edge indices. In constrast
  135. // to `add_edges` this will (purposefully) clober existing edges.
  136. //
  137. // Inputs:
  138. // P #P by 3 list of vertex positions
  139. // E #E by 2 list of edge indices into P
  140. // C #E|1 by 3 color(s)
  141. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  142. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  143. void add_label (const Eigen::VectorXd& P, const std::string& str);
  144. // Save the OpenGL transformation matrices used for the previous rendering pass
  145. Eigen::Matrix4f view;
  146. Eigen::Matrix4f model;
  147. Eigen::Matrix4f proj;
  148. Eigen::Vector4f viewport;
  149. // UI Enumerations
  150. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  151. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  152. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  153. Viewer();
  154. ~Viewer();
  155. // Mesh IO
  156. bool load_mesh_from_file(const char* mesh_file_name);
  157. bool save_mesh_to_file(const char* mesh_file_name);
  158. // Callbacks
  159. bool key_down(unsigned char key, int modifier);
  160. bool key_up(unsigned char key, int modifier);
  161. bool mouse_down(MouseButton button, int modifier);
  162. bool mouse_up(MouseButton button, int modifier);
  163. bool mouse_move(int mouse_x, int mouse_y);
  164. bool mouse_scroll(float delta_y);
  165. // Scene IO
  166. bool load_scene();
  167. bool save_scene();
  168. // Determines how much to zoom and shift such that the mesh fills the unit
  169. // box (centered at the origin)
  170. static void get_scale_and_shift_to_fit_mesh(
  171. const Eigen::MatrixXd& V,
  172. const Eigen::MatrixXi& F,
  173. float & zoom,
  174. Eigen::Vector3f& shift);
  175. // Draw everything
  176. void draw();
  177. // OpenGL context resize
  178. void resize(int w, int h);
  179. // C-style callbacks
  180. bool (*callback_pre_draw)(Viewer& viewer);
  181. bool (*callback_post_draw)(Viewer& viewer);
  182. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  183. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  184. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  185. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  186. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  187. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  188. // Pointers to per-callback data
  189. void* callback_pre_draw_data;
  190. void* callback_post_draw_data;
  191. void* callback_mouse_down_data;
  192. void* callback_mouse_up_data;
  193. void* callback_mouse_move_data;
  194. void* callback_mouse_scroll_data;
  195. void* callback_key_down_data;
  196. void* callback_key_up_data;
  197. /********* AntTweakBar callbacks *********/
  198. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  199. static void TW_CALL save_scene_cb(void *clientData);
  200. static void TW_CALL load_scene_cb(void *clientData);
  201. static void TW_CALL open_dialog_mesh(void *clientData);
  202. static void TW_CALL align_camera_center_cb(void *clientData);
  203. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  204. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  205. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  206. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  207. public:
  208. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  209. };
  210. } // end namespace
  211. #ifndef IGL_STATIC_LIBRARY
  212. # include "Viewer.cpp"
  213. #endif
  214. #endif