ViewerCore.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_OPENGL_VIEWERCORE_H
  9. #define IGL_OPENGL_VIEWERCORE_H
  10. #include <igl/opengl/State.h>
  11. #ifdef IGL_VIEWER_WITH_NANOGUI
  12. #include <igl/opengl/glfw/TextRenderer.h>
  13. #endif
  14. #include <igl/ViewerData.h>
  15. #include <igl/opengl/State.h>
  16. #include <igl/igl_inline.h>
  17. #include <Eigen/Geometry>
  18. #include <Eigen/Core>
  19. namespace igl
  20. {
  21. namespace opengl
  22. {
  23. // Basic class of the 3D mesh viewer
  24. // TODO: write documentation
  25. class ViewerCore
  26. {
  27. public:
  28. IGL_INLINE ViewerCore();
  29. // Initialization
  30. IGL_INLINE void init();
  31. // Shutdown
  32. IGL_INLINE void shut();
  33. // Serialization code
  34. IGL_INLINE void InitSerialization();
  35. // ------------------- Camera control functions
  36. // Adjust the view to see the entire model
  37. IGL_INLINE void align_camera_center(
  38. const Eigen::MatrixXd& V,
  39. const Eigen::MatrixXi& F);
  40. // Determines how much to zoom and shift such that the mesh fills the unit
  41. // box (centered at the origin)
  42. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  43. const Eigen::MatrixXd& V,
  44. const Eigen::MatrixXi& F,
  45. float & zoom,
  46. Eigen::Vector3f& shift);
  47. // Adjust the view to see the entire model
  48. IGL_INLINE void align_camera_center(
  49. const Eigen::MatrixXd& V);
  50. // Determines how much to zoom and shift such that the mesh fills the unit
  51. // box (centered at the origin)
  52. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  53. const Eigen::MatrixXd& V,
  54. float & zoom,
  55. Eigen::Vector3f& shift);
  56. // ------------------- Drawing functions
  57. // Clear the frame buffers
  58. IGL_INLINE void clear_framebuffers();
  59. // Draw everything
  60. //
  61. // data cannot be const because it is being set to "clean"
  62. IGL_INLINE void draw(ViewerData& data, State& opengl, bool update_matrices = true);
  63. IGL_INLINE void draw_buffer(
  64. ViewerData& data,
  65. State& opengl,
  66. bool update_matrices,
  67. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  68. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  69. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  70. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  71. // Trackball angle (quaternion)
  72. enum RotationType
  73. {
  74. ROTATION_TYPE_TRACKBALL = 0,
  75. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  76. NUM_ROTATION_TYPES = 2
  77. };
  78. IGL_INLINE void set_rotation_type(const RotationType & value);
  79. // ------------------- Properties
  80. #ifdef IGL_VIEWER_WITH_NANOGUI
  81. // Text rendering helper
  82. TextRenderer textrenderer;
  83. #endif
  84. // Shape material
  85. float shininess;
  86. // Colors
  87. Eigen::Vector4f background_color;
  88. Eigen::Vector4f line_color;
  89. // Lighting
  90. Eigen::Vector3f light_position;
  91. float lighting_factor;
  92. RotationType rotation_type;
  93. Eigen::Quaternionf trackball_angle;
  94. // Model viewing parameters
  95. float model_zoom;
  96. Eigen::Vector3f model_translation;
  97. // Model viewing paramters (uv coordinates)
  98. float model_zoom_uv;
  99. Eigen::Vector3f model_translation_uv;
  100. // Camera parameters
  101. float camera_zoom;
  102. bool orthographic;
  103. Eigen::Vector3f camera_eye;
  104. Eigen::Vector3f camera_up;
  105. Eigen::Vector3f camera_center;
  106. float camera_view_angle;
  107. float camera_dnear;
  108. float camera_dfar;
  109. // Visualization options
  110. bool show_overlay;
  111. bool show_overlay_depth;
  112. bool show_texture;
  113. bool show_faces;
  114. bool show_lines;
  115. bool show_vertid;
  116. bool show_faceid;
  117. bool invert_normals;
  118. bool depth_test;
  119. // Point size / line width
  120. float point_size;
  121. float line_width;
  122. // Animation
  123. bool is_animating;
  124. double animation_max_fps;
  125. // Caches the two-norm between the min/max point of the bounding box
  126. float object_scale;
  127. // Viewport size
  128. Eigen::Vector4f viewport;
  129. // Save the OpenGL transformation matrices used for the previous rendering pass
  130. Eigen::Matrix4f view;
  131. Eigen::Matrix4f model;
  132. Eigen::Matrix4f proj;
  133. public:
  134. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  135. };
  136. }
  137. }
  138. // Alec: Is this the best place for this?
  139. #ifdef ENABLE_SERIALIZATION
  140. #include <igl/serialize.h>
  141. namespace igl {
  142. namespace serialization {
  143. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  144. {
  145. SERIALIZE_MEMBER(shininess);
  146. SERIALIZE_MEMBER(background_color);
  147. SERIALIZE_MEMBER(line_color);
  148. SERIALIZE_MEMBER(light_position);
  149. SERIALIZE_MEMBER(lighting_factor);
  150. SERIALIZE_MEMBER(trackball_angle);
  151. SERIALIZE_MEMBER(rotation_type);
  152. SERIALIZE_MEMBER(model_zoom);
  153. SERIALIZE_MEMBER(model_translation);
  154. SERIALIZE_MEMBER(model_zoom_uv);
  155. SERIALIZE_MEMBER(model_translation_uv);
  156. SERIALIZE_MEMBER(camera_zoom);
  157. SERIALIZE_MEMBER(orthographic);
  158. SERIALIZE_MEMBER(camera_view_angle);
  159. SERIALIZE_MEMBER(camera_dnear);
  160. SERIALIZE_MEMBER(camera_dfar);
  161. SERIALIZE_MEMBER(camera_eye);
  162. SERIALIZE_MEMBER(camera_center);
  163. SERIALIZE_MEMBER(camera_up);
  164. SERIALIZE_MEMBER(show_faces);
  165. SERIALIZE_MEMBER(show_lines);
  166. SERIALIZE_MEMBER(invert_normals);
  167. SERIALIZE_MEMBER(show_overlay);
  168. SERIALIZE_MEMBER(show_overlay_depth);
  169. SERIALIZE_MEMBER(show_vertid);
  170. SERIALIZE_MEMBER(show_faceid);
  171. SERIALIZE_MEMBER(show_texture);
  172. SERIALIZE_MEMBER(depth_test);
  173. SERIALIZE_MEMBER(point_size);
  174. SERIALIZE_MEMBER(line_width);
  175. SERIALIZE_MEMBER(is_animating);
  176. SERIALIZE_MEMBER(animation_max_fps);
  177. SERIALIZE_MEMBER(object_scale);
  178. SERIALIZE_MEMBER(viewport);
  179. SERIALIZE_MEMBER(view);
  180. SERIALIZE_MEMBER(model);
  181. SERIALIZE_MEMBER(proj);
  182. }
  183. template<>
  184. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  185. {
  186. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  187. }
  188. template<>
  189. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  190. {
  191. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  192. }
  193. }
  194. }
  195. #endif
  196. #ifndef IGL_STATIC_LIBRARY
  197. # include "ViewerCore.cpp"
  198. #endif
  199. #endif