Viewer.cpp 40 KB

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