ViewerCore.cpp 13 KB

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