ViewerCore.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifdef IGL_VIEWER_WITH_NANOGUI
  12. #include <igl/opengl/glfw/TextRenderer.h>
  13. #endif
  14. #include <igl/opengl/ViewerData.h>
  15. #include <igl/igl_inline.h>
  16. #include <Eigen/Geometry>
  17. #include <Eigen/Core>
  18. namespace igl
  19. {
  20. namespace opengl
  21. {
  22. // Basic class of the 3D mesh viewer
  23. // TODO: write documentation
  24. class ViewerCore
  25. {
  26. public:
  27. IGL_INLINE ViewerCore();
  28. // Initialization
  29. IGL_INLINE void init();
  30. // Shutdown
  31. IGL_INLINE void shut();
  32. // Serialization code
  33. IGL_INLINE void InitSerialization();
  34. // ------------------- Camera control functions
  35. // Adjust the view to see the entire model
  36. IGL_INLINE void align_camera_center(
  37. const Eigen::MatrixXd& V,
  38. const Eigen::MatrixXi& F);
  39. // Determines how much to zoom and shift such that the mesh fills the unit
  40. // box (centered at the origin)
  41. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  42. const Eigen::MatrixXd& V,
  43. const Eigen::MatrixXi& F,
  44. float & zoom,
  45. Eigen::Vector3f& shift);
  46. // Adjust the view to see the entire model
  47. IGL_INLINE void align_camera_center(
  48. const Eigen::MatrixXd& V);
  49. // Determines how much to zoom and shift such that the mesh fills the unit
  50. // box (centered at the origin)
  51. IGL_INLINE void get_scale_and_shift_to_fit_mesh(
  52. const Eigen::MatrixXd& V,
  53. float & zoom,
  54. Eigen::Vector3f& shift);
  55. // ------------------- Drawing functions
  56. // Clear the frame buffers
  57. IGL_INLINE void clear_framebuffers();
  58. // Draw everything
  59. //
  60. // data cannot be const because it is being set to "clean"
  61. IGL_INLINE void draw(ViewerData& data, MeshGL& opengl, bool update_matrices = true);
  62. IGL_INLINE void draw_buffer(
  63. ViewerData& data,
  64. MeshGL& opengl,
  65. bool update_matrices,
  66. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  67. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  68. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  69. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  70. // Trackball angle (quaternion)
  71. enum RotationType
  72. {
  73. ROTATION_TYPE_TRACKBALL = 0,
  74. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  75. ROTATION_TYPE_NO_ROTATION = 2,
  76. NUM_ROTATION_TYPES = 3
  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. // Colors
  85. Eigen::Vector4f background_color;
  86. // Lighting
  87. Eigen::Vector3f light_position;
  88. float lighting_factor;
  89. RotationType rotation_type;
  90. Eigen::Quaternionf trackball_angle;
  91. // Model viewing parameters
  92. float model_zoom;
  93. Eigen::Vector3f model_translation;
  94. // Model viewing parameters (uv coordinates)
  95. float model_zoom_uv;
  96. Eigen::Vector3f model_translation_uv;
  97. // Camera parameters
  98. float camera_zoom;
  99. bool orthographic;
  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
  115. // pass
  116. Eigen::Matrix4f view;
  117. Eigen::Matrix4f model;
  118. Eigen::Matrix4f proj;
  119. public:
  120. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  121. };
  122. }
  123. }
  124. #ifdef ENABLE_SERIALIZATION
  125. #include <igl/serialize.h>
  126. namespace igl {
  127. namespace serialization {
  128. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  129. {
  130. SERIALIZE_MEMBER(background_color);
  131. SERIALIZE_MEMBER(light_position);
  132. SERIALIZE_MEMBER(lighting_factor);
  133. SERIALIZE_MEMBER(trackball_angle);
  134. SERIALIZE_MEMBER(rotation_type);
  135. SERIALIZE_MEMBER(model_zoom);
  136. SERIALIZE_MEMBER(model_translation);
  137. SERIALIZE_MEMBER(model_zoom_uv);
  138. SERIALIZE_MEMBER(model_translation_uv);
  139. SERIALIZE_MEMBER(camera_zoom);
  140. SERIALIZE_MEMBER(orthographic);
  141. SERIALIZE_MEMBER(camera_view_angle);
  142. SERIALIZE_MEMBER(camera_dnear);
  143. SERIALIZE_MEMBER(camera_dfar);
  144. SERIALIZE_MEMBER(camera_eye);
  145. SERIALIZE_MEMBER(camera_center);
  146. SERIALIZE_MEMBER(camera_up);
  147. SERIALIZE_MEMBER(depth_test);
  148. SERIALIZE_MEMBER(is_animating);
  149. SERIALIZE_MEMBER(animation_max_fps);
  150. SERIALIZE_MEMBER(object_scale);
  151. SERIALIZE_MEMBER(viewport);
  152. SERIALIZE_MEMBER(view);
  153. SERIALIZE_MEMBER(model);
  154. SERIALIZE_MEMBER(proj);
  155. }
  156. template<>
  157. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  158. {
  159. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  160. }
  161. template<>
  162. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  163. {
  164. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  165. }
  166. }
  167. }
  168. #endif
  169. #ifndef IGL_STATIC_LIBRARY
  170. # include "ViewerCore.cpp"
  171. #endif
  172. #endif