ViewerCore.h 5.7 KB

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