ViewerCore.cpp 12 KB

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