Viewer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. namespace igl
  21. {
  22. // Forward declaration of the viewer_plugin class
  23. class Viewer_plugin;
  24. class Viewer
  25. {
  26. public:
  27. int launch(std::string filename = "");
  28. void init();
  29. class Options
  30. #ifdef ENABLE_XML_SERIALIZATION
  31. : public ::igl::XMLSerialization
  32. #endif
  33. {
  34. public:
  35. Options()
  36. #ifdef ENABLE_XML_SERIALIZATION
  37. : XMLSerialization("Options")
  38. #endif
  39. {};
  40. void InitSerialization();
  41. // Shape material
  42. float shininess;
  43. // Colors
  44. Eigen::Vector3f background_color;
  45. Eigen::Vector3f line_color;
  46. // Lighting
  47. Eigen::Vector3f light_position;
  48. float lighting_factor;
  49. // Trackball angle (quaternion)
  50. Eigen::Vector4f trackball_angle;
  51. // Model viewing parameters
  52. float model_zoom;
  53. Eigen::Vector3f model_translation;
  54. // Model viewing paramters (uv coordinates)
  55. float model_zoom_uv;
  56. Eigen::Vector3f model_translation_uv;
  57. // Camera parameters
  58. float camera_zoom;
  59. bool orthographic;
  60. Eigen::Vector3f camera_eye;
  61. Eigen::Vector3f camera_up;
  62. Eigen::Vector3f camera_center;
  63. float camera_view_angle;
  64. float camera_dnear;
  65. float camera_dfar;
  66. // Visualization options
  67. bool show_overlay;
  68. bool show_overlay_depth;
  69. bool show_texture;
  70. bool show_faces;
  71. bool show_lines;
  72. bool show_vertid;
  73. bool show_faceid;
  74. bool invert_normals;
  75. // Point size / line width
  76. float point_size;
  77. float line_width;
  78. // Enable per-face colors and normals
  79. bool face_based;
  80. // Animation
  81. bool is_animating;
  82. double animation_max_fps;
  83. };
  84. // Stores all the viewing options
  85. Options options;
  86. // Stores all the data that should be visualized
  87. igl::ViewerData data;
  88. // Stores the vbos indices and opengl related settings
  89. igl::OpenGL_state opengl;
  90. // List of registered plugins
  91. std::vector<Viewer_plugin*> plugins;
  92. void init_plugins();
  93. void shutdown_plugins();
  94. // Temporary data stored when the mouse button is pressed
  95. Eigen::Vector4f down_rotation;
  96. int current_mouse_x;
  97. int current_mouse_y;
  98. int down_mouse_x;
  99. int down_mouse_y;
  100. float down_mouse_z;
  101. Eigen::Vector3f down_translation;
  102. bool down;
  103. bool hack_never_moved;
  104. // Anttweak bar
  105. TwBar* bar;
  106. // Window size
  107. int width;
  108. int height;
  109. // Keep track of the global position of the scrollwheel
  110. float scroll_position;
  111. // Useful functions
  112. void compute_normals(); // Computes the normals of the mesh
  113. void uniform_colors(Eigen::Vector3d ambient, Eigen::Vector3d diffuse, Eigen::Vector3d specular); // assign uniform colors to all faces/vertices
  114. void grid_texture(); // Generate a default grid texture
  115. void clear_mesh(); // Clear the mesh data
  116. void align_camera_center(); // Adjust the view to see the entire model
  117. // Change the visualization mode, invalidating the cache if necessary
  118. void set_face_based(bool newvalue);
  119. // Helpers that can draw the most common meshes
  120. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  121. void set_vertices(const Eigen::MatrixXd& V);
  122. void set_normals(const Eigen::MatrixXd& N);
  123. // Set the color of the mesh
  124. //
  125. // Inputs:
  126. // C #V|#F|1 by 3 list of colors
  127. void set_colors(const Eigen::MatrixXd &C);
  128. void set_uv(const Eigen::MatrixXd& UV);
  129. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  130. void set_texture(
  131. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  132. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  133. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  134. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  135. // Sets edges given a list of edge vertices and edge indices. In constrast
  136. // to `add_edges` this will (purposefully) clober existing edges.
  137. //
  138. // Inputs:
  139. // P #P by 3 list of vertex positions
  140. // E #E by 2 list of edge indices into P
  141. // C #E|1 by 3 color(s)
  142. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  143. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  144. void add_label (const Eigen::VectorXd& P, const std::string& str);
  145. // Save the OpenGL transformation matrices used for the previous rendering pass
  146. Eigen::Matrix4f view;
  147. Eigen::Matrix4f model;
  148. Eigen::Matrix4f proj;
  149. Eigen::Vector4f viewport;
  150. // UI Enumerations
  151. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  152. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  153. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  154. Viewer();
  155. ~Viewer();
  156. // Mesh IO
  157. bool load_mesh_from_file(const char* mesh_file_name);
  158. bool save_mesh_to_file(const char* mesh_file_name);
  159. // Callbacks
  160. bool key_down(unsigned char key, int modifier);
  161. bool key_up(unsigned char key, int modifier);
  162. bool mouse_down(MouseButton button, int modifier);
  163. bool mouse_up(MouseButton button, int modifier);
  164. bool mouse_move(int mouse_x, int mouse_y);
  165. bool mouse_scroll(float delta_y);
  166. // Scene IO
  167. bool load_scene();
  168. bool save_scene();
  169. // Determines how much to zoom and shift such that the mesh fills the unit
  170. // box (centered at the origin)
  171. static void get_scale_and_shift_to_fit_mesh(
  172. const Eigen::MatrixXd& V,
  173. const Eigen::MatrixXi& F,
  174. float & zoom,
  175. Eigen::Vector3f& shift);
  176. // Draw everything
  177. void draw();
  178. // OpenGL context resize
  179. void resize(int w, int h);
  180. // C-style callbacks
  181. bool (*callback_pre_draw)(Viewer& viewer);
  182. bool (*callback_post_draw)(Viewer& viewer);
  183. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  184. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  185. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  186. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  187. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  188. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  189. // Pointers to per-callback data
  190. void* callback_pre_draw_data;
  191. void* callback_post_draw_data;
  192. void* callback_mouse_down_data;
  193. void* callback_mouse_up_data;
  194. void* callback_mouse_move_data;
  195. void* callback_mouse_scroll_data;
  196. void* callback_key_down_data;
  197. void* callback_key_up_data;
  198. /********* AntTweakBar callbacks *********/
  199. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  200. static void TW_CALL save_scene_cb(void *clientData);
  201. static void TW_CALL load_scene_cb(void *clientData);
  202. static void TW_CALL open_dialog_mesh(void *clientData);
  203. static void TW_CALL align_camera_center_cb(void *clientData);
  204. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  205. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  206. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  207. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  208. public:
  209. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  210. };
  211. // Abstract class for plugins
  212. // All plugins MUST have this class as their parent and implement all the callbacks
  213. // For an example of a basic plugins see plugins/skeleton.h
  214. //
  215. // Return value of callbacks: returning true to any of the callbacks tells Preview3D that the event has been
  216. // handled and that it should not be passed to other plugins or to other internal functions of Preview3D
  217. class Viewer_plugin
  218. #ifdef ENABLE_XML_SERIALIZATION
  219. : public ::igl::XMLSerialization
  220. #endif
  221. {
  222. public:
  223. Viewer_plugin()
  224. #ifdef ENABLE_XML_SERIALIZATION
  225. : XMLSerialization("dummy")
  226. #endif
  227. {plugin_name = "dummy";};
  228. ~Viewer_plugin(){};
  229. // This function is called when the viewer is initialized (no mesh will be loaded at this stage)
  230. virtual void init(igl::Viewer *_viewer)
  231. {
  232. viewer = _viewer;
  233. }
  234. // This function is called before shutdown
  235. virtual void shutdown()
  236. {
  237. }
  238. // This function is called before a mesh is loaded
  239. virtual bool load(std::string filename)
  240. {
  241. return false;
  242. }
  243. // This function is called before a mesh is saved
  244. virtual bool save(std::string filename)
  245. {
  246. return false;
  247. }
  248. // Runs immediately after a new mesh had been loaded.
  249. virtual bool post_load()
  250. {
  251. return false;
  252. }
  253. // This function is called before the draw procedure of Preview3D
  254. virtual bool pre_draw()
  255. {
  256. return false;
  257. }
  258. // This function is called after the draw procedure of Preview3D
  259. virtual bool post_draw()
  260. {
  261. return false;
  262. }
  263. // This function is called when the mouse button is pressed
  264. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  265. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  266. virtual bool mouse_down(int button, int modifier)
  267. {
  268. return false;
  269. }
  270. // This function is called when the mouse button is released
  271. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  272. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  273. virtual bool mouse_up(int button, int modifier)
  274. {
  275. return false;
  276. }
  277. // This function is called every time the mouse is moved
  278. // - mouse_x and mouse_y are the new coordinates of the mouse pointer in screen coordinates
  279. virtual bool mouse_move(int mouse_x, int mouse_y)
  280. {
  281. return false;
  282. }
  283. // This function is called every time the scroll wheel is moved
  284. // Note: this callback is not working with every glut implementation
  285. virtual bool mouse_scroll(float delta_y)
  286. {
  287. return false;
  288. }
  289. // This function is called when a keyboard key is pressed
  290. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  291. virtual bool key_down(unsigned char key, int modifiers)
  292. {
  293. return false;
  294. }
  295. // This function is called when a keyboard key is release
  296. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  297. virtual bool key_up(unsigned char key, int modifiers)
  298. {
  299. return false;
  300. }
  301. // Priority of the plugin (use only positive numbers, negative are reserved for internal use)
  302. // The plugins will be initialized in increasing priority
  303. virtual int priority()
  304. {
  305. return 0;
  306. }
  307. std::string plugin_name;
  308. protected:
  309. // Pointer to the main Preview3D class
  310. Viewer *viewer;
  311. public:
  312. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  313. };
  314. } // end namespace
  315. #ifndef IGL_STATIC_LIBRARY
  316. # include "Viewer.cpp"
  317. #endif
  318. #endif