ViewerCore.cpp 13 KB

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