Viewer.cpp 40 KB

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