example.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // Small GLUT application to test different scene rotation paradigms
  2. //
  3. #include <igl/readOBJ.h>
  4. #include <igl/writeOBJ.h>
  5. #include <igl/writeOFF.h>
  6. #include <igl/readWRL.h>
  7. #include <igl/report_gl_error.h>
  8. #include <igl/triangulate.h>
  9. #include <igl/readOFF.h>
  10. #include <igl/readMESH.h>
  11. #include <igl/draw_mesh.h>
  12. #include <igl/draw_floor.h>
  13. #include <igl/pathinfo.h>
  14. #include <igl/list_to_matrix.h>
  15. #include <igl/quat_to_mat.h>
  16. #include <igl/per_face_normals.h>
  17. #include <igl/material_colors.h>
  18. #include <igl/trackball.h>
  19. #include <igl/snap_to_canonical_view_quat.h>
  20. #include <igl/REDRUM.h>
  21. #include <igl/Camera.h>
  22. #include <igl/ReAntTweakBar.h>
  23. #include <igl/get_seconds.h>
  24. #include <igl/jet.h>
  25. #include <igl/randperm.h>
  26. #include <igl/normalize_row_lengths.h>
  27. #include <igl/boost/components.h>
  28. #include <igl/boost/bfs_orient.h>
  29. #include <igl/orient_outward.h>
  30. #include <igl/embree/orient_outward_ao.h>
  31. #include <igl/unique_simplices.h>
  32. #include <igl/C_STR.h>
  33. #include <igl/write.h>
  34. #include <igl/snap_to_fixed_up.h>
  35. #include <Eigen/Core>
  36. #include <Eigen/Geometry>
  37. #ifdef WIN32
  38. #include <GL/glut.h>
  39. #else
  40. #include <GLUT/glut.h>
  41. #endif
  42. #ifndef GLUT_WHEEL_UP
  43. #define GLUT_WHEEL_UP 3
  44. #define GLUT_WHEEL_DOWN 4
  45. #define GLUT_WHEEL_RIGHT 5
  46. #define GLUT_WHEEL_LEFT 6
  47. #define GLUT_ACTIVE_COMMAND 1
  48. #endif
  49. #include <ctime>
  50. #include <string>
  51. #include <vector>
  52. #include <stack>
  53. #include <iostream>
  54. int cc_selected = -1;
  55. Eigen::MatrixXd V;
  56. Eigen::VectorXd Vmid,Vmin,Vmax;
  57. double bbd = 1.0;
  58. Eigen::MatrixXi F;
  59. Eigen::VectorXi CC;
  60. struct State
  61. {
  62. igl::Camera camera;
  63. Eigen::MatrixXd N;
  64. Eigen::MatrixXd C;
  65. } s;
  66. std::string out_filename;
  67. // See README for descriptions
  68. enum RotationType
  69. {
  70. ROTATION_TYPE_IGL_TRACKBALL = 0,
  71. ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP = 1,
  72. NUM_ROTATION_TYPES = 2,
  73. } rotation_type;
  74. enum OrientMethod
  75. {
  76. ORIENT_METHOD_OUTWARD = 0,
  77. ORIENT_METHOD_AO = 1,
  78. NUM_ORIENT_METHODS = 2,
  79. } orient_method = ORIENT_METHOD_AO;
  80. std::stack<State> undo_stack;
  81. std::stack<State> redo_stack;
  82. bool wireframe_visible = false;
  83. bool fill_visible = true;
  84. bool is_rotating = false;
  85. int down_x,down_y;
  86. igl::Camera down_camera;
  87. bool is_animating = false;
  88. double animation_start_time = 0;
  89. double ANIMATION_DURATION = 0.5;
  90. Eigen::Quaterniond animation_from_quat;
  91. Eigen::Quaterniond animation_to_quat;
  92. int width,height;
  93. Eigen::Vector4f light_pos(-0.1,-0.1,0.9,0);
  94. #define REBAR_NAME "temp.rbr"
  95. igl::ReTwBar rebar;
  96. // Forward
  97. void init_patches();
  98. void init_relative();
  99. void push_undo()
  100. {
  101. undo_stack.push(s);
  102. // Clear
  103. redo_stack = std::stack<State>();
  104. }
  105. void TW_CALL set_camera_rotation(const void * value, void *clientData)
  106. {
  107. using namespace std;
  108. // case current value to double
  109. const double * quat = (const double *)(value);
  110. std::copy(quat,quat+4,s.camera.rotation);
  111. }
  112. void TW_CALL set_orient_method(const void * value, void * clientData)
  113. {
  114. const OrientMethod old_orient_method = orient_method;
  115. orient_method = *(const OrientMethod *)value;
  116. if(orient_method != old_orient_method)
  117. {
  118. init_patches();
  119. init_relative();
  120. }
  121. }
  122. void TW_CALL get_orient_method(void * value, void *clientData)
  123. {
  124. OrientMethod * om = (OrientMethod *)(value);
  125. *om = orient_method;
  126. }
  127. void TW_CALL set_rotation_type(const void * value, void * clientData)
  128. {
  129. using namespace Eigen;
  130. using namespace std;
  131. using namespace igl;
  132. const RotationType old_rotation_type = rotation_type;
  133. rotation_type = *(const RotationType *)(value);
  134. if(rotation_type == ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP &&
  135. old_rotation_type != ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP)
  136. {
  137. push_undo();
  138. copy(s.camera.rotation,s.camera.rotation+4,animation_from_quat.coeffs().data());
  139. snap_to_fixed_up(animation_from_quat,animation_to_quat);
  140. // start animation
  141. animation_start_time = get_seconds();
  142. is_animating = true;
  143. }
  144. }
  145. void TW_CALL get_rotation_type(void * value, void *clientData)
  146. {
  147. RotationType * rt = (RotationType *)(value);
  148. *rt = rotation_type;
  149. }
  150. void TW_CALL get_camera_rotation(void * value, void *clientData)
  151. {
  152. using namespace std;
  153. // case current value to double
  154. double * quat = (double *)(value);
  155. std::copy(s.camera.rotation,s.camera.rotation+4,quat);
  156. }
  157. void reshape(int width, int height)
  158. {
  159. ::width = width;
  160. ::height = height;
  161. glViewport(0,0,width,height);
  162. // Send the new window size to AntTweakBar
  163. TwWindowSize(width, height);
  164. }
  165. void push_scene()
  166. {
  167. using namespace igl;
  168. using namespace std;
  169. const double angle = s.camera.angle;
  170. glMatrixMode(GL_PROJECTION);
  171. glPushMatrix();
  172. glLoadIdentity();
  173. double zNear = 1e-2;
  174. double zFar = 100;
  175. double aspect = ((double)width)/((double)height);
  176. // Amount of scaling needed to "fix" perspective z-shift
  177. double z_fix = 1.0;
  178. // 5 is far enough to see unit "things" well
  179. const double camera_z = 2;
  180. // Test if should be using true orthographic projection
  181. if(angle == 0)
  182. {
  183. glOrtho(
  184. -0.5*camera_z*aspect,
  185. 0.5*camera_z*aspect,
  186. -0.5*camera_z,
  187. 0.5*camera_z,
  188. zNear,
  189. zFar);
  190. }else
  191. {
  192. // Make sure aspect is sane
  193. aspect = aspect < 0.01 ? 0.01 : aspect;
  194. gluPerspective(angle,aspect,zNear,zFar);
  195. z_fix = 2.*tan(angle/2./360.*2.*M_PI);
  196. }
  197. glMatrixMode(GL_MODELVIEW);
  198. glPushMatrix();
  199. glLoadIdentity();
  200. gluLookAt(0,0,camera_z,0,0,0,0,1,0);
  201. // Adjust scale to correct perspective
  202. glScaled(z_fix,z_fix,z_fix);
  203. // scale, pan
  204. glScaled( s.camera.zoom, s.camera.zoom, s.camera.zoom);
  205. double mat[4*4];
  206. quat_to_mat(s.camera.rotation,mat);
  207. glMultMatrixd(mat);
  208. }
  209. void push_object()
  210. {
  211. using namespace igl;
  212. glPushMatrix();
  213. glScaled(2./bbd,2./bbd,2./bbd);
  214. glTranslated(-Vmid(0),-Vmid(1),-Vmid(2));
  215. }
  216. void pop_object()
  217. {
  218. glPopMatrix();
  219. }
  220. void pop_scene()
  221. {
  222. glMatrixMode(GL_PROJECTION);
  223. glPopMatrix();
  224. glMatrixMode(GL_MODELVIEW);
  225. glPopMatrix();
  226. }
  227. // Set up double-sided lights
  228. void lights()
  229. {
  230. using namespace std;
  231. using namespace Eigen;
  232. glEnable(GL_LIGHTING);
  233. //glLightModelf(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
  234. glEnable(GL_LIGHT0);
  235. float WHITE[4] = {1,1,1,1.};
  236. float GREY[4] = {0.4,0.4,0.4,1.};
  237. float BLACK[4] = {0.,0.,0.,1.};
  238. float NEAR_BLACK[4] = {0.1,0.1,0.1,1.};
  239. Vector4f pos = light_pos;
  240. glLightfv(GL_LIGHT0,GL_AMBIENT,BLACK);
  241. glLightfv(GL_LIGHT0,GL_DIFFUSE,WHITE);
  242. glLightfv(GL_LIGHT0,GL_SPECULAR,BLACK);
  243. glLightfv(GL_LIGHT0,GL_POSITION,pos.data());
  244. //glEnable(GL_LIGHT1);
  245. //pos(0) *= -1;
  246. //pos(1) *= -1;
  247. //pos(2) *= -1;
  248. //glLightfv(GL_LIGHT1,GL_AMBIENT,BLACK);
  249. //glLightfv(GL_LIGHT1,GL_DIFFUSE,NEAR_BLACK);
  250. //glLightfv(GL_LIGHT1,GL_SPECULAR,BLACK);
  251. //glLightfv(GL_LIGHT1,GL_POSITION,pos.data());
  252. }
  253. void display()
  254. {
  255. using namespace igl;
  256. using namespace std;
  257. using namespace Eigen;
  258. glClearColor(1,1,1,0);
  259. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  260. if(is_animating)
  261. {
  262. double t = (get_seconds() - animation_start_time)/ANIMATION_DURATION;
  263. if(t > 1)
  264. {
  265. t = 1;
  266. is_animating = false;
  267. }
  268. Quaterniond q;
  269. q.coeffs() =
  270. animation_to_quat.coeffs()*t + animation_from_quat.coeffs()*(1.-t);
  271. q.normalize();
  272. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  273. }
  274. glEnable(GL_DEPTH_TEST);
  275. glEnable(GL_NORMALIZE);
  276. lights();
  277. push_scene();
  278. push_object();
  279. // Set material properties
  280. glEnable(GL_COLOR_MATERIAL);
  281. glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
  282. if(wireframe_visible)
  283. {
  284. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  285. if(fill_visible)
  286. {
  287. glColor3f(0,0,0);
  288. draw_mesh(V,F,s.N);
  289. }else
  290. {
  291. draw_mesh(V,F,s.N,s.C);
  292. }
  293. // visualize selected patch
  294. glLineWidth(10);
  295. glBegin(GL_TRIANGLES);
  296. glColor3d(0, 0, 0);
  297. // loop over faces
  298. for(int i = 0; i<F.rows();i++)
  299. {
  300. if (CC(i) != cc_selected) continue;
  301. // loop over corners of triangle
  302. for(int j = 0;j<3;j++)
  303. {
  304. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  305. }
  306. }
  307. glEnd();
  308. glLineWidth(1);
  309. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  310. glBegin(GL_TRIANGLES);
  311. glColor3d(1, 0, 0);
  312. // loop over faces
  313. for(int i = 0; i<F.rows();i++)
  314. {
  315. if (CC(i) != cc_selected) continue;
  316. // loop over corners of triangle
  317. glNormal3d(s.N(i,0),s.N(i,1),s.N(i,2));
  318. for(int j = 0;j<3;j++)
  319. {
  320. glVertex3d(V(F(i,j),0),V(F(i,j),1),V(F(i,j),2));
  321. }
  322. }
  323. glEnd();
  324. }
  325. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  326. if(fill_visible)
  327. {
  328. glEnable(GL_POLYGON_OFFSET_FILL); // Avoid Stitching!
  329. glPolygonOffset(1.0, 0);
  330. draw_mesh(V,F,s.N,s.C);
  331. }
  332. pop_object();
  333. // Draw a nice floor
  334. glPushMatrix();
  335. const double floor_offset =
  336. -2./bbd*(V.col(1).maxCoeff()-Vmid(1));
  337. glTranslated(0,floor_offset,0);
  338. const float GREY[4] = {0.5,0.5,0.6,1.0};
  339. const float DARK_GREY[4] = {0.2,0.2,0.3,1.0};
  340. draw_floor(GREY,DARK_GREY);
  341. glPopMatrix();
  342. pop_scene();
  343. report_gl_error();
  344. TwDraw();
  345. glutSwapBuffers();
  346. glutPostRedisplay();
  347. }
  348. void mouse_wheel(int wheel, int direction, int mouse_x, int mouse_y)
  349. {
  350. using namespace std;
  351. if(wheel == 0)
  352. {
  353. static double mouse_scroll_y = 0;
  354. const double delta_y = 0.125*direction;
  355. mouse_scroll_y += delta_y;
  356. // absolute scale difference when changing zooms (+1)
  357. const double z_diff = 0.01;
  358. GLint viewport[4];
  359. glGetIntegerv(GL_VIEWPORT,viewport);
  360. if(TwMouseMotion(mouse_x, viewport[3] - mouse_y))
  361. {
  362. TwMouseWheel(mouse_scroll_y);
  363. }else
  364. {
  365. s.camera.zoom *= (1.0+double(direction)*z_diff);
  366. const double min_zoom = 0.01;
  367. const double max_zoom = 10.0;
  368. s.camera.zoom = min(max_zoom,max(min_zoom,s.camera.zoom));
  369. }
  370. }else
  371. {
  372. if(!is_rotating)
  373. {
  374. // Change viewing angle (reshape will take care of adjust zoom)
  375. const double a_diff = 1.0;
  376. s.camera.angle += double(direction)*a_diff;
  377. const double min_angle = 15.0;
  378. s.camera.angle =
  379. min(90.0,max(min_angle,s.camera.angle));
  380. }
  381. }
  382. }
  383. void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
  384. {
  385. using namespace std;
  386. using namespace Eigen;
  387. using namespace igl;
  388. bool tw_using = TwEventMouseButtonGLUT(glutButton,glutState,mouse_x,mouse_y);
  389. switch(glutButton)
  390. {
  391. case GLUT_RIGHT_BUTTON:
  392. case GLUT_LEFT_BUTTON:
  393. {
  394. switch(glutState)
  395. {
  396. case 1:
  397. // up
  398. glutSetCursor(GLUT_CURSOR_INHERIT);
  399. is_rotating = false;
  400. break;
  401. case 0:
  402. if(!tw_using)
  403. {
  404. push_undo();
  405. glutSetCursor(GLUT_CURSOR_CYCLE);
  406. // collect information for trackball
  407. is_rotating = true;
  408. down_camera = s.camera;
  409. down_x = mouse_x;
  410. down_y = mouse_y;
  411. }
  412. break;
  413. }
  414. break;
  415. // Scroll down
  416. case GLUT_WHEEL_DOWN:
  417. {
  418. mouse_wheel(0,-1,mouse_x,mouse_y);
  419. break;
  420. }
  421. // Scroll up
  422. case GLUT_WHEEL_UP:
  423. {
  424. mouse_wheel(0,1,mouse_x,mouse_y);
  425. break;
  426. }
  427. // Scroll left
  428. case GLUT_WHEEL_LEFT:
  429. {
  430. mouse_wheel(1,-1,mouse_x,mouse_y);
  431. break;
  432. }
  433. // Scroll right
  434. case GLUT_WHEEL_RIGHT:
  435. {
  436. mouse_wheel(1,1,mouse_x,mouse_y);
  437. break;
  438. }
  439. }
  440. }
  441. }
  442. void mouse_drag(int mouse_x, int mouse_y)
  443. {
  444. using namespace igl;
  445. using namespace std;
  446. using namespace Eigen;
  447. bool tw_using = TwMouseMotion(mouse_x,mouse_y);
  448. if(is_rotating)
  449. {
  450. glutSetCursor(GLUT_CURSOR_CYCLE);
  451. switch(rotation_type)
  452. {
  453. case ROTATION_TYPE_IGL_TRACKBALL:
  454. {
  455. // Rotate according to trackball
  456. igl::trackball<double>(
  457. width,
  458. height,
  459. 2.0,
  460. down_camera.rotation,
  461. down_x,
  462. down_y,
  463. mouse_x,
  464. mouse_y,
  465. s.camera.rotation);
  466. break;
  467. }
  468. case ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP:
  469. {
  470. Quaterniond down_q;
  471. copy(down_camera.rotation,down_camera.rotation+4,down_q.coeffs().data());
  472. Vector3d axis(0,1,0);
  473. const double speed = 2.0;
  474. Quaterniond q;
  475. q = down_q *
  476. Quaterniond(
  477. AngleAxisd(
  478. M_PI*((double)(mouse_x-down_x))/(double)width*speed/2.0,
  479. axis.normalized()));
  480. q.normalize();
  481. {
  482. Vector3d axis(1,0,0);
  483. const double speed = 2.0;
  484. if(axis.norm() != 0)
  485. {
  486. q =
  487. Quaterniond(
  488. AngleAxisd(
  489. M_PI*(mouse_y-down_y)/(double)width*speed/2.0,
  490. axis.normalized())) * q;
  491. q.normalize();
  492. }
  493. }
  494. copy(q.coeffs().data(),q.coeffs().data()+4,s.camera.rotation);
  495. break;
  496. }
  497. default:
  498. break;
  499. }
  500. }
  501. }
  502. void init_relative()
  503. {
  504. using namespace Eigen;
  505. using namespace igl;
  506. per_face_normals(V,F,s.N);
  507. normalize_row_lengths(s.N,s.N);
  508. Vmax = V.colwise().maxCoeff();
  509. Vmin = V.colwise().minCoeff();
  510. Vmid = 0.5*(Vmax + Vmin);
  511. bbd = (Vmax-Vmin).norm();
  512. }
  513. void randomly_color(
  514. const Eigen::VectorXi & CC,
  515. Eigen::MatrixXd & C)
  516. {
  517. using namespace Eigen;
  518. using namespace igl;
  519. using namespace std;
  520. VectorXi I;
  521. srand ( unsigned ( time(0) ) );
  522. double num_cc = (double)CC.maxCoeff()+1.0;
  523. randperm(num_cc,I);
  524. C.resize(CC.rows(),3);
  525. for(int f = 0;f<CC.rows();f++)
  526. {
  527. jet(
  528. (double)I(CC(f))/num_cc,
  529. C(f,0),
  530. C(f,1),
  531. C(f,2));
  532. }
  533. }
  534. void TW_CALL randomize_colors(void * /*clientData*/)
  535. {
  536. push_undo();
  537. randomly_color(CC,s.C);
  538. }
  539. void init_patches()
  540. {
  541. using namespace Eigen;
  542. using namespace igl;
  543. using namespace std;
  544. {
  545. VectorXi VCC;
  546. components(F,VCC);
  547. cout<<"There are "<<VCC.maxCoeff()+1<<" connected components of vertices."<<endl;
  548. }
  549. bfs_orient(F,F,CC);
  550. VectorXi I;
  551. switch(orient_method)
  552. {
  553. case ORIENT_METHOD_AO:
  554. cout<<"orient_outward_ao()"<<endl;
  555. orient_outward_ao(V,F,CC,100, F.rows() * 100,F,I);
  556. break;
  557. case ORIENT_METHOD_OUTWARD:
  558. default:
  559. cout<<"orient_outward()"<<endl;
  560. orient_outward(V,F,CC,F,I);
  561. break;
  562. }
  563. double num_cc = (double)CC.maxCoeff()+1.0;
  564. cout<<"There are "<<num_cc<<" 'manifold/orientable' patches of faces."<<endl;
  565. }
  566. void undo()
  567. {
  568. using namespace std;
  569. if(!undo_stack.empty())
  570. {
  571. redo_stack.push(s);
  572. s = undo_stack.top();
  573. undo_stack.pop();
  574. }
  575. }
  576. void redo()
  577. {
  578. using namespace std;
  579. if(!redo_stack.empty())
  580. {
  581. undo_stack.push(s);
  582. s = redo_stack.top();
  583. redo_stack.pop();
  584. }
  585. }
  586. bool save(const std::string & out_filename)
  587. {
  588. using namespace std;
  589. using namespace igl;
  590. if(write(out_filename,V,F))
  591. {
  592. cout<<GREENGIN("Saved mesh to `"<<out_filename<<"` successfully.")<<endl;
  593. return true;
  594. }else
  595. {
  596. cout<<REDRUM("Failed to save mesh to `"<<out_filename<<"`.")<<endl;
  597. return false;
  598. }
  599. }
  600. void TW_CALL saveCB(void * /*clientData*/)
  601. {
  602. save(out_filename);
  603. }
  604. void key(unsigned char key, int mouse_x, int mouse_y)
  605. {
  606. using namespace std;
  607. int mod = glutGetModifiers();
  608. switch(key)
  609. {
  610. // ESC
  611. case char(27):
  612. rebar.save(REBAR_NAME);
  613. // ^C
  614. case char(3):
  615. exit(0);
  616. case 'I':
  617. case 'i':
  618. {
  619. push_undo();
  620. s.N *= -1.0;
  621. F = F.rowwise().reverse().eval();
  622. break;
  623. }
  624. case 'z':
  625. case 'Z':
  626. if(mod & GLUT_ACTIVE_COMMAND)
  627. {
  628. if(mod & GLUT_ACTIVE_SHIFT)
  629. {
  630. redo();
  631. }else
  632. {
  633. undo();
  634. }
  635. break;
  636. }else
  637. {
  638. push_undo();
  639. igl::snap_to_canonical_view_quat<double>(
  640. s.camera.rotation,
  641. 1.0,
  642. s.camera.rotation);
  643. break;
  644. }
  645. case 'u':
  646. mouse_wheel(0, 1,mouse_x,mouse_y);
  647. break;
  648. case 'j':
  649. mouse_wheel(0,-1,mouse_x,mouse_y);
  650. break;
  651. case 'n':
  652. cc_selected = (cc_selected + 1) % (CC.maxCoeff() + 2);
  653. cout << "selected cc: " << cc_selected << endl;
  654. glutPostRedisplay();
  655. break;
  656. default:
  657. if(!TwEventKeyboardGLUT(key,mouse_x,mouse_y))
  658. {
  659. cout<<"Unknown key command: "<<key<<" "<<int(key)<<endl;
  660. }
  661. }
  662. }
  663. int main(int argc, char * argv[])
  664. {
  665. using namespace std;
  666. using namespace Eigen;
  667. using namespace igl;
  668. string filename = "../shared/truck.obj";
  669. switch(argc)
  670. {
  671. case 3:
  672. out_filename = argv[2];
  673. case 2:
  674. // Read and prepare mesh
  675. filename = argv[1];
  676. break;
  677. default:
  678. cerr<<"Usage:"<<endl<<" ./example input.obj (output.obj)"<<endl;
  679. cout<<endl<<"Opening default mesh..."<<endl;
  680. break;
  681. }
  682. // print key commands
  683. cout<<"[Click] and [drag] Rotate model using trackball."<<endl;
  684. cout<<"[Z,z] Snap rotation to canonical view."<<endl;
  685. cout<<"[Command+Z] Undo."<<endl;
  686. cout<<"[Shift+Command+Z] Redo."<<endl;
  687. cout<<"[^C,ESC] Exit."<<endl;
  688. // dirname, basename, extension and filename
  689. string d,b,ext,f;
  690. pathinfo(filename,d,b,ext,f);
  691. // Convert extension to lower case
  692. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  693. vector<vector<double > > vV,vN,vTC;
  694. vector<vector<int > > vF,vFTC,vFN;
  695. if(ext == "obj")
  696. {
  697. // Convert extension to lower case
  698. if(!igl::readOBJ(filename,vV,vTC,vN,vF,vFTC,vFN))
  699. {
  700. return 1;
  701. }
  702. }else if(ext == "off")
  703. {
  704. // Convert extension to lower case
  705. if(!igl::readOFF(filename,vV,vF,vN))
  706. {
  707. return 1;
  708. }
  709. }else if(ext == "wrl")
  710. {
  711. // Convert extension to lower case
  712. if(!igl::readWRL(filename,vV,vF))
  713. {
  714. return 1;
  715. }
  716. //}else
  717. //{
  718. // // Convert extension to lower case
  719. // MatrixXi T;
  720. // if(!igl::readMESH(filename,V,T,F))
  721. // {
  722. // return 1;
  723. // }
  724. // //if(F.size() > T.size() || F.size() == 0)
  725. // {
  726. // boundary_faces(T,F);
  727. // }
  728. }
  729. if(vV.size() > 0)
  730. {
  731. if(!list_to_matrix(vV,V))
  732. {
  733. return 1;
  734. }
  735. triangulate(vF,F);
  736. }
  737. MatrixXi F_unique;
  738. unique_simplices(F, F_unique);
  739. F = F_unique;
  740. init_patches();
  741. init_relative();
  742. randomly_color(CC,s.C);
  743. // Init glut
  744. glutInit(&argc,argv);
  745. if( !TwInit(TW_OPENGL, NULL) )
  746. {
  747. // A fatal error occured
  748. fprintf(stderr, "AntTweakBar initialization failed: %s\n", TwGetLastError());
  749. return 1;
  750. }
  751. // Create a tweak bar
  752. rebar.TwNewBar("bar");
  753. TwDefine("bar label='Patches' size='200 550' text=light alpha='200' color='68 68 68'");
  754. rebar.TwAddVarCB("camera_rotation", TW_TYPE_QUAT4D, set_camera_rotation,get_camera_rotation, NULL, "open readonly=true");
  755. TwEnumVal RotationTypesEV[NUM_ROTATION_TYPES] =
  756. {
  757. {ROTATION_TYPE_IGL_TRACKBALL,"igl trackball"},
  758. {ROTATION_TYPE_TWO_AXIS_VALUATOR_FIXED_UP,"two axis fixed up"}
  759. };
  760. TwType RotationTypeTW =
  761. ReTwDefineEnum(
  762. "RotationType",
  763. RotationTypesEV,
  764. NUM_ROTATION_TYPES);
  765. rebar.TwAddVarCB( "rotation_type", RotationTypeTW,
  766. set_rotation_type,get_rotation_type,NULL,"keyIncr=] keyDecr=[");
  767. TwEnumVal OrientMethodEV[NUM_ORIENT_METHODS] =
  768. {
  769. {ORIENT_METHOD_OUTWARD,"outward"},
  770. {ORIENT_METHOD_AO,"ambient occlusion"}
  771. };
  772. TwType OrientMethodTW =
  773. ReTwDefineEnum(
  774. "OrientMethod",
  775. OrientMethodEV,
  776. NUM_ROTATION_TYPES);
  777. rebar.TwAddVarCB( "orient_method", OrientMethodTW,
  778. set_orient_method,get_orient_method,NULL,"keyIncr=< keyDecr=>");
  779. rebar.TwAddVarRW("wireframe_visible",TW_TYPE_BOOLCPP,&wireframe_visible,"key=l");
  780. rebar.TwAddVarRW("fill_visible",TW_TYPE_BOOLCPP,&fill_visible,"key=f");
  781. rebar.TwAddButton("randomize_colors",randomize_colors,NULL,"key=c");
  782. if(out_filename != "")
  783. {
  784. rebar.TwAddButton("save",
  785. saveCB,NULL,
  786. C_STR("label='Save to `"<<out_filename<<"`' "<<
  787. "key=s"));
  788. }
  789. rebar.load(REBAR_NAME);
  790. animation_from_quat = Quaterniond(1,0,0,0);
  791. copy(s.camera.rotation,s.camera.rotation+4,animation_to_quat.coeffs().data());
  792. animation_start_time = get_seconds();
  793. // Init antweakbar
  794. #ifdef __APPLE__
  795. glutInitDisplayString( "rgba depth double samples>=8");
  796. #else
  797. glutInitDisplayString( "rgba depth double "); // samples>=8 somehow not supported on Kenshi's machines...?
  798. #endif
  799. glutInitWindowSize(glutGet(GLUT_SCREEN_WIDTH)/2.0,glutGet(GLUT_SCREEN_HEIGHT)/2.0);
  800. glutCreateWindow("patches");
  801. glutDisplayFunc(display);
  802. glutReshapeFunc(reshape);
  803. glutKeyboardFunc(key);
  804. glutMouseFunc(mouse);
  805. glutMotionFunc(mouse_drag);
  806. glutPassiveMotionFunc((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  807. glutMainLoop();
  808. return 0;
  809. }