Viewer.cpp 38 KB

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