ViewerCore.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  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 "ViewerData.h"
  10. #include "gl.h"
  11. #include "../quat_to_mat.h"
  12. #include "../snap_to_fixed_up.h"
  13. #include "../look_at.h"
  14. #include "../frustum.h"
  15. #include "../ortho.h"
  16. #include "../massmatrix.h"
  17. #include "../barycenter.h"
  18. #include "../PI.h"
  19. #include <Eigen/Geometry>
  20. #include <iostream>
  21. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  22. const Eigen::MatrixXd& V,
  23. const Eigen::MatrixXi& F)
  24. {
  25. if(V.rows() == 0)
  26. return;
  27. get_scale_and_shift_to_fit_mesh(V,F,camera_base_zoom,camera_base_translation);
  28. // Rather than crash on empty mesh...
  29. if(V.size() > 0)
  30. {
  31. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  32. }
  33. }
  34. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  35. const Eigen::MatrixXd& V,
  36. const Eigen::MatrixXi& F,
  37. float& zoom,
  38. Eigen::Vector3f& shift)
  39. {
  40. if (V.rows() == 0)
  41. return;
  42. Eigen::MatrixXd BC;
  43. if (F.rows() <= 1)
  44. {
  45. BC = V;
  46. } else
  47. {
  48. igl::barycenter(V,F,BC);
  49. }
  50. return get_scale_and_shift_to_fit_mesh(BC,zoom,shift);
  51. }
  52. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  53. const Eigen::MatrixXd& V)
  54. {
  55. if(V.rows() == 0)
  56. return;
  57. get_scale_and_shift_to_fit_mesh(V,camera_base_zoom,camera_base_translation);
  58. // Rather than crash on empty mesh...
  59. if(V.size() > 0)
  60. {
  61. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  62. }
  63. }
  64. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  65. const Eigen::MatrixXd& V,
  66. float& zoom,
  67. Eigen::Vector3f& shift)
  68. {
  69. if (V.rows() == 0)
  70. return;
  71. auto min_point = V.colwise().minCoeff();
  72. auto max_point = V.colwise().maxCoeff();
  73. auto centroid = (0.5*(min_point + max_point)).eval();
  74. shift.setConstant(0);
  75. shift.head(centroid.size()) = -centroid.cast<float>();
  76. zoom = 2.0 / (max_point-min_point).array().abs().maxCoeff();
  77. }
  78. IGL_INLINE void igl::opengl::ViewerCore::clear_framebuffers()
  79. {
  80. // The glScissor call ensures we only clear this core's buffers,
  81. // (in case the user wants different background colors in each viewport.)
  82. glScissor(viewport(0), viewport(1), viewport(2), viewport(3));
  83. glEnable(GL_SCISSOR_TEST);
  84. glClearColor(background_color[0],
  85. background_color[1],
  86. background_color[2],
  87. background_color[3]);
  88. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  89. glDisable(GL_SCISSOR_TEST);
  90. }
  91. IGL_INLINE void igl::opengl::ViewerCore::draw(
  92. ViewerData& data,
  93. bool update_matrices)
  94. {
  95. using namespace std;
  96. using namespace Eigen;
  97. if (depth_test)
  98. glEnable(GL_DEPTH_TEST);
  99. else
  100. glDisable(GL_DEPTH_TEST);
  101. glEnable(GL_BLEND);
  102. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  103. /* Bind and potentially refresh mesh/line/point data */
  104. if (data.dirty)
  105. {
  106. data.updateGL(data, data.invert_normals, data.meshgl);
  107. data.dirty = MeshGL::DIRTY_NONE;
  108. }
  109. data.meshgl.bind_mesh();
  110. // Initialize uniform
  111. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  112. if(update_matrices)
  113. {
  114. view = Eigen::Matrix4f::Identity();
  115. proj = Eigen::Matrix4f::Identity();
  116. norm = Eigen::Matrix4f::Identity();
  117. float width = viewport(2);
  118. float height = viewport(3);
  119. // Set view
  120. look_at( camera_eye, camera_center, camera_up, view);
  121. view = view
  122. * (trackball_angle * Eigen::Scaling(camera_zoom * camera_base_zoom)
  123. * Eigen::Translation3f(camera_translation + camera_base_translation)).matrix();
  124. norm = view.inverse().transpose();
  125. // Set projection
  126. if (orthographic)
  127. {
  128. float length = (camera_eye - camera_center).norm();
  129. float h = tan(camera_view_angle/360.0 * igl::PI) * (length);
  130. ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
  131. }
  132. else
  133. {
  134. float fH = tan(camera_view_angle / 360.0 * igl::PI) * camera_dnear;
  135. float fW = fH * (double)width/(double)height;
  136. frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
  137. }
  138. }
  139. // Send transformations to the GPU
  140. GLint viewi = glGetUniformLocation(data.meshgl.shader_mesh,"view");
  141. GLint proji = glGetUniformLocation(data.meshgl.shader_mesh,"proj");
  142. GLint normi = glGetUniformLocation(data.meshgl.shader_mesh,"normal_matrix");
  143. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  144. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  145. glUniformMatrix4fv(normi, 1, GL_FALSE, norm.data());
  146. // Light parameters
  147. GLint specular_exponenti = glGetUniformLocation(data.meshgl.shader_mesh,"specular_exponent");
  148. GLint light_position_eyei = glGetUniformLocation(data.meshgl.shader_mesh,"light_position_eye");
  149. GLint lighting_factori = glGetUniformLocation(data.meshgl.shader_mesh,"lighting_factor");
  150. GLint fixed_colori = glGetUniformLocation(data.meshgl.shader_mesh,"fixed_color");
  151. GLint texture_factori = glGetUniformLocation(data.meshgl.shader_mesh,"texture_factor");
  152. GLint matcap_factori = glGetUniformLocation(data.meshgl.shader_mesh,"matcap_factor");
  153. GLint double_sidedi = glGetUniformLocation(data.meshgl.shader_mesh,"double_sided");
  154. glUniform1f(specular_exponenti, data.shininess);
  155. glUniform3fv(light_position_eyei, 1, light_position.data());
  156. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  157. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  158. if (data.V.rows()>0)
  159. {
  160. // Render fill
  161. if (is_set(data.show_faces))
  162. {
  163. // Texture
  164. glUniform1f(texture_factori, is_set(data.show_texture) ? 1.0f : 0.0f);
  165. glUniform1f(matcap_factori, is_set(data.use_matcap) ? 1.0f : 0.0f);
  166. glUniform1f(double_sidedi, data.double_sided ? 1.0f : 0.0f);
  167. data.meshgl.draw_mesh(true);
  168. glUniform1f(matcap_factori, 0.0f);
  169. glUniform1f(texture_factori, 0.0f);
  170. }
  171. // Render wireframe
  172. if (is_set(data.show_lines))
  173. {
  174. glLineWidth(data.line_width);
  175. glUniform4f(fixed_colori,
  176. data.line_color[0],
  177. data.line_color[1],
  178. data.line_color[2],
  179. data.line_color[3]);
  180. data.meshgl.draw_mesh(false);
  181. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  182. }
  183. }
  184. if (is_set(data.show_overlay))
  185. {
  186. if (is_set(data.show_overlay_depth))
  187. glEnable(GL_DEPTH_TEST);
  188. else
  189. glDisable(GL_DEPTH_TEST);
  190. if (data.lines.rows() > 0)
  191. {
  192. data.meshgl.bind_overlay_lines();
  193. viewi = glGetUniformLocation(data.meshgl.shader_overlay_lines,"view");
  194. proji = glGetUniformLocation(data.meshgl.shader_overlay_lines,"proj");
  195. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  196. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  197. // This must be enabled, otherwise glLineWidth has no effect
  198. glEnable(GL_LINE_SMOOTH);
  199. glLineWidth(data.line_width);
  200. data.meshgl.draw_overlay_lines();
  201. }
  202. if (data.points.rows() > 0)
  203. {
  204. data.meshgl.bind_overlay_points();
  205. viewi = glGetUniformLocation(data.meshgl.shader_overlay_points,"view");
  206. proji = glGetUniformLocation(data.meshgl.shader_overlay_points,"proj");
  207. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  208. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  209. glPointSize(data.point_size);
  210. data.meshgl.draw_overlay_points();
  211. }
  212. glEnable(GL_DEPTH_TEST);
  213. }
  214. if(is_set(data.show_vertex_labels)&&data.vertex_labels_positions.rows()>0)
  215. draw_labels(data, data.meshgl.vertex_labels);
  216. if(is_set(data.show_face_labels)&&data.face_labels_positions.rows()>0)
  217. draw_labels(data, data.meshgl.face_labels);
  218. if(is_set(data.show_custom_labels)&&data.labels_positions.rows()>0)
  219. draw_labels(data, data.meshgl.custom_labels);
  220. }
  221. IGL_INLINE void igl::opengl::ViewerCore::draw_buffer(ViewerData& data,
  222. bool update_matrices,
  223. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  224. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  225. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  226. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  227. {
  228. assert(R.rows() == G.rows() && G.rows() == B.rows() && B.rows() == A.rows());
  229. assert(R.cols() == G.cols() && G.cols() == B.cols() && B.cols() == A.cols());
  230. unsigned width = R.rows();
  231. unsigned height = R.cols();
  232. // https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing
  233. unsigned int framebuffer;
  234. glGenFramebuffers(1, &framebuffer);
  235. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  236. // create a multisampled color attachment texture
  237. unsigned int textureColorBufferMultiSampled;
  238. glGenTextures(1, &textureColorBufferMultiSampled);
  239. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled);
  240. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
  241. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
  242. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled, 0);
  243. // create a (also multisampled) renderbuffer object for depth and stencil attachments
  244. unsigned int rbo;
  245. glGenRenderbuffers(1, &rbo);
  246. glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  247. glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
  248. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  249. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
  250. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  251. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  252. // configure second post-processing framebuffer
  253. unsigned int intermediateFBO;
  254. glGenFramebuffers(1, &intermediateFBO);
  255. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  256. // create a color attachment texture
  257. unsigned int screenTexture;
  258. glGenTextures(1, &screenTexture);
  259. glBindTexture(GL_TEXTURE_2D, screenTexture);
  260. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  261. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  262. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  263. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0); // we only need a color buffer
  264. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  265. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  266. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  267. // Clear the buffer
  268. glClearColor(background_color(0), background_color(1), background_color(2), 0.f);
  269. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  270. // Save old viewport
  271. Eigen::Vector4f viewport_ori = viewport;
  272. viewport << 0,0,width,height;
  273. // Draw
  274. draw(data,update_matrices);
  275. // Restore viewport
  276. viewport = viewport_ori;
  277. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
  278. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO);
  279. glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  280. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  281. // Copy back in the given Eigen matrices
  282. GLubyte* pixels = (GLubyte*)calloc(width*height*4,sizeof(GLubyte));
  283. glReadPixels(0, 0,width, height,GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  284. // Clean up
  285. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  286. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  287. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  288. glDeleteTextures(1, &screenTexture);
  289. glDeleteTextures(1, &textureColorBufferMultiSampled);
  290. glDeleteFramebuffers(1, &framebuffer);
  291. glDeleteFramebuffers(1, &intermediateFBO);
  292. glDeleteRenderbuffers(1, &rbo);
  293. int count = 0;
  294. for (unsigned j=0; j<height; ++j)
  295. {
  296. for (unsigned i=0; i<width; ++i)
  297. {
  298. R(i,j) = pixels[count*4+0];
  299. G(i,j) = pixels[count*4+1];
  300. B(i,j) = pixels[count*4+2];
  301. A(i,j) = pixels[count*4+3];
  302. ++count;
  303. }
  304. }
  305. // Clean up
  306. free(pixels);
  307. }
  308. // Define uniforms for text labels
  309. IGL_INLINE void igl::opengl::ViewerCore::draw_labels(
  310. ViewerData& data,
  311. const igl::opengl::MeshGL::TextGL& labels
  312. ){
  313. glDisable(GL_LINE_SMOOTH); // Clear settings if overlay is activated
  314. data.meshgl.bind_labels(labels);
  315. GLint viewi = glGetUniformLocation(data.meshgl.shader_text,"view");
  316. GLint proji = glGetUniformLocation(data.meshgl.shader_text,"proj");
  317. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  318. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  319. // Parameters for mapping characters from font atlass
  320. float width = viewport(2);
  321. float height = viewport(3);
  322. float text_shift_scale_factor = orthographic ? 0.01 : 0.03;
  323. float render_scale = (orthographic ? 0.6 : 1.7) * data.label_size;
  324. glUniform1f(glGetUniformLocation(data.meshgl.shader_text, "TextShiftFactor"), text_shift_scale_factor);
  325. glUniform3f(glGetUniformLocation(data.meshgl.shader_text, "TextColor"), data.label_color(0), data.label_color(1), data.label_color(2));
  326. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "CellSize"), 1.0f / 16, (300.0f / 384) / 6);
  327. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "CellOffset"), 0.5 / 256.0, 0.5 / 256.0);
  328. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "RenderSize"),
  329. render_scale * 0.75 * 16 / (width),
  330. render_scale * 0.75 * 33.33 / (height));
  331. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "RenderOrigin"), -2, 2);
  332. data.meshgl.draw_labels(labels);
  333. glEnable(GL_DEPTH_TEST);
  334. }
  335. IGL_INLINE void igl::opengl::ViewerCore::set_rotation_type(
  336. const igl::opengl::ViewerCore::RotationType & value)
  337. {
  338. using namespace Eigen;
  339. using namespace std;
  340. const RotationType old_rotation_type = rotation_type;
  341. rotation_type = value;
  342. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  343. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  344. {
  345. snap_to_fixed_up(Quaternionf(trackball_angle),trackball_angle);
  346. }
  347. }
  348. IGL_INLINE void igl::opengl::ViewerCore::set(unsigned int &property_mask, bool value) const
  349. {
  350. if (!value)
  351. unset(property_mask);
  352. else
  353. property_mask |= id;
  354. }
  355. IGL_INLINE void igl::opengl::ViewerCore::unset(unsigned int &property_mask) const
  356. {
  357. property_mask &= ~id;
  358. }
  359. IGL_INLINE void igl::opengl::ViewerCore::toggle(unsigned int &property_mask) const
  360. {
  361. property_mask ^= id;
  362. }
  363. IGL_INLINE bool igl::opengl::ViewerCore::is_set(unsigned int property_mask) const
  364. {
  365. return (property_mask & id);
  366. }
  367. IGL_INLINE igl::opengl::ViewerCore::ViewerCore()
  368. {
  369. // Default colors
  370. background_color << 0.3f, 0.3f, 0.5f, 1.0f;
  371. // Default lights settings
  372. light_position << 0.0f, 0.3f, 0.0f;
  373. lighting_factor = 1.0f; //on
  374. // Default trackball
  375. trackball_angle = Eigen::Quaternionf::Identity();
  376. rotation_type = ViewerCore::ROTATION_TYPE_TRACKBALL;
  377. set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  378. // Camera parameters
  379. camera_base_zoom = 1.0f;
  380. camera_zoom = 1.0f;
  381. orthographic = false;
  382. camera_view_angle = 45.0;
  383. camera_dnear = 1.0;
  384. camera_dfar = 100.0;
  385. camera_base_translation << 0, 0, 0;
  386. camera_translation << 0, 0, 0;
  387. camera_eye << 0, 0, 5;
  388. camera_center << 0, 0, 0;
  389. camera_up << 0, 1, 0;
  390. depth_test = true;
  391. is_animating = false;
  392. animation_max_fps = 30.;
  393. viewport.setZero();
  394. }
  395. IGL_INLINE void igl::opengl::ViewerCore::init()
  396. {
  397. }
  398. IGL_INLINE void igl::opengl::ViewerCore::shut()
  399. {
  400. }