ViewerCore.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_VIEWER_VIEWER_CORE_H
  9. #define IGL_VIEWER_VIEWER_CORE_H
  10. #ifdef IGL_VIEWER_WITH_NANOGUI
  11. #include <igl/viewer/TextRenderer.h>
  12. #endif
  13. #include <igl/ViewerData.h>
  14. #include <igl/opengl/State.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. IGL_INLINE void draw(ViewerData& data, State& opengl, bool update_matrices = true);
  60. IGL_INLINE void draw_buffer(
  61. ViewerData& data,
  62. State& opengl,
  63. bool update_matrices,
  64. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  65. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  66. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  67. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A);
  68. // Trackball angle (quaternion)
  69. enum RotationType
  70. {
  71. ROTATION_TYPE_TRACKBALL = 0,
  72. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  73. NUM_ROTATION_TYPES = 2
  74. };
  75. IGL_INLINE void set_rotation_type(const RotationType & value);
  76. // ------------------- Properties
  77. #ifdef IGL_VIEWER_WITH_NANOGUI
  78. // Text rendering helper
  79. TextRenderer textrenderer;
  80. #endif
  81. // Shape material
  82. float shininess;
  83. // Colors
  84. Eigen::Vector4f background_color;
  85. Eigen::Vector4f line_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 paramters (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. // Visualization options
  107. bool show_overlay;
  108. bool show_overlay_depth;
  109. bool show_texture;
  110. bool show_faces;
  111. bool show_lines;
  112. bool show_vertid;
  113. bool show_faceid;
  114. bool invert_normals;
  115. bool depth_test;
  116. // Point size / line width
  117. float point_size;
  118. float line_width;
  119. // Animation
  120. bool is_animating;
  121. double animation_max_fps;
  122. // Caches the two-norm between the min/max point of the bounding box
  123. float object_scale;
  124. // Viewport size
  125. Eigen::Vector4f viewport;
  126. // Save the OpenGL transformation matrices used for the previous rendering pass
  127. Eigen::Matrix4f view;
  128. Eigen::Matrix4f model;
  129. Eigen::Matrix4f proj;
  130. public:
  131. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  132. };
  133. }
  134. }
  135. // Alec: Is this the best place for this?
  136. #ifdef ENABLE_SERIALIZATION
  137. #include <igl/serialize.h>
  138. namespace igl {
  139. namespace serialization {
  140. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  141. {
  142. SERIALIZE_MEMBER(shininess);
  143. SERIALIZE_MEMBER(background_color);
  144. SERIALIZE_MEMBER(line_color);
  145. SERIALIZE_MEMBER(light_position);
  146. SERIALIZE_MEMBER(lighting_factor);
  147. SERIALIZE_MEMBER(trackball_angle);
  148. SERIALIZE_MEMBER(rotation_type);
  149. SERIALIZE_MEMBER(model_zoom);
  150. SERIALIZE_MEMBER(model_translation);
  151. SERIALIZE_MEMBER(model_zoom_uv);
  152. SERIALIZE_MEMBER(model_translation_uv);
  153. SERIALIZE_MEMBER(camera_zoom);
  154. SERIALIZE_MEMBER(orthographic);
  155. SERIALIZE_MEMBER(camera_view_angle);
  156. SERIALIZE_MEMBER(camera_dnear);
  157. SERIALIZE_MEMBER(camera_dfar);
  158. SERIALIZE_MEMBER(camera_eye);
  159. SERIALIZE_MEMBER(camera_center);
  160. SERIALIZE_MEMBER(camera_up);
  161. SERIALIZE_MEMBER(show_faces);
  162. SERIALIZE_MEMBER(show_lines);
  163. SERIALIZE_MEMBER(invert_normals);
  164. SERIALIZE_MEMBER(show_overlay);
  165. SERIALIZE_MEMBER(show_overlay_depth);
  166. SERIALIZE_MEMBER(show_vertid);
  167. SERIALIZE_MEMBER(show_faceid);
  168. SERIALIZE_MEMBER(show_texture);
  169. SERIALIZE_MEMBER(depth_test);
  170. SERIALIZE_MEMBER(point_size);
  171. SERIALIZE_MEMBER(line_width);
  172. SERIALIZE_MEMBER(is_animating);
  173. SERIALIZE_MEMBER(animation_max_fps);
  174. SERIALIZE_MEMBER(object_scale);
  175. SERIALIZE_MEMBER(viewport);
  176. SERIALIZE_MEMBER(view);
  177. SERIALIZE_MEMBER(model);
  178. SERIALIZE_MEMBER(proj);
  179. }
  180. template<>
  181. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  182. {
  183. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  184. }
  185. template<>
  186. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  187. {
  188. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  189. }
  190. }
  191. }
  192. #endif
  193. #ifndef IGL_STATIC_LIBRARY
  194. # include "ViewerCore.cpp"
  195. #endif
  196. #endif