ViewerCore.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include "ViewerCore.h"
  9. #include <igl/quat_to_mat.h>
  10. #include <igl/look_at.h>
  11. #include <igl/frustum.h>
  12. #include <igl/ortho.h>
  13. #include <igl/massmatrix.h>
  14. #include <igl/barycenter.h>
  15. #include <Eigen/Geometry>
  16. #include <iostream>
  17. #ifdef ENABLE_SERIALIZATION
  18. #include <igl/serialize.h>
  19. namespace igl {
  20. namespace serialization {
  21. IGL_INLINE void serialization(bool s,ViewerCore& obj,std::vector<char>& buffer)
  22. {
  23. SERIALIZE_MEMBER(shininess);
  24. SERIALIZE_MEMBER(background_color);
  25. SERIALIZE_MEMBER(line_color);
  26. SERIALIZE_MEMBER(light_position);
  27. SERIALIZE_MEMBER(lighting_factor);
  28. SERIALIZE_MEMBER(trackball_angle);
  29. SERIALIZE_MEMBER(model_zoom);
  30. SERIALIZE_MEMBER(model_translation);
  31. SERIALIZE_MEMBER(model_zoom_uv);
  32. SERIALIZE_MEMBER(model_translation_uv);
  33. SERIALIZE_MEMBER(object_scale);
  34. SERIALIZE_MEMBER(camera_zoom);
  35. SERIALIZE_MEMBER(orthographic);
  36. SERIALIZE_MEMBER(camera_view_angle);
  37. SERIALIZE_MEMBER(camera_dnear);
  38. SERIALIZE_MEMBER(camera_dfar);
  39. SERIALIZE_MEMBER(camera_eye);
  40. SERIALIZE_MEMBER(camera_center);
  41. SERIALIZE_MEMBER(camera_up);
  42. SERIALIZE_MEMBER(show_faces);
  43. SERIALIZE_MEMBER(show_lines);
  44. SERIALIZE_MEMBER(invert_normals);
  45. SERIALIZE_MEMBER(show_overlay);
  46. SERIALIZE_MEMBER(show_overlay_depth);
  47. SERIALIZE_MEMBER(show_vertid);
  48. SERIALIZE_MEMBER(show_faceid);
  49. SERIALIZE_MEMBER(show_texture);
  50. SERIALIZE_MEMBER(point_size);
  51. SERIALIZE_MEMBER(line_width);
  52. SERIALIZE_MEMBER(is_animating);
  53. SERIALIZE_MEMBER(animation_max_fps);
  54. SERIALIZE_MEMBER(viewport);
  55. SERIALIZE_MEMBER(view);
  56. SERIALIZE_MEMBER(model);
  57. SERIALIZE_MEMBER(proj);
  58. }
  59. IGL_INLINE void serialize(const ViewerCore& obj,std::vector<char>& buffer)
  60. {
  61. serialization(true,const_cast<ViewerCore&>(obj),buffer);
  62. }
  63. IGL_INLINE void deserialize(ViewerCore& obj,const std::vector<char>& buffer)
  64. {
  65. serialization(false,obj,const_cast<std::vector<char>&>(buffer));
  66. }
  67. }
  68. }
  69. #endif
  70. IGL_INLINE void igl::ViewerCore::align_camera_center(
  71. const Eigen::MatrixXd& V,
  72. const Eigen::MatrixXi& F)
  73. {
  74. if(V.rows() == 0)
  75. return;
  76. get_scale_and_shift_to_fit_mesh(V,F,model_zoom,model_translation);
  77. // Rather than crash on empty mesh...
  78. if(V.size() > 0)
  79. {
  80. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  81. }
  82. }
  83. IGL_INLINE void igl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  84. const Eigen::MatrixXd& V,
  85. const Eigen::MatrixXi& F,
  86. float& zoom,
  87. Eigen::Vector3f& shift)
  88. {
  89. if (V.rows() == 0)
  90. return;
  91. //Eigen::SparseMatrix<double> M;
  92. //igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  93. //const auto & MV = M*V;
  94. //Eigen::RowVector3d centroid = MV.colwise().sum()/M.diagonal().sum();
  95. Eigen::MatrixXd BC;
  96. igl::barycenter(V,F,BC);
  97. Eigen::RowVector3d min_point = BC.colwise().minCoeff();
  98. Eigen::RowVector3d max_point = BC.colwise().maxCoeff();
  99. Eigen::RowVector3d centroid = 0.5*(min_point + max_point);
  100. shift = -centroid.cast<float>();
  101. double x_scale = fabs(max_point[0] - min_point[0]);
  102. double y_scale = fabs(max_point[1] - min_point[1]);
  103. double z_scale = fabs(max_point[2] - min_point[2]);
  104. zoom = 2.0 / std::max(z_scale,std::max(x_scale,y_scale));
  105. }
  106. IGL_INLINE void igl::ViewerCore::clear_framebuffers()
  107. {
  108. glClearColor(background_color[0],
  109. background_color[1],
  110. background_color[2],
  111. 1.0f);
  112. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  113. }
  114. IGL_INLINE void igl::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl)
  115. {
  116. using namespace std;
  117. using namespace Eigen;
  118. glEnable(GL_DEPTH_TEST);
  119. /* Bind and potentially refresh mesh/line/point data */
  120. if (data.dirty)
  121. {
  122. opengl.set_data(data, invert_normals);
  123. data.dirty = ViewerData::DIRTY_NONE;
  124. }
  125. opengl.bind_mesh();
  126. // Initialize uniform
  127. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  128. model = Eigen::Matrix4f::Identity();
  129. view = Eigen::Matrix4f::Identity();
  130. proj = Eigen::Matrix4f::Identity();
  131. // Set view
  132. look_at( camera_eye, camera_center, camera_up, view);
  133. float width = viewport(2);
  134. float height = viewport(3);
  135. // Set projection
  136. if (orthographic)
  137. {
  138. float length = (camera_eye - camera_center).norm();
  139. float h = tan(camera_view_angle/360.0 * M_PI) * (length);
  140. ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
  141. }
  142. else
  143. {
  144. float fH = tan(camera_view_angle / 360.0 * M_PI) * camera_dnear;
  145. float fW = fH * (double)width/(double)height;
  146. frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
  147. }
  148. // end projection
  149. // Set model transformation
  150. float mat[16];
  151. igl::quat_to_mat(trackball_angle.data(), mat);
  152. for (unsigned i=0;i<4;++i)
  153. for (unsigned j=0;j<4;++j)
  154. model(i,j) = mat[i+4*j];
  155. // Why not just use Eigen::Transform<double,3,Projective> for model...?
  156. model.topLeftCorner(3,3)*=camera_zoom;
  157. model.topLeftCorner(3,3)*=model_zoom;
  158. model.col(3).head(3) += model.topLeftCorner(3,3)*model_translation;
  159. // Send transformations to the GPU
  160. GLint modeli = opengl.shader_mesh.uniform("model");
  161. GLint viewi = opengl.shader_mesh.uniform("view");
  162. GLint proji = opengl.shader_mesh.uniform("proj");
  163. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  164. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  165. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  166. // Light parameters
  167. GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
  168. GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
  169. GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
  170. GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
  171. GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
  172. glUniform1f(specular_exponenti, shininess);
  173. Vector3f rev_light = -1.*light_position;
  174. glUniform3fv(light_position_worldi, 1, rev_light.data());
  175. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  176. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  177. if (data.V.rows()>0)
  178. {
  179. // Render fill
  180. if (show_faces)
  181. {
  182. // Texture
  183. glUniform1f(texture_factori, show_texture ? 1.0f : 0.0f);
  184. opengl.draw_mesh(true);
  185. glUniform1f(texture_factori, 0.0f);
  186. }
  187. // Render wireframe
  188. if (show_lines)
  189. {
  190. glLineWidth(line_width);
  191. glUniform4f(fixed_colori, line_color[0], line_color[1],
  192. line_color[2], 1.0f);
  193. opengl.draw_mesh(false);
  194. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  195. }
  196. if (show_vertid)
  197. {
  198. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  199. for (int i=0; i<data.V.rows(); ++i)
  200. textrenderer.DrawText(data.V.row(i), data.V_normals.row(i), to_string(i));
  201. textrenderer.EndDraw();
  202. }
  203. if (show_faceid)
  204. {
  205. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  206. for (int i=0; i<data.F.rows(); ++i)
  207. {
  208. Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
  209. for (int j=0;j<data.F.cols();++j)
  210. p += data.V.row(data.F(i,j));
  211. p /= data.F.cols();
  212. textrenderer.DrawText(p, data.F_normals.row(i), to_string(i));
  213. }
  214. textrenderer.EndDraw();
  215. }
  216. }
  217. if (show_overlay)
  218. {
  219. if (show_overlay_depth)
  220. glEnable(GL_DEPTH_TEST);
  221. else
  222. glDisable(GL_DEPTH_TEST);
  223. if (data.lines.rows() > 0)
  224. {
  225. opengl.bind_overlay_lines();
  226. modeli = opengl.shader_overlay_lines.uniform("model");
  227. viewi = opengl.shader_overlay_lines.uniform("view");
  228. proji = opengl.shader_overlay_lines.uniform("proj");
  229. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  230. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  231. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  232. // This must be enabled, otherwise glLineWidth has no effect
  233. glEnable(GL_LINE_SMOOTH);
  234. glLineWidth(line_width);
  235. opengl.draw_overlay_lines();
  236. }
  237. if (data.points.rows() > 0)
  238. {
  239. opengl.bind_overlay_points();
  240. modeli = opengl.shader_overlay_points.uniform("model");
  241. viewi = opengl.shader_overlay_points.uniform("view");
  242. proji = opengl.shader_overlay_points.uniform("proj");
  243. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  244. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  245. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  246. glPointSize(point_size);
  247. opengl.draw_overlay_points();
  248. }
  249. if (data.labels_positions.rows() > 0)
  250. {
  251. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  252. for (int i=0; i<data.labels_positions.rows(); ++i)
  253. textrenderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
  254. data.labels_strings[i]);
  255. textrenderer.EndDraw();
  256. }
  257. glEnable(GL_DEPTH_TEST);
  258. }
  259. }
  260. IGL_INLINE igl::ViewerCore::ViewerCore()
  261. {
  262. // Default shininess
  263. shininess = 35.0f;
  264. // Default colors
  265. background_color << 0.3f, 0.3f, 0.5f;
  266. line_color << 0.0f, 0.0f, 0.0f;
  267. // Default lights settings
  268. light_position << 0.0f, -0.30f, -5.0f;
  269. lighting_factor = 1.0f; //on
  270. // Default trackball
  271. trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
  272. // Defalut model viewing parameters
  273. model_zoom = 1.0f;
  274. model_translation << 0,0,0;
  275. // Camera parameters
  276. camera_zoom = 1.0f;
  277. orthographic = false;
  278. camera_view_angle = 45.0;
  279. camera_dnear = 1.0;
  280. camera_dfar = 100.0;
  281. camera_eye << 0, 0, 5;
  282. camera_center << 0, 0, 0;
  283. camera_up << 0, 1, 0;
  284. // Default visualization options
  285. show_faces = true;
  286. show_lines = true;
  287. invert_normals = false;
  288. show_overlay = true;
  289. show_overlay_depth = true;
  290. show_vertid = false;
  291. show_faceid = false;
  292. show_texture = false;
  293. // Default point size / line width
  294. point_size = 15;
  295. line_width = 0.5f;
  296. is_animating = false;
  297. animation_max_fps = 30.;
  298. }
  299. IGL_INLINE void igl::ViewerCore::init()
  300. {
  301. textrenderer.Init();
  302. }
  303. IGL_INLINE void igl::ViewerCore::shut()
  304. {
  305. textrenderer.Shut();
  306. }