ViewerCore.cpp 14 KB

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