2
0

ViewerCore.cpp 22 KB

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