ViewerCore.cpp 16 KB

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