ViewerCore.cpp 15 KB

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