Viewer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_VIEWER_H
  9. #define IGL_VIEWER_H
  10. #include <AntTweakBar.h>
  11. #include <vector>
  12. #include <string>
  13. #include <cstdint>
  14. #define IGL_MOD_SHIFT 0x0001
  15. #define IGL_MOD_CONTROL 0x0002
  16. #define IGL_MOD_ALT 0x0004
  17. #define IGL_MOD_SUPER 0x0008
  18. #ifdef ENABLE_XML_SERIALIZATION
  19. #include <igl/xml/XMLSerializer.h>
  20. #include <igl/xml/XMLSerialization.h>
  21. #endif
  22. #include <Eigen/Core>
  23. #include <Eigen/Geometry>
  24. #include <igl/viewer/OpenGL_shader.h>
  25. #include <igl/viewer/ViewerData.h>
  26. #include <igl/viewer/OpenGL_state.h>
  27. #include <igl/viewer/ViewerPlugin.h>
  28. #include <igl/viewer/ViewerCore.h>
  29. namespace igl
  30. {
  31. // GLFW-based mesh viewer
  32. class Viewer
  33. {
  34. public:
  35. int launch(std::string filename = "");
  36. void init();
  37. // Stores all the viewing options
  38. igl::ViewerCore core;
  39. // Stores all the data that should be visualized
  40. igl::ViewerData data;
  41. // Stores the vbos indices and opengl related settings
  42. igl::OpenGL_state opengl;
  43. // List of registered plugins
  44. std::vector<ViewerPlugin*> plugins;
  45. void init_plugins();
  46. void shutdown_plugins();
  47. // Temporary data stored when the mouse button is pressed
  48. Eigen::Vector4f down_rotation;
  49. int current_mouse_x;
  50. int current_mouse_y;
  51. int down_mouse_x;
  52. int down_mouse_y;
  53. float down_mouse_z;
  54. Eigen::Vector3f down_translation;
  55. bool down;
  56. bool hack_never_moved;
  57. // Anttweak bar
  58. TwBar* bar;
  59. // Keep track of the global position of the scrollwheel
  60. float scroll_position;
  61. // UI Enumerations
  62. enum MouseButton {IGL_LEFT, IGL_MIDDLE, IGL_RIGHT};
  63. enum MouseMode { NOTHING, ROTATION, ZOOM, PAN, TRANSLATE} mouse_mode;
  64. enum KeyModifier { NO_KEY = TW_KMOD_NONE, SHIFT = TW_KMOD_SHIFT, CTRL =TW_KMOD_CTRL, ALT = TW_KMOD_ALT } key_modifier;
  65. Viewer();
  66. ~Viewer();
  67. // Mesh IO
  68. bool load_mesh_from_file(const char* mesh_file_name);
  69. bool save_mesh_to_file(const char* mesh_file_name);
  70. // Callbacks
  71. bool key_down(unsigned char key, int modifier);
  72. bool key_up(unsigned char key, int modifier);
  73. bool mouse_down(MouseButton button, int modifier);
  74. bool mouse_up(MouseButton button, int modifier);
  75. bool mouse_move(int mouse_x, int mouse_y);
  76. bool mouse_scroll(float delta_y);
  77. // Scene IO
  78. bool load_scene();
  79. bool save_scene();
  80. // Draw everything
  81. void draw();
  82. // OpenGL context resize
  83. void resize(int w, int h);
  84. // C-style callbacks
  85. bool (*callback_pre_draw)(Viewer& viewer);
  86. bool (*callback_post_draw)(Viewer& viewer);
  87. bool (*callback_mouse_down)(Viewer& viewer, int button, int modifier);
  88. bool (*callback_mouse_up)(Viewer& viewer, int button, int modifier);
  89. bool (*callback_mouse_move)(Viewer& viewer, int mouse_x, int mouse_y);
  90. bool (*callback_mouse_scroll)(Viewer& viewer, float delta_y);
  91. bool (*callback_key_down)(Viewer& viewer, unsigned char key, int modifiers);
  92. bool (*callback_key_up)(Viewer& viewer, unsigned char key, int modifiers);
  93. // Pointers to per-callback data
  94. void* callback_pre_draw_data;
  95. void* callback_post_draw_data;
  96. void* callback_mouse_down_data;
  97. void* callback_mouse_up_data;
  98. void* callback_mouse_move_data;
  99. void* callback_mouse_scroll_data;
  100. void* callback_key_down_data;
  101. void* callback_key_up_data;
  102. /********* AntTweakBar callbacks *********/
  103. static void TW_CALL snap_to_canonical_quaternion_cb(void *clientData);
  104. static void TW_CALL save_scene_cb(void *clientData);
  105. static void TW_CALL load_scene_cb(void *clientData);
  106. static void TW_CALL open_dialog_mesh(void *clientData);
  107. static void TW_CALL align_camera_center_cb(void *clientData);
  108. static void TW_CALL set_face_based_cb(const void *param, void *clientData);
  109. static void TW_CALL get_face_based_cb(void *param, void *clientData);
  110. static void TW_CALL set_invert_normals_cb(const void *param, void *clientData);
  111. static void TW_CALL get_invert_normals_cb(void *param, void *clientData);
  112. public:
  113. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  114. };
  115. } // end namespace
  116. #ifndef IGL_STATIC_LIBRARY
  117. # include "Viewer.cpp"
  118. #endif
  119. #endif