ViewerCore.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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 "MeshGL.h"
  11. #include "../igl_inline.h"
  12. #include <Eigen/Geometry>
  13. #include <Eigen/Core>
  14. namespace igl
  15. {
  16. namespace opengl
  17. {
  18. // Forward declaration
  19. class ViewerData;
  20. /// Basic class of the 3D mesh viewer
  21. class ViewerCore
  22. {
  23. public:
  24. using GLuint = MeshGL::GLuint;
  25. IGL_INLINE ViewerCore();
  26. /// Initialization
  27. IGL_INLINE void init();
  28. /// Shutdown
  29. IGL_INLINE void shut();
  30. /// Serialization code
  31. IGL_INLINE void InitSerialization();
  32. /// Adjust the view to see the entire model
  33. /// @param[in] V #V by 3 list of vertex positions
  34. /// @param[in] F #F by 3 list of triangle indices into V
  35. IGL_INLINE void align_camera_center(
  36. const Eigen::MatrixXd& V,
  37. const Eigen::MatrixXi& F);
  38. /// \overload
  39. IGL_INLINE void align_camera_center(
  40. const Eigen::MatrixXd& V);
  41. /// Determines how much to zoom and shift such that the mesh fills the unit
  42. /// box (centered at the origin)
  43. /// @param[in] V #V by 3 list of vertex positions
  44. /// @param[in] F #F by 3 list of triangle indices into V
  45. /// @param[out] zoom zoom factor
  46. /// @param[out] shift 3d shift
  47. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  48. const Eigen::MatrixXd& V,
  49. const Eigen::MatrixXi& F,
  50. float & zoom,
  51. Eigen::Vector3f& shift);
  52. /// \overload
  53. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  54. const Eigen::MatrixXd& V,
  55. float & zoom,
  56. Eigen::Vector3f& shift);
  57. /// Clear the frame buffers
  58. IGL_INLINE void clear_framebuffers();
  59. /// Draw everything
  60. ///
  61. /// \note data cannot be const because it is being set to "clean"
  62. ///
  63. /// @param[in] data which ViewerData to draw
  64. /// @param[in] update_matrices whether to update view, proj, and norm
  65. /// matrices in shaders
  66. IGL_INLINE void draw(ViewerData& data, bool update_matrices = true);
  67. /// initialize shadow pass
  68. IGL_INLINE void initialize_shadow_pass();
  69. /// deinitialize shadow pass
  70. IGL_INLINE void deinitialize_shadow_pass();
  71. /// Draw everything to shadow map
  72. /// @param[in] data which ViewerData to draw
  73. /// @param[in] update_matrices whether to update view, proj, and norm
  74. IGL_INLINE void draw_shadow_pass(ViewerData& data, bool update_matrices = true);
  75. /// Render given ViewerData to a buffer. The width and height are determined by
  76. /// non-zeros dimensions of R (and G,B,A should match) or – if both are zero —
  77. /// are set to this core's viewport sizes.
  78. ///
  79. /// @param[in] data which ViewerData to draw
  80. /// @param[in] update_matrices whether to update view, proj, and norm matrices in
  81. /// shaders
  82. /// @param[out] R width by height red pixel color values
  83. /// @param[out] G width by height green pixel color values
  84. /// @param[out] B width by height blue pixel color values
  85. /// @param[out] A width by height alpha pixel color values
  86. ///
  87. IGL_INLINE void draw_buffer(
  88. ViewerData& data,
  89. bool update_matrices,
  90. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  91. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  92. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  93. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  94. /// Draw the text lables
  95. /// @param[in] data which ViewerData to draw
  96. /// @param[in] labels text labels to draw
  97. IGL_INLINE void draw_labels(
  98. ViewerData& data,
  99. const igl::opengl::MeshGL::TextGL& labels
  100. );
  101. /// Type of user interface for changing the view rotation based on the mouse
  102. /// draggin.
  103. enum RotationType
  104. {
  105. /// Typical trackball rotation (like Meshlab)
  106. ROTATION_TYPE_TRACKBALL = 0,
  107. /// Fixed up rotation (like Blender, Maya, etc.)
  108. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  109. /// No rotation suitable for 2D
  110. ROTATION_TYPE_NO_ROTATION = 2,
  111. /// Total number of rotation types
  112. NUM_ROTATION_TYPES = 3
  113. };
  114. /// Set the current rotation type
  115. /// @param[in] value the new rotation type
  116. IGL_INLINE void set_rotation_type(const RotationType & value);
  117. /// Set a ViewerData visualization option for this viewport
  118. /// @param[in] property_mask a bit mask of visualization option
  119. /// @param[in] value whether to set or unset the property
  120. IGL_INLINE void set(unsigned int &property_mask, bool value = true) const;
  121. /// Unset a ViewerData visualization option for this viewport
  122. /// @param[in] property_mask a bit mask of visualization option
  123. IGL_INLINE void unset(unsigned int &property_mask) const;
  124. /// Toggle a ViewerData visualization option for this viewport
  125. /// @param[in] property_mask a bit mask of visualization option
  126. IGL_INLINE void toggle(unsigned int &property_mask) const;
  127. /// Check whether a ViewerData visualization option is set for this viewport
  128. /// @param[in] property_mask a bit mask of visualization option
  129. /// @returns whether the property is set
  130. IGL_INLINE bool is_set(unsigned int property_mask) const;
  131. /// delete the shadow buffers
  132. IGL_INLINE void delete_shadow_buffers();
  133. /// generate the shadow buffers
  134. IGL_INLINE void generate_shadow_buffers();
  135. // ------------------- Properties
  136. /// Unique identifier
  137. unsigned int id = 1u;
  138. /// Background color as RGBA
  139. Eigen::Vector4f background_color;
  140. /// Light position (or direction to light)
  141. Eigen::Vector3f light_position;
  142. /// Whether to treat `light_position` as a point or direction
  143. bool is_directional_light;
  144. /// Whether shadow mapping is on
  145. bool is_shadow_mapping;
  146. /// Width of the shadow map
  147. GLuint shadow_width;
  148. /// Height of the shadow map
  149. GLuint shadow_height;
  150. /// Shadow map depth texture
  151. GLuint shadow_depth_tex;
  152. /// Shadow map depth framebuffer object
  153. GLuint shadow_depth_fbo;
  154. /// Shadow map color render buffer object
  155. GLuint shadow_color_rbo;
  156. /// Factor of lighting (0: no lighting, 1: full lighting)
  157. float lighting_factor;
  158. /// Type of rotation interaction
  159. RotationType rotation_type;
  160. /// View rotation as quaternion
  161. Eigen::Quaternionf trackball_angle;
  162. /// Base zoom of camera
  163. float camera_base_zoom;
  164. /// Current zoom of camera
  165. float camera_zoom;
  166. /// Whether camera is orthographic (or perspective)
  167. bool orthographic;
  168. /// Base translation of camera
  169. Eigen::Vector3f camera_base_translation;
  170. /// Current translation of camera
  171. Eigen::Vector3f camera_translation;
  172. /// Current "eye" / origin position of camera
  173. Eigen::Vector3f camera_eye;
  174. /// Current "up" vector of camera
  175. Eigen::Vector3f camera_up;
  176. /// Current "look at" position of camera
  177. Eigen::Vector3f camera_center;
  178. /// Current view angle of camera
  179. float camera_view_angle;
  180. /// Near plane of camera
  181. float camera_dnear;
  182. /// Far plane of camera
  183. float camera_dfar;
  184. /// Whether testing for depth is enabled
  185. bool depth_test;
  186. /// Whether "animating" (continuous drawing) is enabled
  187. bool is_animating;
  188. /// Max fps of animation loop (e.g. 30fps or 60fps)
  189. double animation_max_fps;
  190. /// Caches the two-norm between the min/max point of the bounding box
  191. float object_scale;
  192. /// Viewport size
  193. Eigen::Vector4f viewport;
  194. /// OpenGL view transformation matrix on last render pass
  195. Eigen::Matrix4f view;
  196. /// OpenGL proj transformation matrix on last render pass
  197. Eigen::Matrix4f proj;
  198. /// OpenGL norm transformation matrix on last render pass
  199. Eigen::Matrix4f norm;
  200. /// OpenGL shadow_view transformation matrix on last render pass
  201. Eigen::Matrix4f shadow_view;
  202. /// OpenGL shadow_proj transformation matrix on last render pass
  203. Eigen::Matrix4f shadow_proj;
  204. public:
  205. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  206. };
  207. }
  208. }
  209. #include "../serialize.h"
  210. namespace igl {
  211. namespace serialization {
  212. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  213. {
  214. SERIALIZE_MEMBER(background_color);
  215. SERIALIZE_MEMBER(light_position);
  216. SERIALIZE_MEMBER(lighting_factor);
  217. SERIALIZE_MEMBER(trackball_angle);
  218. SERIALIZE_MEMBER(rotation_type);
  219. SERIALIZE_MEMBER(camera_base_zoom);
  220. SERIALIZE_MEMBER(camera_zoom);
  221. SERIALIZE_MEMBER(orthographic);
  222. SERIALIZE_MEMBER(camera_base_translation);
  223. SERIALIZE_MEMBER(camera_translation);
  224. SERIALIZE_MEMBER(camera_view_angle);
  225. SERIALIZE_MEMBER(camera_dnear);
  226. SERIALIZE_MEMBER(camera_dfar);
  227. SERIALIZE_MEMBER(camera_eye);
  228. SERIALIZE_MEMBER(camera_center);
  229. SERIALIZE_MEMBER(camera_up);
  230. SERIALIZE_MEMBER(depth_test);
  231. SERIALIZE_MEMBER(is_animating);
  232. SERIALIZE_MEMBER(animation_max_fps);
  233. SERIALIZE_MEMBER(object_scale);
  234. SERIALIZE_MEMBER(viewport);
  235. SERIALIZE_MEMBER(view);
  236. SERIALIZE_MEMBER(proj);
  237. SERIALIZE_MEMBER(norm);
  238. }
  239. template<>
  240. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  241. {
  242. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  243. }
  244. template<>
  245. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  246. {
  247. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  248. }
  249. }
  250. }
  251. #ifndef IGL_STATIC_LIBRARY
  252. # include "ViewerCore.cpp"
  253. #endif
  254. #endif