ViewerCore.cpp 22 KB

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