ViewerCore.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 "../null.h"
  13. #include "../snap_to_fixed_up.h"
  14. #include "../look_at.h"
  15. #include "../frustum.h"
  16. #include "../ortho.h"
  17. #include "../massmatrix.h"
  18. #include "../barycenter.h"
  19. #include "../PI.h"
  20. #include "report_gl_error.h"
  21. #include "read_pixels.h"
  22. #include <Eigen/Geometry>
  23. #include <iostream>
  24. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  25. const Eigen::MatrixXd& V,
  26. const Eigen::MatrixXi& F)
  27. {
  28. if(V.rows() == 0)
  29. return;
  30. get_scale_and_shift_to_fit_mesh(V,F,camera_base_zoom,camera_base_translation);
  31. // Rather than crash on empty mesh...
  32. if(V.size() > 0)
  33. {
  34. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  35. }
  36. }
  37. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  38. const Eigen::MatrixXd& V,
  39. const Eigen::MatrixXi& F,
  40. float& zoom,
  41. Eigen::Vector3f& shift)
  42. {
  43. if (V.rows() == 0)
  44. return;
  45. Eigen::MatrixXd BC;
  46. if (F.rows() <= 1)
  47. {
  48. BC = V;
  49. } else
  50. {
  51. igl::barycenter(V,F,BC);
  52. }
  53. return get_scale_and_shift_to_fit_mesh(BC,zoom,shift);
  54. }
  55. IGL_INLINE void igl::opengl::ViewerCore::align_camera_center(
  56. const Eigen::MatrixXd& V)
  57. {
  58. if(V.rows() == 0)
  59. return;
  60. get_scale_and_shift_to_fit_mesh(V,camera_base_zoom,camera_base_translation);
  61. // Rather than crash on empty mesh...
  62. if(V.size() > 0)
  63. {
  64. object_scale = (V.colwise().maxCoeff() - V.colwise().minCoeff()).norm();
  65. }
  66. }
  67. IGL_INLINE void igl::opengl::ViewerCore::get_scale_and_shift_to_fit_mesh(
  68. const Eigen::MatrixXd& V,
  69. float& zoom,
  70. Eigen::Vector3f& shift)
  71. {
  72. if (V.rows() == 0)
  73. return;
  74. auto min_point = V.colwise().minCoeff();
  75. auto max_point = V.colwise().maxCoeff();
  76. auto centroid = (0.5*(min_point + max_point)).eval();
  77. shift.setConstant(0);
  78. shift.head(centroid.size()) = -centroid.cast<float>();
  79. zoom = 2.0 / (max_point-min_point).array().abs().maxCoeff();
  80. }
  81. IGL_INLINE void igl::opengl::ViewerCore::clear_framebuffers()
  82. {
  83. // The glScissor call ensures we only clear this core's buffers,
  84. // (in case the user wants different background colors in each viewport.)
  85. glScissor(viewport(0), viewport(1), viewport(2), viewport(3));
  86. glEnable(GL_SCISSOR_TEST);
  87. glClearColor(background_color[0],
  88. background_color[1],
  89. background_color[2],
  90. background_color[3]);
  91. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. glDisable(GL_SCISSOR_TEST);
  93. }
  94. IGL_INLINE void igl::opengl::ViewerCore::draw(
  95. ViewerData& data,
  96. bool update_matrices)
  97. {
  98. using namespace std;
  99. using namespace Eigen;
  100. if (depth_test)
  101. glEnable(GL_DEPTH_TEST);
  102. else
  103. glDisable(GL_DEPTH_TEST);
  104. glEnable(GL_BLEND);
  105. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  106. /* Bind and potentially refresh mesh/line/point data */
  107. if (data.dirty)
  108. {
  109. data.updateGL(data, data.invert_normals, data.meshgl);
  110. data.dirty = MeshGL::DIRTY_NONE;
  111. }
  112. data.meshgl.bind_mesh();
  113. // Initialize uniform
  114. glViewport(viewport(0), viewport(1), viewport(2), viewport(3));
  115. if(update_matrices)
  116. {
  117. view = Eigen::Matrix4f::Identity();
  118. proj = Eigen::Matrix4f::Identity();
  119. norm = Eigen::Matrix4f::Identity();
  120. float width = viewport(2);
  121. float height = viewport(3);
  122. // Set view
  123. look_at( camera_eye, camera_center, camera_up, view);
  124. view = view
  125. * (trackball_angle * Eigen::Scaling(camera_zoom * camera_base_zoom)
  126. * Eigen::Translation3f(camera_translation + camera_base_translation)).matrix();
  127. norm = view.inverse().transpose();
  128. // Set projection
  129. if (orthographic)
  130. {
  131. float length = (camera_eye - camera_center).norm();
  132. float h = tan(camera_view_angle/360.0 * igl::PI) * (length);
  133. ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
  134. }
  135. else
  136. {
  137. float fH = tan(camera_view_angle / 360.0 * igl::PI) * camera_dnear;
  138. float fW = fH * (double)width/(double)height;
  139. frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
  140. }
  141. }
  142. // Send transformations to the GPU
  143. GLint viewi = glGetUniformLocation(data.meshgl.shader_mesh,"view");
  144. GLint proji = glGetUniformLocation(data.meshgl.shader_mesh,"proj");
  145. GLint normi = glGetUniformLocation(data.meshgl.shader_mesh,"normal_matrix");
  146. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  147. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  148. glUniformMatrix4fv(normi, 1, GL_FALSE, norm.data());
  149. // Light parameters
  150. GLint specular_exponenti = glGetUniformLocation(data.meshgl.shader_mesh,"specular_exponent");
  151. GLint light_position_eyei = glGetUniformLocation(data.meshgl.shader_mesh,"light_position_eye");
  152. GLint lighting_factori = glGetUniformLocation(data.meshgl.shader_mesh,"lighting_factor");
  153. GLint fixed_colori = glGetUniformLocation(data.meshgl.shader_mesh,"fixed_color");
  154. GLint texture_factori = glGetUniformLocation(data.meshgl.shader_mesh,"texture_factor");
  155. GLint matcap_factori = glGetUniformLocation(data.meshgl.shader_mesh,"matcap_factor");
  156. GLint double_sidedi = glGetUniformLocation(data.meshgl.shader_mesh,"double_sided");
  157. GLint pseudocolor_with_normalsi = glGetUniformLocation(data.meshgl.shader_mesh,"pseudocolor_with_normals");
  158. const bool eff_is_directional_light = is_directional_light || is_shadow_mapping;
  159. glUniform1f(specular_exponenti, data.shininess);
  160. if(eff_is_directional_light)
  161. {
  162. Eigen::Vector3f light_direction = light_position.normalized();
  163. glUniform3fv(light_position_eyei, 1, light_direction.data());
  164. }else
  165. {
  166. glUniform3fv(light_position_eyei, 1, light_position.data());
  167. }
  168. if(is_shadow_mapping)
  169. {
  170. glUniformMatrix4fv(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_view"), 1, GL_FALSE, shadow_view.data());
  171. glUniformMatrix4fv(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_proj"), 1, GL_FALSE, shadow_proj.data());
  172. glActiveTexture(GL_TEXTURE0+1);
  173. glBindTexture(GL_TEXTURE_2D, shadow_depth_tex);
  174. {
  175. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_tex"), 1);
  176. }
  177. }
  178. glUniform1f(lighting_factori, lighting_factor); // enables lighting
  179. glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
  180. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"is_directional_light"),eff_is_directional_light);
  181. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"is_shadow_mapping"),is_shadow_mapping);
  182. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_pass"),false);
  183. if (data.V.rows()>0)
  184. {
  185. // Render fill
  186. if (is_set(data.show_faces))
  187. {
  188. // Texture
  189. glUniform1f(texture_factori, is_set(data.show_texture) ? 1.0f : 0.0f);
  190. glUniform1f(matcap_factori, is_set(data.use_matcap) ? 1.0f : 0.0f);
  191. glUniform1f(double_sidedi, data.double_sided ? 1.0f : 0.0f);
  192. glUniform1i(pseudocolor_with_normalsi, data.pseudocolor_with_normals);
  193. data.meshgl.draw_mesh(true);
  194. glUniform1f(matcap_factori, 0.0f);
  195. glUniform1f(texture_factori, 0.0f);
  196. }
  197. // Render wireframe
  198. if (is_set(data.show_lines))
  199. {
  200. glLineWidth(data.line_width);
  201. glUniform4f(fixed_colori,
  202. data.line_color[0],
  203. data.line_color[1],
  204. data.line_color[2],
  205. data.line_color[3]);
  206. glUniform1i(pseudocolor_with_normalsi, false);
  207. data.meshgl.draw_mesh(false);
  208. glUniform4f(fixed_colori, 0.0f, 0.0f, 0.0f, 0.0f);
  209. }
  210. }
  211. if (is_set(data.show_overlay))
  212. {
  213. if (is_set(data.show_overlay_depth))
  214. glEnable(GL_DEPTH_TEST);
  215. else
  216. glDisable(GL_DEPTH_TEST);
  217. if (data.lines.rows() > 0)
  218. {
  219. data.meshgl.bind_overlay_lines();
  220. viewi = glGetUniformLocation(data.meshgl.shader_overlay_lines,"view");
  221. proji = glGetUniformLocation(data.meshgl.shader_overlay_lines,"proj");
  222. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  223. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  224. // This must be enabled, otherwise glLineWidth has no effect
  225. glEnable(GL_LINE_SMOOTH);
  226. glLineWidth(data.line_width);
  227. data.meshgl.draw_overlay_lines();
  228. }
  229. if (data.points.rows() > 0)
  230. {
  231. data.meshgl.bind_overlay_points();
  232. viewi = glGetUniformLocation(data.meshgl.shader_overlay_points,"view");
  233. proji = glGetUniformLocation(data.meshgl.shader_overlay_points,"proj");
  234. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  235. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  236. glPointSize(data.point_size);
  237. data.meshgl.draw_overlay_points();
  238. }
  239. glEnable(GL_DEPTH_TEST);
  240. }
  241. if(is_set(data.show_vertex_labels)&&data.vertex_labels_positions.rows()>0)
  242. draw_labels(data, data.meshgl.vertex_labels);
  243. if(is_set(data.show_face_labels)&&data.face_labels_positions.rows()>0)
  244. draw_labels(data, data.meshgl.face_labels);
  245. if(is_set(data.show_custom_labels)&&data.labels_positions.rows()>0)
  246. draw_labels(data, data.meshgl.custom_labels);
  247. }
  248. IGL_INLINE void igl::opengl::ViewerCore::initialize_shadow_pass()
  249. {
  250. // attach buffers
  251. glBindFramebuffer(GL_FRAMEBUFFER, shadow_depth_fbo);
  252. glBindRenderbuffer(GL_RENDERBUFFER, shadow_color_rbo);
  253. // clear buffer
  254. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  255. // In the libigl viewer setup, each mesh has its own shader program. This is
  256. // kind of funny because they should all be the same, just different uniform
  257. // values.
  258. glViewport(0,0,shadow_width,shadow_height);
  259. // Assumes light is directional
  260. assert(is_directional_light);
  261. Eigen::Vector3f camera_eye = light_position.normalized()*5;
  262. Eigen::Vector3f camera_up = [&camera_eye]()
  263. {
  264. Eigen::Matrix<float,3,2> T;
  265. igl::null(camera_eye.transpose().eval(),T);
  266. return T.col(0);
  267. }();
  268. Eigen::Vector3f camera_center = this->camera_center;
  269. // Same camera parameters except 2× field of view and reduced far plane
  270. float camera_view_angle = 2*this->camera_view_angle;
  271. float camera_dnear = this->camera_dnear;
  272. float camera_dfar = this->camera_dfar;
  273. Eigen::Quaternionf trackball_angle = this->trackball_angle;
  274. float camera_zoom = this->camera_zoom;
  275. float camera_base_zoom = this->camera_base_zoom;
  276. Eigen::Vector3f camera_translation = this->camera_translation;
  277. Eigen::Vector3f camera_base_translation = this->camera_base_translation;
  278. camera_dfar = exp2( 0.5 * ( log2(camera_dnear) + log2(camera_dfar)));
  279. igl::look_at( camera_eye, camera_center, camera_up, shadow_view);
  280. shadow_view = shadow_view
  281. * (trackball_angle * Eigen::Scaling(camera_zoom * camera_base_zoom)
  282. * Eigen::Translation3f(camera_translation + camera_base_translation)).matrix();
  283. float length = (camera_eye - camera_center).norm();
  284. float h = tan(camera_view_angle/360.0 * igl::PI) * (length);
  285. igl::ortho(-h*shadow_width/shadow_height, h*shadow_width/shadow_height, -h, h, camera_dnear, camera_dfar,shadow_proj);
  286. }
  287. IGL_INLINE void igl::opengl::ViewerCore::deinitialize_shadow_pass()
  288. {
  289. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  290. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  291. }
  292. IGL_INLINE void igl::opengl::ViewerCore::draw_shadow_pass(
  293. ViewerData& data,
  294. bool /*update_matrices*/)
  295. {
  296. if (data.dirty)
  297. {
  298. data.updateGL(data, data.invert_normals, data.meshgl);
  299. data.dirty = igl::opengl::MeshGL::DIRTY_NONE;
  300. }
  301. data.meshgl.bind_mesh();
  302. // Send transformations to the GPU as if rendering from shadow point of view
  303. GLint viewi = glGetUniformLocation(data.meshgl.shader_mesh,"view");
  304. GLint proji = glGetUniformLocation(data.meshgl.shader_mesh,"proj");
  305. glUniformMatrix4fv(viewi, 1, GL_FALSE, shadow_view.data());
  306. glUniformMatrix4fv(proji, 1, GL_FALSE, shadow_proj.data());
  307. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_pass"),true);
  308. data.meshgl.draw_mesh(true);
  309. glUniform1i(glGetUniformLocation(data.meshgl.shader_mesh,"shadow_pass"),false);
  310. }
  311. IGL_INLINE void igl::opengl::ViewerCore::draw_buffer(
  312. ViewerData& data,
  313. bool update_matrices,
  314. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& R,
  315. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& G,
  316. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& B,
  317. Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic>& A)
  318. {
  319. assert(R.rows() == G.rows() && G.rows() == B.rows() && B.rows() == A.rows());
  320. assert(R.cols() == G.cols() && G.cols() == B.cols() && B.cols() == A.cols());
  321. unsigned width = R.rows();
  322. unsigned height = R.cols();
  323. if(width == 0 && height == 0)
  324. {
  325. width = viewport(2);
  326. height = viewport(3);
  327. }
  328. R.resize(width,height);
  329. G.resize(width,height);
  330. B.resize(width,height);
  331. A.resize(width,height);
  332. ////////////////////////////////////////////////////////////////////////
  333. // PREPARE width×height BUFFERS does *not* depend on `data`
  334. // framebuffer
  335. // textureColorBufferMultiSampled
  336. // rbo
  337. // intermediateFBO
  338. // screenTexture
  339. //
  340. ////////////////////////////////////////////////////////////////////////
  341. // https://learnopengl.com/Advanced-OpenGL/Anti-Aliasing
  342. // Create an initial multisampled framebuffer
  343. unsigned int framebuffer;
  344. glGenFramebuffers(1, &framebuffer);
  345. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  346. // create a multisampled color attachment texture
  347. unsigned int textureColorBufferMultiSampled;
  348. glGenTextures(1, &textureColorBufferMultiSampled);
  349. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled);
  350. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
  351. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
  352. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textureColorBufferMultiSampled, 0);
  353. // create a (also multisampled) renderbuffer object for depth and stencil attachments
  354. unsigned int rbo;
  355. glGenRenderbuffers(1, &rbo);
  356. glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  357. glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
  358. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  359. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
  360. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  361. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  362. // configure second post-processing framebuffer
  363. unsigned int intermediateFBO;
  364. glGenFramebuffers(1, &intermediateFBO);
  365. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  366. // create a color attachment texture
  367. unsigned int screenTexture;
  368. glGenTextures(1, &screenTexture);
  369. glBindTexture(GL_TEXTURE_2D, screenTexture);
  370. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  371. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  372. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  373. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0); // we only need a color buffer
  374. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  375. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  376. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  377. // Clear the buffer
  378. glClearColor(background_color(0), background_color(1), background_color(2), 0.f);
  379. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  380. // Save old viewport
  381. Eigen::Vector4f viewport_ori = viewport;
  382. viewport << 0,0,width,height;
  383. // Draw
  384. draw(data,update_matrices);
  385. // Restore viewport
  386. viewport = viewport_ori;
  387. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
  388. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO);
  389. glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  390. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  391. // Copy back in the given Eigen matrices
  392. GLubyte* pixels = (GLubyte*)calloc(width*height*4,sizeof(GLubyte));
  393. glReadPixels(0, 0,width, height,GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  394. // Clean up
  395. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  396. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  397. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  398. glDeleteTextures(1, &screenTexture);
  399. glDeleteTextures(1, &textureColorBufferMultiSampled);
  400. glDeleteFramebuffers(1, &framebuffer);
  401. glDeleteFramebuffers(1, &intermediateFBO);
  402. glDeleteRenderbuffers(1, &rbo);
  403. int count = 0;
  404. for (unsigned j=0; j<height; ++j)
  405. {
  406. for (unsigned i=0; i<width; ++i)
  407. {
  408. R(i,j) = pixels[count*4+0];
  409. G(i,j) = pixels[count*4+1];
  410. B(i,j) = pixels[count*4+2];
  411. A(i,j) = pixels[count*4+3];
  412. ++count;
  413. }
  414. }
  415. // Clean up
  416. free(pixels);
  417. }
  418. // Define uniforms for text labels
  419. IGL_INLINE void igl::opengl::ViewerCore::draw_labels(
  420. ViewerData& data,
  421. const igl::opengl::MeshGL::TextGL& labels
  422. ){
  423. glDisable(GL_LINE_SMOOTH); // Clear settings if overlay is activated
  424. data.meshgl.bind_labels(labels);
  425. GLint viewi = glGetUniformLocation(data.meshgl.shader_text,"view");
  426. GLint proji = glGetUniformLocation(data.meshgl.shader_text,"proj");
  427. glUniformMatrix4fv(viewi, 1, GL_FALSE, view.data());
  428. glUniformMatrix4fv(proji, 1, GL_FALSE, proj.data());
  429. // Parameters for mapping characters from font atlass
  430. float width = viewport(2);
  431. float height = viewport(3);
  432. float text_shift_scale_factor = orthographic ? 0.01 : 0.03;
  433. float render_scale = (orthographic ? 0.6 : 1.7) * data.label_size;
  434. glUniform1f(glGetUniformLocation(data.meshgl.shader_text, "TextShiftFactor"), text_shift_scale_factor);
  435. glUniform3f(glGetUniformLocation(data.meshgl.shader_text, "TextColor"), data.label_color(0), data.label_color(1), data.label_color(2));
  436. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "CellSize"), 1.0f / 16, (300.0f / 384) / 6);
  437. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "CellOffset"), 0.5 / 256.0, 0.5 / 256.0);
  438. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "RenderSize"),
  439. render_scale * 0.75 * 16 / (width),
  440. render_scale * 0.75 * 33.33 / (height));
  441. glUniform2f(glGetUniformLocation(data.meshgl.shader_text, "RenderOrigin"), -2, 2);
  442. data.meshgl.draw_labels(labels);
  443. glEnable(GL_DEPTH_TEST);
  444. }
  445. IGL_INLINE void igl::opengl::ViewerCore::set_rotation_type(
  446. const igl::opengl::ViewerCore::RotationType & value)
  447. {
  448. using namespace Eigen;
  449. using namespace std;
  450. const RotationType old_rotation_type = rotation_type;
  451. rotation_type = value;
  452. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  453. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  454. {
  455. snap_to_fixed_up(Quaternionf(trackball_angle),trackball_angle);
  456. }
  457. }
  458. IGL_INLINE void igl::opengl::ViewerCore::set(unsigned int &property_mask, bool value) const
  459. {
  460. if (!value)
  461. unset(property_mask);
  462. else
  463. property_mask |= id;
  464. }
  465. IGL_INLINE void igl::opengl::ViewerCore::unset(unsigned int &property_mask) const
  466. {
  467. property_mask &= ~id;
  468. }
  469. IGL_INLINE void igl::opengl::ViewerCore::toggle(unsigned int &property_mask) const
  470. {
  471. property_mask ^= id;
  472. }
  473. IGL_INLINE bool igl::opengl::ViewerCore::is_set(unsigned int property_mask) const
  474. {
  475. return (property_mask & id);
  476. }
  477. IGL_INLINE igl::opengl::ViewerCore::ViewerCore()
  478. {
  479. // Default colors
  480. background_color << 0.3f, 0.3f, 0.5f, 1.0f;
  481. // Default lights settings
  482. light_position << 0.0f, 0.3f, 0.0f;
  483. is_directional_light = false;
  484. is_shadow_mapping = false;
  485. shadow_width = 2056;
  486. shadow_height = 2056;
  487. lighting_factor = 1.0f; //on
  488. // Default trackball
  489. trackball_angle = Eigen::Quaternionf::Identity();
  490. rotation_type = ViewerCore::ROTATION_TYPE_TRACKBALL;
  491. set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  492. // Camera parameters
  493. camera_base_zoom = 1.0f;
  494. camera_zoom = 1.0f;
  495. orthographic = false;
  496. camera_view_angle = 45.0;
  497. camera_dnear = 1.0;
  498. camera_dfar = 100.0;
  499. camera_base_translation << 0, 0, 0;
  500. camera_translation << 0, 0, 0;
  501. camera_eye << 0, 0, 5;
  502. camera_center << 0, 0, 0;
  503. camera_up << 0, 1, 0;
  504. depth_test = true;
  505. is_animating = false;
  506. animation_max_fps = 30.;
  507. viewport.setZero();
  508. }
  509. IGL_INLINE void igl::opengl::ViewerCore::init()
  510. {
  511. delete_shadow_buffers();
  512. generate_shadow_buffers();
  513. }
  514. IGL_INLINE void igl::opengl::ViewerCore::shut()
  515. {
  516. delete_shadow_buffers();
  517. }
  518. IGL_INLINE void igl::opengl::ViewerCore::delete_shadow_buffers()
  519. {
  520. glDeleteTextures(1,&shadow_depth_tex);
  521. glDeleteFramebuffers(1,&shadow_depth_fbo);
  522. glDeleteRenderbuffers(1,&shadow_color_rbo);
  523. }
  524. IGL_INLINE void igl::opengl::ViewerCore::generate_shadow_buffers()
  525. {
  526. // Create a texture for writing the shadow map depth values into
  527. {
  528. glDeleteTextures(1,&shadow_depth_tex);
  529. glGenTextures(1, &shadow_depth_tex);
  530. glBindTexture(GL_TEXTURE_2D, shadow_depth_tex);
  531. // Should this be using double/float precision?
  532. glTexImage2D(
  533. GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
  534. shadow_width,
  535. shadow_height,
  536. 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  537. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  538. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  539. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  540. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  541. glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, Eigen::Vector4f(1,1,1,1).data() );
  542. glBindTexture(GL_TEXTURE_2D, 0);
  543. }
  544. // Generate a framebuffer with depth attached to this texture and color
  545. // attached to a render buffer object
  546. glGenFramebuffers(1, &shadow_depth_fbo);
  547. glBindFramebuffer(GL_FRAMEBUFFER, shadow_depth_fbo);
  548. // Attach depth texture
  549. glFramebufferTexture2D(
  550. GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
  551. shadow_depth_tex,0);
  552. // Generate a render buffer to write colors into. Low precision we don't
  553. // care about them. Is there a way to not write/compute them at? Probably
  554. // just need to change fragment shader.
  555. glGenRenderbuffers(1,&shadow_color_rbo);
  556. glBindRenderbuffer(GL_RENDERBUFFER,shadow_color_rbo);
  557. glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, shadow_width, shadow_height);
  558. // Attach color buffer
  559. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
  560. GL_RENDERBUFFER, shadow_color_rbo);
  561. //Does the GPU support current FBO configuration?
  562. GLenum status;
  563. status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  564. switch(status)
  565. {
  566. case GL_FRAMEBUFFER_COMPLETE:
  567. break;
  568. default:
  569. printf("[ViewerCore] Error: We failed to set up a good FBO: %d\n",status);
  570. assert(false);
  571. }
  572. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  573. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  574. }