example.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #include <igl/svd3x3/arap.h>
  2. #include <igl/writeDMAT.h>
  3. #include <igl/partition.h>
  4. #include <igl/cotmatrix.h>
  5. #include <igl/massmatrix.h>
  6. #include <igl/invert_diag.h>
  7. #include <igl/OpenGL_convenience.h>
  8. #include <igl/per_face_normals.h>
  9. #include <igl/per_vertex_normals.h>
  10. #include <igl/two_axis_valuator_fixed_up.h>
  11. #include <igl/normalize_row_lengths.h>
  12. #include <igl/draw_mesh.h>
  13. #include <igl/draw_floor.h>
  14. #include <igl/quat_to_mat.h>
  15. #include <igl/report_gl_error.h>
  16. #include <igl/readOBJ.h>
  17. #include <igl/readDMAT.h>
  18. #include <igl/readOFF.h>
  19. #include <igl/readMESH.h>
  20. #include <igl/jet.h>
  21. #include <igl/readWRL.h>
  22. #include <igl/trackball.h>
  23. #include <igl/list_to_matrix.h>
  24. #include <igl/snap_to_canonical_view_quat.h>
  25. #include <igl/snap_to_fixed_up.h>
  26. #include <igl/triangulate.h>
  27. #include <igl/material_colors.h>
  28. #include <igl/barycenter.h>
  29. #include <igl/matlab_format.h>
  30. #include <igl/ReAntTweakBar.h>
  31. #include <igl/pathinfo.h>
  32. #include <igl/Camera.h>
  33. #include <igl/get_seconds.h>
  34. #include <igl/PI.h>
  35. #include <igl/STR.h>
  36. #include <YImage.hpp>
  37. #ifdef __APPLE__
  38. # include <GLUT/glut.h>
  39. #else
  40. # include <GL/glut.h>
  41. #endif
  42. #include <Eigen/Core>
  43. #include <vector>
  44. #include <iostream>
  45. #include <algorithm>
  46. struct State
  47. {
  48. igl::Camera camera;
  49. } s;
  50. enum RotationType
  51. {
  52. ROTATION_TYPE_IGL_TRACKBALL = 0,
  53. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  54. NUM_ROTATION_TYPES = 2,
  55. } rotation_type;
  56. bool is_rotating = false;
  57. int down_x,down_y;
  58. igl::Camera down_camera;
  59. bool is_animating = false;
  60. double animation_start_time = 0;
  61. double ANIMATION_DURATION = 0.5;
  62. Eigen::Quaterniond animation_from_quat;
  63. Eigen::Quaterniond animation_to_quat;
  64. // Use vector for range-based `for`
  65. std::vector<State> undo_stack;
  66. std::vector<State> redo_stack;
  67. void push_undo()
  68. {
  69. undo_stack.push_back(s);
  70. // Clear
  71. redo_stack = std::vector<State>();
  72. }
  73. void undo()
  74. {
  75. using namespace std;
  76. if(!undo_stack.empty())
  77. {
  78. redo_stack.push_back(s);
  79. s = undo_stack.front();
  80. undo_stack.pop_back();
  81. }
  82. }
  83. void redo()
  84. {
  85. using namespace std;
  86. if(!redo_stack.empty())
  87. {
  88. undo_stack.push_back(s);
  89. s = redo_stack.front();
  90. redo_stack.pop_back();
  91. }
  92. }
  93. void TW_CALL set_rotation_type(const void * value, void * clientData)
  94. {
  95. using namespace Eigen;
  96. using namespace std;
  97. using namespace igl;
  98. const RotationType old_rotation_type = rotation_type;
  99. rotation_type = *(const RotationType *)(value);
  100. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  101. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  102. {
  103. push_undo();
  104. animation_from_quat = s.camera.m_rotation_conj;
  105. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  106. // start animation
  107. animation_start_time = get_seconds();
  108. is_animating = true;
  109. }
  110. }
  111. void TW_CALL get_rotation_type(void * value, void *clientData)
  112. {
  113. RotationType * rt = (RotationType *)(value);
  114. *rt = rotation_type;
  115. }
  116. // Width and height of window
  117. int width,height;
  118. // Position of light
  119. float light_pos[4] = {0.1,0.1,-0.9,0};
  120. // Vertex positions, normals, colors and centroid
  121. Eigen::MatrixXd V,U,N,mid;
  122. Eigen::VectorXi S;
  123. igl::ARAPData arap_data;
  124. Eigen::MatrixXi F;
  125. int selected_col = 0;
  126. // Faces
  127. // Bounding box diagonal length
  128. double bbd;
  129. int tot_num_samples = 0;
  130. #define REBAR_NAME "temp.rbr"
  131. igl::ReTwBar rebar; // Pointer to the tweak bar
  132. bool flip_y = false;
  133. bool rotate_xy = false;
  134. int num_in_selection(const Eigen::VectorXi & S)
  135. {
  136. int count = 0;
  137. for(int v = 0;v<S.rows(); v++)
  138. {
  139. if(S(v) >= 0)
  140. {
  141. count++;
  142. }
  143. }
  144. return count;
  145. }
  146. bool harmonic(
  147. const Eigen::MatrixXd & V,
  148. const Eigen::MatrixXi & F,
  149. const Eigen::VectorXi & b,
  150. const Eigen::MatrixXd & bc,
  151. const int k,
  152. Eigen::MatrixXd & W)
  153. {
  154. using namespace igl;
  155. using namespace Eigen;
  156. SparseMatrix<double> L,M,Mi;
  157. cotmatrix(V,F,L);
  158. massmatrix(V,F,MASSMATRIX_VORONOI,M);
  159. invert_diag(M,Mi);
  160. SparseMatrix<double> Q = -L;
  161. for(int p = 1;p<k;p++)
  162. {
  163. Q = (Q*Mi*-L).eval();
  164. }
  165. const VectorXd B = VectorXd::Zero(V.rows(),1);
  166. min_quad_with_fixed_data<double> data;
  167. min_quad_with_fixed_precompute(Q,b,SparseMatrix<double>(),true,data);
  168. W.resize(V.rows(),bc.cols());
  169. for(int w = 0;w<bc.cols();w++)
  170. {
  171. const VectorXd bcw = bc.col(w);
  172. VectorXd Ww;
  173. if(!min_quad_with_fixed_solve(data,B,bcw,VectorXd(),Ww))
  174. {
  175. return false;
  176. }
  177. W.col(w) = Ww;
  178. }
  179. writeDMAT("W.dmat",W);
  180. return true;
  181. }
  182. bool init_arap()
  183. {
  184. using namespace igl;
  185. using namespace Eigen;
  186. using namespace std;
  187. VectorXi b(num_in_selection(S));
  188. assert(S.rows() == V.rows());
  189. MatrixXd bc = MatrixXd::Zero(b.size(),S.maxCoeff()+1);
  190. // get b from S
  191. {
  192. int bi = 0;
  193. for(int v = 0;v<S.rows(); v++)
  194. {
  195. if(S(v) >= 0)
  196. {
  197. b(bi) = v;
  198. bc(bi,S(v)) = 1;
  199. bi++;
  200. }
  201. }
  202. }
  203. // Store current mesh
  204. U = V;
  205. VectorXi _S;
  206. VectorXd _D;
  207. MatrixXd W;
  208. if(!harmonic(V,F,b,bc,1,W))
  209. {
  210. return false;
  211. }
  212. partition(W,100,arap_data.G,_S,_D);
  213. return arap_precomputation(V,F,b,arap_data);
  214. }
  215. bool update_arap()
  216. {
  217. using namespace Eigen;
  218. using namespace igl;
  219. using namespace std;
  220. MatrixXd bc(num_in_selection(S),V.cols());
  221. // get b from S
  222. {
  223. int bi = 0;
  224. for(int v = 0;v<S.rows(); v++)
  225. {
  226. if(S(v) >= 0)
  227. {
  228. bc.row(bi) = V.row(v);
  229. switch(S(v))
  230. {
  231. case 0:
  232. {
  233. const double r = mid(0)*0.25;
  234. bc(bi,0) += r*cos(0.5*get_seconds()*2.*PI);
  235. bc(bi,1) -= r+r*sin(0.5*get_seconds()*2.*PI);
  236. break;
  237. }
  238. case 1:
  239. {
  240. const double r = mid(1)*0.15;
  241. bc(bi,1) += r+r*cos(0.15*get_seconds()*2.*PI);
  242. bc(bi,2) -= r*sin(0.15*get_seconds()*2.*PI);
  243. break;
  244. }
  245. default:
  246. break;
  247. }
  248. bi++;
  249. }
  250. }
  251. }
  252. if(!arap_solve(bc,arap_data,U))
  253. {
  254. cerr<<"arap_solve failed."<<endl;
  255. return false;
  256. }
  257. per_face_normals(U,F,N);
  258. return true;
  259. }
  260. void reshape(int width,int height)
  261. {
  262. using namespace std;
  263. // Save width and height
  264. ::width = width;
  265. ::height = height;
  266. glMatrixMode(GL_PROJECTION);
  267. glLoadIdentity();
  268. glViewport(0,0,width,height);
  269. // Send the new window size to AntTweakBar
  270. TwWindowSize(width, height);
  271. // Set aspect for all cameras
  272. s.camera.m_aspect = (double)width/(double)height;
  273. for(auto & s : undo_stack)
  274. {
  275. s.camera.m_aspect = (double)width/(double)height;
  276. }
  277. for(auto & s : redo_stack)
  278. {
  279. s.camera.m_aspect = (double)width/(double)height;
  280. }
  281. }
  282. void push_scene()
  283. {
  284. using namespace igl;
  285. using namespace std;
  286. glMatrixMode(GL_PROJECTION);
  287. glPushMatrix();
  288. glLoadIdentity();
  289. auto & camera = s.camera;
  290. gluPerspective(camera.m_angle,camera.m_aspect,camera.m_near,camera.m_far);
  291. glMatrixMode(GL_MODELVIEW);
  292. glPushMatrix();
  293. glLoadIdentity();
  294. gluLookAt(
  295. camera.eye()(0), camera.eye()(1), camera.eye()(2),
  296. camera.at()(0), camera.at()(1), camera.at()(2),
  297. camera.up()(0), camera.up()(1), camera.up()(2));
  298. }
  299. void pop_scene()
  300. {
  301. glMatrixMode(GL_PROJECTION);
  302. glPopMatrix();
  303. glMatrixMode(GL_MODELVIEW);
  304. glPopMatrix();
  305. }
  306. void pop_object()
  307. {
  308. glPopMatrix();
  309. }
  310. // Scale and shift for object
  311. void push_object()
  312. {
  313. glPushMatrix();
  314. glScaled(2./bbd,2./bbd,2./bbd);
  315. glTranslated(-mid(0,0),-mid(0,1),-mid(0,2));
  316. }
  317. // Set up double-sided lights
  318. void lights()
  319. {
  320. using namespace std;
  321. glEnable(GL_LIGHTING);
  322. glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  323. glEnable(GL_LIGHT0);
  324. glEnable(GL_LIGHT1);
  325. float amb[4];
  326. amb[0] = amb[1] = amb[2] = 0;
  327. amb[3] = 1.0;
  328. float diff[4] = {0.0,0.0,0.0,0.0};
  329. diff[0] = diff[1] = diff[2] = (1.0 - 0/0.4);;
  330. diff[3] = 1.0;
  331. float zeros[4] = {0.0,0.0,0.0,0.0};
  332. float pos[4];
  333. copy(light_pos,light_pos+4,pos);
  334. glLightfv(GL_LIGHT0,GL_AMBIENT,amb);
  335. glLightfv(GL_LIGHT0,GL_DIFFUSE,diff);
  336. glLightfv(GL_LIGHT0,GL_SPECULAR,zeros);
  337. glLightfv(GL_LIGHT0,GL_POSITION,pos);
  338. pos[0] *= -1;
  339. pos[1] *= -1;
  340. pos[2] *= -1;
  341. glLightfv(GL_LIGHT1,GL_AMBIENT,amb);
  342. glLightfv(GL_LIGHT1,GL_DIFFUSE,diff);
  343. glLightfv(GL_LIGHT1,GL_SPECULAR,zeros);
  344. glLightfv(GL_LIGHT1,GL_POSITION,pos);
  345. }
  346. const float back[4] = {30.0/255.0,30.0/255.0,50.0/255.0,0};
  347. void display()
  348. {
  349. using namespace Eigen;
  350. using namespace igl;
  351. using namespace std;
  352. // Update
  353. update_arap();
  354. glClearColor(back[0],back[1],back[2],0);
  355. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  356. if(is_animating)
  357. {
  358. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  359. if(t > 1)
  360. {
  361. t = 1;
  362. is_animating = false;
  363. }
  364. const Quaterniond q = animation_from_quat.slerp(t,animation_to_quat).normalized();
  365. s.camera.orbit(q.conjugate());
  366. }
  367. glDisable(GL_LIGHTING);
  368. lights();
  369. push_scene();
  370. glEnable(GL_DEPTH_TEST);
  371. glDepthFunc(GL_LESS);
  372. glEnable(GL_NORMALIZE);
  373. push_object();
  374. // Draw the model
  375. // Set material properties
  376. glDisable(GL_COLOR_MATERIAL);
  377. glMaterialfv(GL_FRONT, GL_AMBIENT, GOLD_AMBIENT);
  378. glMaterialfv(GL_FRONT, GL_DIFFUSE, GOLD_DIFFUSE );
  379. glMaterialfv(GL_FRONT, GL_SPECULAR, GOLD_SPECULAR);
  380. glMaterialf (GL_FRONT, GL_SHININESS, 128);
  381. glMaterialfv(GL_BACK, GL_AMBIENT, SILVER_AMBIENT);
  382. glMaterialfv(GL_BACK, GL_DIFFUSE, FAST_GREEN_DIFFUSE );
  383. glMaterialfv(GL_BACK, GL_SPECULAR, SILVER_SPECULAR);
  384. glMaterialf (GL_BACK, GL_SHININESS, 128);
  385. draw_mesh(U,F,N);
  386. glDisable(GL_COLOR_MATERIAL);
  387. pop_object();
  388. // Draw a nice floor
  389. glPushMatrix();
  390. const double floor_offset =
  391. -2./bbd*(V.col(1).maxCoeff()-mid(1));
  392. glTranslated(0,floor_offset,0);
  393. const float GREY[4] = {0.5,0.5,0.6,1.0};
  394. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  395. draw_floor(GREY,DARK_GREY);
  396. glPopMatrix();
  397. pop_scene();
  398. report_gl_error();
  399. TwDraw();
  400. glutSwapBuffers();
  401. //if(is_animating)
  402. //{
  403. glutPostRedisplay();
  404. //}
  405. }
  406. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  407. {
  408. using namespace std;
  409. using namespace igl;
  410. using namespace Eigen;
  411. GLint viewport[4];
  412. glGetIntegerv(GL_VIEWPORT,viewport);
  413. if(wheel == 0 && TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  414. {
  415. static double mouse_scroll_y = 0;
  416. const double delta_y = 0.125*direction;
  417. mouse_scroll_y += delta_y;
  418. TwMouseWheel(mouse_scroll_y);
  419. return;
  420. }
  421. push_undo();
  422. auto & camera = s.camera;
  423. if(wheel==0)
  424. {
  425. // factor of zoom change
  426. double s = (1.-0.01*direction);
  427. //// FOV zoom: just widen angle. This is hardly ever appropriate.
  428. //camera.m_angle *= s;
  429. //camera.m_angle = min(max(camera.m_angle,1),89);
  430. camera.push_away(s);
  431. }else
  432. {
  433. // Dolly zoom:
  434. camera.dolly_zoom((double)direction*1.0);
  435. }
  436. }
  437. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  438. {
  439. using namespace std;
  440. using namespace Eigen;
  441. using namespace igl;
  442. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  443. switch(glutButton)
  444. {
  445. case GLUT_RIGHT_BUTTON:
  446. case GLUT_LEFT_BUTTON:
  447. {
  448. switch(glutState)
  449. {
  450. case 1:
  451. // up
  452. glutSetCursor(GLUT_CURSOR_LEFT_ARROW);
  453. is_rotating = false;
  454. break;
  455. case 0:
  456. // down
  457. if(!tw_using)
  458. {
  459. glutSetCursor(GLUT_CURSOR_CYCLE);
  460. // collect information for trackball
  461. is_rotating = true;
  462. down_camera = s.camera;
  463. down_x = mouse_x;
  464. down_y = mouse_y;
  465. }
  466. break;
  467. }
  468. break;
  469. }
  470. // Scroll down
  471. case 3:
  472. {
  473. mouse_wheel(0,-1,mouse_x,mouse_y);
  474. break;
  475. }
  476. // Scroll up
  477. case 4:
  478. {
  479. mouse_wheel(0,1,mouse_x,mouse_y);
  480. break;
  481. }
  482. // Scroll left
  483. case 5:
  484. {
  485. mouse_wheel(1,-1,mouse_x,mouse_y);
  486. break;
  487. }
  488. // Scroll right
  489. case 6:
  490. {
  491. mouse_wheel(1,1,mouse_x,mouse_y);
  492. break;
  493. }
  494. }
  495. glutPostRedisplay();
  496. }
  497. void mouse_drag(int mouse_x, int mouse_y)
  498. {
  499. using namespace igl;
  500. using namespace Eigen;
  501. if(is_rotating)
  502. {
  503. glutSetCursor(GLUT_CURSOR_CYCLE);
  504. Quaterniond q;
  505. auto & camera = s.camera;
  506. switch(rotation_type)
  507. {
  508. case ROTATION_TYPE_IGL_TRACKBALL:
  509. {
  510. // Rotate according to trackball
  511. igl::trackball<double>(
  512. width,
  513. height,
  514. 2.0,
  515. down_camera.m_rotation_conj.coeffs().data(),
  516. down_x,
  517. down_y,
  518. mouse_x,
  519. mouse_y,
  520. q.coeffs().data());
  521. break;
  522. }
  523. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  524. {
  525. // Rotate according to two axis valuator with fixed up vector
  526. two_axis_valuator_fixed_up(
  527. width, height,
  528. 2.0,
  529. down_camera.m_rotation_conj,
  530. down_x, down_y, mouse_x, mouse_y,
  531. q);
  532. break;
  533. }
  534. default:
  535. break;
  536. }
  537. camera.orbit(q.conjugate());
  538. }else
  539. {
  540. TwEventMouseMotionGLUT(mouse_x, mouse_y);
  541. }
  542. glutPostRedisplay();
  543. }
  544. void key(unsigned char key, int mouse_x, int mouse_y)
  545. {
  546. using namespace std;
  547. switch(key)
  548. {
  549. // ESC
  550. case char(27):
  551. rebar.save(REBAR_NAME);
  552. // ^C
  553. case char(3):
  554. exit(0);
  555. default:
  556. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  557. {
  558. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  559. }
  560. }
  561. glutPostRedisplay();
  562. }
  563. int main(int argc, char * argv[])
  564. {
  565. using namespace Eigen;
  566. using namespace igl;
  567. using namespace std;
  568. // init mesh
  569. string filename = "../shared/decimated-knight.obj";
  570. string sfilename = "../shared/decimated-knight-selection.dmat";
  571. if(argc < 3)
  572. {
  573. cerr<<"Usage:"<<endl<<" ./example input.obj selection.dmat"<<endl;
  574. cout<<endl<<"Opening default mesh..."<<endl;
  575. }else
  576. {
  577. // Read and prepare mesh
  578. filename = argv[1];
  579. sfilename = argv[2];
  580. }
  581. vector<vector<double > > vV,vN,vTC;
  582. vector<vector<int > > vF,vTF,vFN;
  583. // Convert extension to lower case
  584. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vTF,vFN))
  585. {
  586. return 1;
  587. }
  588. if(vV.size() > 0)
  589. {
  590. if(!list_to_matrix(vV,V))
  591. {
  592. cerr<<"Bad V"<<endl;
  593. return 1;
  594. }
  595. triangulate(vF,F);
  596. }
  597. per_face_normals(V,F,N);
  598. if(!readDMAT(sfilename,S))
  599. {
  600. return 1;
  601. }
  602. // Compute normals, centroid, colors, bounding box diagonal
  603. mid = 0.5*(V.colwise().maxCoeff() + V.colwise().minCoeff());
  604. bbd = (V.colwise().maxCoeff() - V.colwise().minCoeff()).maxCoeff();
  605. // Init glut
  606. glutInit(&argc,argv);
  607. if( !TwInit(TW_OPENGL, NULL) )
  608. {
  609. // A fatal error occured
  610. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  611. return 1;
  612. }
  613. // Create a tweak bar
  614. rebar.TwNewBar("TweakBar");
  615. rebar.TwAddVarRW("camera_rotation", TW_TYPE_QUAT4D,
  616. s.camera.m_rotation_conj.coeffs().data(), "open readonly=true");
  617. s.camera.push_away(3);
  618. s.camera.dolly_zoom(25-s.camera.m_angle);
  619. TwType RotationTypeTW = ReTwDefineEnumFromString("RotationType",
  620. "igl_trackball,two-a...-fixed-up");
  621. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  622. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  623. rebar.TwAddVarRW("flip_y", TW_TYPE_BOOLCPP, &flip_y,"key=f");
  624. rebar.TwAddVarRW("rotate_xy", TW_TYPE_BOOLCPP, &rotate_xy,"key=r");
  625. rebar.load(REBAR_NAME);
  626. glutInitDisplayString( "rgba depth double samples>=8 ");
  627. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT));
  628. glutCreateWindow("colored-mesh");
  629. glutDisplayFunc(display);
  630. glutReshapeFunc(reshape);
  631. glutKeyboardFunc(key);
  632. glutMouseFunc(mouse);
  633. glutMotionFunc(mouse_drag);
  634. glutPassiveMotionFunc(
  635. [](int x, int y)
  636. {
  637. TwEventMouseMotionGLUT(x,y);
  638. glutPostRedisplay();
  639. });
  640. static std::function<void(int)> timer_bounce;
  641. auto timer = [] (int ms) {
  642. timer_bounce(ms);
  643. };
  644. timer_bounce = [&] (int ms) {
  645. glutTimerFunc(ms, timer, ms);
  646. glutPostRedisplay();
  647. };
  648. glutTimerFunc(500, timer, 500);
  649. if(!init_arap())
  650. {
  651. cerr<<"Initializing arap failed."<<endl;
  652. return 1;
  653. }
  654. glutMainLoop();
  655. return 0;
  656. }