Viewer.h 17 KB

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