ViewerCore.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/MeshGL.h>
  11. #include <igl/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. // TODO: write documentation
  22. class ViewerCore
  23. {
  24. public:
  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. // ------------------- Camera control functions
  33. // Adjust the view to see the entire model
  34. IGL_INLINE void align_camera_center(
  35. const Eigen::MatrixXd& V,
  36. const Eigen::MatrixXi& F);
  37. // Determines how much to zoom and shift such that the mesh fills the unit
  38. // box (centered at the origin)
  39. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  40. const Eigen::MatrixXd& V,
  41. const Eigen::MatrixXi& F,
  42. float & zoom,
  43. Eigen::Vector3f& shift);
  44. // Adjust the view to see the entire model
  45. IGL_INLINE void align_camera_center(
  46. const Eigen::MatrixXd& V);
  47. // Determines how much to zoom and shift such that the mesh fills the unit
  48. // box (centered at the origin)
  49. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  50. const Eigen::MatrixXd& V,
  51. float & zoom,
  52. Eigen::Vector3f& shift);
  53. // ------------------- Drawing functions
  54. // Clear the frame buffers
  55. IGL_INLINE void clear_framebuffers();
  56. // Draw everything
  57. //
  58. // data cannot be const because it is being set to "clean"
  59. IGL_INLINE void draw(ViewerData& data, bool update_matrices = true);
  60. IGL_INLINE void draw_buffer(
  61. ViewerData& data,
  62. bool update_matrices,
  63. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  64. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  65. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  66. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  67. // Trackball angle (quaternion)
  68. enum RotationType
  69. {
  70. ROTATION_TYPE_TRACKBALL = 0,
  71. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  72. ROTATION_TYPE_NO_ROTATION = 2,
  73. NUM_ROTATION_TYPES = 3
  74. };
  75. IGL_INLINE void set_rotation_type(const RotationType & value);
  76. // ------------------- Option helpers
  77. // Set a ViewerData visualization option for this viewport
  78. IGL_INLINE void set(unsigned int &property_mask, bool value = true) const;
  79. // Unset a ViewerData visualization option for this viewport
  80. IGL_INLINE void unset(unsigned int &property_mask) const;
  81. // Toggle a ViewerData visualization option for this viewport
  82. IGL_INLINE void toggle(unsigned int &property_mask) const;
  83. // Check whether a ViewerData visualization option is set for this viewport
  84. IGL_INLINE bool is_set(unsigned int property_mask) const;
  85. // ------------------- Properties
  86. // Unique identifier
  87. unsigned int id = 1u;
  88. // Colors
  89. Eigen::Vector4f background_color;
  90. // Lighting
  91. Eigen::Vector3f light_position;
  92. float lighting_factor;
  93. RotationType rotation_type;
  94. Eigen::Quaternionf trackball_angle;
  95. // Camera parameters
  96. float camera_base_zoom;
  97. float camera_zoom;
  98. bool orthographic;
  99. Eigen::Vector3f camera_base_translation;
  100. Eigen::Vector3f camera_translation;
  101. Eigen::Vector3f camera_eye;
  102. Eigen::Vector3f camera_up;
  103. Eigen::Vector3f camera_center;
  104. float camera_view_angle;
  105. float camera_dnear;
  106. float camera_dfar;
  107. bool depth_test;
  108. // Animation
  109. bool is_animating;
  110. double animation_max_fps;
  111. // Caches the two-norm between the min/max point of the bounding box
  112. float object_scale;
  113. // Viewport size
  114. Eigen::Vector4f viewport;
  115. // Save the OpenGL transformation matrices used for the previous rendering pass
  116. Eigen::Matrix4f view;
  117. Eigen::Matrix4f proj;
  118. Eigen::Matrix4f norm;
  119. public:
  120. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  121. };
  122. }
  123. }
  124. #include <igl/serialize.h>
  125. namespace igl {
  126. namespace serialization {
  127. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  128. {
  129. SERIALIZE_MEMBER(background_color);
  130. SERIALIZE_MEMBER(light_position);
  131. SERIALIZE_MEMBER(lighting_factor);
  132. SERIALIZE_MEMBER(trackball_angle);
  133. SERIALIZE_MEMBER(rotation_type);
  134. SERIALIZE_MEMBER(camera_base_zoom);
  135. SERIALIZE_MEMBER(camera_zoom);
  136. SERIALIZE_MEMBER(orthographic);
  137. SERIALIZE_MEMBER(camera_base_translation);
  138. SERIALIZE_MEMBER(camera_translation);
  139. SERIALIZE_MEMBER(camera_view_angle);
  140. SERIALIZE_MEMBER(camera_dnear);
  141. SERIALIZE_MEMBER(camera_dfar);
  142. SERIALIZE_MEMBER(camera_eye);
  143. SERIALIZE_MEMBER(camera_center);
  144. SERIALIZE_MEMBER(camera_up);
  145. SERIALIZE_MEMBER(depth_test);
  146. SERIALIZE_MEMBER(is_animating);
  147. SERIALIZE_MEMBER(animation_max_fps);
  148. SERIALIZE_MEMBER(object_scale);
  149. SERIALIZE_MEMBER(viewport);
  150. SERIALIZE_MEMBER(view);
  151. SERIALIZE_MEMBER(proj);
  152. SERIALIZE_MEMBER(norm);
  153. }
  154. template<>
  155. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  156. {
  157. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  158. }
  159. template<>
  160. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  161. {
  162. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  163. }
  164. }
  165. }
  166. #ifndef IGL_STATIC_LIBRARY
  167. # include "ViewerCore.cpp"
  168. #endif
  169. #endif