Viewer.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 "Viewer.h"
  9. #include <chrono>
  10. #include <thread>
  11. #include <Eigen/LU>
  12. #include "../gl.h"
  13. #include "../report_gl_error.h"
  14. #include <GLFW/glfw3.h>
  15. #include <cmath>
  16. #include <cstdio>
  17. #include <sstream>
  18. #include <iomanip>
  19. #include <iostream>
  20. #include <fstream>
  21. #include <algorithm>
  22. #include <limits>
  23. #include <cassert>
  24. #include <igl/project.h>
  25. #include <igl/get_seconds.h>
  26. #include <igl/readOBJ.h>
  27. #include <igl/read_triangle_mesh.h>
  28. #include <igl/adjacency_list.h>
  29. #include <igl/writeOBJ.h>
  30. #include <igl/writeOFF.h>
  31. #include <igl/massmatrix.h>
  32. #include <igl/file_dialog_open.h>
  33. #include <igl/file_dialog_save.h>
  34. #include <igl/quat_mult.h>
  35. #include <igl/axis_angle_to_quat.h>
  36. #include <igl/trackball.h>
  37. #include <igl/two_axis_valuator_fixed_up.h>
  38. #include <igl/snap_to_canonical_view_quat.h>
  39. #include <igl/unproject.h>
  40. #include <igl/serialize.h>
  41. // Internal global variables used for glfw event handling
  42. static igl::opengl::glfw::Viewer * __viewer;
  43. static double highdpi = 1;
  44. static double scroll_x = 0;
  45. static double scroll_y = 0;
  46. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  47. {
  48. igl::opengl::glfw::Viewer::MouseButton mb;
  49. if (button == GLFW_MOUSE_BUTTON_1)
  50. mb = igl::opengl::glfw::Viewer::MouseButton::Left;
  51. else if (button == GLFW_MOUSE_BUTTON_2)
  52. mb = igl::opengl::glfw::Viewer::MouseButton::Right;
  53. else //if (button == GLFW_MOUSE_BUTTON_3)
  54. mb = igl::opengl::glfw::Viewer::MouseButton::Middle;
  55. if (action == GLFW_PRESS)
  56. __viewer->mouse_down(mb,modifier);
  57. else
  58. __viewer->mouse_up(mb,modifier);
  59. }
  60. static void glfw_error_callback(int error, const char* description)
  61. {
  62. fputs(description, stderr);
  63. }
  64. static void glfw_char_mods_callback(GLFWwindow* window, unsigned int codepoint, int modifier)
  65. {
  66. __viewer->key_pressed(codepoint, modifier);
  67. }
  68. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  69. {
  70. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  71. glfwSetWindowShouldClose(window, GL_TRUE);
  72. if (action == GLFW_PRESS)
  73. __viewer->key_down(key, modifier);
  74. else if(action == GLFW_RELEASE)
  75. __viewer->key_up(key, modifier);
  76. }
  77. static void glfw_window_size(GLFWwindow* window, int width, int height)
  78. {
  79. int w = width*highdpi;
  80. int h = height*highdpi;
  81. __viewer->post_resize(w, h);
  82. }
  83. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  84. {
  85. __viewer->mouse_move(x*highdpi, y*highdpi);
  86. }
  87. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  88. {
  89. using namespace std;
  90. scroll_x += x;
  91. scroll_y += y;
  92. __viewer->mouse_scroll(y);
  93. }
  94. static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
  95. {
  96. }
  97. namespace igl
  98. {
  99. namespace opengl
  100. {
  101. namespace glfw
  102. {
  103. IGL_INLINE int Viewer::launch(bool resizable /*= true*/, bool fullscreen /*= false*/,
  104. const std::string &name, int windowWidth /*= 0*/, int windowHeight /*= 0*/)
  105. {
  106. // TODO return values are being ignored...
  107. launch_init(resizable,fullscreen,name,windowWidth,windowHeight);
  108. launch_rendering(true);
  109. launch_shut();
  110. return EXIT_SUCCESS;
  111. }
  112. IGL_INLINE int Viewer::launch_init(bool resizable, bool fullscreen,
  113. const std::string &name, int windowWidth, int windowHeight)
  114. {
  115. glfwSetErrorCallback(glfw_error_callback);
  116. if (!glfwInit())
  117. {
  118. return EXIT_FAILURE;
  119. }
  120. glfwWindowHint(GLFW_SAMPLES, 8);
  121. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  122. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  123. #ifdef __APPLE__
  124. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  125. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  126. #endif
  127. if(fullscreen)
  128. {
  129. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  130. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  131. window = glfwCreateWindow(mode->width,mode->height,name.c_str(),monitor,nullptr);
  132. windowWidth = mode->width;
  133. windowHeight = mode->height;
  134. }
  135. else
  136. {
  137. // Set default windows width
  138. if (windowWidth <= 0 && core_list.size() == 1 && core().viewport[2] > 0)
  139. windowWidth = core().viewport[2];
  140. else if (windowWidth <= 0)
  141. windowWidth = 1280;
  142. // Set default windows height
  143. if (windowHeight <= 0 && core_list.size() == 1 && core().viewport[3] > 0)
  144. windowHeight = core().viewport[3];
  145. else if (windowHeight <= 0)
  146. windowHeight = 800;
  147. window = glfwCreateWindow(windowWidth,windowHeight,name.c_str(),nullptr,nullptr);
  148. }
  149. if (!window)
  150. {
  151. glfwTerminate();
  152. return EXIT_FAILURE;
  153. }
  154. glfwMakeContextCurrent(window);
  155. // Load OpenGL and its extensions
  156. if (!gladLoadGL((GLADloadfunc) glfwGetProcAddress))
  157. {
  158. printf("Failed to load OpenGL and its extensions\n");
  159. return(-1);
  160. }
  161. #if defined(DEBUG) || defined(_DEBUG)
  162. printf("OpenGL Version %d.%d loaded\n", GLVersion.major, GLVersion.minor);
  163. int major, minor, rev;
  164. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  165. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  166. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  167. printf("OpenGL version received: %d.%d.%d\n", major, minor, rev);
  168. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  169. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  170. #endif
  171. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  172. // Initialize FormScreen
  173. __viewer = this;
  174. // Register callbacks
  175. glfwSetKeyCallback(window, glfw_key_callback);
  176. glfwSetCursorPosCallback(window,glfw_mouse_move);
  177. glfwSetWindowSizeCallback(window,glfw_window_size);
  178. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  179. glfwSetScrollCallback(window,glfw_mouse_scroll);
  180. glfwSetCharModsCallback(window,glfw_char_mods_callback);
  181. glfwSetDropCallback(window,glfw_drop_callback);
  182. // Handle retina displays (windows and mac)
  183. int width, height;
  184. glfwGetFramebufferSize(window, &width, &height);
  185. int width_window, height_window;
  186. glfwGetWindowSize(window, &width_window, &height_window);
  187. highdpi = windowWidth/width_window;
  188. glfw_window_size(window,width_window,height_window);
  189. // Initialize IGL viewer
  190. init();
  191. for(auto &core : this->core_list)
  192. {
  193. for(auto &data : this->data_list)
  194. {
  195. if(data.is_visible & core.id)
  196. {
  197. this->core(core.id).align_camera_center(data.V, data.F);
  198. }
  199. }
  200. }
  201. return EXIT_SUCCESS;
  202. }
  203. IGL_INLINE bool Viewer::launch_rendering(bool loop)
  204. {
  205. // glfwMakeContextCurrent(window);
  206. // Rendering loop
  207. const int num_extra_frames = 5;
  208. int frame_counter = 0;
  209. while (!glfwWindowShouldClose(window))
  210. {
  211. double tic = get_seconds();
  212. draw();
  213. glfwSwapBuffers(window);
  214. if(core().is_animating || frame_counter++ < num_extra_frames)
  215. {
  216. glfwPollEvents();
  217. }else
  218. {
  219. glfwWaitEvents();
  220. frame_counter = 0;
  221. }
  222. // In microseconds
  223. double duration = 1000000.*(get_seconds()-tic);
  224. const double min_duration = 1000000./core().animation_max_fps;
  225. if(duration<min_duration)
  226. {
  227. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  228. }
  229. if (!loop)
  230. return !glfwWindowShouldClose(window);
  231. #ifdef __APPLE__
  232. static bool first_time_hack = true;
  233. if(first_time_hack) {
  234. glfwHideWindow(window);
  235. glfwShowWindow(window);
  236. first_time_hack = false;
  237. }
  238. #endif
  239. }
  240. return EXIT_SUCCESS;
  241. }
  242. IGL_INLINE void Viewer::launch_shut()
  243. {
  244. for(auto & data : data_list)
  245. {
  246. data.meshgl.free();
  247. }
  248. for(auto &core : this->core_list)
  249. {
  250. core.shut();
  251. }
  252. shutdown_plugins();
  253. glfwDestroyWindow(window);
  254. glfwTerminate();
  255. return;
  256. }
  257. IGL_INLINE void Viewer::init()
  258. {
  259. for(auto &core : this->core_list)
  260. {
  261. core.init();
  262. }
  263. if (callback_init)
  264. if (callback_init(*this))
  265. return;
  266. init_plugins();
  267. }
  268. IGL_INLINE void Viewer::init_plugins()
  269. {
  270. // Init all plugins
  271. for (unsigned int i = 0; i<plugins.size(); ++i)
  272. {
  273. plugins[i]->init(this);
  274. }
  275. }
  276. IGL_INLINE void Viewer::shutdown_plugins()
  277. {
  278. for (unsigned int i = 0; i<plugins.size(); ++i)
  279. {
  280. plugins[i]->shutdown();
  281. }
  282. }
  283. IGL_INLINE Viewer::Viewer():
  284. data_list(1),
  285. selected_data_index(0),
  286. next_data_id(1),
  287. selected_core_index(0),
  288. next_core_id(2)
  289. {
  290. window = nullptr;
  291. data_list.front().id = 0;
  292. core_list.emplace_back(ViewerCore());
  293. core_list.front().id = 1;
  294. // Temporary variables initialization
  295. down = false;
  296. hack_never_moved = true;
  297. scroll_position = 0.0f;
  298. // Per face
  299. data().set_face_based(false);
  300. // C-style callbacks
  301. callback_init = nullptr;
  302. callback_pre_draw = nullptr;
  303. callback_post_draw = nullptr;
  304. callback_mouse_down = nullptr;
  305. callback_mouse_up = nullptr;
  306. callback_mouse_move = nullptr;
  307. callback_mouse_scroll = nullptr;
  308. callback_key_down = nullptr;
  309. callback_key_up = nullptr;
  310. callback_init_data = nullptr;
  311. callback_pre_draw_data = nullptr;
  312. callback_post_draw_data = nullptr;
  313. callback_mouse_down_data = nullptr;
  314. callback_mouse_up_data = nullptr;
  315. callback_mouse_move_data = nullptr;
  316. callback_mouse_scroll_data = nullptr;
  317. callback_key_down_data = nullptr;
  318. callback_key_up_data = nullptr;
  319. #ifndef IGL_VIEWER_VIEWER_QUIET
  320. const std::string usage(R"(igl::opengl::glfw::Viewer usage:
  321. [drag] Rotate scene
  322. A,a Toggle animation (tight draw loop)
  323. D,d Toggle double sided lighting
  324. F,f Toggle face based
  325. I,i Toggle invert normals
  326. L,l Toggle wireframe
  327. O,o Toggle orthographic/perspective projection
  328. S,s Toggle shadows
  329. T,t Toggle filled faces
  330. Z Snap to canonical view
  331. [,] Toggle between rotation control types (trackball, two-axis
  332. valuator with fixed up, 2D mode with no rotation))
  333. <,> Toggle between models
  334. ; Toggle vertex labels
  335. : Toggle face labels)"
  336. );
  337. std::cout<<usage<<std::endl;
  338. #endif
  339. }
  340. IGL_INLINE Viewer::~Viewer()
  341. {
  342. }
  343. IGL_INLINE bool Viewer::load_mesh_from_file(
  344. const std::string & mesh_file_name_string)
  345. {
  346. // first try to load it with a plugin
  347. for (unsigned int i = 0; i<plugins.size(); ++i)
  348. {
  349. if (plugins[i]->load(mesh_file_name_string))
  350. {
  351. return true;
  352. }
  353. }
  354. // Create new data slot and set to selected
  355. if(!(data().F.rows() == 0 && data().V.rows() == 0))
  356. {
  357. append_mesh();
  358. }
  359. data().clear();
  360. size_t last_dot = mesh_file_name_string.rfind('.');
  361. if (last_dot == std::string::npos)
  362. {
  363. std::cerr<<"Error: No file extension found in "<<
  364. mesh_file_name_string<<std::endl;
  365. return false;
  366. }
  367. std::string extension = mesh_file_name_string.substr(last_dot+1);
  368. if (extension == "obj" || extension =="OBJ")
  369. {
  370. Eigen::MatrixXd corner_normals;
  371. Eigen::MatrixXi fNormIndices;
  372. Eigen::MatrixXd UV_V;
  373. Eigen::MatrixXi UV_F;
  374. Eigen::MatrixXd V;
  375. Eigen::MatrixXi F;
  376. if (!(
  377. igl::readOBJ(
  378. mesh_file_name_string,
  379. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  380. {
  381. return false;
  382. }
  383. data().set_mesh(V,F);
  384. if(UV_V.rows() != 0 && UV_F.rows() != 0)
  385. {
  386. data().set_uv(UV_V,UV_F);
  387. }
  388. }else
  389. {
  390. Eigen::MatrixXd V;
  391. Eigen::MatrixXi F;
  392. if (!igl::read_triangle_mesh(mesh_file_name_string, V, F))
  393. {
  394. // unrecognized file type
  395. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  396. return false;
  397. }
  398. data().set_mesh(V,F);
  399. }
  400. data().compute_normals();
  401. data().uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  402. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  403. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  404. for(int i=0;i<core_list.size(); i++)
  405. core_list[i].align_camera_center(data().V,data().F);
  406. for (unsigned int i = 0; i<plugins.size(); ++i)
  407. if (plugins[i]->post_load())
  408. return true;
  409. return true;
  410. }
  411. IGL_INLINE bool Viewer::save_mesh_to_file(
  412. const std::string & mesh_file_name_string)
  413. {
  414. // first try to load it with a plugin
  415. for (unsigned int i = 0; i<plugins.size(); ++i)
  416. if (plugins[i]->save(mesh_file_name_string))
  417. return true;
  418. size_t last_dot = mesh_file_name_string.rfind('.');
  419. if (last_dot == std::string::npos)
  420. {
  421. // No file type determined
  422. std::cerr<<"Error: No file extension found in "<<
  423. mesh_file_name_string<<std::endl;
  424. return false;
  425. }
  426. std::string extension = mesh_file_name_string.substr(last_dot+1);
  427. if (extension == "off" || extension =="OFF")
  428. {
  429. return igl::writeOFF(
  430. mesh_file_name_string,data().V,data().F);
  431. }
  432. else if (extension == "obj" || extension =="OBJ")
  433. {
  434. Eigen::MatrixXd corner_normals;
  435. Eigen::MatrixXi fNormIndices;
  436. Eigen::MatrixXd UV_V;
  437. Eigen::MatrixXi UV_F;
  438. return igl::writeOBJ(mesh_file_name_string,
  439. data().V,
  440. data().F,
  441. corner_normals, fNormIndices, UV_V, UV_F);
  442. }
  443. else
  444. {
  445. // unrecognized file type
  446. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  447. return false;
  448. }
  449. return true;
  450. }
  451. IGL_INLINE bool Viewer::key_pressed(unsigned int unicode_key,int modifiers)
  452. {
  453. for (unsigned int i = 0; i<plugins.size(); ++i)
  454. {
  455. if (plugins[i]->key_pressed(unicode_key, modifiers))
  456. {
  457. return true;
  458. }
  459. }
  460. if (callback_key_pressed)
  461. if (callback_key_pressed(*this,unicode_key,modifiers))
  462. return true;
  463. switch(unicode_key)
  464. {
  465. case 'A':
  466. case 'a':
  467. {
  468. core().is_animating = !core().is_animating;
  469. return true;
  470. }
  471. case 'D':
  472. case 'd':
  473. {
  474. data().double_sided = !data().double_sided;
  475. return true;
  476. }
  477. case 'F':
  478. case 'f':
  479. {
  480. data().set_face_based(!data().face_based);
  481. return true;
  482. }
  483. case 'I':
  484. case 'i':
  485. {
  486. data().dirty |= MeshGL::DIRTY_NORMAL;
  487. data().invert_normals = !data().invert_normals;
  488. return true;
  489. }
  490. case 'L':
  491. case 'l':
  492. {
  493. core().toggle(data().show_lines);
  494. return true;
  495. }
  496. case 'O':
  497. case 'o':
  498. {
  499. core().orthographic = !core().orthographic;
  500. return true;
  501. }
  502. case 'S':
  503. case 's':
  504. {
  505. if(core().is_directional_light)
  506. {
  507. core().is_shadow_mapping = !core().is_shadow_mapping;
  508. }else
  509. {
  510. if(core().is_shadow_mapping)
  511. {
  512. core().is_shadow_mapping = false;
  513. }else
  514. {
  515. // The light_position when !is_directional_light is interpretted as
  516. // a position relative to the _eye_ (not look-at) position of the
  517. // camera.
  518. //
  519. // Meanwhile shadows only current work in is_directional_light mode.
  520. //
  521. // If the user wants to flip back and forth between [positional lights
  522. // without shadows] and [directional lights with shadows] then they
  523. // can high-jack this key_pressed with a callback.
  524. //
  525. // Until shadows support positional lights, let's switch to
  526. // directional lights here and match the direction best as possible to
  527. // the current light position.
  528. core().is_directional_light = true;
  529. core().light_position = core().light_position + core().camera_eye;
  530. core().is_shadow_mapping = true;
  531. }
  532. }
  533. return true;
  534. }
  535. case 'T':
  536. case 't':
  537. {
  538. core().toggle(data().show_faces);
  539. return true;
  540. }
  541. case 'Z':
  542. {
  543. snap_to_canonical_quaternion();
  544. return true;
  545. }
  546. case '[':
  547. case ']':
  548. {
  549. if(core().rotation_type == ViewerCore::ROTATION_TYPE_TRACKBALL)
  550. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP);
  551. else
  552. core().set_rotation_type(ViewerCore::ROTATION_TYPE_TRACKBALL);
  553. return true;
  554. }
  555. case '<':
  556. case '>':
  557. {
  558. selected_data_index =
  559. (selected_data_index + data_list.size() + (unicode_key=='>'?1:-1))%data_list.size();
  560. return true;
  561. }
  562. case '{':
  563. case '}':
  564. {
  565. selected_core_index =
  566. (selected_core_index + core_list.size() + (unicode_key=='}'?1:-1))%core_list.size();
  567. return true;
  568. }
  569. case ';':
  570. data().show_vertex_labels = !data().show_vertex_labels;
  571. return true;
  572. case ':':
  573. data().show_face_labels = !data().show_face_labels;
  574. return true;
  575. default: break;//do nothing
  576. }
  577. return false;
  578. }
  579. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  580. {
  581. for (unsigned int i = 0; i<plugins.size(); ++i)
  582. if (plugins[i]->key_down(key, modifiers))
  583. return true;
  584. if (callback_key_down)
  585. if (callback_key_down(*this,key,modifiers))
  586. return true;
  587. return false;
  588. }
  589. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  590. {
  591. for (unsigned int i = 0; i<plugins.size(); ++i)
  592. if (plugins[i]->key_up(key, modifiers))
  593. return true;
  594. if (callback_key_up)
  595. if (callback_key_up(*this,key,modifiers))
  596. return true;
  597. return false;
  598. }
  599. IGL_INLINE void Viewer::select_hovered_core()
  600. {
  601. int width_window, height_window;
  602. glfwGetFramebufferSize(window, &width_window, &height_window);
  603. for (int i = 0; i < core_list.size(); i++)
  604. {
  605. Eigen::Vector4f viewport = core_list[i].viewport;
  606. if ((current_mouse_x > viewport[0]) &&
  607. (current_mouse_x < viewport[0] + viewport[2]) &&
  608. ((height_window - current_mouse_y) > viewport[1]) &&
  609. ((height_window - current_mouse_y) < viewport[1] + viewport[3]))
  610. {
  611. selected_core_index = i;
  612. break;
  613. }
  614. }
  615. }
  616. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  617. {
  618. // Remember mouse location at down even if used by callback/plugin
  619. down_mouse_x = current_mouse_x;
  620. down_mouse_y = current_mouse_y;
  621. for (unsigned int i = 0; i<plugins.size(); ++i)
  622. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  623. return true;
  624. if (callback_mouse_down)
  625. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  626. return true;
  627. down = true;
  628. // Select the core containing the click location.
  629. select_hovered_core();
  630. down_translation = core().camera_translation;
  631. // Initialization code for the trackball
  632. Eigen::RowVector3d center = Eigen::RowVector3d(0,0,0);
  633. if(data().V.rows() > 0)
  634. {
  635. // be careful that V may be 2D
  636. center.head(data().V.cols()) = data().V.colwise().sum()/data().V.rows();
  637. }
  638. Eigen::Vector3f coord =
  639. igl::project(
  640. Eigen::Vector3f(center(0),center(1),center(2)),
  641. core().view,
  642. core().proj,
  643. core().viewport);
  644. down_mouse_z = coord[2];
  645. down_rotation = core().trackball_angle;
  646. mouse_mode = MouseMode::Rotation;
  647. switch (button)
  648. {
  649. case MouseButton::Left:
  650. if (core().rotation_type == ViewerCore::ROTATION_TYPE_NO_ROTATION) {
  651. mouse_mode = MouseMode::Translation;
  652. } else {
  653. mouse_mode = MouseMode::Rotation;
  654. }
  655. break;
  656. case MouseButton::Right:
  657. mouse_mode = MouseMode::Translation;
  658. break;
  659. default:
  660. mouse_mode = MouseMode::None;
  661. break;
  662. }
  663. return true;
  664. }
  665. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  666. {
  667. down = false;
  668. for (unsigned int i = 0; i<plugins.size(); ++i)
  669. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  670. return true;
  671. if (callback_mouse_up)
  672. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  673. return true;
  674. mouse_mode = MouseMode::None;
  675. return true;
  676. }
  677. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  678. {
  679. if(hack_never_moved)
  680. {
  681. down_mouse_x = mouse_x;
  682. down_mouse_y = mouse_y;
  683. hack_never_moved = false;
  684. }
  685. current_mouse_x = mouse_x;
  686. current_mouse_y = mouse_y;
  687. for (unsigned int i = 0; i<plugins.size(); ++i)
  688. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  689. return true;
  690. if (callback_mouse_move)
  691. if (callback_mouse_move(*this, mouse_x, mouse_y))
  692. return true;
  693. if (down)
  694. {
  695. // We need the window height to transform the mouse click coordinates into viewport-mouse-click coordinates
  696. // for igl::trackball and igl::two_axis_valuator_fixed_up
  697. int width_window, height_window;
  698. glfwGetFramebufferSize(window, &width_window, &height_window);
  699. switch (mouse_mode)
  700. {
  701. case MouseMode::Rotation:
  702. {
  703. switch(core().rotation_type)
  704. {
  705. default:
  706. assert(false && "Unknown rotation type");
  707. case ViewerCore::ROTATION_TYPE_NO_ROTATION:
  708. break;
  709. case ViewerCore::ROTATION_TYPE_TRACKBALL:
  710. igl::trackball(
  711. core().viewport(2),
  712. core().viewport(3),
  713. 2.0f,
  714. down_rotation,
  715. down_mouse_x - core().viewport(0),
  716. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  717. mouse_x - core().viewport(0),
  718. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  719. core().trackball_angle);
  720. break;
  721. case ViewerCore::ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  722. igl::two_axis_valuator_fixed_up(
  723. core().viewport(2),core().viewport(3),
  724. 2.0,
  725. down_rotation,
  726. down_mouse_x - core().viewport(0),
  727. down_mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  728. mouse_x - core().viewport(0),
  729. mouse_y - (height_window - core().viewport(1) - core().viewport(3)),
  730. core().trackball_angle);
  731. break;
  732. }
  733. //Eigen::Vector4f snapq = core().trackball_angle;
  734. break;
  735. }
  736. case MouseMode::Translation:
  737. {
  738. //translation
  739. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core().viewport[3] - mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  740. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, core().viewport[3] - down_mouse_y, down_mouse_z), core().view, core().proj, core().viewport);
  741. Eigen::Vector3f diff = pos1 - pos0;
  742. core().camera_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  743. break;
  744. }
  745. case MouseMode::Zoom:
  746. {
  747. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  748. core().camera_zoom *= 1 + delta;
  749. down_mouse_x = mouse_x;
  750. down_mouse_y = mouse_y;
  751. break;
  752. }
  753. default:
  754. break;
  755. }
  756. }
  757. return true;
  758. }
  759. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  760. {
  761. // Direct the scrolling operation to the appropriate viewport
  762. // (unless the core selection is locked by an ongoing mouse interaction).
  763. if (!down)
  764. select_hovered_core();
  765. scroll_position += delta_y;
  766. for (unsigned int i = 0; i<plugins.size(); ++i)
  767. if (plugins[i]->mouse_scroll(delta_y))
  768. return true;
  769. if (callback_mouse_scroll)
  770. if (callback_mouse_scroll(*this,delta_y))
  771. return true;
  772. // Only zoom if there's actually a change
  773. if(delta_y != 0)
  774. {
  775. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  776. const float min_zoom = 0.1f;
  777. core().camera_zoom = (core().camera_zoom * mult > min_zoom ? core().camera_zoom * mult : min_zoom);
  778. }
  779. return true;
  780. }
  781. IGL_INLINE bool Viewer::load_scene()
  782. {
  783. std::string fname = igl::file_dialog_open();
  784. if(fname.length() == 0)
  785. return false;
  786. return load_scene(fname);
  787. }
  788. IGL_INLINE bool Viewer::load_scene(std::string fname)
  789. {
  790. igl::deserialize(core(),"Core",fname.c_str());
  791. igl::deserialize(data(),"Data",fname.c_str());
  792. return true;
  793. }
  794. IGL_INLINE bool Viewer::save_scene()
  795. {
  796. std::string fname = igl::file_dialog_save();
  797. if (fname.length() == 0)
  798. return false;
  799. return save_scene(fname);
  800. }
  801. IGL_INLINE bool Viewer::save_scene(std::string fname)
  802. {
  803. igl::serialize(core(),"Core",fname.c_str(),true);
  804. igl::serialize(data(),"Data",fname.c_str());
  805. return true;
  806. }
  807. IGL_INLINE void Viewer::draw()
  808. {
  809. using namespace std;
  810. using namespace Eigen;
  811. int width, height;
  812. glfwGetFramebufferSize(window, &width, &height);
  813. int width_window, height_window;
  814. glfwGetWindowSize(window, &width_window, &height_window);
  815. auto highdpi_tmp = (width_window == 0 || width == 0) ? highdpi : (width/width_window);
  816. if(fabs(highdpi_tmp-highdpi)>1e-8)
  817. {
  818. post_resize(width, height);
  819. highdpi=highdpi_tmp;
  820. }
  821. for (auto& core : core_list)
  822. {
  823. core.clear_framebuffers();
  824. }
  825. for (unsigned int i = 0; i<plugins.size(); ++i)
  826. {
  827. if (plugins[i]->pre_draw())
  828. {
  829. return;
  830. }
  831. }
  832. if (callback_pre_draw)
  833. {
  834. if (callback_pre_draw(*this))
  835. {
  836. return;
  837. }
  838. }
  839. // Shadow pass
  840. for (auto& core : core_list)
  841. {
  842. if(core.is_shadow_mapping)
  843. {
  844. core.initialize_shadow_pass();
  845. for (auto& mesh : data_list)
  846. {
  847. if (mesh.is_visible & core.id)
  848. {
  849. core.draw_shadow_pass(mesh);
  850. }
  851. }
  852. core.deinitialize_shadow_pass();
  853. }
  854. }
  855. for (auto& core : core_list)
  856. {
  857. for (auto& mesh : data_list)
  858. {
  859. if (mesh.is_visible & core.id)
  860. {
  861. core.draw(mesh);
  862. }
  863. }
  864. }
  865. for (unsigned int i = 0; i<plugins.size(); ++i)
  866. {
  867. if (plugins[i]->post_draw())
  868. {
  869. break;
  870. }
  871. }
  872. if (callback_post_draw)
  873. {
  874. if (callback_post_draw(*this))
  875. {
  876. return;
  877. }
  878. }
  879. }
  880. template <typename T>
  881. IGL_INLINE void Viewer::draw_buffer(
  882. igl::opengl::ViewerCore & core,
  883. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & R,
  884. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & G,
  885. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B,
  886. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  887. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & D)
  888. {
  889. // follows igl::opengl::ViewerCore::draw_buffer, image is transposed from
  890. // typical matrix view
  891. const int width = R.rows() ? R.rows() : core.viewport(2);
  892. const int height = R.cols() ? R.cols() : core.viewport(3);
  893. R.resize(width,height);
  894. G.resize(width,height);
  895. B.resize(width,height);
  896. A.resize(width,height);
  897. D.resize(width,height);
  898. ////////////////////////////////////////////////////////////////////////
  899. // Create an initial multisampled framebuffer
  900. ////////////////////////////////////////////////////////////////////////
  901. unsigned int framebuffer;
  902. unsigned int color_buffer;
  903. unsigned int depth_buffer;
  904. {
  905. glGenFramebuffers(1, &framebuffer);
  906. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  907. // create a multisampled color attachment texture (is a texture really
  908. // needed? Could this be a renderbuffer instead?)
  909. glGenTextures(1, &color_buffer);
  910. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, color_buffer);
  911. glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 4, GL_RGBA, width, height, GL_TRUE);
  912. glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
  913. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, color_buffer, 0);
  914. // create a (also multisampled) renderbuffer object for depth and stencil attachments
  915. glGenRenderbuffers(1, &depth_buffer);
  916. glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer);
  917. glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_DEPTH24_STENCIL8, width, height);
  918. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  919. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_buffer);
  920. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  921. report_gl_error("glCheckFramebufferStatus: ");
  922. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  923. }
  924. ////////////////////////////////////////////////////////////////////////
  925. // configure second post-processing framebuffer
  926. ////////////////////////////////////////////////////////////////////////
  927. unsigned int intermediateFBO;
  928. unsigned int screenTexture, depthTexture;
  929. {
  930. glGenFramebuffers(1, &intermediateFBO);
  931. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  932. // create a color attachment texture
  933. glGenTextures(1, &screenTexture);
  934. glBindTexture(GL_TEXTURE_2D, screenTexture);
  935. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  936. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  937. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  938. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, screenTexture, 0);
  939. // create depth attachment texture
  940. glGenTextures(1, &depthTexture);
  941. glBindTexture(GL_TEXTURE_2D, depthTexture);
  942. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, width, height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
  943. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  944. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  945. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
  946. assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  947. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  948. }
  949. ////////////////////////////////////////////////////////////////////////
  950. // attach initial framebuffer and draw all `data`
  951. ////////////////////////////////////////////////////////////////////////
  952. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  953. // Clear the buffer
  954. glClearColor(
  955. core.background_color(0),
  956. core.background_color(1),
  957. core.background_color(2),
  958. core.background_color(3));
  959. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  960. // Save old viewport
  961. Eigen::Vector4f viewport_ori = core.viewport;
  962. core.viewport << 0,0,width,height;
  963. // Draw all `data`
  964. for (auto& data : data_list)
  965. {
  966. if (data.is_visible & core.id)
  967. {
  968. core.draw(data);
  969. }
  970. }
  971. // Restore viewport
  972. core.viewport = viewport_ori;
  973. ////////////////////////////////////////////////////////////////////////
  974. // attach second framebuffer and redraw (for anti-aliasing?)
  975. ////////////////////////////////////////////////////////////////////////
  976. glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
  977. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO);
  978. report_gl_error("before: ");
  979. glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT, GL_NEAREST);
  980. report_gl_error("glBlitFramebuffer: ");
  981. ////////////////////////////////////////////////////////////////////////
  982. // Read pixel data from framebuffer, write into buffers
  983. ////////////////////////////////////////////////////////////////////////
  984. glBindFramebuffer(GL_FRAMEBUFFER, intermediateFBO);
  985. // Copy back in the given Eigen matrices
  986. {
  987. typedef typename std::conditional< std::is_floating_point<T>::value,GLfloat,GLubyte>::type GLType;
  988. GLenum type = std::is_floating_point<T>::value ? GL_FLOAT : GL_UNSIGNED_BYTE;
  989. GLType* pixels = (GLType*)calloc(width*height*4,sizeof(GLType));
  990. GLType * depth = (GLType*)calloc(width*height*1,sizeof(GLType));
  991. glReadPixels(0, 0,width, height,GL_RGBA, type, pixels);
  992. glReadPixels(0, 0,width, height,GL_DEPTH_COMPONENT, type, depth);
  993. int count = 0;
  994. for (unsigned j=0; j<height; ++j)
  995. {
  996. for (unsigned i=0; i<width; ++i)
  997. {
  998. R(i,j) = pixels[count*4+0];
  999. G(i,j) = pixels[count*4+1];
  1000. B(i,j) = pixels[count*4+2];
  1001. A(i,j) = pixels[count*4+3];
  1002. D(i,j) = depth[count*1+0];
  1003. ++count;
  1004. }
  1005. }
  1006. // Clean up
  1007. free(pixels);
  1008. free(depth);
  1009. }
  1010. // Clean up
  1011. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
  1012. glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
  1013. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  1014. glDeleteTextures(1, &screenTexture);
  1015. glDeleteTextures(1, &depthTexture);
  1016. glDeleteTextures(1, &color_buffer);
  1017. glDeleteRenderbuffers(1, &depth_buffer);
  1018. glDeleteFramebuffers(1, &framebuffer);
  1019. glDeleteFramebuffers(1, &intermediateFBO);
  1020. }
  1021. IGL_INLINE void Viewer::resize(int w,int h)
  1022. {
  1023. if (window) {
  1024. glfwSetWindowSize(window, w/highdpi, h/highdpi);
  1025. }
  1026. post_resize(w, h);
  1027. }
  1028. IGL_INLINE void Viewer::post_resize(int w,int h)
  1029. {
  1030. if (core_list.size() == 1)
  1031. {
  1032. core().viewport = Eigen::Vector4f(0,0,w,h);
  1033. }
  1034. else
  1035. {
  1036. // It is up to the user to define the behavior of the post_resize() function
  1037. // when there are multiple viewports (through the `callback_post_resize` callback)
  1038. }
  1039. for (unsigned int i = 0; i<plugins.size(); ++i)
  1040. {
  1041. plugins[i]->post_resize(w, h);
  1042. }
  1043. if (callback_post_resize)
  1044. {
  1045. callback_post_resize(*this, w, h);
  1046. }
  1047. }
  1048. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  1049. {
  1050. Eigen::Quaternionf snapq = this->core().trackball_angle;
  1051. igl::snap_to_canonical_view_quat(snapq,1.0f,this->core().trackball_angle);
  1052. }
  1053. IGL_INLINE void Viewer::open_dialog_load_mesh()
  1054. {
  1055. std::string fname = igl::file_dialog_open();
  1056. if (fname.length() == 0)
  1057. return;
  1058. this->load_mesh_from_file(fname.c_str());
  1059. }
  1060. IGL_INLINE void Viewer::open_dialog_save_mesh()
  1061. {
  1062. std::string fname = igl::file_dialog_save();
  1063. if(fname.length() == 0)
  1064. return;
  1065. this->save_mesh_to_file(fname.c_str());
  1066. }
  1067. IGL_INLINE ViewerData& Viewer::data(int mesh_id /*= -1*/)
  1068. {
  1069. assert(!data_list.empty() && "data_list should never be empty");
  1070. int index;
  1071. if (mesh_id == -1)
  1072. index = selected_data_index;
  1073. else
  1074. index = mesh_index(mesh_id);
  1075. assert((index >= 0 && index < data_list.size()) &&
  1076. "selected_data_index or mesh_id should be in bounds");
  1077. return data_list[index];
  1078. }
  1079. IGL_INLINE const ViewerData& Viewer::data(int mesh_id /*= -1*/) const
  1080. {
  1081. assert(!data_list.empty() && "data_list should never be empty");
  1082. int index;
  1083. if (mesh_id == -1)
  1084. index = selected_data_index;
  1085. else
  1086. index = mesh_index(mesh_id);
  1087. assert((index >= 0 && index < data_list.size()) &&
  1088. "selected_data_index or mesh_id should be in bounds");
  1089. return data_list[index];
  1090. }
  1091. IGL_INLINE int Viewer::append_mesh(bool visible /*= true*/)
  1092. {
  1093. assert(data_list.size() >= 1);
  1094. data_list.emplace_back();
  1095. selected_data_index = data_list.size()-1;
  1096. data_list.back().id = next_data_id++;
  1097. if (visible)
  1098. for (int i = 0; i < core_list.size(); i++)
  1099. data_list.back().set_visible(true, core_list[i].id);
  1100. else
  1101. data_list.back().is_visible = 0;
  1102. return data_list.back().id;
  1103. }
  1104. IGL_INLINE bool Viewer::erase_mesh(const size_t index)
  1105. {
  1106. assert((index >= 0 && index < data_list.size()) && "index should be in bounds");
  1107. assert(data_list.size() >= 1);
  1108. if(data_list.size() == 1)
  1109. {
  1110. // Cannot remove last mesh
  1111. return false;
  1112. }
  1113. data_list[index].meshgl.free();
  1114. data_list.erase(data_list.begin() + index);
  1115. if(selected_data_index >= index && selected_data_index > 0)
  1116. {
  1117. selected_data_index--;
  1118. }
  1119. return true;
  1120. }
  1121. IGL_INLINE size_t Viewer::mesh_index(const int id) const {
  1122. for (size_t i = 0; i < data_list.size(); ++i)
  1123. {
  1124. if (data_list[i].id == id)
  1125. return i;
  1126. }
  1127. return 0;
  1128. }
  1129. IGL_INLINE ViewerCore& Viewer::core(unsigned core_id /*= 0*/)
  1130. {
  1131. assert(!core_list.empty() && "core_list should never be empty");
  1132. int core_index;
  1133. if (core_id == 0)
  1134. core_index = selected_core_index;
  1135. else
  1136. core_index = this->core_index(core_id);
  1137. assert((core_index >= 0 && core_index < core_list.size()) && "selected_core_index should be in bounds");
  1138. return core_list[core_index];
  1139. }
  1140. IGL_INLINE const ViewerCore& Viewer::core(unsigned core_id /*= 0*/) const
  1141. {
  1142. assert(!core_list.empty() && "core_list should never be empty");
  1143. int core_index;
  1144. if (core_id == 0)
  1145. core_index = selected_core_index;
  1146. else
  1147. core_index = this->core_index(core_id);
  1148. assert((core_index >= 0 && core_index < core_list.size()) && "selected_core_index should be in bounds");
  1149. return core_list[core_index];
  1150. }
  1151. IGL_INLINE bool Viewer::erase_core(const size_t index)
  1152. {
  1153. assert((index >= 0 && index < core_list.size()) && "index should be in bounds");
  1154. assert(data_list.size() >= 1);
  1155. if (core_list.size() == 1)
  1156. {
  1157. // Cannot remove last viewport
  1158. return false;
  1159. }
  1160. core_list[index].shut(); // does nothing
  1161. core_list.erase(core_list.begin() + index);
  1162. if (selected_core_index >= index && selected_core_index > 0)
  1163. {
  1164. selected_core_index--;
  1165. }
  1166. return true;
  1167. }
  1168. IGL_INLINE size_t Viewer::core_index(const int id) const {
  1169. for (size_t i = 0; i < core_list.size(); ++i)
  1170. {
  1171. if (core_list[i].id == id)
  1172. return i;
  1173. }
  1174. return 0;
  1175. }
  1176. IGL_INLINE int Viewer::append_core(Eigen::Vector4f viewport, bool append_empty /*= false*/)
  1177. {
  1178. core_list.push_back(core()); // copies the previous active core and only changes the viewport
  1179. core_list.back().viewport = viewport;
  1180. core_list.back().id = next_core_id;
  1181. next_core_id <<= 1;
  1182. if (!append_empty)
  1183. {
  1184. for (auto &data : data_list)
  1185. {
  1186. data.set_visible(true, core_list.back().id);
  1187. data.copy_options(core(), core_list.back());
  1188. }
  1189. }
  1190. selected_core_index = core_list.size()-1;
  1191. return core_list.back().id;
  1192. }
  1193. } // end namespace
  1194. } // end namespace
  1195. }
  1196. #ifdef IGL_STATIC_LIBRARY
  1197. template void igl::opengl::glfw::Viewer::draw_buffer<unsigned char>(igl::opengl::ViewerCore&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&);
  1198. template void igl::opengl::glfw::Viewer::draw_buffer<double>(igl::opengl::ViewerCore&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  1199. template void igl::opengl::glfw::Viewer::draw_buffer<float>(igl::opengl::ViewerCore&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&);
  1200. #endif