ViewerCore.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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,igl::viewer::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(depth_test);
  51. SERIALIZE_MEMBER(point_size);
  52. SERIALIZE_MEMBER(line_width);
  53. SERIALIZE_MEMBER(is_animating);
  54. SERIALIZE_MEMBER(animation_max_fps);
  55. SERIALIZE_MEMBER(viewport);
  56. SERIALIZE_MEMBER(view);
  57. SERIALIZE_MEMBER(model);
  58. SERIALIZE_MEMBER(proj);
  59. }
  60. IGL_INLINE void serialize(const igl::viewer::ViewerCore& obj,std::vector<char>& buffer)
  61. {
  62. serialization(true,const_cast<igl::viewer::ViewerCore&>(obj),buffer);
  63. }
  64. IGL_INLINE void deserialize(igl::viewer::ViewerCore& obj,const std::vector<char>& buffer)
  65. {
  66. serialization(false,obj,const_cast<std::vector<char>&>(buffer));
  67. }
  68. }
  69. }
  70. #endif
  71. IGL_INLINE void igl::viewer::ViewerCore::align_camera_center(
  72. const Eigen::MatrixXd& V,
  73. const Eigen::MatrixXi& F)
  74. {
  75. if(V.rows() == 0)
  76. return;
  77. get_scale_and_shift_to_fit_mesh(V,F,model_zoom,model_translation);
  78. // Rather than crash on empty mesh...
  79. if(V.size() > 0)
  80. {
  81. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  82. }
  83. }
  84. IGL_INLINE void igl::viewer::ViewerCore::get_scale_and_shift_to_fit_mesh(
  85. const Eigen::MatrixXd& V,
  86. const Eigen::MatrixXi& F,
  87. float& zoom,
  88. Eigen::Vector3f& shift)
  89. {
  90. if (V.rows() == 0)
  91. return;
  92. //Eigen::SparseMatrix<double> M;
  93. //igl::massmatrix(V,F,igl::MASSMATRIX_TYPE_VORONOI,M);
  94. //const auto & MV = M*V;
  95. //Eigen::RowVector3d centroid = MV.colwise().sum()/M.diagonal().sum();
  96. Eigen::MatrixXd BC;
  97. igl::barycenter(V,F,BC);
  98. Eigen::RowVector3d min_point = BC.colwise().minCoeff();
  99. Eigen::RowVector3d max_point = BC.colwise().maxCoeff();
  100. Eigen::RowVector3d centroid = 0.5*(min_point + max_point);
  101. shift = -centroid.cast<float>();
  102. double x_scale = fabs(max_point[0] - min_point[0]);
  103. double y_scale = fabs(max_point[1] - min_point[1]);
  104. double z_scale = fabs(max_point[2] - min_point[2]);
  105. zoom = 2.0 / std::max(z_scale,std::max(x_scale,y_scale));
  106. }
  107. IGL_INLINE void igl::viewer::ViewerCore::align_camera_center(
  108. const Eigen::MatrixXd& V)
  109. {
  110. if(V.rows() == 0)
  111. return;
  112. get_scale_and_shift_to_fit_mesh(V,model_zoom,model_translation);
  113. // Rather than crash on empty mesh...
  114. if(V.size() > 0)
  115. {
  116. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  117. }
  118. }
  119. IGL_INLINE void igl::viewer::ViewerCore::get_scale_and_shift_to_fit_mesh(
  120. const Eigen::MatrixXd& V,
  121. float& zoom,
  122. Eigen::Vector3f& shift)
  123. {
  124. if (V.rows() == 0)
  125. return;
  126. Eigen::RowVector3d min_point;
  127. Eigen::RowVector3d max_point;
  128. Eigen::RowVector3d centroid;
  129. if (V.cols() == 3)
  130. {
  131. min_point = V.colwise().minCoeff();
  132. max_point = V.colwise().maxCoeff();
  133. }
  134. else if (V.cols() == 2)
  135. {
  136. min_point << V.colwise().minCoeff(),0;
  137. max_point << V.colwise().maxCoeff(),0;
  138. }
  139. else
  140. return;
  141. centroid = 0.5 * (min_point + max_point);
  142. shift = -centroid.cast<float>();
  143. double x_scale = fabs(max_point[0] - min_point[0]);
  144. double y_scale = fabs(max_point[1] - min_point[1]);
  145. double z_scale = fabs(max_point[2] - min_point[2]);
  146. zoom = 2.0 / std::max(z_scale,std::max(x_scale,y_scale));
  147. }
  148. IGL_INLINE void igl::viewer::ViewerCore::clear_framebuffers()
  149. {
  150. glClearColor(background_color[0],
  151. background_color[1],
  152. background_color[2],
  153. 1.0f);
  154. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  155. }
  156. IGL_INLINE void igl::viewer::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl, bool update_matrices)
  157. {
  158. using namespace std;
  159. using namespace Eigen;
  160. if (depth_test)
  161. glEnable(GL_DEPTH_TEST);
  162. else
  163. glDisable(GL_DEPTH_TEST);
  164. /* Bind and potentially refresh mesh/line/point data */
  165. if (data.dirty)
  166. {
  167. opengl.set_data(data, invert_normals);
  168. data.dirty = ViewerData::DIRTY_NONE;
  169. }
  170. opengl.bind_mesh();
  171. // Initialize uniform
  172. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  173. if(update_matrices)
  174. {
  175. model = Eigen::Matrix4f::Identity();
  176. view = Eigen::Matrix4f::Identity();
  177. proj = Eigen::Matrix4f::Identity();
  178. // Set view
  179. look_at( camera_eye, camera_center, camera_up, view);
  180. float width = viewport(2);
  181. float height = viewport(3);
  182. // Set projection
  183. if (orthographic)
  184. {
  185. float length = (camera_eye - camera_center).norm();
  186. float h = tan(camera_view_angle/360.0 * M_PI) * (length);
  187. ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
  188. }
  189. else
  190. {
  191. float fH = tan(camera_view_angle / 360.0 * M_PI) * camera_dnear;
  192. float fW = fH * (double)width/(double)height;
  193. frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
  194. }
  195. // end projection
  196. // Set model transformation
  197. float mat[16];
  198. igl::quat_to_mat(trackball_angle.coeffs().data(), mat);
  199. for (unsigned i=0;i<4;++i)
  200. for (unsigned j=0;j<4;++j)
  201. model(i,j) = mat[i+4*j];
  202. // Why not just use Eigen::Transform<double,3,Projective> for model...?
  203. model.topLeftCorner(3,3)*=camera_zoom;
  204. model.topLeftCorner(3,3)*=model_zoom;
  205. model.col(3).head(3) += model.topLeftCorner(3,3)*model_translation;
  206. }
  207. // Send transformations to the GPU
  208. GLint modeli = opengl.shader_mesh.uniform("model");
  209. GLint viewi = opengl.shader_mesh.uniform("view");
  210. GLint proji = opengl.shader_mesh.uniform("proj");
  211. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  212. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  213. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  214. // Light parameters
  215. GLint specular_exponenti = opengl.shader_mesh.uniform("specular_exponent");
  216. GLint light_position_worldi = opengl.shader_mesh.uniform("light_position_world");
  217. GLint lighting_factori = opengl.shader_mesh.uniform("lighting_factor");
  218. GLint fixed_colori = opengl.shader_mesh.uniform("fixed_color");
  219. GLint texture_factori = opengl.shader_mesh.uniform("texture_factor");
  220. glUniform1f(specular_exponenti, shininess);
  221. Vector3f rev_light = -1.*light_position;
  222. glUniform3fv(light_position_worldi, 1, rev_light.data());
  223. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  224. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  225. if (data.V.rows()>0)
  226. {
  227. // Render fill
  228. if (show_faces)
  229. {
  230. // Texture
  231. glUniform1f(texture_factori, show_texture ? 1.0f : 0.0f);
  232. opengl.draw_mesh(true);
  233. glUniform1f(texture_factori, 0.0f);
  234. }
  235. // Render wireframe
  236. if (show_lines)
  237. {
  238. glLineWidth(line_width);
  239. glUniform4f(fixed_colori, line_color[0], line_color[1],
  240. line_color[2], 1.0f);
  241. opengl.draw_mesh(false);
  242. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  243. }
  244. if (show_vertid)
  245. {
  246. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  247. for (int i=0; i<data.V.rows(); ++i)
  248. textrenderer.DrawText(data.V.row(i),data.V_normals.row(i),to_string(i));
  249. textrenderer.EndDraw();
  250. }
  251. if (show_faceid)
  252. {
  253. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  254. for (int i=0; i<data.F.rows(); ++i)
  255. {
  256. Eigen::RowVector3d p = Eigen::RowVector3d::Zero();
  257. for (int j=0;j<data.F.cols();++j)
  258. p += data.V.row(data.F(i,j));
  259. p /= data.F.cols();
  260. textrenderer.DrawText(p, data.F_normals.row(i), to_string(i));
  261. }
  262. textrenderer.EndDraw();
  263. }
  264. }
  265. if (show_overlay)
  266. {
  267. if (show_overlay_depth)
  268. glEnable(GL_DEPTH_TEST);
  269. else
  270. glDisable(GL_DEPTH_TEST);
  271. if (data.lines.rows() > 0)
  272. {
  273. opengl.bind_overlay_lines();
  274. modeli = opengl.shader_overlay_lines.uniform("model");
  275. viewi = opengl.shader_overlay_lines.uniform("view");
  276. proji = opengl.shader_overlay_lines.uniform("proj");
  277. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  278. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  279. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  280. // This must be enabled, otherwise glLineWidth has no effect
  281. glEnable(GL_LINE_SMOOTH);
  282. glLineWidth(line_width);
  283. opengl.draw_overlay_lines();
  284. }
  285. if (data.points.rows() > 0)
  286. {
  287. opengl.bind_overlay_points();
  288. modeli = opengl.shader_overlay_points.uniform("model");
  289. viewi = opengl.shader_overlay_points.uniform("view");
  290. proji = opengl.shader_overlay_points.uniform("proj");
  291. glUniformMatrix4fv(modeli, 1, GL_FALSE, model.data());
  292. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  293. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  294. glPointSize(point_size);
  295. opengl.draw_overlay_points();
  296. }
  297. if (data.labels_positions.rows() > 0)
  298. {
  299. textrenderer.BeginDraw(view*model, proj, viewport, object_scale);
  300. for (int i=0; i<data.labels_positions.rows(); ++i)
  301. textrenderer.DrawText(data.labels_positions.row(i), Eigen::Vector3d(0.0,0.0,0.0),
  302. data.labels_strings[i]);
  303. textrenderer.EndDraw();
  304. }
  305. glEnable(GL_DEPTH_TEST);
  306. }
  307. }
  308. IGL_INLINE void igl::viewer::ViewerCore::draw_buffer(ViewerData& data,
  309. OpenGL_state& opengl,
  310. bool update_matrices,
  311. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  312. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  313. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  314. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  315. {
  316. assert(R.rows() == G.rows() && G.rows() == B.rows() && B.rows() == A.rows());
  317. assert(R.cols() == G.cols() && G.cols() == B.cols() && B.cols() == A.cols());
  318. int x = R.rows();
  319. int y = R.cols();
  320. // Create frame buffer
  321. GLuint frameBuffer;
  322. glGenFramebuffers(1, &frameBuffer);
  323. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  324. // Create texture to hold color buffer
  325. GLuint texColorBuffer;
  326. glGenTextures(1, &texColorBuffer);
  327. glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  328. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  329. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  330. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  331. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
  332. // Create Renderbuffer Object to hold depth and stencil buffers
  333. GLuint rboDepthStencil;
  334. glGenRenderbuffers(1, &rboDepthStencil);
  335. glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil);
  336. glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, x, y);
  337. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthStencil);
  338. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  339. glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  340. // Clear the buffer
  341. glClearColor(background_color(0), background_color(1), background_color(2), 0.f);
  342. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  343. // Save old viewport
  344. Eigen::Vector4f viewport_ori = viewport;
  345. viewport << 0,0,x,y;
  346. // Draw
  347. draw(data,opengl,update_matrices);
  348. // Restore viewport
  349. viewport = viewport_ori;
  350. // Copy back in the given Eigen matrices
  351. GLubyte* pixels = (GLubyte*)calloc(x*y*4,sizeof(GLubyte));
  352. glReadPixels
  353. (
  354. 0, 0,
  355. x, y,
  356. GL_RGBA, GL_UNSIGNED_BYTE, pixels
  357. );
  358. int count = 0;
  359. for (unsigned j=0; j<y; ++j)
  360. {
  361. for (unsigned i=0; i<x; ++i)
  362. {
  363. R(i,j) = pixels[count*4+0];
  364. G(i,j) = pixels[count*4+1];
  365. B(i,j) = pixels[count*4+2];
  366. A(i,j) = pixels[count*4+3];
  367. ++count;
  368. }
  369. }
  370. // Clean up
  371. free(pixels);
  372. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  373. glDeleteRenderbuffers(1, &rboDepthStencil);
  374. glDeleteTextures(1, &texColorBuffer);
  375. glDeleteFramebuffers(1, &frameBuffer);
  376. }
  377. IGL_INLINE igl::viewer::ViewerCore::ViewerCore()
  378. {
  379. // Default shininess
  380. shininess = 35.0f;
  381. // Default colors
  382. background_color << 0.3f, 0.3f, 0.5f;
  383. line_color << 0.0f, 0.0f, 0.0f;
  384. // Default lights settings
  385. light_position << 0.0f, -0.30f, -5.0f;
  386. lighting_factor = 1.0f; //on
  387. // Default trackball
  388. trackball_angle = Eigen::Quaternionf::Identity();
  389. rotation_type = ViewerCore::ROTATION_TYPE_TRACKBALL;
  390. // Defalut model viewing parameters
  391. model_zoom = 1.0f;
  392. model_translation << 0,0,0;
  393. // Camera parameters
  394. camera_zoom = 1.0f;
  395. orthographic = false;
  396. camera_view_angle = 45.0;
  397. camera_dnear = 1.0;
  398. camera_dfar = 100.0;
  399. camera_eye << 0, 0, 5;
  400. camera_center << 0, 0, 0;
  401. camera_up << 0, 1, 0;
  402. // Default visualization options
  403. show_faces = true;
  404. show_lines = true;
  405. invert_normals = false;
  406. show_overlay = true;
  407. show_overlay_depth = true;
  408. show_vertid = false;
  409. show_faceid = false;
  410. show_texture = false;
  411. depth_test = true;
  412. // Default point size / line width
  413. point_size = 30;
  414. line_width = 0.5f;
  415. is_animating = false;
  416. animation_max_fps = 30.;
  417. }
  418. IGL_INLINE void igl::viewer::ViewerCore::init()
  419. {
  420. textrenderer.Init();
  421. }
  422. IGL_INLINE void igl::viewer::ViewerCore::shut()
  423. {
  424. textrenderer.Shut();
  425. }