example.cpp 17 KB

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