Viewer.h 18 KB

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