ViewerCore.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // Shape material
  86. float shininess;
  87. // Colors
  88. Eigen::Vector4f background_color;
  89. Eigen::Vector4f line_color;
  90. // Lighting
  91. Eigen::Vector3f light_position;
  92. float lighting_factor;
  93. RotationType rotation_type;
  94. Eigen::Quaternionf trackball_angle;
  95. // Model viewing parameters
  96. float model_zoom;
  97. Eigen::Vector3f model_translation;
  98. // Model viewing paramters (uv coordinates)
  99. float model_zoom_uv;
  100. Eigen::Vector3f model_translation_uv;
  101. // Camera parameters
  102. float camera_zoom;
  103. bool orthographic;
  104. Eigen::Vector3f camera_eye;
  105. Eigen::Vector3f camera_up;
  106. Eigen::Vector3f camera_center;
  107. float camera_view_angle;
  108. float camera_dnear;
  109. float camera_dfar;
  110. // Visualization options
  111. bool show_overlay;
  112. bool show_overlay_depth;
  113. bool show_texture;
  114. bool show_faces;
  115. bool show_lines;
  116. bool show_vertid;
  117. bool show_faceid;
  118. bool invert_normals;
  119. bool depth_test;
  120. // Point size / line width
  121. float point_size;
  122. float line_width;
  123. // Animation
  124. bool is_animating;
  125. double animation_max_fps;
  126. // Caches the two-norm between the min/max point of the bounding box
  127. float object_scale;
  128. // Viewport size
  129. Eigen::Vector4f viewport;
  130. // Save the OpenGL transformation matrices used for the previous rendering pass
  131. Eigen::Matrix4f view;
  132. Eigen::Matrix4f model;
  133. Eigen::Matrix4f proj;
  134. public:
  135. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  136. };
  137. }
  138. }
  139. // Alec: Is this the best place for this?
  140. #ifdef ENABLE_SERIALIZATION
  141. #include <igl/serialize.h>
  142. namespace igl {
  143. namespace serialization {
  144. inline void serialization(bool s, igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  145. {
  146. SERIALIZE_MEMBER(shininess);
  147. SERIALIZE_MEMBER(background_color);
  148. SERIALIZE_MEMBER(line_color);
  149. SERIALIZE_MEMBER(light_position);
  150. SERIALIZE_MEMBER(lighting_factor);
  151. SERIALIZE_MEMBER(trackball_angle);
  152. SERIALIZE_MEMBER(rotation_type);
  153. SERIALIZE_MEMBER(model_zoom);
  154. SERIALIZE_MEMBER(model_translation);
  155. SERIALIZE_MEMBER(model_zoom_uv);
  156. SERIALIZE_MEMBER(model_translation_uv);
  157. SERIALIZE_MEMBER(camera_zoom);
  158. SERIALIZE_MEMBER(orthographic);
  159. SERIALIZE_MEMBER(camera_view_angle);
  160. SERIALIZE_MEMBER(camera_dnear);
  161. SERIALIZE_MEMBER(camera_dfar);
  162. SERIALIZE_MEMBER(camera_eye);
  163. SERIALIZE_MEMBER(camera_center);
  164. SERIALIZE_MEMBER(camera_up);
  165. SERIALIZE_MEMBER(show_faces);
  166. SERIALIZE_MEMBER(show_lines);
  167. SERIALIZE_MEMBER(invert_normals);
  168. SERIALIZE_MEMBER(show_overlay);
  169. SERIALIZE_MEMBER(show_overlay_depth);
  170. SERIALIZE_MEMBER(show_vertid);
  171. SERIALIZE_MEMBER(show_faceid);
  172. SERIALIZE_MEMBER(show_texture);
  173. SERIALIZE_MEMBER(depth_test);
  174. SERIALIZE_MEMBER(point_size);
  175. SERIALIZE_MEMBER(line_width);
  176. SERIALIZE_MEMBER(is_animating);
  177. SERIALIZE_MEMBER(animation_max_fps);
  178. SERIALIZE_MEMBER(object_scale);
  179. SERIALIZE_MEMBER(viewport);
  180. SERIALIZE_MEMBER(view);
  181. SERIALIZE_MEMBER(model);
  182. SERIALIZE_MEMBER(proj);
  183. }
  184. template<>
  185. inline void serialize(const igl::opengl::ViewerCore& obj, std::vector<char>& buffer)
  186. {
  187. serialization(true, const_cast<igl::opengl::ViewerCore&>(obj), buffer);
  188. }
  189. template<>
  190. inline void deserialize(igl::opengl::ViewerCore& obj, const std::vector<char>& buffer)
  191. {
  192. serialization(false, obj, const_cast<std::vector<char>&>(buffer));
  193. }
  194. }
  195. }
  196. #endif
  197. #ifndef IGL_STATIC_LIBRARY
  198. # include "ViewerCore.cpp"
  199. #endif
  200. #endif