Viewer.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  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. #ifdef _WIN32
  10. # include <windows.h>
  11. # undef max
  12. # undef min
  13. #endif
  14. #include <chrono>
  15. #include <thread>
  16. #ifndef __APPLE__
  17. # define GLEW_STATIC
  18. # include <GL/glew.h>
  19. #endif
  20. #ifdef __APPLE__
  21. # include <OpenGL/gl3.h>
  22. # define __gl_h_ /* Prevent inclusion of the old gl.h */
  23. #else
  24. # include <GL/gl.h>
  25. #endif
  26. #include <Eigen/LU>
  27. #define GLFW_INCLUDE_GLU
  28. #ifndef _WIN32
  29. #define GLFW_INCLUDE_GLCOREARB
  30. #endif
  31. #include <GLFW/glfw3.h>
  32. #include <cmath>
  33. #include <cstdio>
  34. #include <sstream>
  35. #include <iomanip>
  36. #include <iostream>
  37. #include <fstream>
  38. #include <algorithm>
  39. #include <limits>
  40. #include <cassert>
  41. #include <nanogui/formscreen.h>
  42. #include <igl/project.h>
  43. #include <igl/get_seconds.h>
  44. #include <igl/readOBJ.h>
  45. #include <igl/readOFF.h>
  46. #include <igl/adjacency_list.h>
  47. #include <igl/writeOBJ.h>
  48. #include <igl/writeOFF.h>
  49. #include <igl/massmatrix.h>
  50. #include <igl/file_dialog_open.h>
  51. #include <igl/file_dialog_save.h>
  52. #include <igl/quat_mult.h>
  53. #include <igl/axis_angle_to_quat.h>
  54. #include <igl/trackball.h>
  55. #include <igl/snap_to_canonical_view_quat.h>
  56. #include <igl/unproject.h>
  57. #ifdef ENABLE_SERIALIZATION
  58. #include <igl/serialize.h>
  59. #endif
  60. // Internal global variables used for glfw event handling
  61. static igl::viewer::Viewer * __viewer;
  62. static double highdpi = 1;
  63. static double scroll_x = 0;
  64. static double scroll_y = 0;
  65. static int global_KMod = 0;
  66. static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
  67. {
  68. bool tw_used = __viewer->ngui->mouseButtonEvent(window,button,action,modifier);
  69. igl::viewer::Viewer::MouseButton mb;
  70. if (button == GLFW_MOUSE_BUTTON_1)
  71. mb = igl::viewer::Viewer::MouseButton::Left;
  72. else if (button == GLFW_MOUSE_BUTTON_2)
  73. mb = igl::viewer::Viewer::MouseButton::Right;
  74. else //if (button == GLFW_MOUSE_BUTTON_3)
  75. mb = igl::viewer::Viewer::MouseButton::Middle;
  76. if (action == GLFW_PRESS)
  77. {
  78. if(!tw_used)
  79. {
  80. __viewer->mouse_down(mb,modifier);
  81. }
  82. } else
  83. {
  84. // Always call mouse_up on up
  85. __viewer->mouse_up(mb,modifier);
  86. }
  87. }
  88. static void glfw_error_callback(int error, const char* description)
  89. {
  90. fputs(description, stderr);
  91. }
  92. static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int modifier)
  93. {
  94. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  95. glfwSetWindowShouldClose(window, GL_TRUE);
  96. if (__viewer->ngui->keyEvent(window,key,scancode,action,modifier) == false)
  97. {
  98. if (action == GLFW_PRESS)
  99. __viewer->key_down(key, modifier);
  100. else if(action == GLFW_RELEASE)
  101. __viewer->key_up(key, modifier);
  102. }
  103. }
  104. static void glfw_window_size(GLFWwindow* window, int width, int height)
  105. {
  106. int w = width*highdpi;
  107. int h = height*highdpi;
  108. __viewer->resize(w, h);
  109. // TODO: repositioning of the nanogui
  110. }
  111. static void glfw_mouse_move(GLFWwindow* window, double x, double y)
  112. {
  113. if(__viewer->ngui->cursorPosEvent(window,x,y) == false || __viewer->down)
  114. {
  115. __viewer->mouse_move(x*highdpi, y*highdpi);
  116. }
  117. }
  118. static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
  119. {
  120. using namespace std;
  121. scroll_x += x;
  122. scroll_y += y;
  123. if (__viewer->ngui->scrollEvent(window,x,y) == false)
  124. __viewer->mouse_scroll(y);
  125. }
  126. static void glfw_char_callback(GLFWwindow* window, unsigned int c)
  127. {
  128. __viewer->ngui->charEvent(window,c);
  129. }
  130. static void glfw_drop_callback(GLFWwindow *window,int count,const char **filenames)
  131. {
  132. __viewer->ngui->dropEvent(window,count,filenames);
  133. }
  134. namespace igl
  135. {
  136. namespace viewer
  137. {
  138. IGL_INLINE void Viewer::init()
  139. {
  140. using namespace nanogui;
  141. ngui->setInputCellSize(Eigen::Vector2i(60,20));
  142. // Create nanogui widgets
  143. ngui->addNewWindow(Eigen::Vector2i(10,10),"libIGL-Viewer");
  144. // ---------------------- LOADING ----------------------
  145. #ifdef ENABLE_SERIALIZATION
  146. ngui->addNewGroup("Workspace",FormScreen::Layout::Horizontal);
  147. ngui->addButton("Load",[&](){this->load_scene();});
  148. ngui->addButton("Save",[&](){this->save_scene();});
  149. #endif
  150. #ifdef ENABLE_IO
  151. ngui->addNewGroup("Mesh",FormScreen::Layout::Horizontal);
  152. ngui->addButton("Load",[&](){this->open_dialog_load_mesh();});
  153. ngui->addButton("Save",[&](){this->open_dialog_save_mesh();});
  154. #endif
  155. ngui->addNewGroup("Viewing Options",FormScreen::Layout::Vertical);
  156. ngui->addButton("Center object",[&](){this->core.align_camera_center(this->data.V,this->data.F);});
  157. ngui->addButton("Snap canonical view",[&]()
  158. {
  159. this->snap_to_canonical_quaternion();
  160. });
  161. ngui->addVariable(core.camera_zoom,"Zoom");
  162. ngui->addVariable(core.orthographic,"Orthographic view");
  163. ngui->addNewGroup("Draw options",FormScreen::Layout::Vertical);
  164. ngui->addVariable([&](bool checked)
  165. {
  166. this->data.set_face_based(checked);
  167. },[&]()
  168. {
  169. return this->data.face_based;
  170. }, "Face-based",false);
  171. ngui->addVariable(core.show_texture,"Show texture");
  172. ngui->addVariable([&](bool checked)
  173. {
  174. this->data.dirty |= ViewerData::DIRTY_NORMAL;
  175. this->core.invert_normals = checked;
  176. },[&]()
  177. {
  178. return this->core.invert_normals;
  179. },
  180. "Invert normals",false);
  181. ngui->addVariable(core.show_overlay,"Show overlay");
  182. ngui->addVariable(core.show_overlay_depth,"Show overlay depth");
  183. ngui->addColorPicker(core.background_color,"Background");
  184. ngui->addColorPicker(core.line_color,"Line color");
  185. ngui->addVariable(core.shininess,"Shininess");
  186. ngui->addNewGroup("Overlays");
  187. ngui->addVariable(core.show_lines,"Wireframe");
  188. ngui->addVariable(core.show_faces,"Fill");
  189. ngui->addVariable(core.show_vertid,"Show vertex labels");
  190. ngui->addVariable(core.show_faceid,"Show faces labels");
  191. ngui->layout();
  192. core.init();
  193. if (callback_init)
  194. if (callback_init(*this))
  195. return;
  196. init_plugins();
  197. }
  198. IGL_INLINE Viewer::Viewer()
  199. {
  200. ngui = nullptr;
  201. // Temporary variables initialization
  202. down = false;
  203. hack_never_moved = true;
  204. scroll_position = 0.0f;
  205. // Per face
  206. data.set_face_based(false);
  207. // C-style callbacks
  208. callback_init = nullptr;
  209. callback_pre_draw = nullptr;
  210. callback_post_draw = nullptr;
  211. callback_mouse_down = nullptr;
  212. callback_mouse_up = nullptr;
  213. callback_mouse_move = nullptr;
  214. callback_mouse_scroll = nullptr;
  215. callback_key_down = nullptr;
  216. callback_key_up = nullptr;
  217. callback_init_data = nullptr;
  218. callback_pre_draw_data = nullptr;
  219. callback_post_draw_data = nullptr;
  220. callback_mouse_down_data = nullptr;
  221. callback_mouse_up_data = nullptr;
  222. callback_mouse_move_data = nullptr;
  223. callback_mouse_scroll_data = nullptr;
  224. callback_key_down_data = nullptr;
  225. callback_key_up_data = nullptr;
  226. }
  227. IGL_INLINE void Viewer::init_plugins()
  228. {
  229. // Init all plugins
  230. for (unsigned int i = 0; i<plugins.size(); ++i)
  231. plugins[i]->init(this);
  232. }
  233. IGL_INLINE Viewer::~Viewer()
  234. {
  235. if(!ngui) delete ngui;
  236. }
  237. IGL_INLINE void Viewer::shutdown_plugins()
  238. {
  239. for (unsigned int i = 0; i<plugins.size(); ++i)
  240. plugins[i]->shutdown();
  241. }
  242. IGL_INLINE bool Viewer::load_mesh_from_file(const char* mesh_file_name)
  243. {
  244. std::string mesh_file_name_string = std::string(mesh_file_name);
  245. // first try to load it with a plugin
  246. for (unsigned int i = 0; i<plugins.size(); ++i)
  247. {
  248. if (plugins[i]->load(mesh_file_name_string))
  249. {
  250. return true;
  251. }
  252. }
  253. data.clear();
  254. size_t last_dot = mesh_file_name_string.rfind('.');
  255. if (last_dot == std::string::npos)
  256. {
  257. printf("Error: No file extension found in %s\n",mesh_file_name);
  258. return false;
  259. }
  260. std::string extension = mesh_file_name_string.substr(last_dot+1);
  261. if (extension == "off" || extension =="OFF")
  262. {
  263. Eigen::MatrixXd V;
  264. Eigen::MatrixXi F;
  265. if (!igl::readOFF(mesh_file_name_string, V, F))
  266. return false;
  267. data.set_mesh(V,F);
  268. }
  269. else if (extension == "obj" || extension =="OBJ")
  270. {
  271. Eigen::MatrixXd corner_normals;
  272. Eigen::MatrixXi fNormIndices;
  273. Eigen::MatrixXd UV_V;
  274. Eigen::MatrixXi UV_F;
  275. Eigen::MatrixXd V;
  276. Eigen::MatrixXi F;
  277. if (!(
  278. igl::readOBJ(
  279. mesh_file_name_string,
  280. V, UV_V, corner_normals, F, UV_F, fNormIndices)))
  281. return false;
  282. data.set_mesh(V,F);
  283. data.set_uv(UV_V,UV_F);
  284. }
  285. else
  286. {
  287. // unrecognized file type
  288. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  289. return false;
  290. }
  291. data.compute_normals();
  292. data.uniform_colors(Eigen::Vector3d(51.0/255.0,43.0/255.0,33.3/255.0),
  293. Eigen::Vector3d(255.0/255.0,228.0/255.0,58.0/255.0),
  294. Eigen::Vector3d(255.0/255.0,235.0/255.0,80.0/255.0));
  295. if (data.V_uv.rows() == 0)
  296. data.grid_texture();
  297. core.align_camera_center(data.V,data.F);
  298. for (unsigned int i = 0; i<plugins.size(); ++i)
  299. if (plugins[i]->post_load())
  300. return true;
  301. return true;
  302. }
  303. IGL_INLINE bool Viewer::save_mesh_to_file(const char* mesh_file_name)
  304. {
  305. std::string mesh_file_name_string(mesh_file_name);
  306. // first try to load it with a plugin
  307. for (unsigned int i = 0; i<plugins.size(); ++i)
  308. if (plugins[i]->save(mesh_file_name_string))
  309. return true;
  310. size_t last_dot = mesh_file_name_string.rfind('.');
  311. if (last_dot == std::string::npos)
  312. {
  313. // No file type determined
  314. printf("Error: No file extension found in %s\n",mesh_file_name);
  315. return false;
  316. }
  317. std::string extension = mesh_file_name_string.substr(last_dot+1);
  318. if (extension == "off" || extension =="OFF")
  319. {
  320. return igl::writeOFF(mesh_file_name_string,data.V,data.F);
  321. }
  322. else if (extension == "obj" || extension =="OBJ")
  323. {
  324. Eigen::MatrixXd corner_normals;
  325. Eigen::MatrixXi fNormIndices;
  326. Eigen::MatrixXd UV_V;
  327. Eigen::MatrixXi UV_F;
  328. return igl::writeOBJ(mesh_file_name_string, data.V,
  329. data.F, corner_normals, fNormIndices, UV_V, UV_F);
  330. }
  331. else
  332. {
  333. // unrecognized file type
  334. printf("Error: %s is not a recognized file type.\n",extension.c_str());
  335. return false;
  336. }
  337. return true;
  338. }
  339. IGL_INLINE bool Viewer::key_down(int key,int modifiers)
  340. {
  341. if (callback_key_down)
  342. if (callback_key_down(*this,key,modifiers))
  343. return true;
  344. for (unsigned int i = 0; i<plugins.size(); ++i)
  345. if (plugins[i]->key_down(key, modifiers))
  346. return true;
  347. char k = key;
  348. return false;
  349. }
  350. IGL_INLINE bool Viewer::key_up(int key,int modifiers)
  351. {
  352. if (callback_key_up)
  353. if (callback_key_up(*this,key,modifiers))
  354. return true;
  355. for (unsigned int i = 0; i<plugins.size(); ++i)
  356. if (plugins[i]->key_up(key, modifiers))
  357. return true;
  358. return false;
  359. }
  360. IGL_INLINE bool Viewer::mouse_down(MouseButton button,int modifier)
  361. {
  362. if (callback_mouse_down)
  363. if (callback_mouse_down(*this,static_cast<int>(button),modifier))
  364. return true;
  365. for (unsigned int i = 0; i<plugins.size(); ++i)
  366. if(plugins[i]->mouse_down(static_cast<int>(button),modifier))
  367. return true;
  368. down = true;
  369. down_mouse_x = current_mouse_x;
  370. down_mouse_y = current_mouse_y;
  371. down_translation = core.model_translation;
  372. // Initialization code for the trackball
  373. Eigen::RowVector3d center;
  374. if (data.V.rows() == 0)
  375. center << 0,0,0;
  376. else
  377. center = data.V.colwise().sum()/data.V.rows();
  378. Eigen::Vector3f coord = igl::project(Eigen::Vector3f(center(0),center(1),center(2)), (core.view * core.model).eval(), core.proj, core.viewport);
  379. down_mouse_z = coord[2];
  380. down_rotation = core.trackball_angle;
  381. mouse_mode = MouseMode::Rotation;
  382. switch (button)
  383. {
  384. case MouseButton::Left:
  385. mouse_mode = MouseMode::Rotation;
  386. break;
  387. case MouseButton::Right:
  388. mouse_mode = MouseMode::Translation;
  389. break;
  390. default:
  391. mouse_mode = MouseMode::None;
  392. break;
  393. }
  394. return true;
  395. }
  396. IGL_INLINE bool Viewer::mouse_up(MouseButton button,int modifier)
  397. {
  398. down = false;
  399. if (callback_mouse_up)
  400. if (callback_mouse_up(*this,static_cast<int>(button),modifier))
  401. return true;
  402. for (unsigned int i = 0; i<plugins.size(); ++i)
  403. if(plugins[i]->mouse_up(static_cast<int>(button),modifier))
  404. return true;
  405. mouse_mode = MouseMode::None;
  406. return true;
  407. }
  408. IGL_INLINE bool Viewer::mouse_move(int mouse_x,int mouse_y)
  409. {
  410. if(hack_never_moved)
  411. {
  412. down_mouse_x = mouse_x;
  413. down_mouse_y = mouse_y;
  414. hack_never_moved = false;
  415. }
  416. current_mouse_x = mouse_x;
  417. current_mouse_y = mouse_y;
  418. if (callback_mouse_move)
  419. if (callback_mouse_move(*this,mouse_x,mouse_y))
  420. return true;
  421. for (unsigned int i = 0; i<plugins.size(); ++i)
  422. if (plugins[i]->mouse_move(mouse_x, mouse_y))
  423. return true;
  424. if (down)
  425. {
  426. switch (mouse_mode)
  427. {
  428. case MouseMode::Rotation :
  429. {
  430. igl::trackball(core.viewport(2),
  431. core.viewport(3),
  432. 2.0f,
  433. down_rotation.data(),
  434. down_mouse_x,
  435. down_mouse_y,
  436. mouse_x,
  437. mouse_y,
  438. core.trackball_angle.data());
  439. //Eigen::Vector4f snapq = core.trackball_angle;
  440. break;
  441. }
  442. case MouseMode::Translation:
  443. {
  444. //translation
  445. Eigen::Vector3f pos1 = igl::unproject(Eigen::Vector3f(mouse_x, core.viewport[3] - mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  446. Eigen::Vector3f pos0 = igl::unproject(Eigen::Vector3f(down_mouse_x, core.viewport[3] - down_mouse_y, down_mouse_z), (core.view * core.model).eval(), core.proj, core.viewport);
  447. Eigen::Vector3f diff = pos1 - pos0;
  448. core.model_translation = down_translation + Eigen::Vector3f(diff[0],diff[1],diff[2]);
  449. break;
  450. }
  451. case MouseMode::Zoom:
  452. {
  453. float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
  454. core.camera_zoom *= 1 + delta;
  455. down_mouse_x = mouse_x;
  456. down_mouse_y = mouse_y;
  457. break;
  458. }
  459. default:
  460. break;
  461. }
  462. }
  463. return true;
  464. }
  465. IGL_INLINE bool Viewer::mouse_scroll(float delta_y)
  466. {
  467. scroll_position += delta_y;
  468. if (callback_mouse_scroll)
  469. if (callback_mouse_scroll(*this,delta_y))
  470. return true;
  471. for (unsigned int i = 0; i<plugins.size(); ++i)
  472. if (plugins[i]->mouse_scroll(delta_y))
  473. return true;
  474. // Only zoom if there's actually a change
  475. if(delta_y != 0)
  476. {
  477. float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
  478. const float min_zoom = 0.1f;
  479. core.camera_zoom = (core.camera_zoom * mult > min_zoom ? core.camera_zoom * mult : min_zoom);
  480. }
  481. return true;
  482. }
  483. IGL_INLINE void Viewer::draw()
  484. {
  485. using namespace std;
  486. using namespace Eigen;
  487. core.clear_framebuffers();
  488. if (callback_pre_draw)
  489. if (callback_pre_draw(*this))
  490. return;
  491. for (unsigned int i = 0; i<plugins.size(); ++i)
  492. if (plugins[i]->pre_draw())
  493. return;
  494. core.draw(data,opengl);
  495. if (callback_post_draw)
  496. if (callback_post_draw(*this))
  497. return;
  498. for (unsigned int i = 0; i<plugins.size(); ++i)
  499. if (plugins[i]->post_draw())
  500. break;
  501. ngui->draw();
  502. }
  503. IGL_INLINE bool Viewer::save_scene()
  504. {
  505. std::string fname = igl::file_dialog_save();
  506. if (fname.length() == 0)
  507. return false;
  508. #ifdef ENABLE_SERIALIZATION
  509. igl::serialize(core,"Core",fname.c_str(),true);
  510. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  511. igl::serialize(data,"Data",fname.c_str());
  512. for(unsigned int i = 0; i <plugins.size(); ++i)
  513. igl::serialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  514. #endif
  515. #endif
  516. return true;
  517. }
  518. IGL_INLINE bool Viewer::load_scene()
  519. {
  520. std::string fname = igl::file_dialog_open();
  521. if(fname.length() == 0)
  522. return false;
  523. return load_scene(fname);
  524. }
  525. IGL_INLINE bool Viewer::load_scene(std::string fname)
  526. {
  527. #ifdef ENABLE_SERIALIZATION
  528. igl::deserialize(core,"Core",fname.c_str());
  529. #ifndef ENABLE_SERIALIZATION_CORE_ONLY
  530. igl::deserialize(data,"Data",fname.c_str());
  531. for(unsigned int i = 0; i <plugins.size(); ++i)
  532. igl::deserialize(*plugins[i],plugins[i]->plugin_name,fname.c_str());
  533. #endif
  534. #endif
  535. return true;
  536. }
  537. IGL_INLINE void Viewer::resize(int w,int h)
  538. {
  539. core.viewport = Eigen::Vector4f(0,0,w,h);
  540. }
  541. IGL_INLINE void Viewer::snap_to_canonical_quaternion()
  542. {
  543. Eigen::Vector4f snapq = this->core.trackball_angle;
  544. igl::snap_to_canonical_view_quat(snapq.data(),1.0f,this->core.trackball_angle.data());
  545. }
  546. IGL_INLINE void Viewer::open_dialog_load_mesh()
  547. {
  548. std::string fname = igl::file_dialog_open();
  549. if (fname.length() == 0)
  550. return;
  551. this->load_mesh_from_file(fname.c_str());
  552. }
  553. IGL_INLINE void Viewer::open_dialog_save_mesh()
  554. {
  555. std::string fname = igl::file_dialog_save();
  556. if(fname.length() == 0)
  557. return;
  558. this->save_mesh_to_file(fname.c_str());
  559. }
  560. IGL_INLINE int Viewer::launch(bool resizable,bool fullscreen)
  561. {
  562. GLFWwindow* window;
  563. glfwSetErrorCallback(glfw_error_callback);
  564. if (!glfwInit())
  565. return EXIT_FAILURE;
  566. glfwWindowHint(GLFW_SAMPLES, 8);
  567. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  568. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  569. #ifdef __APPLE__
  570. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  571. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  572. #endif
  573. if(fullscreen)
  574. {
  575. GLFWmonitor *monitor = glfwGetPrimaryMonitor();
  576. const GLFWvidmode *mode = glfwGetVideoMode(monitor);
  577. window = glfwCreateWindow(mode->width,mode->height,"libigl viewer",monitor,nullptr);
  578. }
  579. else
  580. {
  581. window = glfwCreateWindow(1280,800,"libigl viewer",nullptr,nullptr);
  582. }
  583. if (!window)
  584. {
  585. glfwTerminate();
  586. return EXIT_FAILURE;
  587. }
  588. glfwMakeContextCurrent(window);
  589. #ifndef __APPLE__
  590. glewExperimental = true;
  591. GLenum err = glewInit();
  592. if(GLEW_OK != err)
  593. {
  594. /* Problem: glewInit failed, something is seriously wrong. */
  595. fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
  596. }
  597. glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
  598. fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  599. #endif
  600. #if defined(DEBUG) || defined(_DEBUG)
  601. int major, minor, rev;
  602. major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
  603. minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
  604. rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
  605. printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
  606. printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
  607. printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
  608. #endif
  609. glfwSetInputMode(window,GLFW_CURSOR,GLFW_CURSOR_NORMAL);
  610. // Initialize FormScreen
  611. ngui = new nanogui::FormScreen();
  612. ngui->init(window);
  613. __viewer = this;
  614. // Register callbacks
  615. glfwSetKeyCallback(window, glfw_key_callback);
  616. glfwSetCursorPosCallback(window,glfw_mouse_move);
  617. glfwSetWindowSizeCallback(window,glfw_window_size);
  618. glfwSetMouseButtonCallback(window,glfw_mouse_press);
  619. glfwSetScrollCallback(window,glfw_mouse_scroll);
  620. glfwSetCharCallback(window,glfw_char_callback);
  621. glfwSetDropCallback(window,glfw_drop_callback);
  622. // Handle retina displays (windows and mac)
  623. int width, height;
  624. glfwGetFramebufferSize(window, &width, &height);
  625. int width_window, height_window;
  626. glfwGetWindowSize(window, &width_window, &height_window);
  627. highdpi = width/width_window;
  628. glfw_window_size(window,width_window,height_window);
  629. opengl.init();
  630. core.align_camera_center(data.V,data.F);
  631. // Initialize IGL viewer
  632. init();
  633. // Rendering loop
  634. while (!glfwWindowShouldClose(window))
  635. {
  636. double tic = get_seconds();
  637. draw();
  638. glfwSwapBuffers(window);
  639. if(core.is_animating)
  640. {
  641. glfwPollEvents();
  642. // In microseconds
  643. double duration = 1000000.*(get_seconds()-tic);
  644. const double min_duration = 1000000./core.animation_max_fps;
  645. if(duration<min_duration)
  646. {
  647. std::this_thread::sleep_for(std::chrono::microseconds((int)(min_duration-duration)));
  648. }
  649. }
  650. else
  651. {
  652. glfwWaitEvents();
  653. }
  654. }
  655. opengl.free();
  656. core.shut();
  657. shutdown_plugins();
  658. glfwDestroyWindow(window);
  659. glfwTerminate();
  660. return EXIT_SUCCESS;
  661. }
  662. } // end namespace
  663. }