Viewer.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. namespace igl
  18. {
  19. class Plugin_manager;
  20. class Viewer
  21. {
  22. public:
  23. int launch(std::string filename = "");
  24. void init(Plugin_manager* pm);
  25. class Options
  26. #ifdef ENABLE_XML_SERIALIZATION
  27. : public ::igl::XMLSerialization
  28. #endif
  29. {
  30. public:
  31. Options()
  32. #ifdef ENABLE_XML_SERIALIZATION
  33. : XMLSerialization("Options")
  34. #endif
  35. {};
  36. void InitSerialization();
  37. // Shape material
  38. float shininess;
  39. // Colors
  40. Eigen::Vector3f background_color;
  41. Eigen::Vector3f line_color;
  42. // Lighting
  43. Eigen::Vector3f light_position;
  44. float lighting_factor;
  45. // Trackball angle (quaternion)
  46. Eigen::Vector4f trackball_angle;
  47. // Model viewing parameters
  48. float model_zoom;
  49. Eigen::Vector3f model_translation;
  50. // Model viewing paramters (uv coordinates)
  51. float model_zoom_uv;
  52. Eigen::Vector3f model_translation_uv;
  53. // Camera parameters
  54. float camera_zoom;
  55. bool orthographic;
  56. Eigen::Vector3f camera_eye;
  57. Eigen::Vector3f camera_up;
  58. Eigen::Vector3f camera_center;
  59. float camera_view_angle;
  60. float camera_dnear;
  61. float camera_dfar;
  62. // Visualization options
  63. bool show_overlay;
  64. bool show_overlay_depth;
  65. bool show_texture;
  66. bool show_faces;
  67. bool show_lines;
  68. bool show_vertid;
  69. bool show_faceid;
  70. bool invert_normals;
  71. // Point size / line width
  72. float point_size;
  73. float line_width;
  74. // Enable per-face colors and normals
  75. bool face_based;
  76. // Animation
  77. bool is_animating;
  78. double animation_max_fps;
  79. };
  80. enum DirtyFlags
  81. {
  82. DIRTY_NONE = 0x0000,
  83. DIRTY_POSITION = 0x0001,
  84. DIRTY_UV = 0x0002,
  85. DIRTY_NORMAL = 0x0004,
  86. DIRTY_AMBIENT = 0x0008,
  87. DIRTY_DIFFUSE = 0x0010,
  88. DIRTY_SPECULAR = 0x0020,
  89. DIRTY_TEXTURE = 0x0040,
  90. DIRTY_FACE = 0x0080,
  91. DIRTY_MESH = 0x00FF,
  92. DIRTY_OVERLAY_LINES = 0x0100,
  93. DIRTY_OVERLAY_POINTS = 0x0200,
  94. DIRTY_ALL = 0x03FF
  95. };
  96. class Data
  97. #ifdef ENABLE_XML_SERIALIZATION
  98. : public ::igl::XMLSerialization
  99. #endif
  100. {
  101. public:
  102. Data()
  103. #ifdef ENABLE_XML_SERIALIZATION
  104. : XMLSerialization("Data"), dirty(DIRTY_ALL)
  105. #endif
  106. {};
  107. void InitSerialization();
  108. Eigen::MatrixXd V; // Vertices of the current mesh (#V x 3)
  109. Eigen::MatrixXi F; // Faces of the mesh (#F x 3)
  110. // Per face attributes
  111. Eigen::MatrixXd F_normals; // One normal per face
  112. Eigen::MatrixXd F_material_ambient; // Per face ambient color
  113. Eigen::MatrixXd F_material_diffuse; // Per face diffuse color
  114. Eigen::MatrixXd F_material_specular; // Per face specular color
  115. // Per vertex attributes
  116. Eigen::MatrixXd V_normals; // One normal per vertex
  117. Eigen::MatrixXd V_material_ambient; // Per vertex ambient color
  118. Eigen::MatrixXd V_material_diffuse; // Per vertex diffuse color
  119. Eigen::MatrixXd V_material_specular; // Per vertex specular color
  120. // UV parametrization
  121. Eigen::MatrixXd V_uv; // UV vertices
  122. Eigen::MatrixXi F_uv; // optional faces for UVs
  123. // Texture
  124. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> texture_R;
  125. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> texture_G;
  126. Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic> texture_B;
  127. // Overlays
  128. // Lines plotted over the scene
  129. // (Every row contains 9 doubles in the following format S_x, S_y, S_z, T_x, T_y, T_z, C_r, C_g, C_b),
  130. // with S and T the coordinates of the two vertices of the line in global coordinates, and C the color in floating point rgb format
  131. Eigen::MatrixXd lines;
  132. // Points plotted over the scene
  133. // (Every row contains 6 doubles in the following format P_x, P_y, P_z, C_r, C_g, C_b),
  134. // with P the position in global coordinates of the center of the point, and C the color in floating point rgb format
  135. Eigen::MatrixXd points;
  136. // Text labels plotted over the scene
  137. // Textp contains, in the i-th row, the position in global coordinates where the i-th label should be anchored
  138. // Texts contains in the i-th position the text of the i-th label
  139. Eigen::MatrixXd labels_positions;
  140. std::vector<std::string > labels_strings;
  141. // Marks dirty buffers that need to be uploaded to OpenGL
  142. uint32_t dirty;
  143. // Caches the two-norm between the min/max point of the bounding box
  144. float object_scale;
  145. /*********************************/
  146. };
  147. class OpenGL_shader
  148. {
  149. public:
  150. typedef unsigned int GLuint;
  151. typedef int GLint;
  152. GLuint vertex_shader;
  153. GLuint fragment_shader;
  154. GLuint geometry_shader;
  155. GLuint program_shader;
  156. OpenGL_shader() : vertex_shader(0), fragment_shader(0),
  157. geometry_shader(0), program_shader(0) { }
  158. // Create a new shader from the specified source strings
  159. bool init(const std::string &vertex_shader_string,
  160. const std::string &fragment_shader_string,
  161. const std::string &fragment_data_name,
  162. const std::string &geometry_shader_string = "",
  163. int geometry_shader_max_vertices = 3);
  164. // Create a new shader from the specified files on disk
  165. bool init_from_files(const std::string &vertex_shader_filename,
  166. const std::string &fragment_shader_filename,
  167. const std::string &fragment_data_name,
  168. const std::string &geometry_shader_filename = "",
  169. int geometry_shader_max_vertices = 3);
  170. // Select this shader for subsequent draw calls
  171. void bind();
  172. // Release all OpenGL objects
  173. void free();
  174. // Return the OpenGL handle of a named shader attribute (-1 if it does not exist)
  175. GLint attrib(const std::string &name) const;
  176. // Return the OpenGL handle of a uniform attribute (-1 if it does not exist)
  177. GLint uniform(const std::string &name) const;
  178. // Bind a per-vertex array attribute and refresh its contents from an Eigen amtrix
  179. GLint bindVertexAttribArray(const std::string &name, GLuint bufferID,
  180. const Eigen::MatrixXf &M, bool refresh) const;
  181. };
  182. class OpenGL_state
  183. {
  184. public:
  185. typedef unsigned int GLuint;
  186. GLuint vao_mesh;
  187. GLuint vao_overlay_lines;
  188. GLuint vao_overlay_points;
  189. OpenGL_shader shader_mesh;
  190. OpenGL_shader shader_overlay_lines;
  191. OpenGL_shader shader_overlay_points;
  192. GLuint vbo_V; // Vertices of the current mesh (#V x 3)
  193. GLuint vbo_V_uv; // UV coordinates for the current mesh (#V x 2)
  194. GLuint vbo_V_normals; // Vertices of the current mesh (#V x 3)
  195. GLuint vbo_V_ambient; // Ambient material (#V x 3)
  196. GLuint vbo_V_diffuse; // Diffuse material (#V x 3)
  197. GLuint vbo_V_specular; // Specular material (#V x 3)
  198. GLuint vbo_F; // Faces of the mesh (#F x 3)
  199. GLuint vbo_tex; // Texture
  200. GLuint vbo_lines_F; // Indices of the line overlay
  201. GLuint vbo_lines_V; // Vertices of the line overlay
  202. GLuint vbo_lines_V_colors; // Color values of the line overlay
  203. GLuint vbo_points_F; // Indices of the point overlay
  204. GLuint vbo_points_V; // Vertices of the point overlay
  205. GLuint vbo_points_V_colors; // Color values of the point overlay
  206. // Temporary copy of the content of each VBO
  207. Eigen::MatrixXf V_vbo;
  208. Eigen::MatrixXf V_normals_vbo;
  209. Eigen::MatrixXf V_ambient_vbo;
  210. Eigen::MatrixXf V_diffuse_vbo;
  211. Eigen::MatrixXf V_specular_vbo;
  212. Eigen::MatrixXf V_uv_vbo;
  213. Eigen::MatrixXf lines_V_vbo;
  214. Eigen::MatrixXf lines_V_colors_vbo;
  215. Eigen::MatrixXf points_V_vbo;
  216. Eigen::MatrixXf points_V_colors_vbo;
  217. int tex_u;
  218. int tex_v;
  219. Eigen::Matrix<char,Eigen::Dynamic,1> tex;
  220. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> F_vbo;
  221. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> lines_F_vbo;
  222. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> points_F_vbo;
  223. // Marks dirty buffers that need to be uploaded to OpenGL
  224. uint32_t dirty;
  225. // Create a new set of OpenGL buffer objects
  226. void init();
  227. // Update contents from a 'Data' instance
  228. void set_data(const Data &data, bool face_based, bool invert_normals);
  229. // Bind the underlying OpenGL buffer objects for subsequent mesh draw calls
  230. void bind_mesh();
  231. /// Draw the currently buffered mesh (either solid or wireframe)
  232. void draw_mesh(bool solid);
  233. // Bind the underlying OpenGL buffer objects for subsequent line overlay draw calls
  234. void bind_overlay_lines();
  235. /// Draw the currently buffered line overlay
  236. void draw_overlay_lines();
  237. // Bind the underlying OpenGL buffer objects for subsequent point overlay draw calls
  238. void bind_overlay_points();
  239. /// Draw the currently buffered point overlay
  240. void draw_overlay_points();
  241. // Release the OpenGL buffer objects
  242. void free();
  243. };
  244. // Stores all the viewing options
  245. Options options;
  246. // Stores all the data that should be visualized
  247. Data data;
  248. // Stores the vbos indices and opengl related settings
  249. OpenGL_state opengl;
  250. // Pointer to the plugin_manager (usually it will be a global variable)
  251. Plugin_manager* plugin_manager;
  252. void init_plugins();
  253. void shutdown_plugins();
  254. // Temporary data stored when the mouse button is pressed
  255. Eigen::Vector4f down_rotation;
  256. int current_mouse_x;
  257. int current_mouse_y;
  258. int down_mouse_x;
  259. int down_mouse_y;
  260. float down_mouse_z;
  261. Eigen::Vector3f down_translation;
  262. bool down;
  263. bool hack_never_moved;
  264. // Anttweak bar
  265. TwBar* bar;
  266. // Window size
  267. int width;
  268. int height;
  269. // Keep track of the global position of the scrollwheel
  270. float scroll_position;
  271. // Useful functions
  272. void compute_normals(); // Computes the normals of the mesh
  273. void uniform_colors(Eigen::Vector3d ambient, Eigen::Vector3d diffuse, Eigen::Vector3d specular); // assign uniform colors to all faces/vertices
  274. void grid_texture(); // Generate a default grid texture
  275. void clear_mesh(); // Clear the mesh data
  276. void align_camera_center(); // Adjust the view to see the entire model
  277. // Change the visualization mode, invalidating the cache if necessary
  278. void set_face_based(bool newvalue);
  279. // Helpers that can draw the most common meshes
  280. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  281. void set_vertices(const Eigen::MatrixXd& V);
  282. void set_normals(const Eigen::MatrixXd& N);
  283. // Set the color of the mesh
  284. //
  285. // Inputs:
  286. // C #V|#F|1 by 3 list of colors
  287. void set_colors(const Eigen::MatrixXd &C);
  288. void set_uv(const Eigen::MatrixXd& UV);
  289. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  290. void set_texture(
  291. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  292. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  293. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  294. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  295. // Sets edges given a list of edge vertices and edge indices. In constrast
  296. // to `add_edges` this will (purposefully) clober existing edges.
  297. //
  298. // Inputs:
  299. // P #P by 3 list of vertex positions
  300. // E #E by 2 list of edge indices into P
  301. // C #E|1 by 3 color(s)
  302. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  303. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  304. void add_label (const Eigen::VectorXd& P, const std::string& str);
  305. // Save the OpenGL transformation matrices used for the previous rendering pass
  306. Eigen::Matrix4f view;
  307. Eigen::Matrix4f model;
  308. Eigen::Matrix4f proj;
  309. Eigen::Vector4f viewport;
  310. // UI Enumerations
  311. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  312. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  313. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  314. Viewer();
  315. ~Viewer();
  316. // Mesh IO
  317. bool load_mesh_from_file(const char* mesh_file_name);
  318. bool save_mesh_to_file(const char* mesh_file_name);
  319. // Callbacks
  320. bool key_down(unsigned char key, int modifier);
  321. bool key_up(unsigned char key, int modifier);
  322. bool mouse_down(MouseButton button, int modifier);
  323. bool mouse_up(MouseButton button, int modifier);
  324. bool mouse_move(int mouse_x, int mouse_y);
  325. bool mouse_scroll(float delta_y);
  326. // Scene IO
  327. bool load_scene();
  328. bool save_scene();
  329. // Determines how much to zoom and shift such that the mesh fills the unit
  330. // box (centered at the origin)
  331. static void get_scale_and_shift_to_fit_mesh(
  332. const Eigen::MatrixXd& V,
  333. const Eigen::MatrixXi& F,
  334. float & zoom,
  335. Eigen::Vector3f& shift);
  336. // Init opengl shaders and VBOs
  337. void init_opengl();
  338. void free_opengl();
  339. // Draw everything
  340. void draw();
  341. // OpenGL context resize
  342. void resize(int w, int h);
  343. // C-style callbacks
  344. bool (*callback_pre_draw)(Viewer& viewer);
  345. bool (*callback_post_draw)(Viewer& viewer);
  346. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  347. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  348. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  349. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  350. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  351. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  352. // Pointers to per-callback data
  353. void* callback_pre_draw_data;
  354. void* callback_post_draw_data;
  355. void* callback_mouse_down_data;
  356. void* callback_mouse_up_data;
  357. void* callback_mouse_move_data;
  358. void* callback_mouse_scroll_data;
  359. void* callback_key_down_data;
  360. void* callback_key_up_data;
  361. /********* AntTweakBar callbacks *********/
  362. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  363. static void TW_CALL save_scene_cb(void *clientData);
  364. static void TW_CALL load_scene_cb(void *clientData);
  365. static void TW_CALL open_dialog_mesh(void *clientData);
  366. static void TW_CALL align_camera_center_cb(void *clientData);
  367. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  368. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  369. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  370. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  371. public:
  372. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  373. };
  374. // Abstract class for plugins
  375. // All plugins MUST have this class as their parent and implement all the callbacks
  376. // For an example of a basic plugins see plugins/skeleton.h
  377. //
  378. // Return value of callbacks: returning true to any of the callbacks tells Preview3D that the event has been
  379. // handled and that it should not be passed to other plugins or to other internal functions of Preview3D
  380. class Viewer_plugin
  381. #ifdef ENABLE_XML_SERIALIZATION
  382. : public ::igl::XMLSerialization
  383. #endif
  384. {
  385. public:
  386. Viewer_plugin()
  387. #ifdef ENABLE_XML_SERIALIZATION
  388. : XMLSerialization("dummy")
  389. #endif
  390. {plugin_name = "dummy";};
  391. ~Viewer_plugin(){};
  392. // This function is called when the viewer is initialized (no mesh will be loaded at this stage)
  393. virtual void init(igl::Viewer *_viewer)
  394. {
  395. viewer = _viewer;
  396. }
  397. // This function is called before shutdown
  398. virtual void shutdown()
  399. {
  400. }
  401. // This function is called before a mesh is loaded
  402. virtual bool load(std::string filename)
  403. {
  404. return false;
  405. }
  406. // This function is called before a mesh is saved
  407. virtual bool save(std::string filename)
  408. {
  409. return false;
  410. }
  411. // Runs immediately after a new mesh had been loaded.
  412. virtual bool post_load()
  413. {
  414. return false;
  415. }
  416. // This function is called before the draw procedure of Preview3D
  417. virtual bool pre_draw()
  418. {
  419. return false;
  420. }
  421. // This function is called after the draw procedure of Preview3D
  422. virtual bool post_draw()
  423. {
  424. return false;
  425. }
  426. // This function is called when the mouse button is pressed
  427. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  428. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  429. virtual bool mouse_down(int button, int modifier)
  430. {
  431. return false;
  432. }
  433. // This function is called when the mouse button is released
  434. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  435. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  436. virtual bool mouse_up(int button, int modifier)
  437. {
  438. return false;
  439. }
  440. // This function is called every time the mouse is moved
  441. // - mouse_x and mouse_y are the new coordinates of the mouse pointer in screen coordinates
  442. virtual bool mouse_move(int mouse_x, int mouse_y)
  443. {
  444. return false;
  445. }
  446. // This function is called every time the scroll wheel is moved
  447. // Note: this callback is not working with every glut implementation
  448. virtual bool mouse_scroll(float delta_y)
  449. {
  450. return false;
  451. }
  452. // This function is called when a keyboard key is pressed
  453. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  454. virtual bool key_down(unsigned char key, int modifiers)
  455. {
  456. return false;
  457. }
  458. // This function is called when a keyboard key is release
  459. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  460. virtual bool key_up(unsigned char key, int modifiers)
  461. {
  462. return false;
  463. }
  464. // Priority of the plugin (use only positive numbers, negative are reserved for internal use)
  465. // The plugins will be initialized in increasing priority
  466. virtual int priority()
  467. {
  468. return 0;
  469. }
  470. std::string plugin_name;
  471. protected:
  472. // Pointer to the main Preview3D class
  473. Viewer *viewer;
  474. public:
  475. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  476. };
  477. // Keeps the lists of plugins
  478. class Plugin_manager
  479. {
  480. public:
  481. Plugin_manager() {}
  482. /** Registers a new plugin. A call to this function should be
  483. implemented in the constructor of all classes derived from PreviewPlugin. */
  484. bool register_plugin(Viewer_plugin* p)
  485. {
  486. auto it = plugin_list.begin();
  487. while(it != plugin_list.end() && (*it)->priority() < p->priority())
  488. ++it;
  489. plugin_list.insert(it,p);
  490. return true;
  491. }
  492. std::vector<Viewer_plugin*> plugin_list;
  493. public:
  494. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  495. };
  496. } // end namespace
  497. #ifndef IGL_STATIC_LIBRARY
  498. # include "Viewer.cpp"
  499. #endif
  500. #endif