Viewer.cpp 40 KB

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