ViewerCore.cpp 16 KB

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