Viewer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. namespace igl
  20. {
  21. // Forward declaration of the viewer_plugin class
  22. class Viewer_plugin;
  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. enum DirtyFlags
  84. {
  85. DIRTY_NONE = 0x0000,
  86. DIRTY_POSITION = 0x0001,
  87. DIRTY_UV = 0x0002,
  88. DIRTY_NORMAL = 0x0004,
  89. DIRTY_AMBIENT = 0x0008,
  90. DIRTY_DIFFUSE = 0x0010,
  91. DIRTY_SPECULAR = 0x0020,
  92. DIRTY_TEXTURE = 0x0040,
  93. DIRTY_FACE = 0x0080,
  94. DIRTY_MESH = 0x00FF,
  95. DIRTY_OVERLAY_LINES = 0x0100,
  96. DIRTY_OVERLAY_POINTS = 0x0200,
  97. DIRTY_ALL = 0x03FF
  98. };
  99. class OpenGL_state
  100. {
  101. public:
  102. typedef unsigned int GLuint;
  103. GLuint vao_mesh;
  104. GLuint vao_overlay_lines;
  105. GLuint vao_overlay_points;
  106. OpenGL_shader shader_mesh;
  107. OpenGL_shader shader_overlay_lines;
  108. OpenGL_shader shader_overlay_points;
  109. GLuint vbo_V; // Vertices of the current mesh (#V x 3)
  110. GLuint vbo_V_uv; // UV coordinates for the current mesh (#V x 2)
  111. GLuint vbo_V_normals; // Vertices of the current mesh (#V x 3)
  112. GLuint vbo_V_ambient; // Ambient material (#V x 3)
  113. GLuint vbo_V_diffuse; // Diffuse material (#V x 3)
  114. GLuint vbo_V_specular; // Specular material (#V x 3)
  115. GLuint vbo_F; // Faces of the mesh (#F x 3)
  116. GLuint vbo_tex; // Texture
  117. GLuint vbo_lines_F; // Indices of the line overlay
  118. GLuint vbo_lines_V; // Vertices of the line overlay
  119. GLuint vbo_lines_V_colors; // Color values of the line overlay
  120. GLuint vbo_points_F; // Indices of the point overlay
  121. GLuint vbo_points_V; // Vertices of the point overlay
  122. GLuint vbo_points_V_colors; // Color values of the point overlay
  123. // Temporary copy of the content of each VBO
  124. Eigen::MatrixXf V_vbo;
  125. Eigen::MatrixXf V_normals_vbo;
  126. Eigen::MatrixXf V_ambient_vbo;
  127. Eigen::MatrixXf V_diffuse_vbo;
  128. Eigen::MatrixXf V_specular_vbo;
  129. Eigen::MatrixXf V_uv_vbo;
  130. Eigen::MatrixXf lines_V_vbo;
  131. Eigen::MatrixXf lines_V_colors_vbo;
  132. Eigen::MatrixXf points_V_vbo;
  133. Eigen::MatrixXf points_V_colors_vbo;
  134. int tex_u;
  135. int tex_v;
  136. Eigen::Matrix<char,Eigen::Dynamic,1> tex;
  137. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> F_vbo;
  138. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> lines_F_vbo;
  139. Eigen::Matrix<unsigned, Eigen::Dynamic, Eigen::Dynamic> points_F_vbo;
  140. // Marks dirty buffers that need to be uploaded to OpenGL
  141. uint32_t dirty;
  142. // Create a new set of OpenGL buffer objects
  143. void init();
  144. // Update contents from a 'Data' instance
  145. void set_data(const igl::ViewerData &data, bool face_based, bool invert_normals);
  146. // Bind the underlying OpenGL buffer objects for subsequent mesh draw calls
  147. void bind_mesh();
  148. /// Draw the currently buffered mesh (either solid or wireframe)
  149. void draw_mesh(bool solid);
  150. // Bind the underlying OpenGL buffer objects for subsequent line overlay draw calls
  151. void bind_overlay_lines();
  152. /// Draw the currently buffered line overlay
  153. void draw_overlay_lines();
  154. // Bind the underlying OpenGL buffer objects for subsequent point overlay draw calls
  155. void bind_overlay_points();
  156. /// Draw the currently buffered point overlay
  157. void draw_overlay_points();
  158. // Release the OpenGL buffer objects
  159. void free();
  160. };
  161. // Stores all the viewing options
  162. Options options;
  163. // Stores all the data that should be visualized
  164. igl::ViewerData data;
  165. // Stores the vbos indices and opengl related settings
  166. OpenGL_state opengl;
  167. // List of registered plugins
  168. std::vector<Viewer_plugin*> plugins;
  169. void init_plugins();
  170. void shutdown_plugins();
  171. // Temporary data stored when the mouse button is pressed
  172. Eigen::Vector4f down_rotation;
  173. int current_mouse_x;
  174. int current_mouse_y;
  175. int down_mouse_x;
  176. int down_mouse_y;
  177. float down_mouse_z;
  178. Eigen::Vector3f down_translation;
  179. bool down;
  180. bool hack_never_moved;
  181. // Anttweak bar
  182. TwBar* bar;
  183. // Window size
  184. int width;
  185. int height;
  186. // Keep track of the global position of the scrollwheel
  187. float scroll_position;
  188. // Useful functions
  189. void compute_normals(); // Computes the normals of the mesh
  190. void uniform_colors(Eigen::Vector3d ambient, Eigen::Vector3d diffuse, Eigen::Vector3d specular); // assign uniform colors to all faces/vertices
  191. void grid_texture(); // Generate a default grid texture
  192. void clear_mesh(); // Clear the mesh data
  193. void align_camera_center(); // Adjust the view to see the entire model
  194. // Change the visualization mode, invalidating the cache if necessary
  195. void set_face_based(bool newvalue);
  196. // Helpers that can draw the most common meshes
  197. void set_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F);
  198. void set_vertices(const Eigen::MatrixXd& V);
  199. void set_normals(const Eigen::MatrixXd& N);
  200. // Set the color of the mesh
  201. //
  202. // Inputs:
  203. // C #V|#F|1 by 3 list of colors
  204. void set_colors(const Eigen::MatrixXd &C);
  205. void set_uv(const Eigen::MatrixXd& UV);
  206. void set_uv(const Eigen::MatrixXd& UV_V, const Eigen::MatrixXi& UV_F);
  207. void set_texture(
  208. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& R,
  209. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& G,
  210. const Eigen::Matrix<char,Eigen::Dynamic,Eigen::Dynamic>& B);
  211. void add_points(const Eigen::MatrixXd& P, const Eigen::MatrixXd& C);
  212. // Sets edges given a list of edge vertices and edge indices. In constrast
  213. // to `add_edges` this will (purposefully) clober existing edges.
  214. //
  215. // Inputs:
  216. // P #P by 3 list of vertex positions
  217. // E #E by 2 list of edge indices into P
  218. // C #E|1 by 3 color(s)
  219. void set_edges (const Eigen::MatrixXd& P, const Eigen::MatrixXi& E, const Eigen::MatrixXd& C);
  220. void add_edges (const Eigen::MatrixXd& P1, const Eigen::MatrixXd& P2, const Eigen::MatrixXd& C);
  221. void add_label (const Eigen::VectorXd& P, const std::string& str);
  222. // Save the OpenGL transformation matrices used for the previous rendering pass
  223. Eigen::Matrix4f view;
  224. Eigen::Matrix4f model;
  225. Eigen::Matrix4f proj;
  226. Eigen::Vector4f viewport;
  227. // UI Enumerations
  228. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  229. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  230. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  231. Viewer();
  232. ~Viewer();
  233. // Mesh IO
  234. bool load_mesh_from_file(const char* mesh_file_name);
  235. bool save_mesh_to_file(const char* mesh_file_name);
  236. // Callbacks
  237. bool key_down(unsigned char key, int modifier);
  238. bool key_up(unsigned char key, int modifier);
  239. bool mouse_down(MouseButton button, int modifier);
  240. bool mouse_up(MouseButton button, int modifier);
  241. bool mouse_move(int mouse_x, int mouse_y);
  242. bool mouse_scroll(float delta_y);
  243. // Scene IO
  244. bool load_scene();
  245. bool save_scene();
  246. // Determines how much to zoom and shift such that the mesh fills the unit
  247. // box (centered at the origin)
  248. static void get_scale_and_shift_to_fit_mesh(
  249. const Eigen::MatrixXd& V,
  250. const Eigen::MatrixXi& F,
  251. float & zoom,
  252. Eigen::Vector3f& shift);
  253. // Init opengl shaders and VBOs
  254. void init_opengl();
  255. void free_opengl();
  256. // Draw everything
  257. void draw();
  258. // OpenGL context resize
  259. void resize(int w, int h);
  260. // C-style callbacks
  261. bool (*callback_pre_draw)(Viewer& viewer);
  262. bool (*callback_post_draw)(Viewer& viewer);
  263. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  264. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  265. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  266. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  267. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  268. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  269. // Pointers to per-callback data
  270. void* callback_pre_draw_data;
  271. void* callback_post_draw_data;
  272. void* callback_mouse_down_data;
  273. void* callback_mouse_up_data;
  274. void* callback_mouse_move_data;
  275. void* callback_mouse_scroll_data;
  276. void* callback_key_down_data;
  277. void* callback_key_up_data;
  278. /********* AntTweakBar callbacks *********/
  279. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  280. static void TW_CALL save_scene_cb(void *clientData);
  281. static void TW_CALL load_scene_cb(void *clientData);
  282. static void TW_CALL open_dialog_mesh(void *clientData);
  283. static void TW_CALL align_camera_center_cb(void *clientData);
  284. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  285. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  286. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  287. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  288. public:
  289. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  290. };
  291. // Abstract class for plugins
  292. // All plugins MUST have this class as their parent and implement all the callbacks
  293. // For an example of a basic plugins see plugins/skeleton.h
  294. //
  295. // Return value of callbacks: returning true to any of the callbacks tells Preview3D that the event has been
  296. // handled and that it should not be passed to other plugins or to other internal functions of Preview3D
  297. class Viewer_plugin
  298. #ifdef ENABLE_XML_SERIALIZATION
  299. : public ::igl::XMLSerialization
  300. #endif
  301. {
  302. public:
  303. Viewer_plugin()
  304. #ifdef ENABLE_XML_SERIALIZATION
  305. : XMLSerialization("dummy")
  306. #endif
  307. {plugin_name = "dummy";};
  308. ~Viewer_plugin(){};
  309. // This function is called when the viewer is initialized (no mesh will be loaded at this stage)
  310. virtual void init(igl::Viewer *_viewer)
  311. {
  312. viewer = _viewer;
  313. }
  314. // This function is called before shutdown
  315. virtual void shutdown()
  316. {
  317. }
  318. // This function is called before a mesh is loaded
  319. virtual bool load(std::string filename)
  320. {
  321. return false;
  322. }
  323. // This function is called before a mesh is saved
  324. virtual bool save(std::string filename)
  325. {
  326. return false;
  327. }
  328. // Runs immediately after a new mesh had been loaded.
  329. virtual bool post_load()
  330. {
  331. return false;
  332. }
  333. // This function is called before the draw procedure of Preview3D
  334. virtual bool pre_draw()
  335. {
  336. return false;
  337. }
  338. // This function is called after the draw procedure of Preview3D
  339. virtual bool post_draw()
  340. {
  341. return false;
  342. }
  343. // This function is called when the mouse button is pressed
  344. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  345. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  346. virtual bool mouse_down(int button, int modifier)
  347. {
  348. return false;
  349. }
  350. // This function is called when the mouse button is released
  351. // - button can be GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON
  352. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  353. virtual bool mouse_up(int button, int modifier)
  354. {
  355. return false;
  356. }
  357. // This function is called every time the mouse is moved
  358. // - mouse_x and mouse_y are the new coordinates of the mouse pointer in screen coordinates
  359. virtual bool mouse_move(int mouse_x, int mouse_y)
  360. {
  361. return false;
  362. }
  363. // This function is called every time the scroll wheel is moved
  364. // Note: this callback is not working with every glut implementation
  365. virtual bool mouse_scroll(float delta_y)
  366. {
  367. return false;
  368. }
  369. // This function is called when a keyboard key is pressed
  370. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  371. virtual bool key_down(unsigned char key, int modifiers)
  372. {
  373. return false;
  374. }
  375. // This function is called when a keyboard key is release
  376. // - modifiers is a bitfield that might one or more of the following bits Preview3D::NO_KEY, Preview3D::SHIFT, Preview3D::CTRL, Preview3D::ALT;
  377. virtual bool key_up(unsigned char key, int modifiers)
  378. {
  379. return false;
  380. }
  381. // Priority of the plugin (use only positive numbers, negative are reserved for internal use)
  382. // The plugins will be initialized in increasing priority
  383. virtual int priority()
  384. {
  385. return 0;
  386. }
  387. std::string plugin_name;
  388. protected:
  389. // Pointer to the main Preview3D class
  390. Viewer *viewer;
  391. public:
  392. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  393. };
  394. } // end namespace
  395. #ifndef IGL_STATIC_LIBRARY
  396. # include "Viewer.cpp"
  397. #endif
  398. #endif