ViewerCore.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. ROTATION_TYPE_NO_ROTATION = 2,
  77. NUM_ROTATION_TYPES = 3
  78. };
  79. IGL_INLINE void set_rotation_type(const RotationType & value);
  80. // ------------------- Properties
  81. #ifdef IGL_VIEWER_WITH_NANOGUI
  82. // Text rendering helper
  83. TextRenderer textrenderer;
  84. #endif
  85. // Colors
  86. Eigen::Vector4f background_color;
  87. // Lighting
  88. Eigen::Vector3f light_position;
  89. float lighting_factor;
  90. RotationType rotation_type;
  91. Eigen::Quaternionf trackball_angle;
  92. // Model viewing parameters
  93. float model_zoom;
  94. Eigen::Vector3f model_translation;
  95. // Model viewing paramters (uv coordinates)
  96. float model_zoom_uv;
  97. Eigen::Vector3f model_translation_uv;
  98. // Camera parameters
  99. float camera_zoom;
  100. bool orthographic;
  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
  116. // pass
  117. Eigen::Matrix4f view;
  118. Eigen::Matrix4f model;
  119. Eigen::Matrix4f proj;
  120. public:
  121. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  122. };
  123. }
  124. }
  125. // Alec: Is this the best place for this?
  126. #ifdef ENABLE_SERIALIZATION
  127. #include <igl/serialize.h>
  128. namespace igl {
  129. namespace serialization {
  130. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  131. {
  132. SERIALIZE_MEMBER(background_color);
  133. SERIALIZE_MEMBER(light_position);
  134. SERIALIZE_MEMBER(lighting_factor);
  135. SERIALIZE_MEMBER(trackball_angle);
  136. SERIALIZE_MEMBER(rotation_type);
  137. SERIALIZE_MEMBER(model_zoom);
  138. SERIALIZE_MEMBER(model_translation);
  139. SERIALIZE_MEMBER(model_zoom_uv);
  140. SERIALIZE_MEMBER(model_translation_uv);
  141. SERIALIZE_MEMBER(camera_zoom);
  142. SERIALIZE_MEMBER(orthographic);
  143. SERIALIZE_MEMBER(camera_view_angle);
  144. SERIALIZE_MEMBER(camera_dnear);
  145. SERIALIZE_MEMBER(camera_dfar);
  146. SERIALIZE_MEMBER(camera_eye);
  147. SERIALIZE_MEMBER(camera_center);
  148. SERIALIZE_MEMBER(camera_up);
  149. SERIALIZE_MEMBER(depth_test);
  150. SERIALIZE_MEMBER(is_animating);
  151. SERIALIZE_MEMBER(animation_max_fps);
  152. SERIALIZE_MEMBER(object_scale);
  153. SERIALIZE_MEMBER(viewport);
  154. SERIALIZE_MEMBER(view);
  155. SERIALIZE_MEMBER(model);
  156. SERIALIZE_MEMBER(proj);
  157. }
  158. template<>
  159. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  160. {
  161. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  162. }
  163. template<>
  164. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  165. {
  166. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  167. }
  168. }
  169. }
  170. #endif
  171. #ifndef IGL_STATIC_LIBRARY
  172. # include "ViewerCore.cpp"
  173. #endif
  174. #endif